From 5319afa563d1014707a346812d4714734c700589 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 24 Aug 2023 15:18:32 +0800 Subject: [PATCH] =?UTF-8?q?chore(editor):=20export=20getDisplayField=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/src/fields/DataSourceInput.vue | 48 +---------------- .../editor/src/utils/data-source/index.ts | 52 +++++++++++++++++++ packages/editor/src/utils/index.ts | 1 + 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/packages/editor/src/fields/DataSourceInput.vue b/packages/editor/src/fields/DataSourceInput.vue index fd79d8c4..e2585eef 100644 --- a/packages/editor/src/fields/DataSourceInput.vue +++ b/packages/editor/src/fields/DataSourceInput.vue @@ -54,6 +54,7 @@ import type { DataSchema, DataSourceSchema } from '@tmagic/schema'; import Icon from '@editor/components/Icon.vue'; import type { Services } from '@editor/type'; +import { getDisplayField } from '@editor/utils/data-source'; defineOptions({ name: 'MEditorDataSourceInput', @@ -87,52 +88,7 @@ const input = computed(() => autocomplete.value?.inputRef?.inp const dataSources = computed(() => dataSourceService?.get('dataSources') || []); const setDisplayState = () => { - displayState.value = []; - - // 匹配es6字符串模块 - const matches = state.value.matchAll(/\$\{([\s\S]+?)\}/g); - let index = 0; - for (const match of matches) { - if (typeof match.index === 'undefined') break; - - // 字符串常量 - displayState.value.push({ - type: 'text', - value: state.value.substring(index, match.index), - }); - - let dsText = ''; - let ds: DataSourceSchema | undefined; - let fields: DataSchema[] | undefined; - - // 将模块解析成数据源对应的值 - match[1].split('.').forEach((item, index) => { - if (index === 0) { - ds = dataSources.value.find((ds) => ds.id === item); - dsText += ds?.title || item; - fields = ds?.fields; - return; - } - - const field = fields?.find((field) => field.name === item); - fields = field?.fields; - dsText += `.${field?.title || item}`; - }); - - displayState.value.push({ - type: 'var', - value: dsText, - }); - - index = match.index + match[0].length; - } - - if (index < state.value.length) { - displayState.value.push({ - type: 'text', - value: state.value.substring(index), - }); - } + displayState.value = getDisplayField(dataSources.value, state.value); }; watch( diff --git a/packages/editor/src/utils/data-source/index.ts b/packages/editor/src/utils/data-source/index.ts index db681fbc..73af7325 100644 --- a/packages/editor/src/utils/data-source/index.ts +++ b/packages/editor/src/utils/data-source/index.ts @@ -1,4 +1,5 @@ import { FormConfig } from '@tmagic/form'; +import { DataSchema, DataSourceSchema } from '@tmagic/schema'; import { DatasourceTypeOption } from '@editor/type'; @@ -46,3 +47,54 @@ export const getFormConfig = ( return fillConfig(configs[type] || [], datasourceTypeList); } }; + +export const getDisplayField = (dataSources: DataSourceSchema[], key: string) => { + const displayState: { value: string; type: 'var' | 'text' }[] = []; + + // 匹配es6字符串模块 + const matches = key.matchAll(/\$\{([\s\S]+?)\}/g); + let index = 0; + for (const match of matches) { + if (typeof match.index === 'undefined') break; + + // 字符串常量 + displayState.push({ + type: 'text', + value: key.substring(index, match.index), + }); + + let dsText = ''; + let ds: DataSourceSchema | undefined; + let fields: DataSchema[] | undefined; + + // 将模块解析成数据源对应的值 + match[1].split('.').forEach((item, index) => { + if (index === 0) { + ds = dataSources.find((ds) => ds.id === item); + dsText += ds?.title || item; + fields = ds?.fields; + return; + } + + const field = fields?.find((field) => field.name === item); + fields = field?.fields; + dsText += `.${field?.title || item}`; + }); + + displayState.push({ + type: 'var', + value: dsText, + }); + + index = match.index + match[0].length; + } + + if (index < key.length) { + displayState.push({ + type: 'text', + value: key.substring(index), + }); + } + + return displayState; +}; diff --git a/packages/editor/src/utils/index.ts b/packages/editor/src/utils/index.ts index 8254ea25..6caf2fe8 100644 --- a/packages/editor/src/utils/index.ts +++ b/packages/editor/src/utils/index.ts @@ -21,3 +21,4 @@ export * from './props'; export * from './logger'; export * from './editor'; export * from './operator'; +export * from './data-source';