From c68d4e05de3baa3b6cf1f842d228059a5af75371 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Mon, 11 Mar 2024 20:21:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(data-source,runtime):=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=E7=BC=96=E8=AF=91=E7=BB=84=E4=BB=B6=E6=97=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E8=A6=81=E7=BC=96=E8=BE=91=E5=AD=90=E5=85=83?= =?UTF-8?q?=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 编译时只需要关注组件本身的配置,子组件有自己的依赖,不需要由父组件来控制,但是在编辑器中就需要,因为为了不改动到编辑器中的dsl,编译后的配置是不会保存起来的,所以容器编译时需要把子组件也一同编译 --- packages/data-source/src/DataSourceManager.ts | 17 ++++++++++++----- .../data-source/src/createDataSourceManager.ts | 10 ++++++---- runtime/react/playground/main.tsx | 2 +- runtime/vue2/playground/App.vue | 2 +- runtime/vue3/playground/App.vue | 2 +- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index 48c2363e..32fbe103 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -162,9 +162,16 @@ class DataSourceManager extends EventEmitter { }); } - public compiledNode(node: MNode, sourceId?: Id) { - if (node.condResult === false) return node; - if (node.visible === false) return node; + public compiledNode({ items, ...node }: MNode, sourceId?: Id, deep = false) { + const newNode = cloneDeep(node); + + if (items) { + newNode.items = + Array.isArray(items) && deep ? items.map((item) => this.compiledNode(item, sourceId, deep)) : items; + } + + if (node.condResult === false) return newNode; + if (node.visible === false) return newNode; return compiledNode( (value: any) => { @@ -201,7 +208,7 @@ class DataSourceManager extends EventEmitter { return value; }, - cloneDeep(node), + newNode, this.app.dsl?.dataSourceDeps || {}, sourceId, ); @@ -229,7 +236,7 @@ class DataSourceManager extends EventEmitter { return compiledNode( (value: string) => template(value)(createIteratorContentData(itemData, dsId, fields)), - cloneDeep(item), + item, { [dsId]: { [item.id]: { diff --git a/packages/data-source/src/createDataSourceManager.ts b/packages/data-source/src/createDataSourceManager.ts index c4d763de..f3aab67b 100644 --- a/packages/data-source/src/createDataSourceManager.ts +++ b/packages/data-source/src/createDataSourceManager.ts @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { cloneDeep, union } from 'lodash-es'; +import { union } from 'lodash-es'; import type { AppCore } from '@tmagic/schema'; import { getDepNodeIds, getNodes } from '@tmagic/utils'; @@ -62,9 +62,11 @@ export const createDataSourceManager = (app: AppCore, useMock?: boolean, initial dataSourceManager.emit( 'update-data', getNodes(nodeIds, dsl.items).map((node) => { - const newNode = cloneDeep(node); - newNode.condResult = dataSourceManager.compliedConds(newNode); - return dataSourceManager.compiledNode(newNode); + if (app.platform !== 'editor') { + node.condResult = dataSourceManager.compliedConds(node); + } + + return dataSourceManager.compiledNode(node); }), sourceId, changeEvent, diff --git a/runtime/react/playground/main.tsx b/runtime/react/playground/main.tsx index 5eece53f..ebd207fe 100644 --- a/runtime/react/playground/main.tsx +++ b/runtime/react/playground/main.tsx @@ -114,7 +114,7 @@ const operations = { }, update({ config, root }: UpdateData) { - replaceChildNode(app.dataSourceManager?.compiledNode(config) || config, root.items); + replaceChildNode(app.dataSourceManager?.compiledNode(config, undefined, true) || config, root.items); updateConfig(cloneDeep(root)); }, diff --git a/runtime/vue2/playground/App.vue b/runtime/vue2/playground/App.vue index 979171d4..efb84b12 100644 --- a/runtime/vue2/playground/App.vue +++ b/runtime/vue2/playground/App.vue @@ -88,7 +88,7 @@ export default defineComponent({ update({ config, parentId }: UpdateData) { if (!root.value || !app) throw new Error('error'); - const newNode = app.dataSourceManager?.compiledNode(config) || config; + const newNode = app.dataSourceManager?.compiledNode(config, undefined, true) || config; replaceChildNode(reactive(newNode), [root.value], parentId); const nodeInstance = app.page?.getNode(config.id); diff --git a/runtime/vue3/playground/App.vue b/runtime/vue3/playground/App.vue index 00817717..8f2a4638 100644 --- a/runtime/vue3/playground/App.vue +++ b/runtime/vue3/playground/App.vue @@ -85,7 +85,7 @@ window.magic?.onRuntimeReady({ update({ config, parentId }: UpdateData) { if (!root.value || !app) throw new Error('error'); - const newNode = app.dataSourceManager?.compiledNode(config) || config; + const newNode = app.dataSourceManager?.compiledNode(config, undefined, true) || config; replaceChildNode(reactive(newNode), [root.value], parentId); const nodeInstance = app.page?.getNode(config.id);