From e4613ba0536d719c87dbab48a1f2d8d1822988b2 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Tue, 17 Oct 2023 20:21:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(editor,data-source):=20=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81=E5=85=B3=E8=81=94=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-source/src/DataSourceManager.ts | 30 ++++++++++++- .../src/fields/DataSourceFieldSelect.vue | 43 ++++++++++++------- packages/editor/src/type.ts | 1 + packages/editor/src/utils/dep.ts | 37 +++++++++++----- packages/editor/src/utils/props.ts | 1 + packages/utils/src/index.ts | 4 +- 6 files changed, 87 insertions(+), 29 deletions(-) diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index 3ac12877..c990dd41 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -21,7 +21,7 @@ import EventEmitter from 'events'; import { cloneDeep, template } from 'lodash-es'; import type { AppCore, DataSourceSchema, Id, MNode } from '@tmagic/schema'; -import { compiledCond, compiledNode } from '@tmagic/utils'; +import { compiledCond, compiledNode, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, isObject } from '@tmagic/utils'; import { DataSource, HttpDataSource } from './data-sources'; import type { DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types'; @@ -143,12 +143,40 @@ class DataSourceManager extends EventEmitter { return compiledNode( (value: any) => { + // 使用data-source-input等表单控件配置的字符串模板,如:`xxx${id.field}xxx` if (typeof value === 'string') { return template(value)(this.data); } + + // 使用data-source-select等表单控件配置的数据源,如:{ isBindDataSource: true, dataSourceId: 'xxx'} if (value?.isBindDataSource && value.dataSourceId) { return this.data[value.dataSourceId]; } + + // 使用data-source-field-select等表单控件的数据源字段,如:[`${DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX}${id}`, 'field'] + if (Array.isArray(value) && typeof value[0] === 'string') { + const [prefixId, ...fields] = value; + const prefixIndex = prefixId.indexOf(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX); + + if (prefixIndex > -1) { + const dsId = prefixId.substring(prefixIndex + DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX.length); + + const data = this.data[dsId]; + + if (!data) return value; + + return fields.reduce((accumulator, currentValue: any) => { + if (Array.isArray(accumulator)) return accumulator; + + if (isObject(accumulator)) { + return accumulator[currentValue]; + } + + return ''; + }, data); + } + } + return value; }, cloneDeep(node), diff --git a/packages/editor/src/fields/DataSourceFieldSelect.vue b/packages/editor/src/fields/DataSourceFieldSelect.vue index 0bb6b66b..ac40d3b0 100644 --- a/packages/editor/src/fields/DataSourceFieldSelect.vue +++ b/packages/editor/src/fields/DataSourceFieldSelect.vue @@ -12,7 +12,9 @@