在组件内而非全局创建editor实例变量,但不赋予响应式

This commit is contained in:
fofolee 2025-02-22 11:22:59 +08:00
parent d9bc4b4f34
commit e97ec5963a

View File

@ -55,7 +55,6 @@ let languageCompletions = importAll(
); );
let monacoCompletionProviders = {}; let monacoCompletionProviders = {};
let editor = null;
// //
const typeDefinitions = { const typeDefinitions = {
@ -140,19 +139,19 @@ export default defineComponent({
modelValue: { modelValue: {
immediate: true, immediate: true,
handler(newValue) { handler(newValue) {
if (!editor || editor.getValue() === newValue) return; if (!this.codeEditor || this.codeEditor.getValue() === newValue) return;
editor.setValue(newValue || ""); this.codeEditor.setValue(newValue || "");
}, },
}, },
cursorPosition: { cursorPosition: {
immediate: true, immediate: true,
handler(newValue) { handler(newValue) {
if (!editor) return; if (!this.codeEditor) return;
const { lineNumber, column } = newValue; const { lineNumber, column } = newValue;
if (!lineNumber || !column) return; if (!lineNumber || !column) return;
const pos = editor.getPosition(); const pos = this.codeEditor.getPosition();
if (pos.lineNumber === lineNumber && pos.column === column) return; if (pos.lineNumber === lineNumber && pos.column === column) return;
editor.setPosition(newValue); this.codeEditor.setPosition(newValue);
}, },
}, },
"$q.dark.isActive": { "$q.dark.isActive": {
@ -164,9 +163,9 @@ export default defineComponent({
language: { language: {
immediate: true, immediate: true,
handler(newValue) { handler(newValue) {
if (!editor) return; if (!this.codeEditor) return;
const language = this.getHighlighter(newValue); const language = this.getHighlighter(newValue);
monaco.editor.setModelLanguage(editor.getModel(), language); monaco.editor.setModelLanguage(this.codeEditor.getModel(), language);
this.loadTypes(); this.loadTypes();
}, },
}, },
@ -194,7 +193,10 @@ export default defineComponent({
wordWrap: this.wordWrap, wordWrap: this.wordWrap,
}; };
editor = monaco.editor.create(this.$refs.editorContainer, options); this.codeEditor = monaco.editor.create(
this.$refs.editorContainer,
options
);
this.listenEditorValue(); this.listenEditorValue();
this.loadTypes(); this.loadTypes();
this.registerLanguage(); this.registerLanguage();
@ -207,13 +209,13 @@ export default defineComponent({
}, },
// //
listenEditorValue() { listenEditorValue() {
editor.focus(); this.codeEditor.focus();
editor.onDidChangeModelContent(() => { this.codeEditor.onDidChangeModelContent(() => {
this.$emit("update:modelValue", editor.getValue()); this.$emit("update:modelValue", this.codeEditor.getValue());
}); });
// //
editor.onDidChangeCursorPosition((e) => { this.codeEditor.onDidChangeCursorPosition((e) => {
this.$emit("update:cursorPosition", e.position); this.$emit("update:cursorPosition", e.position);
}); });
}, },
@ -223,19 +225,19 @@ export default defineComponent({
clearTimeout(this.resizeTimeout); clearTimeout(this.resizeTimeout);
} }
this.resizeTimeout = setTimeout(() => { this.resizeTimeout = setTimeout(() => {
editor.layout(); this.codeEditor.layout();
}, 50); }, 50);
}, },
// //
destroyEditor() { destroyEditor() {
window.removeEventListener("resize", this.resizeEditor); window.removeEventListener("resize", this.resizeEditor);
if (!editor) return; if (!this.codeEditor) return;
editor.dispose(); this.codeEditor.dispose();
editor = null; this.codeEditor = null;
}, },
// //
focus() { focus() {
editor && editor.focus(); this.codeEditor && this.codeEditor.focus();
}, },
registerLanguage() { registerLanguage() {
const identifierPattern = "([a-zA-Z_]\\w*)"; const identifierPattern = "([a-zA-Z_]\\w*)";
@ -363,20 +365,23 @@ export default defineComponent({
}, },
setCursorPosition(position) { setCursorPosition(position) {
if (!position.lineNumber || !position.column) return; if (!position.lineNumber || !position.column) return;
editor.setPosition(position); this.codeEditor.setPosition(position);
}, },
bindKeys() { bindKeys() {
// alt + z // alt + z
editor.addCommand(monaco.KeyMod.Alt | monaco.KeyCode.KeyZ, () => { this.codeEditor.addCommand(
monaco.KeyMod.Alt | monaco.KeyCode.KeyZ,
() => {
this.wordWrap = this.wordWrap === "on" ? "off" : "on"; this.wordWrap = this.wordWrap === "on" ? "off" : "on";
editor.updateOptions({ this.codeEditor.updateOptions({
wordWrap: this.wordWrap, wordWrap: this.wordWrap,
}); });
}); }
);
}, },
// //
repacleEditorSelection(text) { repacleEditorSelection(text) {
var selection = editor.getSelection(); var selection = this.codeEditor.getSelection();
var range = new monaco.Range( var range = new monaco.Range(
selection.startLineNumber, selection.startLineNumber,
selection.startColumn, selection.startColumn,
@ -390,15 +395,15 @@ export default defineComponent({
text: text, text: text,
forceMoveMarkers: true, forceMoveMarkers: true,
}; };
editor.executeEdits("my-source", [op]); this.codeEditor.executeEdits("my-source", [op]);
}, },
// //
formatDocument() { formatDocument() {
editor.getAction("editor.action.formatDocument").run(); this.codeEditor.getAction("editor.action.formatDocument").run();
}, },
updateEditorValue(type, value) { updateEditorValue(type, value) {
if (type === "replace") { if (type === "replace") {
editor.setValue(value); this.codeEditor.setValue(value);
} else if (type === "insert") { } else if (type === "insert") {
this.repacleEditorSelection(value); this.repacleEditorSelection(value);
} }