diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 55298321..2f0fa1df 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -44,7 +44,7 @@ interface EventCache { class App extends EventEmitter { public env; - public codeDsl: CodeBlockDSL | undefined; + public codeDsl: CodeBlockDSL; public pages = new Map(); public page: Page | undefined; @@ -61,6 +61,7 @@ class App extends EventEmitter { super(); this.env = new Env(options.ua); + this.codeDsl = options.config?.methods; options.platform && (this.platform = options.platform); options.jsEngine && (this.jsEngine = options.jsEngine); options.designWidth && (this.designWidth = options.designWidth); diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index d3a4e5a1..d73112de 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -66,10 +66,10 @@ class Node extends EventEmitter { private listenLifeSafe() { this.once('created', async (instance: any) => { this.instance = instance; - if (Array.isArray(this.data.created)) { + if (Array.isArray(this.data.created) && this.app?.codeDsl) { await Promise.all( this.data.created.map(async (codeId) => { - if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') { + if (this.app.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') { await this.app.codeDsl[codeId].content(this); } }), diff --git a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue index 8b19d35e..48a542ac 100644 --- a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue +++ b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue @@ -46,6 +46,15 @@ import { CodeBlockDSL, EditorMode } from '../../../type'; import codeBlockEditor from './CodeBlockEditor.vue'; +enum ErrorType { + UNDELETEABLE = 'undeleteable', + BIND = 'bind', +} + +const props = defineProps<{ + customError: (id: string, errorType: ErrorType) => any; +}>(); + const services = inject('services'); // 代码块列表 const codeList = ref(null); @@ -88,7 +97,11 @@ const deleteCode = (key: string) => { // 无绑定关系,且不在不可删除列表中 services?.codeBlockService.deleteCodeDslByIds([key]); } else { - ElMessage.error('代码块删除失败'); + if (typeof props.customError === 'function') { + props.customError(key, codeIds.includes(key) ? ErrorType.BIND : ErrorType.UNDELETEABLE); + } else { + ElMessage.error('代码块删除失败'); + } } }; diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index ad582e92..8f6e0085 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -80,7 +80,7 @@ export interface CodeBlockContent { /** 代码块名称 */ name: string; /** 代码块内容 */ - content: string; + content: any; } export interface PastePosition { left?: number;