diff --git a/docs/api/editor/codeBlockServiceMethods.md b/docs/api/editor/codeBlockServiceMethods.md index 56f4bcf4..75960e3d 100644 --- a/docs/api/editor/codeBlockServiceMethods.md +++ b/docs/api/editor/codeBlockServiceMethods.md @@ -4,8 +4,6 @@ ## setCodeDsl -- **[扩展支持](../../guide/editor-expand#行为扩展):** 是 - - **参数:** - @@ -18,8 +16,6 @@ ## getCodeDsl -- **[扩展支持](../../guide/editor-expand#行为扩展):** 是 - - **参数:** - @@ -30,8 +26,6 @@ - **详情:** -## getCodeDslSync - ## getCodeContentById - **[扩展支持](../../guide/editor-expand#行为扩展):** 是 diff --git a/docs/api/editor/editorServiceMethods.md b/docs/api/editor/editorServiceMethods.md index 880cd7b7..2a993081 100644 --- a/docs/api/editor/editorServiceMethods.md +++ b/docs/api/editor/editorServiceMethods.md @@ -587,44 +587,6 @@ alignCenter可以支持一次水平居中多个组件,alignCenter是通过调 重置当前记录的修改过的节点id记录,通常用于保存之后 -## getCodeDsl - -- **[扩展支持](../../guide/editor-expand#行为扩展):** 是 - -- **返回:** - - - {Promise<[CodeBlockDSL](https://github.com/Tencent/tmagic-editor/blob/c143a5f7670ae61d80c1a2cfcc780cfb5259849d/packages/schema/src/index.ts#L75) | null>} - -- **详情:** - - 从dsl中的codeBlocks字段读取活动的代码块 - -## getCodeDslSync - -- **返回:** - - - [CodeBlockDSL](https://github.com/Tencent/tmagic-editor/blob/c143a5f7670ae61d80c1a2cfcc780cfb5259849d/packages/schema/src/index.ts#L75) | null - -- **详情:** - - 从dsl中的codeBlocks字段读取活动的代码块 - -## setCodeDsl - -- **[扩展支持](../../guide/editor-expand#行为扩展):** 是 - -- **参数:** - - - {[CodeBlockDSL](https://github.com/Tencent/tmagic-editor/blob/c143a5f7670ae61d80c1a2cfcc780cfb5259849d/packages/schema/src/index.ts#L75)} CodeBlockDSL - -- **返回:** - - - `{Promise}` - -- **详情:** - - 设置代码块到dsl的codeBlocks字段 - ## resetState - **详情:** diff --git a/packages/editor/src/Editor.vue b/packages/editor/src/Editor.vue index ba895a61..81659aec 100644 --- a/packages/editor/src/Editor.vue +++ b/packages/editor/src/Editor.vue @@ -247,6 +247,10 @@ export default defineComponent({ if (toRaw(value) !== toRaw(preValue)) { emit('update:modelValue', value); } + + value.codeBlocks = value.codeBlocks || {}; + + codeBlockService.setCodeDsl(value.codeBlocks); }; editorService.on('root-change', rootChangeHandler); diff --git a/packages/editor/src/components/FunctionEditor.vue b/packages/editor/src/components/FunctionEditor.vue index 503830b3..ab968d2d 100644 --- a/packages/editor/src/components/FunctionEditor.vue +++ b/packages/editor/src/components/FunctionEditor.vue @@ -136,7 +136,7 @@ watchEffect(() => { }); const initTableModel = (): void => { - const codeDsl = cloneDeep(services?.codeBlockService.getCodeDslSync()); + const codeDsl = cloneDeep(services?.codeBlockService.getCodeDsl()); if (!codeDsl) return; tableModel.value = { params: codeDsl[props.id]?.params || [], diff --git a/packages/editor/src/fields/CodeSelect.vue b/packages/editor/src/fields/CodeSelect.vue index 3bf4037f..dfed080c 100644 --- a/packages/editor/src/fields/CodeSelect.vue +++ b/packages/editor/src/fields/CodeSelect.vue @@ -44,7 +44,7 @@ const props = defineProps<{ name: string; size: 'mini' | 'small' | 'medium'; }>(); -const codeDsl = computed(() => services?.codeBlockService.getCodeDslSync()); +const codeDsl = computed(() => services?.codeBlockService.getCodeDsl()); const tableConfig = computed(() => { const defaultConfig = { diff --git a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue index 79fdefde..2fb43b9d 100644 --- a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue +++ b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue @@ -164,7 +164,7 @@ watch( ); watch( - [() => services?.codeBlockService.getCodeDslSync(), () => services?.codeBlockService.getCombineInfo()], + [() => services?.codeBlockService.getCodeDsl(), () => services?.codeBlockService.getCombineInfo()], () => { refreshCodeList(); }, diff --git a/packages/editor/src/services/codeBlock.ts b/packages/editor/src/services/codeBlock.ts index 021c5e66..ed9d2cb5 100644 --- a/packages/editor/src/services/codeBlock.ts +++ b/packages/editor/src/services/codeBlock.ts @@ -41,8 +41,6 @@ class CodeBlock extends BaseService { constructor() { super([ - 'setCodeDsl', - 'getCodeDsl', 'getCodeContentById', 'getCodeDslByIds', 'getCurrentDsl', @@ -62,7 +60,6 @@ class CodeBlock extends BaseService { */ public async setCodeDsl(codeDsl: CodeBlockDSL): Promise { this.state.codeDsl = codeDsl; - await editorService.setCodeDsl(this.state.codeDsl); info('[code-block]:code-dsl-change', this.state.codeDsl); this.emit('code-dsl-change', this.state.codeDsl); } @@ -73,14 +70,7 @@ class CodeBlock extends BaseService { * @param {boolean} forceRefresh 是否强制从活动dsl拉取刷新 * @returns {CodeBlockDSL | null} */ - public async getCodeDsl(forceRefresh = false): Promise { - return this.getCodeDslSync(forceRefresh); - } - - public getCodeDslSync(forceRefresh = false): CodeBlockDSL | null { - if (!this.state.codeDsl || forceRefresh) { - this.state.codeDsl = editorService.getCodeDslSync(); - } + public getCodeDsl(): CodeBlockDSL | null { return this.state.codeDsl; } @@ -103,31 +93,25 @@ class CodeBlock extends BaseService { * @returns {void} */ public async setCodeDslById(id: Id, codeConfig: CodeBlockContent): Promise { - let codeDsl = await this.getCodeDsl(); + const codeDsl = await this.getCodeDsl(); + + if (!codeDsl) { + throw new Error('dsl中没有codeBlocks'); + } + const codeConfigProcessed = codeConfig; if (codeConfig.content) { // 在保存的时候转换代码内容 // eslint-disable-next-line no-eval codeConfigProcessed.content = eval(codeConfig.content); } - if (!codeDsl) { - // dsl中无代码块字段 - codeDsl = { - [id]: { - ...codeConfigProcessed, - }, - }; - } else { - const existContent = codeDsl[id] || {}; - codeDsl = { - ...codeDsl, - [id]: { - ...existContent, - ...codeConfigProcessed, - }, - }; - } - await this.setCodeDsl(codeDsl); + + const existContent = codeDsl[id] || {}; + + codeDsl[id] = { + ...existContent, + ...codeConfigProcessed, + }; } /** @@ -320,13 +304,15 @@ class CodeBlock extends BaseService { /** * 在dsl数据源中删除指定id的代码块 * @param {Id[]} codeIds 需要删除的代码块id数组 - * @returns {CodeBlockDSL} 删除后的code dsl */ - public async deleteCodeDslByIds(codeIds: Id[]): Promise { + public async deleteCodeDslByIds(codeIds: Id[]): Promise { const currentDsl = await this.getCodeDsl(); - const newDsl = omit(currentDsl, codeIds); - await this.setCodeDsl(newDsl); - return newDsl; + + if (!currentDsl) return; + + codeIds.forEach((id) => { + delete currentDsl[id]; + }); } /** diff --git a/packages/editor/src/services/editor.ts b/packages/editor/src/services/editor.ts index 4b1ab2ce..44f8aefe 100644 --- a/packages/editor/src/services/editor.ts +++ b/packages/editor/src/services/editor.ts @@ -19,7 +19,7 @@ import { reactive, toRaw } from 'vue'; import { cloneDeep, isObject, mergeWith, uniq } from 'lodash-es'; -import type { CodeBlockDSL, Id, MApp, MComponent, MContainer, MNode, MPage } from '@tmagic/schema'; +import type { Id, MApp, MComponent, MContainer, MNode, MPage } from '@tmagic/schema'; import { NodeType } from '@tmagic/schema'; import StageCore from '@tmagic/stage'; import { getNodePath, isNumber, isPage, isPop } from '@tmagic/utils'; @@ -80,8 +80,6 @@ class Editor extends BaseService { 'undo', 'redo', 'highlight', - 'getCodeDsl', - 'setCodeDsl', ], // 需要注意循环依赖问题,如果函数间有相互调用的话,不能设置为串行调用 ['select', 'update', 'moveLayer'], @@ -832,30 +830,6 @@ class Editor extends BaseService { this.get('modifiedNodeIds').clear(); } - /** - * 从dsl中的codeBlocks字段读取活动的代码块 - * @returns {CodeBlockDSL | null} - */ - public async getCodeDsl(): Promise { - const root = this.get('root'); - return root?.codeBlocks || null; - } - - public getCodeDslSync(): CodeBlockDSL | null { - const root = this.get('root'); - return root?.codeBlocks || null; - } - - /** - * 设置代码块到dsl的codeBlocks字段 - * @param {CodeBlockDSL} codeDsl 代码DSL - * @returns {void} - */ - public async setCodeDsl(codeDsl: CodeBlockDSL): Promise { - if (!this.state.root) return; - this.state.root.codeBlocks = codeDsl; - } - private addModifiedNodeId(id: Id) { if (!this.isHistoryStateChange) { this.get('modifiedNodeIds').set(id, id);