diff --git a/build/constant.ts b/build/constant.ts index dfa2f491..38ff0454 100644 --- a/build/constant.ts +++ b/build/constant.ts @@ -1,4 +1,7 @@ -export const OUTPUT_DIR = 'dist'; +export const OUTPUT_DIR = 'dist' + +// monaco-editor 路径 +export const prefix = `monaco-editor/esm/vs` // chunk 警告大小 export const chunkSizeWarningLimit = 2000 @@ -11,7 +14,14 @@ export const rollupOptions = { output: { chunkFileNames: 'static/js/[name]-[hash].js', entryFileNames: 'static/js/[name]-[hash].js', - assetFileNames: 'static/[ext]/[name]-[hash].[ext]' + assetFileNames: 'static/[ext]/[name]-[hash].[ext]', + manualChunks: { + jsonWorker: [`${prefix}/language/json/json.worker`], + cssWorker: [`${prefix}/language/css/css.worker`], + htmlWorker: [`${prefix}/language/html/html.worker`], + tsWorker: [`${prefix}/language/typescript/ts.worker`], + editorWorker: [`${prefix}/editor/editor.worker`] + } } } @@ -22,4 +32,4 @@ export const terserOptions = { drop_console: true, drop_debugger: true } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 5b01c51f..98295f6f 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "vite-plugin-compression": "^0.5.1", "vite-plugin-importer": "^0.2.5", "vite-plugin-mock": "^2.9.6", + "vite-plugin-monaco-editor": "^1.1.0", "vue-echarts": "^6.0.2", "vue-tsc": "^0.28.10" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4a73acf9..eb4b2211 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,6 +47,7 @@ specifiers: vite-plugin-compression: ^0.5.1 vite-plugin-importer: ^0.2.5 vite-plugin-mock: ^2.9.6 + vite-plugin-monaco-editor: ^1.1.0 vue: ^3.2.31 vue-demi: ^0.13.1 vue-echarts: ^6.0.2 @@ -113,6 +114,7 @@ devDependencies: vite-plugin-compression: 0.5.1_vite@2.9.5 vite-plugin-importer: 0.2.5 vite-plugin-mock: 2.9.6_mockjs@1.1.0+vite@2.9.5 + vite-plugin-monaco-editor: 1.1.0_monaco-editor@0.33.0 vue-echarts: 6.0.3_echarts@5.3.3+vue@3.2.37 vue-tsc: 0.28.10_typescript@4.7.3 @@ -5111,6 +5113,14 @@ packages: - supports-color dev: true + /vite-plugin-monaco-editor/1.1.0_monaco-editor@0.33.0: + resolution: {integrity: sha512-IvtUqZotrRoVqwT0PBBDIZPNraya3BxN/bfcNfnxZ5rkJiGcNtO5eAOWWSgT7zullIAEqQwxMU83yL9J5k7gww==} + peerDependencies: + monaco-editor: '>=0.33.0' + dependencies: + monaco-editor: 0.33.0 + dev: true + /vite/2.9.5_sass@1.52.3: resolution: {integrity: sha512-dvMN64X2YEQgSXF1lYabKXw3BbN6e+BL67+P3Vy4MacnY+UzT1AfkHiioFSi9+uiDUiaDy7Ax/LQqivk6orilg==} engines: {node: '>=12.2.0'} diff --git a/src/components/Pages/MonacoEditor/index.hook.ts b/src/components/Pages/MonacoEditor/index.hook.ts index d68ef4de..e6f808e3 100644 --- a/src/components/Pages/MonacoEditor/index.hook.ts +++ b/src/components/Pages/MonacoEditor/index.hook.ts @@ -1,46 +1,83 @@ +import { ref, onBeforeUnmount, nextTick } from 'vue' import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js' +import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' +import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker' +// import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker' +// import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker' +import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker' -export const useMonacoEditor = (language = 'json') => { +export const useMonacoEditor = (language = 'javascript') => { let monacoEditor: monaco.editor.IStandaloneCodeEditor | null = null let initReadOnly = false - const updateVal = async (val: string) => { - monacoEditor?.setValue(val) - setTimeout(async () => { - initReadOnly && monacoEditor?.updateOptions({ readOnly: false }) - // await monacoEditor?.getAction('editor.action.formatDocument')?.run() - initReadOnly && monacoEditor?.updateOptions({ readOnly: true }) - }, 100) + const el = ref(null) + + // @ts-ignore + self.MonacoEnvironment = { + getWorker(_: any, label: string) { + if (label === 'json') { + return new jsonWorker() + } + // if (label === 'css' || label === 'scss' || label === 'less') { + // return new cssWorker() + // } + // if (label === 'html' || label === 'handlebars' || label === 'razor') { + // return new htmlWorker() + // } + if (label === 'typescript' || label === 'javascript') { + return new tsWorker() + } + return new editorWorker() + } } - const createEditor = ( - el: HTMLElement | null, - editorOption: monaco.editor.IStandaloneEditorConstructionOptions = {} - ) => { - if (monacoEditor) { - return - } + // 格式化 + const onFormatDoc = async () => { + await monacoEditor?.getAction('monacoEditor.action.formatDocument')?.run() + } + + // 更新 + const updateVal = (val: string) => { + nextTick(async () => { + monacoEditor?.setValue(val) + initReadOnly && monacoEditor?.updateOptions({ readOnly: false }) + await onFormatDoc() + initReadOnly && monacoEditor?.updateOptions({ readOnly: true }) + }) + } + + // 创建实例 + const createEditor = (editorOption: monaco.editor.IStandaloneEditorConstructionOptions = {}) => { + if (!el.value) return + const javascriptModel = monaco.editor.createModel('', language) initReadOnly = !!editorOption.readOnly - monacoEditor = - el && - monaco.editor.create(el, { - language, - minimap: { enabled: false }, - theme: 'vs-dark', - multiCursorModifier: 'ctrlCmd', - scrollbar: { - verticalScrollbarSize: 8, - horizontalScrollbarSize: 8 - }, - tabSize: 2, - automaticLayout: true, // 自适应宽高 - ...editorOption - }) + // 创建 + monacoEditor = monaco.editor.create(el.value, { + model: javascriptModel, + minimap: { enabled: true }, + roundedSelection: false, + theme: 'vs-dark', + multiCursorModifier: 'ctrlCmd', + scrollbar: { + verticalScrollbarSize: 8, + horizontalScrollbarSize: 8 + }, + tabSize: 2, + fontSize: 16, //字体大小 + autoIndent: 'advanced', //自动布局 + automaticLayout: true, // 自适应宽高 + ...editorOption + }) + return monacoEditor } - const onFormatDoc = () => { - monacoEditor?.getAction('editor.action.formatDocument').run() - } + + // 卸载 + onBeforeUnmount(() => { + if (monacoEditor) monacoEditor.dispose() + }) + return { + el, updateVal, getEditor: () => monacoEditor, createEditor, diff --git a/src/components/Pages/MonacoEditor/index.vue b/src/components/Pages/MonacoEditor/index.vue index 9c2dc43f..c13190ca 100644 --- a/src/components/Pages/MonacoEditor/index.vue +++ b/src/components/Pages/MonacoEditor/index.vue @@ -1,75 +1,65 @@ - \ No newline at end of file + diff --git a/src/views/chart/ContentConfigurations/components/ChartData/components/ChartDataMonacoEditor/index.vue b/src/views/chart/ContentConfigurations/components/ChartData/components/ChartDataMonacoEditor/index.vue index 50722eaf..9f361c32 100644 --- a/src/views/chart/ContentConfigurations/components/ChartData/components/ChartDataMonacoEditor/index.vue +++ b/src/views/chart/ContentConfigurations/components/ChartData/components/ChartDataMonacoEditor/index.vue @@ -56,9 +56,11 @@
// 数据参数 => data -

function  filter(data)  {

- -

}

+ + function  filter(data)  { + + + }
@@ -171,9 +173,6 @@ const saveFilter = () => { targetData.value.filter = filter.value closeFilter() } - -// 执行过滤处理 -const filterData = (data: any) => {}