1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-10 00:16:18 +02:00

CodeEditor: wrap cm commands in deferred too

This commit is contained in:
Kushagra Gour
2019-01-27 23:13:31 +05:30
parent 6b24e76951
commit 86515bb4bd
2 changed files with 35 additions and 22 deletions

View File

@ -111,9 +111,11 @@ export default class CodeEditor extends Component {
this.initEditor();
}
setModel(model) {
this.editorReadyDeferred.promise.then(() => {
this.instance.swapDoc
? this.instance.swapDoc(model)
: this.instance.setModel(model);
});
}
setValue(value) {
// HACK: We set a flag on window for an ultra-short duration, which 'change'
@ -129,6 +131,7 @@ export default class CodeEditor extends Component {
// We save last set value so that when editor type changes, we can
// populate that last value
this.lastSetValue = value;
this.refresh();
}
getValue() {
return this.instance.getValue();
@ -145,7 +148,7 @@ export default class CodeEditor extends Component {
}
setOption(option, value) {
if (this.props.type === 'monaco') {
this.monacoEditorReadyDeferred.promise.then(() => {
this.editorReadyDeferred.promise.then(() => {
this.instance.updateOptions({ [option]: value });
});
} else {
@ -155,13 +158,12 @@ export default class CodeEditor extends Component {
setLanguage(value) {
if (!window.monaco) return;
this.editorReadyDeferred.promise.then(() => {
if (this.props.type === 'monaco') {
this.monacoEditorReadyDeferred.promise.then(() => {
monaco.editor.setModelLanguage(
this.instance.getModel(),
this.getMonacoLanguageFromMode(modes[value].cmMode)
);
});
} else {
this.instance.setOption('mode', modes[value].cmMode);
CodeMirror.autoLoadMode(
@ -169,12 +171,15 @@ export default class CodeEditor extends Component {
modes[value].cmPath || modes[value].cmMode
);
}
});
}
clearGutter(gutterName) {
this.editorReadyDeferred.promise.then(() => {
if (this.instance.clearGutter) {
this.instance.clearGutter(gutterName);
}
});
}
showErrors(errors) {
@ -191,10 +196,14 @@ export default class CodeEditor extends Component {
}
refresh() {
this.editorReadyDeferred.promise.then(() => {
this.instance.refresh ? this.instance.refresh() : this.instance.layout();
});
}
focus() {
this.editorReadyDeferred.promise.then(() => {
this.instance.focus();
});
}
/**
@ -235,7 +244,9 @@ export default class CodeEditor extends Component {
}
async initEditor() {
this.monacoEditorReadyDeferred = deferred();
console.log('init editor');
this.editorReadyDeferred = deferred();
await this.loadDeps();
const { options, prefs } = this.props;
@ -273,7 +284,7 @@ export default class CodeEditor extends Component {
}
}
);
this.monacoEditorReadyDeferred.resolve();
this.editorReadyDeferred.resolve();
} else {
this.instance = CodeMirror.fromTextArea(this.node, {
mode: options.mode,
@ -347,6 +358,8 @@ export default class CodeEditor extends Component {
Enter: 'emmetInsertLineBreak'
}
});
this.editorReadyDeferred.resolve();
this.instance.on('focus', editor => {
if (typeof this.props.onFocus === 'function')
this.props.onFocus(editor);

View File

@ -302,7 +302,7 @@ export default class ContentWrapFiles extends Component {
this.editor.setValue(this.state.selectedFile.content);
}
if (this.editor) {
this.editor.refresh();
// this.editor.refresh();
}
window.cm = this.cm;