From 98c86d61f1bf53abf36679a0e5a8a8aaef121e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E4=B9=8B?= Date: Tue, 18 Mar 2025 00:11:28 +0800 Subject: [PATCH] 1 --- package.json | 21 ++-- pnpm-lock.yaml | 111 ++++++------------ rspack.config.ts | 10 ++ src/pages/components/CodeEditor/index.tsx | 78 ++++++------ src/pages/options/main.tsx | 2 + .../options/routes/script/ScriptEditor.tsx | 19 ++- 6 files changed, 103 insertions(+), 138 deletions(-) diff --git a/package.json b/package.json index 14caccb..b74568e 100644 --- a/package.json +++ b/package.json @@ -46,16 +46,6 @@ "@rspack/cli": "^1.2.3", "@rspack/core": "^1.2.3", "@rspack/plugin-react-refresh": "^1.0.1", - "cross-env": "^7.0.3", - "eslint": "^9.19.0", - "eslint-plugin-react": "^7.37.4", - "eslint-plugin-react-hooks": "^5.1.0", - "globals": "^15.14.0", - "prettier": "^3.4.2", - "react-refresh": "^0.16.0", - "ts-node": "^10.9.2", - "typescript": "^5.7.3", - "typescript-eslint": "^8.22.0", "@types/chrome": "^0.0.279", "@types/crypto-js": "^4.2.2", "@types/node": "^22.10.2", @@ -67,9 +57,20 @@ "@unocss/postcss": "0.65.0-beta.2", "@vitest/coverage-v8": "2.1.4", "autoprefixer": "^10.4.20", + "compression-webpack-plugin": "^11.1.0", + "cross-env": "^7.0.3", + "eslint": "^9.19.0", + "eslint-plugin-react": "^7.37.4", + "eslint-plugin-react-hooks": "^5.1.0", + "globals": "^15.14.0", "jsdom": "^25.0.1", "postcss": "^8.4.49", "postcss-loader": "^8.1.1", + "prettier": "^3.4.2", + "react-refresh": "^0.16.0", + "ts-node": "^10.9.2", + "typescript": "^5.7.3", + "typescript-eslint": "^8.22.0", "unocss": "0.65.0-beta.2", "vitest": "^2.1.4" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4270812..df6fb12 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -126,6 +126,9 @@ importers: autoprefixer: specifier: ^10.4.20 version: 10.4.20(postcss@8.4.49) + compression-webpack-plugin: + specifier: ^11.1.0 + version: 11.1.0(webpack@5.96.1(esbuild@0.23.1)) cross-env: specifier: ^7.0.3 version: 7.0.3 @@ -920,55 +923,46 @@ packages: resolution: {integrity: sha512-10ICosOwYChROdQoQo589N5idQIisxjaFE/PAnX2i0Zr84mY0k9zul1ArH0rnJ/fpgiqfu13TFZR5A5YJLOYZA==} cpu: [arm] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.24.4': resolution: {integrity: sha512-ySAfWs69LYC7QhRDZNKqNhz2UKN8LDfbKSMAEtoEI0jitwfAG2iZwVqGACJT+kfYvvz3/JgsLlcBP+WWoKCLcw==} cpu: [arm] os: [linux] - libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.24.4': resolution: {integrity: sha512-uHYJ0HNOI6pGEeZ/5mgm5arNVTI0nLlmrbdph+pGXpC9tFHFDQmDMOEqkmUObRfosJqpU8RliYoGz06qSdtcjg==} cpu: [arm64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.24.4': resolution: {integrity: sha512-38yiWLemQf7aLHDgTg85fh3hW9stJ0Muk7+s6tIkSUOMmi4Xbv5pH/5Bofnsb6spIwD5FJiR+jg71f0CH5OzoA==} cpu: [arm64] os: [linux] - libc: [musl] '@rollup/rollup-linux-powerpc64le-gnu@4.24.4': resolution: {integrity: sha512-q73XUPnkwt9ZNF2xRS4fvneSuaHw2BXuV5rI4cw0fWYVIWIBeDZX7c7FWhFQPNTnE24172K30I+dViWRVD9TwA==} cpu: [ppc64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.24.4': resolution: {integrity: sha512-Aie/TbmQi6UXokJqDZdmTJuZBCU3QBDA8oTKRGtd4ABi/nHgXICulfg1KI6n9/koDsiDbvHAiQO3YAUNa/7BCw==} cpu: [riscv64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.24.4': resolution: {integrity: sha512-P8MPErVO/y8ohWSP9JY7lLQ8+YMHfTI4bAdtCi3pC2hTeqFJco2jYspzOzTUB8hwUWIIu1xwOrJE11nP+0JFAQ==} cpu: [s390x] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.24.4': resolution: {integrity: sha512-K03TljaaoPK5FOyNMZAAEmhlyO49LaE4qCsr0lYHUKyb6QacTNF9pnfPpXnFlFD3TXuFbFbz7tJ51FujUXkXYA==} cpu: [x64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-musl@4.24.4': resolution: {integrity: sha512-VJYl4xSl/wqG2D5xTYncVWW+26ICV4wubwN9Gs5NrqhJtayikwCXzPL8GDsLnaLU3WwhQ8W02IinYSFJfyo34Q==} cpu: [x64] os: [linux] - libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.24.4': resolution: {integrity: sha512-ku2GvtPwQfCqoPFIJCqZ8o7bJcj+Y54cZSr43hHca6jLwAiCbZdBUOrqE6y29QFajNAzzpIOwsckaTFmN6/8TA==} @@ -1033,7 +1027,6 @@ packages: resolution: {integrity: sha512-K2u/fPUmKujlKSWL3q2zaUu8/6ZK/bOGKcqJSib8jdanQQ/GFKwKtPAFOOa/vvqbzhDocqKOobFR10FhgJqCHg==} cpu: [arm64] os: [linux] - libc: [glibc] '@rspack/binding-linux-arm64-musl@1.0.14': resolution: {integrity: sha512-qgybhxI/nnoa8CUz7zKTC0Oh37NZt9uRxsSV7+ZYrfxqbrVCoNVuutPpY724uUHy1M6W34kVEm1uT1N4Ka5cZg==} @@ -1045,7 +1038,6 @@ packages: resolution: {integrity: sha512-mgovdzGb6cH9hQsjTyzDbfZWCPhTcoHcLro1P7UbiqcLPMDJp/k3Io9xV2/EJhaDA1aynIdq7XfY0fuk4+6Irw==} cpu: [arm64] os: [linux] - libc: [musl] '@rspack/binding-linux-x64-gnu@1.0.14': resolution: {integrity: sha512-5vzaDRw3/sGKo3ax/1cU3/cxqNjajwlt2LU288vXNe1/n8oe/pcDfYcTugpOe/A1DqzadanudJszLpFcKsaFtQ==} @@ -1057,7 +1049,6 @@ packages: resolution: {integrity: sha512-542lwJzB1RMGuVdBdA3cOWTlmL9okpOppHUBWcNCjmJM+9zTI+0jwjVe8HaqOqtuR8XzNsoCwT9QonU/GLcuhg==} cpu: [x64] os: [linux] - libc: [glibc] '@rspack/binding-linux-x64-musl@1.0.14': resolution: {integrity: sha512-4U6QD9xVS1eGme52DuJr6Fg/KdcUfJ+iKwH49Up460dZ/fLvGylnVGA+V0mzPlKi8gfy7NwFuYXZdu3Pwi1YYg==} @@ -1069,7 +1060,6 @@ packages: resolution: {integrity: sha512-dJromiREDcTWqzfCOI5y1IVoYmUnCv7vCp63AEq0+13fJJdk7+pcNN3VV2jOKpk9VECSvjg1c01wl+UzXAXFMw==} cpu: [x64] os: [linux] - libc: [musl] '@rspack/binding-win32-arm64-msvc@1.0.14': resolution: {integrity: sha512-SjeYw7qqRHYZ5RPClu+ffKZsShQdU3amA1OwC3M0AS6dbfEcji8482St3Y8Z+QSzYRapCEZij9LMM/9ypEhISg==} @@ -1902,6 +1892,12 @@ packages: resolution: {integrity: sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==} engines: {node: '>= 0.6'} + compression-webpack-plugin@11.1.0: + resolution: {integrity: sha512-zDOQYp10+upzLxW+VRSjEpRRwBXJdsb5lBMlRxx1g8hckIFBpe3DTI0en2w7h+beuq89576RVzfiXrkdPGrHhA==} + engines: {node: '>= 18.12.0'} + peerDependencies: + webpack: ^5.1.0 + compression@1.7.4: resolution: {integrity: sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==} engines: {node: '>= 0.8.0'} @@ -4886,7 +4882,6 @@ snapshots: dependencies: '@jridgewell/gen-mapping': 0.3.5 '@jridgewell/trace-mapping': 0.3.25 - optional: true '@jridgewell/sourcemap-codec@1.5.0': {} @@ -5292,13 +5287,11 @@ snapshots: dependencies: '@types/eslint': 9.6.1 '@types/estree': 1.0.6 - optional: true '@types/eslint@9.6.1': dependencies: '@types/estree': 1.0.6 '@types/json-schema': 7.0.15 - optional: true '@types/estree@1.0.6': {} @@ -5855,26 +5848,20 @@ snapshots: dependencies: '@webassemblyjs/helper-numbers': 1.13.2 '@webassemblyjs/helper-wasm-bytecode': 1.13.2 - optional: true - '@webassemblyjs/floating-point-hex-parser@1.13.2': - optional: true + '@webassemblyjs/floating-point-hex-parser@1.13.2': {} - '@webassemblyjs/helper-api-error@1.13.2': - optional: true + '@webassemblyjs/helper-api-error@1.13.2': {} - '@webassemblyjs/helper-buffer@1.14.1': - optional: true + '@webassemblyjs/helper-buffer@1.14.1': {} '@webassemblyjs/helper-numbers@1.13.2': dependencies: '@webassemblyjs/floating-point-hex-parser': 1.13.2 '@webassemblyjs/helper-api-error': 1.13.2 '@xtuc/long': 4.2.2 - optional: true - '@webassemblyjs/helper-wasm-bytecode@1.13.2': - optional: true + '@webassemblyjs/helper-wasm-bytecode@1.13.2': {} '@webassemblyjs/helper-wasm-section@1.14.1': dependencies: @@ -5882,20 +5869,16 @@ snapshots: '@webassemblyjs/helper-buffer': 1.14.1 '@webassemblyjs/helper-wasm-bytecode': 1.13.2 '@webassemblyjs/wasm-gen': 1.14.1 - optional: true '@webassemblyjs/ieee754@1.13.2': dependencies: '@xtuc/ieee754': 1.2.0 - optional: true '@webassemblyjs/leb128@1.13.2': dependencies: '@xtuc/long': 4.2.2 - optional: true - '@webassemblyjs/utf8@1.13.2': - optional: true + '@webassemblyjs/utf8@1.13.2': {} '@webassemblyjs/wasm-edit@1.14.1': dependencies: @@ -5907,7 +5890,6 @@ snapshots: '@webassemblyjs/wasm-opt': 1.14.1 '@webassemblyjs/wasm-parser': 1.14.1 '@webassemblyjs/wast-printer': 1.14.1 - optional: true '@webassemblyjs/wasm-gen@1.14.1': dependencies: @@ -5916,7 +5898,6 @@ snapshots: '@webassemblyjs/ieee754': 1.13.2 '@webassemblyjs/leb128': 1.13.2 '@webassemblyjs/utf8': 1.13.2 - optional: true '@webassemblyjs/wasm-opt@1.14.1': dependencies: @@ -5924,7 +5905,6 @@ snapshots: '@webassemblyjs/helper-buffer': 1.14.1 '@webassemblyjs/wasm-gen': 1.14.1 '@webassemblyjs/wasm-parser': 1.14.1 - optional: true '@webassemblyjs/wasm-parser@1.14.1': dependencies: @@ -5934,19 +5914,15 @@ snapshots: '@webassemblyjs/ieee754': 1.13.2 '@webassemblyjs/leb128': 1.13.2 '@webassemblyjs/utf8': 1.13.2 - optional: true '@webassemblyjs/wast-printer@1.14.1': dependencies: '@webassemblyjs/ast': 1.14.1 '@xtuc/long': 4.2.2 - optional: true - '@xtuc/ieee754@1.2.0': - optional: true + '@xtuc/ieee754@1.2.0': {} - '@xtuc/long@4.2.2': - optional: true + '@xtuc/long@4.2.2': {} accepts@1.3.8: dependencies: @@ -5982,7 +5958,6 @@ snapshots: ajv-keywords@3.5.2(ajv@6.12.6): dependencies: ajv: 6.12.6 - optional: true ajv-keywords@5.1.0(ajv@8.17.1): dependencies: @@ -6174,8 +6149,7 @@ snapshots: node-releases: 2.0.18 update-browserslist-db: 1.1.1(browserslist@4.24.2) - buffer-from@1.1.2: - optional: true + buffer-from@1.1.2: {} bundle-name@4.1.0: dependencies: @@ -6248,8 +6222,7 @@ snapshots: optionalDependencies: fsevents: 2.3.3 - chrome-trace-event@1.0.4: - optional: true + chrome-trace-event@1.0.4: {} cliui@8.0.1: dependencies: @@ -6287,8 +6260,7 @@ snapshots: dependencies: delayed-stream: 1.0.0 - commander@2.20.3: - optional: true + commander@2.20.3: {} commander@7.2.0: {} @@ -6296,6 +6268,12 @@ snapshots: dependencies: mime-db: 1.53.0 + compression-webpack-plugin@11.1.0(webpack@5.96.1(esbuild@0.23.1)): + dependencies: + schema-utils: 4.2.0 + serialize-javascript: 6.0.2 + webpack: 5.96.1(esbuild@0.23.1) + compression@1.7.4: dependencies: accepts: 1.3.8 @@ -6524,7 +6502,6 @@ snapshots: dependencies: graceful-fs: 4.2.11 tapable: 2.2.1 - optional: true entities@4.5.0: {} @@ -6685,8 +6662,7 @@ snapshots: iterator.prototype: 1.1.5 safe-array-concat: 1.1.3 - es-module-lexer@1.5.4: - optional: true + es-module-lexer@1.5.4: {} es-object-atoms@1.0.0: dependencies: @@ -6836,7 +6812,6 @@ snapshots: dependencies: esrecurse: 4.3.0 estraverse: 4.3.0 - optional: true eslint-scope@8.1.0: dependencies: @@ -6955,8 +6930,7 @@ snapshots: dependencies: estraverse: 5.3.0 - estraverse@4.3.0: - optional: true + estraverse@4.3.0: {} estraverse@5.3.0: {} @@ -6974,8 +6948,7 @@ snapshots: eventemitter3@5.0.1: {} - events@3.3.0: - optional: true + events@3.3.0: {} execa@5.1.1: dependencies: @@ -7195,8 +7168,7 @@ snapshots: dependencies: is-glob: 4.0.3 - glob-to-regexp@0.4.1: - optional: true + glob-to-regexp@0.4.1: {} glob@10.4.5: dependencies: @@ -7637,7 +7609,6 @@ snapshots: '@types/node': 22.10.2 merge-stream: 2.0.0 supports-color: 8.1.1 - optional: true jiti@1.21.6: {} @@ -7714,8 +7685,7 @@ snapshots: load-tsconfig@0.2.5: {} - loader-runner@4.3.0: - optional: true + loader-runner@4.3.0: {} local-pkg@0.5.1: dependencies: @@ -7834,8 +7804,7 @@ snapshots: negotiator@0.6.3: {} - neo-async@2.6.2: - optional: true + neo-async@2.6.2: {} node-fetch-native@1.6.4: {} @@ -8074,7 +8043,6 @@ snapshots: randombytes@2.1.0: dependencies: safe-buffer: 5.2.1 - optional: true range-parser@1.2.1: {} @@ -8384,7 +8352,6 @@ snapshots: '@types/json-schema': 7.0.15 ajv: 6.12.6 ajv-keywords: 3.5.2(ajv@6.12.6) - optional: true schema-utils@4.2.0: dependencies: @@ -8433,7 +8400,6 @@ snapshots: serialize-javascript@6.0.2: dependencies: randombytes: 2.1.0 - optional: true serve-index@1.9.1: dependencies: @@ -8563,10 +8529,8 @@ snapshots: dependencies: buffer-from: 1.1.2 source-map: 0.6.1 - optional: true - source-map@0.6.1: - optional: true + source-map@0.6.1: {} spdy-transport@3.0.0: dependencies: @@ -8710,14 +8674,12 @@ snapshots: supports-color@8.1.1: dependencies: has-flag: 4.0.0 - optional: true supports-preserve-symlinks-flag@1.0.0: {} symbol-tree@3.2.4: {} - tapable@2.2.1: - optional: true + tapable@2.2.1: {} terser-webpack-plugin@5.3.10(esbuild@0.23.1)(webpack@5.96.1(esbuild@0.23.1)): dependencies: @@ -8729,7 +8691,6 @@ snapshots: webpack: 5.96.1(esbuild@0.23.1) optionalDependencies: esbuild: 0.23.1 - optional: true terser@5.36.0: dependencies: @@ -8737,7 +8698,6 @@ snapshots: acorn: 8.14.0 commander: 2.20.3 source-map-support: 0.5.21 - optional: true test-exclude@7.0.1: dependencies: @@ -9215,7 +9175,6 @@ snapshots: dependencies: glob-to-regexp: 0.4.1 graceful-fs: 4.2.11 - optional: true wbuf@1.7.3: dependencies: @@ -9336,8 +9295,7 @@ snapshots: - supports-color - utf-8-validate - webpack-sources@3.2.3: - optional: true + webpack-sources@3.2.3: {} webpack-virtual-modules@0.6.2: optional: true @@ -9371,7 +9329,6 @@ snapshots: - '@swc/core' - esbuild - uglify-js - optional: true websocket-driver@0.7.4: dependencies: diff --git a/rspack.config.ts b/rspack.config.ts index a747820..5d611bf 100644 --- a/rspack.config.ts +++ b/rspack.config.ts @@ -2,10 +2,13 @@ import * as path from "path"; import { defineConfig } from "@rspack/cli"; import { rspack } from "@rspack/core"; import { version } from "./package.json"; +import CompressionPlugin from "compression-webpack-plugin"; const isDev = process.env.NODE_ENV === "development"; const isBeta = version.includes("-"); +console.log(CompressionPlugin); + // Target browsers, see: https://github.com/browserslist/browserslist const targets = ["chrome >= 87", "edge >= 88", "firefox >= 78", "safari >= 14"]; @@ -29,6 +32,8 @@ export default defineConfig({ popup: `${src}/pages/popup/main.tsx`, install: `${src}/pages/install/main.tsx`, options: `${src}/pages/options/main.tsx`, + "editor.worker": "monaco-editor/esm/vs/editor/editor.worker.js", + "ts.worker": "monaco-editor/esm/vs/language/typescript/ts.worker.js", }, output: { path: `${dist}/ext/src`, @@ -168,6 +173,11 @@ export default defineConfig({ minify: true, chunks: ["sandbox"], }), + new CompressionPlugin({ + test: /ts.worker.js$/, + filename: () => "ts.worker.js", + deleteOriginalAssets: true, + }), ].filter(Boolean), optimization: { minimizer: [ diff --git a/src/pages/components/CodeEditor/index.tsx b/src/pages/components/CodeEditor/index.tsx index d0cfd2d..acb5072 100644 --- a/src/pages/components/CodeEditor/index.tsx +++ b/src/pages/components/CodeEditor/index.tsx @@ -2,7 +2,7 @@ import Cache from "@App/app/cache"; import { LinterWorker } from "@App/pkg/utils/monaco-editor"; import { useAppSelector } from "@App/pages/store/hooks"; import { editor, Range } from "monaco-editor"; -import React, { useEffect, useImperativeHandle, useState } from "react"; +import React, { useEffect, useImperativeHandle, useRef, useState } from "react"; type Props = { className?: string; @@ -18,60 +18,59 @@ const CodeEditor: React.ForwardRefRenderFunction<{ editor: editor.ICodeEditor | ) => { const settings = useAppSelector((state) => state.setting); const [monacoEditor, setEditor] = useState(); + const div = useRef(null); useImperativeHandle(ref, () => ({ editor: monacoEditor, })); useEffect(() => { - if (diffCode === undefined || code === undefined) { + if (diffCode === undefined || code === undefined || !div.current) { return () => {}; } let edit: editor.IStandaloneDiffEditor | editor.IStandaloneCodeEditor; + const inlineDiv = document.getElementById(id) as HTMLDivElement; // @ts-ignore - const ts = window.tsUrl ? 0 : 200; - setTimeout(() => { - const div = document.getElementById(id) as HTMLDivElement; - if (diffCode) { - edit = editor.createDiffEditor(div, { - enableSplitViewResizing: false, - renderSideBySide: false, - folding: true, - foldingStrategy: "indentation", - automaticLayout: true, - overviewRulerBorder: false, - scrollBeyondLastLine: false, - readOnly: true, - diffWordWrap: "off", - glyphMargin: true, - }); - edit.setModel({ - original: editor.createModel(diffCode, "javascript"), - modified: editor.createModel(code, "javascript"), - }); - } else { - edit = editor.create(div, { - language: "javascript", - theme: document.body.getAttribute("arco-theme") === "dark" ? "vs-dark" : "vs", - folding: true, - foldingStrategy: "indentation", - automaticLayout: true, - overviewRulerBorder: false, - scrollBeyondLastLine: false, - readOnly: !editable, - glyphMargin: true, - }); - edit.setValue(code); + if (diffCode) { + edit = editor.createDiffEditor(inlineDiv, { + enableSplitViewResizing: false, + renderSideBySide: false, + folding: true, + foldingStrategy: "indentation", + automaticLayout: true, + overviewRulerBorder: false, + scrollBeyondLastLine: false, + readOnly: true, + diffWordWrap: "off", + glyphMargin: true, + }); + edit.setModel({ + original: editor.createModel(diffCode, "javascript"), + modified: editor.createModel(code, "javascript"), + }); + } else { + edit = editor.create(inlineDiv, { + language: "javascript", + theme: document.body.getAttribute("arco-theme") === "dark" ? "vs-dark" : "vs", + folding: true, + foldingStrategy: "indentation", + automaticLayout: true, + overviewRulerBorder: false, + scrollBeyondLastLine: false, + readOnly: !editable, + glyphMargin: true, + }); + edit.setValue(code); - setEditor(edit); - } - }, ts); + setEditor(edit); + } return () => { if (edit) { edit.dispose(); } }; - }, [code, diffCode, editable, id]); + }, [div, code, diffCode, editable, id]); useEffect(() => { + return () => {}; if (!settings.eslint.enable) { return () => {}; } @@ -218,6 +217,7 @@ const CodeEditor: React.ForwardRefRenderFunction<{ editor: editor.ICodeEditor | overflow: "hidden", }} className={className} + ref={div} /> ); }; diff --git a/src/pages/options/main.tsx b/src/pages/options/main.tsx index 45592a0..3ffa9dc 100644 --- a/src/pages/options/main.tsx +++ b/src/pages/options/main.tsx @@ -12,9 +12,11 @@ import migrate from "@App/app/migrate.ts"; import LoggerCore from "@App/app/logger/core.ts"; import { LoggerDAO } from "@App/app/repo/logger.ts"; import DBWriter from "@App/app/logger/db_writer.ts"; +import registerEditor from "@App/pkg/utils/monaco-editor.ts"; // 初始化数据库 migrate(); +registerEditor(); // 初始化日志组件 const loggerCore = new LoggerCore({ writer: new DBWriter(new LoggerDAO()), diff --git a/src/pages/options/routes/script/ScriptEditor.tsx b/src/pages/options/routes/script/ScriptEditor.tsx index 3712901..366cc3a 100644 --- a/src/pages/options/routes/script/ScriptEditor.tsx +++ b/src/pages/options/routes/script/ScriptEditor.tsx @@ -1,4 +1,4 @@ -import { Script, ScriptCodeDAO, ScriptDAO } from "@App/app/repo/scripts"; +import { Script, ScriptAndCode, ScriptCodeDAO, ScriptDAO } from "@App/app/repo/scripts"; import CodeEditor from "@App/pages/components/CodeEditor"; import React, { useEffect, useRef, useState } from "react"; import { useNavigate, useParams, useSearchParams } from "react-router-dom"; @@ -30,7 +30,7 @@ type HotKey = { const Editor: React.FC<{ id: string; - script: Script; + script: ScriptAndCode; hotKeys: HotKey[]; callbackEditor: (e: editor.IStandaloneCodeEditor) => void; onChange: (code: string) => void; @@ -41,11 +41,9 @@ const Editor: React.FC<{ ScriptMap.set(script.uuid, script); useEffect(() => { if (!codeEditor.current || !codeEditor.current.editor) { - setTimeout(() => { - setInit(true); - }, 200); return () => {}; } + console.log(codeEditor); // 初始化editor时将Script的uuid绑定到editor上 // @ts-ignore if (!codeEditor.current.editor.uuid) { @@ -72,7 +70,7 @@ const Editor: React.FC<{ }); callbackEditor(codeEditor.current.editor); return () => {}; - }, [init]); + }, []); return ; }; @@ -150,8 +148,7 @@ function ScriptEditor() { const [visible, setVisible] = useState<{ [key: string]: boolean }>({}); const [editors, setEditors] = useState< { - script: Script; - code: string; + script: ScriptAndCode; active: boolean; hotKeys: HotKey[]; editor?: editor.IStandaloneCodeEditor; @@ -378,7 +375,6 @@ function ScriptEditor() { return prev.map((item) => { if (item.script.uuid === scripts[i].uuid) { item.active = true; - item.code = code!.code; } else { item.active = false; } @@ -386,8 +382,7 @@ function ScriptEditor() { }); } prev.push({ - script: scripts[i], - code: code!.code, + script: Object.assign(scripts[i], code), active: true, hotKeys, isChanged: false, @@ -870,7 +865,7 @@ function ScriptEditor() { }); }} onChange={(code) => { - const isChanged = !(item.code === code); + const isChanged = !(item.script.code === code); if (isChanged !== item.isChanged) { setEditors((prev) => { prev.forEach((v) => {