1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-09 16:06:21 +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(); this.initEditor();
} }
setModel(model) { setModel(model) {
this.instance.swapDoc this.editorReadyDeferred.promise.then(() => {
? this.instance.swapDoc(model) this.instance.swapDoc
: this.instance.setModel(model); ? this.instance.swapDoc(model)
: this.instance.setModel(model);
});
} }
setValue(value) { setValue(value) {
// HACK: We set a flag on window for an ultra-short duration, which 'change' // 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 // We save last set value so that when editor type changes, we can
// populate that last value // populate that last value
this.lastSetValue = value; this.lastSetValue = value;
this.refresh();
} }
getValue() { getValue() {
return this.instance.getValue(); return this.instance.getValue();
@ -145,7 +148,7 @@ export default class CodeEditor extends Component {
} }
setOption(option, value) { setOption(option, value) {
if (this.props.type === 'monaco') { if (this.props.type === 'monaco') {
this.monacoEditorReadyDeferred.promise.then(() => { this.editorReadyDeferred.promise.then(() => {
this.instance.updateOptions({ [option]: value }); this.instance.updateOptions({ [option]: value });
}); });
} else { } else {
@ -155,26 +158,28 @@ export default class CodeEditor extends Component {
setLanguage(value) { setLanguage(value) {
if (!window.monaco) return; if (!window.monaco) return;
if (this.props.type === 'monaco') { this.editorReadyDeferred.promise.then(() => {
this.monacoEditorReadyDeferred.promise.then(() => { if (this.props.type === 'monaco') {
monaco.editor.setModelLanguage( monaco.editor.setModelLanguage(
this.instance.getModel(), this.instance.getModel(),
this.getMonacoLanguageFromMode(modes[value].cmMode) this.getMonacoLanguageFromMode(modes[value].cmMode)
); );
}); } else {
} else { this.instance.setOption('mode', modes[value].cmMode);
this.instance.setOption('mode', modes[value].cmMode); CodeMirror.autoLoadMode(
CodeMirror.autoLoadMode( this.instance,
this.instance, modes[value].cmPath || modes[value].cmMode
modes[value].cmPath || modes[value].cmMode );
); }
} });
} }
clearGutter(gutterName) { clearGutter(gutterName) {
if (this.instance.clearGutter) { this.editorReadyDeferred.promise.then(() => {
this.instance.clearGutter(gutterName); if (this.instance.clearGutter) {
} this.instance.clearGutter(gutterName);
}
});
} }
showErrors(errors) { showErrors(errors) {
@ -191,10 +196,14 @@ export default class CodeEditor extends Component {
} }
refresh() { refresh() {
this.instance.refresh ? this.instance.refresh() : this.instance.layout(); this.editorReadyDeferred.promise.then(() => {
this.instance.refresh ? this.instance.refresh() : this.instance.layout();
});
} }
focus() { focus() {
this.instance.focus(); this.editorReadyDeferred.promise.then(() => {
this.instance.focus();
});
} }
/** /**
@ -235,7 +244,9 @@ export default class CodeEditor extends Component {
} }
async initEditor() { async initEditor() {
this.monacoEditorReadyDeferred = deferred(); console.log('init editor');
this.editorReadyDeferred = deferred();
await this.loadDeps(); await this.loadDeps();
const { options, prefs } = this.props; const { options, prefs } = this.props;
@ -273,7 +284,7 @@ export default class CodeEditor extends Component {
} }
} }
); );
this.monacoEditorReadyDeferred.resolve(); this.editorReadyDeferred.resolve();
} else { } else {
this.instance = CodeMirror.fromTextArea(this.node, { this.instance = CodeMirror.fromTextArea(this.node, {
mode: options.mode, mode: options.mode,
@ -347,6 +358,8 @@ export default class CodeEditor extends Component {
Enter: 'emmetInsertLineBreak' Enter: 'emmetInsertLineBreak'
} }
}); });
this.editorReadyDeferred.resolve();
this.instance.on('focus', editor => { this.instance.on('focus', editor => {
if (typeof this.props.onFocus === 'function') if (typeof this.props.onFocus === 'function')
this.props.onFocus(editor); this.props.onFocus(editor);

View File

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