tmagic-editor/packages/data-source/src/createDataSourceManager.ts
roymondchen c68d4e05de feat(data-source,runtime): 数据源编译组件时新增一个参数控制是否要编辑子元素
编译时只需要关注组件本身的配置,子组件有自己的依赖,不需要由父组件来控制,但是在编辑器中就需要,因为为了不改动到编辑器中的dsl,编译后的配置是不会保存起来的,所以容器编译时需要把子组件也一同编译
2024-03-11 20:21:10 +08:00

79 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { union } from 'lodash-es';
import type { AppCore } from '@tmagic/schema';
import { getDepNodeIds, getNodes } from '@tmagic/utils';
import DataSourceManager from './DataSourceManager';
import type { ChangeEvent, DataSourceManagerData } from './types';
import { updateNode } from './utils';
/**
* 创建数据源管理器
* @param app AppCore
* @param useMock 是否使用mock数据
* @param initialData 初始化数据ssr数据可以由此传入
* @returns DataSourceManager | undefined
*/
export const createDataSourceManager = (app: AppCore, useMock?: boolean, initialData?: DataSourceManagerData) => {
const { dsl, platform } = app;
if (!dsl?.dataSources) return;
const dataSourceManager = new DataSourceManager({ app, useMock, initialData });
if (dsl.dataSources && dsl.dataSourceCondDeps && platform !== 'editor') {
getNodes(getDepNodeIds(dsl.dataSourceCondDeps), dsl.items).forEach((node) => {
node.condResult = dataSourceManager.compliedConds(node);
updateNode(node, dsl!);
});
}
if (dsl.dataSources && dsl.dataSourceDeps) {
getNodes(getDepNodeIds(dsl.dataSourceDeps), dsl.items).forEach((node) => {
updateNode(dataSourceManager.compiledNode(node), dsl!);
});
}
// ssr环境下数据应该是提前准备好的放到initialData中不应该发生变化无需监听
// 有initialData不一定是在ssr环境下
if (app.jsEngine !== 'nodejs') {
dataSourceManager.on('change', (sourceId: string, changeEvent: ChangeEvent) => {
const dep = dsl.dataSourceDeps?.[sourceId] || {};
const condDep = dsl.dataSourceCondDeps?.[sourceId] || {};
const nodeIds = union([...Object.keys(condDep), ...Object.keys(dep)]);
dataSourceManager.emit(
'update-data',
getNodes(nodeIds, dsl.items).map((node) => {
if (app.platform !== 'editor') {
node.condResult = dataSourceManager.compliedConds(node);
}
return dataSourceManager.compiledNode(node);
}),
sourceId,
changeEvent,
);
});
}
return dataSourceManager;
};