refactor(工具箱): 优化逻辑,提取 MonacoEnvironment 配置

This commit is contained in:
2026-01-03 19:04:10 +08:00
parent 69e64fb08c
commit 13ed3a8fe4
4 changed files with 36 additions and 41 deletions

View File

@@ -0,0 +1,21 @@
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
/** 初始化 MonacoEnvironment */
export function initMonacoEnvironment() {
if (!self.MonacoEnvironment) {
// 配置编辑器环境Service Worker 等)
self.MonacoEnvironment = {
getWorker(workerId, label) {
if (label === 'javascript' || label === 'typescript') {
return new tsWorker();
} else if (label === 'json') {
return new jsonWorker();
} else {
return new editorWorker();
}
},
};
}
}

View File

@@ -16,24 +16,12 @@ import {
onBeforeUnmount, onMounted, onBeforeUnmount, onMounted,
} from 'vue'; } from 'vue';
import {
initMonacoEnvironment,
} from '@/assets/js/monaco-editor';
import * as monaco from 'monaco-editor'; import * as monaco from 'monaco-editor';
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
// 配置编辑器环境Service Worker 等)
if (!self.MonacoEnvironment) {
self.MonacoEnvironment = {
getWorker(workerId, label) {
if (label === 'json') {
return new jsonWorker();
} else {
return new editorWorker();
}
},
};
}
/** 模块名称 */ /** 模块名称 */
const PREFIX = '[JsonEditor]'; const PREFIX = '[JsonEditor]';
@@ -126,6 +114,7 @@ function resetEditor() {
} }
onMounted(() => { onMounted(() => {
initMonacoEnvironment();
initEditor(); initEditor();
window.addEventListener('resize', handleWindowResize); window.addEventListener('resize', handleWindowResize);
}); });

View File

@@ -47,21 +47,15 @@ import {
useFileDialog, useFileDialog,
} from '@vueuse/core'; } from '@vueuse/core';
import {
initMonacoEnvironment,
} from '@/assets/js/monaco-editor';
import { import {
$dialog, $message, $dialog, $message,
} from '@/assets/js/naive-ui'; } from '@/assets/js/naive-ui';
import * as monaco from 'monaco-editor'; import * as monaco from 'monaco-editor';
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
// 配置编辑器环境Service Worker 等)
if (!self.MonacoEnvironment) {
self.MonacoEnvironment = {
getWorker() {
return new editorWorker();
},
};
}
/** 模块名称 */ /** 模块名称 */
const PREFIX = '[TextEditor]'; const PREFIX = '[TextEditor]';
@@ -220,6 +214,7 @@ fileOnChange((files) => {
}); });
onMounted(() => { onMounted(() => {
initMonacoEnvironment();
initEditor(); initEditor();
window.addEventListener('resize', handleWindowResize); window.addEventListener('resize', handleWindowResize);
}); });

View File

@@ -49,26 +49,15 @@ import {
onBeforeUnmount, onMounted, onBeforeUnmount, onMounted,
} from 'vue'; } from 'vue';
import {
initMonacoEnvironment,
} from '@/assets/js/monaco-editor';
import { import {
$dialog, $message, $dialog, $message,
} from '@/assets/js/naive-ui'; } from '@/assets/js/naive-ui';
import * as monaco from 'monaco-editor'; import * as monaco from 'monaco-editor';
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
// 配置编辑器环境Service Worker 等)
if (!self.MonacoEnvironment) {
self.MonacoEnvironment = {
getWorker(workerId, label) {
if (label === 'javascript' || label === 'typescript') {
return new tsWorker();
} else {
return new editorWorker();
}
},
};
}
/** 模块名称 */ /** 模块名称 */
const PREFIX = '[RunJavaScript]'; const PREFIX = '[RunJavaScript]';
@@ -259,6 +248,7 @@ function updateEditorContent(text = '') {
} }
onMounted(() => { onMounted(() => {
initMonacoEnvironment();
initEditor(); initEditor();
window.addEventListener('resize', handleWindowResize); window.addEventListener('resize', handleWindowResize);
}); });