Skip to content

Commit 74ea9f3

Browse files
author
keuby
authored
Merge pull request #40 from KNXCloud/feat-data-helper
feat(vue-simulator-renderer): 适配器支持设计器 dataHelper 参数
2 parents 101186f + b3fa0db commit 74ea9f3

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

packages/vue-simulator-renderer/src/interface.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'vue-router';
2-
import { Config } from '@knxcloud/lowcode-vue-renderer';
2+
import { Config, I18nMessages } from '@knxcloud/lowcode-vue-renderer';
33
import { Component, ComponentPublicInstance, App } from 'vue';
44
import { ComponentSchema, NpmInfo, RootSchema } from '@alilc/lowcode-types';
55
import { BuiltinSimulatorRenderer, DocumentModel, Node } from '@alilc/lowcode-designer';
@@ -37,9 +37,11 @@ export interface DocumentInstance {
3737
readonly id: string;
3838
readonly key: string;
3939
readonly path: string;
40+
readonly scope: Record<string, unknown>;
4041
readonly document: DocumentModel;
4142
readonly instancesMap: Map<string, ComponentInstance[]>;
4243
readonly schema: RootSchema;
44+
readonly messages: I18nMessages;
4345
getComponentInstance(id: number): ComponentInstance | null;
4446
mountInstance(id: string, instance: ComponentInstance): (() => void) | void;
4547
unmountIntance(id: string, instance: ComponentInstance): void;

packages/vue-simulator-renderer/src/simulator-view.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,19 @@ export const Renderer = defineComponent({
5353
},
5454
render() {
5555
const { documentInstance, simulator } = this;
56-
const { schema } = documentInstance;
56+
const { schema, scope, messages } = documentInstance;
5757
const { designMode, device, locale, components } = simulator;
5858

5959
if (!simulator.autoRender) return null;
6060

6161
return h(LowCodeRenderer, {
62+
scope: scope,
6263
schema: schema,
63-
components: components,
6464
locale: locale,
65-
messages: {},
66-
designMode: designMode,
6765
device: device,
66+
messages: messages,
67+
components: components,
68+
designMode: designMode,
6869
getNode: (id) => documentInstance.getNode(id),
6970
onCompGetCtx: (schema, ref) => documentInstance.mountInstance(schema.id!, ref),
7071
});

packages/vue-simulator-renderer/src/simulator.ts

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
onUnmounted,
1313
} from 'vue';
1414
import { config } from '@knxcloud/lowcode-vue-renderer';
15-
import { AssetLoader, buildComponents, getSubComponent } from '@knxcloud/lowcode-utils';
15+
import {
16+
AssetLoader,
17+
buildUtils,
18+
buildComponents,
19+
getSubComponent,
20+
} from '@knxcloud/lowcode-utils';
1621
import {
1722
ComponentInstance,
1823
DocumentInstance,
@@ -25,6 +30,7 @@ import { Slot, Leaf, Page } from './buildin-components';
2530
import { host } from './host';
2631
import {
2732
cursor,
33+
deepMerge,
2834
findDOMNodes,
2935
getClientRects,
3036
getCompRootData,
@@ -41,14 +47,28 @@ const loader = new AssetLoader();
4147

4248
const builtinComponents = { Slot, Leaf, Page };
4349

44-
function createDocumentInstance(document: DocumentModel): DocumentInstance {
50+
export interface ProjectContext {
51+
i18n: Record<string, object>;
52+
appHelper: {
53+
utils?: Record<string, unknown>;
54+
constants?: Record<string, unknown>;
55+
[x: string]: unknown;
56+
};
57+
}
58+
59+
function createDocumentInstance(
60+
document: DocumentModel,
61+
context: ProjectContext
62+
): DocumentInstance {
4563
/** 记录单个节点的组件实例列表 */
4664
const instancesMap = new Map<string, ComponentInstance[]>();
4765
/** 记录 vue 组件实例和组件 uid 的映射关系 */
4866
const vueInstanceMap = new Map<number, ComponentInstance>();
4967

5068
const timestamp = ref(Date.now());
5169

70+
const schema = computed(() => document.export(TransformStage.Render));
71+
5272
const checkInstanceMounted = (instance: ComponentInstance): boolean => {
5373
return instance.$.isMounted;
5474
};
@@ -141,8 +161,30 @@ function createDocumentInstance(document: DocumentModel): DocumentInstance {
141161
return fileName.startsWith('/') ? fileName : `/${fileName}`;
142162
}),
143163
key: computed(() => `${document.id}:${timestamp.value}`),
144-
schema: computed(() => document.export(TransformStage.Render)),
164+
scope: computed(() => {
165+
const _schema = schema.value;
166+
167+
const {
168+
utils: utilsInContext,
169+
constants: constantsInContext,
170+
...otherHelpers
171+
} = context.appHelper;
172+
173+
return {
174+
utils: {
175+
...utilsInContext,
176+
...buildUtils(host.libraryMap, Reflect.get(_schema, 'utils') ?? []),
177+
},
178+
constants: {
179+
...constantsInContext,
180+
...Reflect.get(_schema, 'constants'),
181+
},
182+
...otherHelpers,
183+
};
184+
}),
185+
schema: schema,
145186
document: computed(() => document),
187+
messages: computed(() => deepMerge(context.i18n, Reflect.get(schema.value, 'i18n'))),
146188
instancesMap: computed(() => instancesMap),
147189
getNode,
148190
mountInstance,
@@ -164,6 +206,14 @@ function createSimulatorRenderer() {
164206
const requestHandlersMap: Ref<Record<string, CallableFunction>> = shallowRef({});
165207
const documentInstances: Ref<DocumentInstance[]> = shallowRef([]);
166208

209+
const context: ProjectContext = reactive({
210+
i18n: {},
211+
appHelper: {
212+
utils: {},
213+
constants: {},
214+
},
215+
});
216+
167217
const disposeFunctions: Array<() => void> = [];
168218

169219
const documentInstanceMap = new Map<string, DocumentInstance>();
@@ -305,7 +355,7 @@ function createSimulatorRenderer() {
305355
documentInstances.value = host.project.documents.map((doc) => {
306356
let documentInstance = documentInstanceMap.get(doc.id);
307357
if (!documentInstance) {
308-
documentInstance = createDocumentInstance(doc);
358+
documentInstance = createDocumentInstance(doc, context);
309359
documentInstanceMap.set(doc.id, documentInstance);
310360
router.addRoute({
311361
name: documentInstance.id,
@@ -342,8 +392,16 @@ function createSimulatorRenderer() {
342392
}
343393
});
344394

345-
host.injectionConsumer.consume(() => {
346-
// TODO: handle designer injection
395+
host.injectionConsumer.consume((data) => {
396+
if (context.appHelper) {
397+
const { utils, constants, ...others } = data.appHelper;
398+
Object.assign(context.appHelper, {
399+
utils: Array.isArray(utils) ? buildUtils(host.libraryMap, utils) : utils ?? {},
400+
constants: constants ?? {},
401+
...others,
402+
});
403+
}
404+
context.i18n = data.i18n ?? {};
347405
});
348406

349407
return simulator;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isObject } from '@knxcloud/lowcode-utils';
2+
3+
export function deepMerge<TL, TR>(o1: TL, o2: TR): TL & TR {
4+
if (isObject(o1) && isObject(o2)) {
5+
const result = Object.assign({}, o1);
6+
Object.keys(o2).forEach((key) => {
7+
Reflect.set(result, key, deepMerge(o1[key], o2[key]));
8+
});
9+
return result as TL & TR;
10+
}
11+
return (o2 ?? o1) as TL & TR;
12+
}

packages/vue-simulator-renderer/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './find-dom-nodes';
77
export * from './logger';
88
export * from './cursor';
99
export * from './navtive-selection';
10+
export * from './deep-merge';

0 commit comments

Comments
 (0)