From 8b7a1e4e3ca2faee77ddf19c0a16abc6d114a907 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 21 Sep 2023 10:58:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(editor):=20=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/fields/EventSelect.vue | 18 +++++++++++++++--- packages/editor/src/services/dataSource.ts | 11 +++++++++++ packages/editor/src/type.ts | 1 + packages/editor/src/utils/data-source/index.ts | 12 ++++++++++++ packages/editor/src/utils/props.ts | 1 + 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/fields/EventSelect.vue b/packages/editor/src/fields/EventSelect.vue index 02758cd6..b17375d3 100644 --- a/packages/editor/src/fields/EventSelect.vue +++ b/packages/editor/src/fields/EventSelect.vue @@ -52,6 +52,7 @@ import { computed, inject } from 'vue'; import { Delete } from '@element-plus/icons-vue'; import { has } from 'lodash-es'; +import type { EventOption } from '@tmagic/core'; import { TMagicButton } from '@tmagic/design'; import type { FieldProps, FormState } from '@tmagic/form'; import { ActionType } from '@tmagic/schema'; @@ -75,11 +76,22 @@ const eventNameConfig = computed(() => { text: '事件', type: 'select', labelWidth: '40px', - options: (mForm: FormState, { formValue }: any) => - services?.eventsService.getEvent(formValue.type).map((option: any) => ({ + options: (mForm: FormState, { formValue }: any) => { + let events: EventOption[] = []; + + if (!services) return events; + + if (props.config.src === 'component') { + events = services.eventsService.getEvent(formValue.type); + } else if (props.config.src === 'datasource') { + events = services.dataSourceService.getFormEvent(formValue.type); + } + + return events.map((option) => ({ text: option.label, value: option.value, - })), + })); + }, }; return { ...defaultEventNameConfig, ...props.config.eventNameConfig }; }); diff --git a/packages/editor/src/services/dataSource.ts b/packages/editor/src/services/dataSource.ts index 4ec89d30..c1107127 100644 --- a/packages/editor/src/services/dataSource.ts +++ b/packages/editor/src/services/dataSource.ts @@ -1,6 +1,7 @@ import { reactive } from 'vue'; import { cloneDeep } from 'lodash-es'; +import type { EventOption } from '@tmagic/core'; import type { FormConfig } from '@tmagic/form'; import type { DataSourceSchema } from '@tmagic/schema'; import { guid } from '@tmagic/utils'; @@ -16,6 +17,7 @@ interface State { editable: boolean; configs: Record; values: Record>; + events: Record; } type StateKey = keyof State; @@ -26,6 +28,7 @@ class DataSource extends BaseService { editable: true, configs: {}, values: {}, + events: {}, }); public set(name: K, value: T) { @@ -52,6 +55,14 @@ class DataSource extends BaseService { this.get('values')[type] = value; } + public getFormEvent(type = 'base') { + return this.get('events')[type] || []; + } + + public setFormEvent(type: string, value: EventOption[] = []) { + this.get('events')[type] = value; + } + public add(config: DataSourceSchema) { const newConfig = { ...config, diff --git a/packages/editor/src/type.ts b/packages/editor/src/type.ts index 6e2b4aef..55b4522b 100644 --- a/packages/editor/src/type.ts +++ b/packages/editor/src/type.ts @@ -418,6 +418,7 @@ export interface HistoryState { export interface EventSelectConfig { name: string; type: 'event-select'; + src: 'datasource' | 'component'; /** 事件名称表单配置 */ eventNameConfig?: FormItem; /** 动作类型配置 */ diff --git a/packages/editor/src/utils/data-source/index.ts b/packages/editor/src/utils/data-source/index.ts index bc4242e4..aa418896 100644 --- a/packages/editor/src/utils/data-source/index.ts +++ b/packages/editor/src/utils/data-source/index.ts @@ -29,6 +29,18 @@ const fillConfig = (config: FormConfig): FormConfig => [ }, ], }, + { + type: 'panel', + title: '事件配置', + display: false, + items: [ + { + name: 'events', + src: 'datasource', + type: 'event-select', + }, + ], + }, ]; export const getFormConfig = (type: string, configs: Record): FormConfig => { diff --git a/packages/editor/src/utils/props.ts b/packages/editor/src/utils/props.ts index b1cad0c7..f50be9c8 100644 --- a/packages/editor/src/utils/props.ts +++ b/packages/editor/src/utils/props.ts @@ -205,6 +205,7 @@ export const eventTabConfig: TabPaneConfig = { items: [ { name: 'events', + src: 'component', type: 'event-select', }, ],