Skip to content

Commit bf388f1

Browse files
author
马蹄疾
authored
Merge pull request #462 from Coding/registerEditorView
registerEditorViewByContentType API 改成 registerEditorView,拦截类型从 conte…
2 parents fa24824 + b25eaf7 commit bf388f1

File tree

5 files changed

+76
-28
lines changed

5 files changed

+76
-28
lines changed

app/components/MonacoEditor/Editors/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ export const editorSet = [
3131
},
3232
];
3333

34-
function matchEditorByContentType(editorType, contentType) {
34+
// 插件形式的编辑器视图会 unshift 到 editorSet 中
35+
function matchEditorByContentType(editorType, contentType, extension) {
3536
for (let i = 0, n = editorSet.length; i < n; i++) {
3637
const set = editorSet[i];
3738
if (set.editorType) {
3839
if (set.editorType === editorType) {
3940
return set.editor;
4041
}
4142
} else if (set.contentTypes && Array.isArray(set.contentTypes)) {
42-
// 插件形式的编辑器视图会 unshift 到 editorSet 中。通过 contentTypes 拦截。
43+
// 通过 contentTypes 拦截。优先级更高
4344
if (set.contentTypes.includes(contentType)) {
4445
return set.editor;
4546
}
47+
} else if (set.extensions && Array.isArray(set.extensions)) {
48+
// 通过 extensions 拦截。优先级更低
49+
if (set.extensions.includes(extension)) {
50+
return set.editor;
51+
}
4652
}
4753
}
4854
return UnknownEditor;
@@ -62,7 +68,7 @@ const EditorWrapper = observer(({ tab, active }) => {
6268
// key is crutial here, it decides whether the component should re-construct
6369
// or keep using the existing instance.
6470
const key = `editor_${file.path}`;
65-
const editorElement = matchEditorByContentType(editor.editorType, editor.contentType);
71+
const editorElement = matchEditorByContentType(editor.editorType, editor.contentType, editor.filePath.split('.').pop());
6672
return React.createElement(editorElement, { editorInfo, key, tab, active, language: config.mainLanguage, path: file.path, size: file.size });
6773
})
6874

app/components/Panel/SideBar/SideBar.jsx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React from 'react'
1+
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
3-
import cx from 'classnames'
43
import PluginArea from 'components/Plugins/component'
54
import { SIDEBAR } from 'components/Plugins/constants'
65
import { observer } from 'mobx-react'
@@ -16,22 +15,51 @@ label = {
1615
}
1716
*/
1817

18+
class SideBarLabel extends Component {
19+
state = {
20+
originWidth: 0,
21+
width: 'auto',
22+
}
23+
textRef = null;
1924

20-
const SideBarLabel = ({ label, isActive, onClick }) => (
21-
<div className={
22-
cx('side-bar-label', {
23-
active: isActive
24-
})}
25-
onClick={onClick}
26-
>
27-
<div className='side-bar-label-container'>
28-
<div className='side-bar-label-content'>
29-
<i className={cx('icon', label.icon)} />
30-
{!label.onlyIcon && <span>{label.text}</span>}
25+
render() {
26+
const { width } = this.state;
27+
const { label, isActive, onClick } = this.props;
28+
return (
29+
<div className={`side-bar-label${isActive ? ' active' : ''}`} onClick={onClick}>
30+
<div className='side-bar-label-container'>
31+
<div className="side-bar-label-content" onMouseEnter={this.mouseEnter} onMouseLeave={this.mouseLeave}>
32+
<i className={`icon${label.icon ? ` ${label.icon}` : ''}`} />
33+
<span className="text" ref={this.handleTextRef} style={{ width }}>{label.text}</span>
34+
</div>
35+
</div>
3136
</div>
32-
</div>
33-
</div>
34-
)
37+
);
38+
}
39+
40+
componentDidMount() {
41+
if (this.props.label.onlyIcon) {
42+
const originWidth = this.textRef.getBoundingClientRect().height;
43+
this.setState({ originWidth, width: 0 });
44+
}
45+
}
46+
47+
handleTextRef = (ref) => {
48+
this.textRef = ref;
49+
}
50+
51+
mouseEnter = () => {
52+
if (this.props.label.onlyIcon) {
53+
this.setState({ width: this.state.originWidth });
54+
}
55+
}
56+
57+
mouseLeave = () => {
58+
if (this.props.label.onlyIcon) {
59+
this.setState({ width: 0 });
60+
}
61+
}
62+
}
3563

3664
SideBarLabel.propTypes = {
3765
isActive: PropTypes.bool,

app/containers/IDE.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import emitter, { STORAGE_CHANGE } from 'utils/emitter'
88
import { notify, NOTIFY_TYPE } from '../components/Notification/actions'
99
import i18n from 'utils/createI18n'
1010
import GlobalPrompt from './GlobalPrompt'
11+
import config from 'config';
12+
import { autorun } from 'mobx';
1113

1214
const Bulletin = ({ close }) => {
1315
return (
@@ -23,7 +25,7 @@ const Bulletin = ({ close }) => {
2325
class IDE extends Component {
2426
constructor (props) {
2527
super(props)
26-
this.state = { isReady: false, isBulletinOn: true }
28+
this.state = { isReady: false, isBulletinOn: false }
2729
}
2830

2931
componentWillMount () { // initLifecycle_3: IDE specific init
@@ -39,6 +41,11 @@ class IDE extends Component {
3941
dismissAfter: 12000
4042
})
4143
}
44+
autorun(() => {
45+
if (config.__PLUGIN_DEV__) {
46+
this.setState({ isBulletinOn: true });
47+
}
48+
});
4249
}
4350

4451
render () {

app/extensions/Editor.ext.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Color from 'color'
99
import { monacoThemeOptions } from '../settings'
1010
import { editorSet } from 'components/MonacoEditor/Editors';
1111

12-
export function getActiveEditor () {
12+
export function getActiveEditor() {
1313
const { EditorTabState } = mobxStore
1414
const activeTab = EditorTabState.activeTab
1515
return activeTab
@@ -20,7 +20,7 @@ export function getActiveEditor () {
2020
: null
2121
}
2222

23-
export function openNewEditor (config) {
23+
export function openNewEditor(config) {
2424
const { path, contentType, ...other } = config
2525
TabActions.createTab({
2626
icon: icons.getClassWithColor(path.split('/').pop()) || 'fa fa-file-text-o',
@@ -32,7 +32,7 @@ export function openNewEditor (config) {
3232
})
3333
}
3434

35-
export function registerLanguage (languageConf) {
35+
export function registerLanguage(languageConf) {
3636
const { contribution } = languageConf
3737
monaco.languages.register(contribution)
3838
monaco.languages.onLanguage(contribution.id, () => contribution.loader().then((mod) => {
@@ -45,16 +45,20 @@ export function registerFormattingEditProvider (languageId, provider) {
4545
// todo
4646
}
4747

48-
export function registerEditorViewByContentTypes(editor, contentTypes) {
48+
export function registerEditorView(editor, { contentTypes, extensions }) {
4949
// 注册编辑器
50-
editorSet.unshift({ editor, contentTypes });
50+
editorSet.unshift({ editor, contentTypes, extensions });
5151
// dispose
5252
return () => {
53-
editorSet = editorSet.filter(editor => String(editor.contentTypes) !== String(contentTypes));
53+
if (contentTypes && Array.isArray(contentTypes)) {
54+
editorSet = editorSet.filter(editor => String(editor.contentTypes) !== String(contentTypes));
55+
} else {
56+
editorSet = editorSet.filter(editor => String(editor.extensions) !== String(extensions));
57+
}
5458
}
5559
}
5660

57-
export function registerContentProvider (component, conf) {
61+
export function registerContentProvider(component, conf) {
5862
const { name } = conf
5963
const type = `CUSTOM_EDITOR_VIEW_${name}`
6064
const activate = (editorType, title, props) => {

app/styles/core-ui/Bar.styl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ $bar-size= 25px
7272
align-items: center;
7373
.icon {margin-right: 4px}
7474
}
75+
.side-bar-label-content .text {
76+
overflow: hidden;
77+
transition: width .2s ease;
78+
}
7579

7680
&.left .side-bar-label-content {
7781
transform: rotate(180deg);
7882
}
7983

8084
}
81-

0 commit comments

Comments
 (0)