@@ -12,7 +12,12 @@ import {
1212 onUnmounted ,
1313} from 'vue' ;
1414import { 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' ;
1621import {
1722 ComponentInstance ,
1823 DocumentInstance ,
@@ -25,6 +30,7 @@ import { Slot, Leaf, Page } from './buildin-components';
2530import { host } from './host' ;
2631import {
2732 cursor ,
33+ deepMerge ,
2834 findDOMNodes ,
2935 getClientRects ,
3036 getCompRootData ,
@@ -41,14 +47,28 @@ const loader = new AssetLoader();
4147
4248const 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 ;
0 commit comments