This commit is contained in:
fofolee 2024-01-22 16:59:06 +08:00
parent 541f266ecf
commit f78bb589cb

View File

@ -47,11 +47,6 @@ export default {
mounted() { mounted() {
this.initEditor(); this.initEditor();
}, },
computed: {
rawEditor() {
return toRaw(this.editor);
},
},
props: { props: {
placeholder: Boolean, placeholder: Boolean,
}, },
@ -110,7 +105,8 @@ export default {
}; };
let createDependencyProposals = (range, keyWords, editor, curWord) => { let createDependencyProposals = (range, keyWords, editor, curWord) => {
let keys = []; let keys = [];
let tokens = getTokens(toRaw(editor).getModel().getValue()); // fix getValue of undefined
let tokens = getTokens(toRaw(editor).getModel()?.getValue());
// //
for (const item of tokens) { for (const item of tokens) {
if (item != curWord.word) { if (item != curWord.word) {
@ -168,23 +164,23 @@ export default {
monaco.editor.setTheme(this.$q.dark.isActive ? "vs-dark" : "vs"); monaco.editor.setTheme(this.$q.dark.isActive ? "vs-dark" : "vs");
}, },
getEditorValue() { getEditorValue() {
return this.rawEditor.getValue(); return this.rawEditor().getValue();
}, },
setEditorValue(value) { setEditorValue(value) {
this.rawEditor.setValue(value); this.rawEditor().setValue(value);
}, },
setEditorLanguage(language) { setEditorLanguage(language) {
monaco.editor.setModelLanguage(this.rawEditor.getModel(), language); monaco.editor.setModelLanguage(this.rawEditor().getModel(), language);
}, },
getCursorPosition() { getCursorPosition() {
return this.rawEditor.getPosition(); return this.rawEditor().getPosition();
}, },
setCursorPosition(pos) { setCursorPosition(pos) {
if (!pos) return; if (!pos) return;
this.rawEditor.setPosition(pos); this.rawEditor().setPosition(pos);
}, },
repacleEditorSelection(text) { repacleEditorSelection(text) {
var selection = this.rawEditor.getSelection(); var selection = this.rawEditor().getSelection();
var range = new monaco.Range( var range = new monaco.Range(
selection.startLineNumber, selection.startLineNumber,
selection.startColumn, selection.startColumn,
@ -198,11 +194,11 @@ export default {
text: text, text: text,
forceMoveMarkers: true, forceMoveMarkers: true,
}; };
this.rawEditor.executeEdits("my-source", [op]); this.rawEditor().executeEdits("my-source", [op]);
}, },
listenEditorValue() { listenEditorValue() {
this.rawEditor.focus(); this.rawEditor().focus();
this.rawEditor.onDidChangeModelContent(() => { this.rawEditor().onDidChangeModelContent(() => {
this.value = this.getEditorValue(); this.value = this.getEditorValue();
this.$emit("typing", this.value); this.$emit("typing", this.value);
}); });
@ -210,26 +206,26 @@ export default {
bindKeys() { bindKeys() {
let that = this; let that = this;
// ctrl + b // ctrl + b
this.rawEditor.addCommand( this.rawEditor().addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyB, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyB,
() => { () => {
that.$emit("keyStroke", "run"); that.$emit("keyStroke", "run");
} }
); );
// alt + z // alt + z
this.rawEditor.addCommand(monaco.KeyMod.Alt | monaco.KeyCode.KeyZ, () => { this.rawEditor().addCommand(monaco.KeyMod.Alt | monaco.KeyCode.KeyZ, () => {
that.wordWrap = that.wordWrap === "off" ? "on" : "off"; that.wordWrap = that.wordWrap === "off" ? "on" : "off";
that.rawEditor.updateOptions({ wordWrap: that.wordWrap }); that.rawEditor.updateOptions({ wordWrap: that.wordWrap });
}); });
// ctrl + s // ctrl + s
this.rawEditor.addCommand( this.rawEditor().addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS,
() => { () => {
that.$emit("keyStroke", "save"); that.$emit("keyStroke", "save");
} }
); );
// ctrl + space console.log // ctrl + space console.log
this.rawEditor.addCommand( this.rawEditor().addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE,
() => { () => {
that.$emit("keyStroke", "log", that.getSelectionOrLineContent()); that.$emit("keyStroke", "log", that.getSelectionOrLineContent());
@ -237,22 +233,25 @@ export default {
); );
}, },
getSelectionOrLineContent() { getSelectionOrLineContent() {
let selection = this.rawEditor.getSelection(); let selection = this.rawEditor().getSelection();
let range = new monaco.Range( let range = new monaco.Range(
selection.startLineNumber, selection.startLineNumber,
selection.startColumn, selection.startColumn,
selection.endLineNumber, selection.endLineNumber,
selection.endColumn selection.endColumn
); );
let model = this.rawEditor.getModel(); let model = this.rawEditor().getModel();
return ( return (
model.getValueInRange(range) || model.getValueInRange(range) ||
model.getLineContent(selection.startLineNumber) model.getLineContent(selection.startLineNumber)
); );
}, },
rawEditor() {
return toRaw(this.editor);
},
}, },
beforeUnmount() { beforeUnmount() {
this.rawEditor.dispose(); this.rawEditor().dispose();
}, },
}; };
</script> </script>