1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-15 11:06:21 +02:00

Get back editor autocompletion in file mode

This commit is contained in:
Kushagra Gour
2018-10-08 15:18:33 +05:30
parent b1347690ad
commit 12c0ba3bf1
2 changed files with 38 additions and 17 deletions

View File

@ -18,7 +18,8 @@ export default class ContentWrapFiles extends Component {
super(props); super(props);
this.state = { this.state = {
isConsoleOpen: false, isConsoleOpen: false,
isCssSettingsModalOpen: false isCssSettingsModalOpen: false,
editorOptions: this.getEditorOptions()
}; };
this.fileBuffers = {}; this.fileBuffers = {};
@ -77,6 +78,29 @@ export default class ContentWrapFiles extends Component {
componentDidMount() { componentDidMount() {
this.props.onRef(this); this.props.onRef(this);
} }
getEditorOptions(fileName = '') {
let options = {
gutters: [
'error-gutter',
'CodeMirror-linenumbers',
'CodeMirror-foldgutter'
],
emmet: true
};
if (fileName.match(/\.css$/)) {
} else if (fileName.match(/\.js$/)) {
delete options.emmet;
} else if (fileName.match(/\.html$/)) {
// HTML
options = {
...options,
noAutocomplete: true,
matchTags: { bothTags: true }
};
}
return options;
}
createEditorDoc(file) { createEditorDoc(file) {
let mode; let mode;
@ -405,7 +429,10 @@ export default class ContentWrapFiles extends Component {
this.props.onEditorFocus(editor); this.props.onEditorFocus(editor);
} }
fileSelectHandler(file) { fileSelectHandler(file) {
this.setState({ selectedFile: file }); this.setState({
editorOptions: this.getEditorOptions(file.name),
selectedFile: file
});
if (!this.fileBuffers[file.name]) { if (!this.fileBuffers[file.name]) {
this.createEditorDoc(file); this.createEditorDoc(file);
} }
@ -553,14 +580,7 @@ export default class ContentWrapFiles extends Component {
</div> </div>
</div> </div>
<UserCodeMirror <UserCodeMirror
options={{ options={this.state.editorOptions}
mode: 'htmlmixed',
profile: 'xhtml',
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
noAutocomplete: true,
matchTags: { bothTags: true },
emmet: true
}}
prefs={this.props.prefs} prefs={this.props.prefs}
onChange={this.onHtmlCodeChange.bind(this)} onChange={this.onHtmlCodeChange.bind(this)}
onCreation={editor => (this.cm = editor)} onCreation={editor => (this.cm = editor)}

View File

@ -45,7 +45,7 @@ export default class UserCodeMirror extends Component {
shouldComponentUpdate(nextProps) { shouldComponentUpdate(nextProps) {
if (nextProps.prefs !== this.props.prefs) { if (nextProps.prefs !== this.props.prefs) {
const { prefs } = nextProps; const { prefs } = nextProps;
console.log('updating', nextProps.options.mode); console.log('updating CM prefs', prefs);
this.cm.setOption('indentWithTabs', prefs.indentWith !== 'spaces'); this.cm.setOption('indentWithTabs', prefs.indentWith !== 'spaces');
this.cm.setOption( this.cm.setOption(
@ -74,7 +74,7 @@ export default class UserCodeMirror extends Component {
initEditor() { initEditor() {
const { options, prefs } = this.props; const { options, prefs } = this.props;
console.log(100, prefs.lineWrap); console.log(100, options);
this.cm = CodeMirror.fromTextArea(this.textarea, { this.cm = CodeMirror.fromTextArea(this.textarea, {
mode: options.mode, mode: options.mode,
lineNumbers: true, lineNumbers: true,
@ -143,10 +143,11 @@ export default class UserCodeMirror extends Component {
this.cm.addKeyMap({ this.cm.addKeyMap({
'Ctrl-Space': 'autocomplete' 'Ctrl-Space': 'autocomplete'
}); });
if (!options.noAutocomplete) {
this.cm.on('inputRead', (editor, input) => { this.cm.on('inputRead', (editor, input) => {
// Process further If this has autocompletition on and also the global
// autocomplete setting is on.
if (!this.props.options.noAutocomplete && this.props.prefs.autoComplete) {
if ( if (
!this.props.prefs.autoComplete ||
input.origin !== '+input' || input.origin !== '+input' ||
input.text[0] === ';' || input.text[0] === ';' ||
input.text[0] === ',' || input.text[0] === ',' ||
@ -157,8 +158,8 @@ export default class UserCodeMirror extends Component {
CodeMirror.commands.autocomplete(this.cm, null, { CodeMirror.commands.autocomplete(this.cm, null, {
completeSingle: false completeSingle: false
}); });
});
} }
});
this.props.onCreation(this.cm); this.props.onCreation(this.cm);
} }