@easy-editor/react-renderer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +9 -0
  3. package/README.md +4 -0
  4. package/dist/cjs/index.development.js +2149 -0
  5. package/dist/cjs/index.development.js.map +1 -0
  6. package/dist/cjs/index.js +2149 -0
  7. package/dist/cjs/index.js.map +1 -0
  8. package/dist/cjs/index.production.js +2149 -0
  9. package/dist/cjs/index.production.js.map +1 -0
  10. package/dist/configure-renderer/SettingSetter.d.ts +7 -0
  11. package/dist/configure-renderer/context.d.ts +12 -0
  12. package/dist/configure-renderer/index.d.ts +10 -0
  13. package/dist/esm/index.development.js +2114 -0
  14. package/dist/esm/index.development.js.map +1 -0
  15. package/dist/esm/index.js +2114 -0
  16. package/dist/esm/index.js.map +1 -0
  17. package/dist/esm/index.production.js +2114 -0
  18. package/dist/esm/index.production.js.map +1 -0
  19. package/dist/index.d.ts +3 -0
  20. package/dist/index.js +2113 -0
  21. package/dist/renderer-core/adapter/index.d.ts +17 -0
  22. package/dist/renderer-core/base.d.ts +11 -0
  23. package/dist/renderer-core/component.d.ts +2 -0
  24. package/dist/renderer-core/components/FaultComponent.d.ts +7 -0
  25. package/dist/renderer-core/components/NotFoundComponent.d.ts +7 -0
  26. package/dist/renderer-core/context.d.ts +6 -0
  27. package/dist/renderer-core/hoc/comp.d.ts +2 -0
  28. package/dist/renderer-core/hoc/index.d.ts +2 -0
  29. package/dist/renderer-core/hoc/leaf.d.ts +27 -0
  30. package/dist/renderer-core/index.d.ts +9 -0
  31. package/dist/renderer-core/page.d.ts +2 -0
  32. package/dist/renderer-core/renderer.d.ts +2 -0
  33. package/dist/renderer-core/types.d.ts +187 -0
  34. package/dist/renderer-core/utils/classnames.d.ts +1 -0
  35. package/dist/renderer-core/utils/common.d.ts +54 -0
  36. package/dist/renderer-core/utils/hoc.d.ts +2 -0
  37. package/dist/renderer-core/utils/index.d.ts +4 -0
  38. package/dist/renderer-core/utils/logger.d.ts +5 -0
  39. package/package.json +82 -0
@@ -0,0 +1,2149 @@
1
+ 'use strict';
2
+
3
+ var mobxReact = require('mobx-react');
4
+ var react = require('react');
5
+ var core = require('@easy-editor/core');
6
+ var lodashEs = require('lodash-es');
7
+
8
+ function _extends() {
9
+ return _extends = Object.assign ? Object.assign.bind() : function (n) {
10
+ for (var e = 1; e < arguments.length; e++) {
11
+ var t = arguments[e];
12
+ for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
13
+ }
14
+ return n;
15
+ }, _extends.apply(null, arguments);
16
+ }
17
+
18
+ const SettingRendererContext = /*#__PURE__*/react.createContext({});
19
+ const useSettingRendererContext = () => {
20
+ try {
21
+ return react.useContext(SettingRendererContext);
22
+ } catch (error) {
23
+ console.warn('useSettingRendererContext must be used within a SettingRendererContextProvider');
24
+ }
25
+ return {};
26
+ };
27
+
28
+ const getSetterInfo = field => {
29
+ const {
30
+ extraProps,
31
+ setter
32
+ } = field;
33
+ const {
34
+ defaultValue
35
+ } = extraProps;
36
+ let setterProps = {};
37
+ let setterType;
38
+ let initialValue = null;
39
+ if (core.isSetterConfig(setter)) {
40
+ setterType = setter.componentName;
41
+ if (setter.props) {
42
+ setterProps = setter.props;
43
+ if (typeof setterProps === 'function') {
44
+ setterProps = setterProps(field);
45
+ }
46
+ }
47
+ if (setter.defaultValue != null) {
48
+ initialValue = setter.defaultValue;
49
+ }
50
+ } else if (setter) {
51
+ setterType = setter;
52
+ }
53
+ if (defaultValue != null && !('defaultValue' in setterProps)) {
54
+ setterProps.defaultValue = defaultValue;
55
+ if (initialValue == null) {
56
+ initialValue = defaultValue;
57
+ }
58
+ }
59
+ if (field.valueState === -1) {
60
+ setterProps.multiValue = true;
61
+ }
62
+
63
+ // 根据是否支持变量配置做相应的更改
64
+ const supportVariable = field.extraProps?.supportVariable;
65
+ const isUseVariableSetter = supportVariable;
66
+ if (isUseVariableSetter === false) {
67
+ return {
68
+ setterProps,
69
+ initialValue,
70
+ setterType
71
+ };
72
+ }
73
+ return {
74
+ setterProps,
75
+ setterType,
76
+ initialValue
77
+ };
78
+ };
79
+ const SettingSetter = mobxReact.observer(({
80
+ field,
81
+ children
82
+ }) => {
83
+ const {
84
+ setterManager
85
+ } = useSettingRendererContext();
86
+ const {
87
+ extraProps
88
+ } = field;
89
+ const visible = extraProps?.condition && typeof extraProps.condition === 'function' ? extraProps.condition(field) !== false : true;
90
+ if (!visible) {
91
+ return null;
92
+ }
93
+ const {
94
+ setterProps = {},
95
+ setterType,
96
+ initialValue = null
97
+ } = getSetterInfo(field);
98
+ const onChange = extraProps?.onChange;
99
+ const value = field.valueState === -1 ? null : field.getValue();
100
+ const {
101
+ component: SetterComponent,
102
+ props: mixedSetterProps
103
+ } = setterManager.createSetterContent(setterType, setterProps);
104
+ return /*#__PURE__*/React.createElement(SetterComponent, _extends({
105
+ key: field.id,
106
+ field: field,
107
+ selected: field.top?.getNode(),
108
+ initialValue: initialValue,
109
+ value: value,
110
+ onChange: newVal => {
111
+ field.setValue(newVal);
112
+ onChange?.(field, newVal);
113
+ },
114
+ onInitial: () => {
115
+ if (initialValue == null) {
116
+ return;
117
+ }
118
+ const value = typeof initialValue === 'function' ? initialValue(field) : initialValue;
119
+ field.setValue(value, true);
120
+ },
121
+ removeProp: () => {
122
+ if (field.name) {
123
+ field.parent.clearPropValue(field.name);
124
+ }
125
+ }
126
+ }, mixedSetterProps), children);
127
+ });
128
+
129
+ const SettingFieldItem = mobxReact.observer(({
130
+ field
131
+ }) => {
132
+ const {
133
+ customFieldItem
134
+ } = useSettingRendererContext();
135
+ if (customFieldItem) {
136
+ return customFieldItem(field, /*#__PURE__*/React.createElement(SettingSetter, {
137
+ field: field
138
+ }));
139
+ }
140
+ return /*#__PURE__*/React.createElement("div", {
141
+ className: "space-y-2"
142
+ }, /*#__PURE__*/React.createElement("label", {
143
+ htmlFor: field.id,
144
+ className: "block text-sm font-medium text-gray-700"
145
+ }, field.title), /*#__PURE__*/React.createElement(SettingSetter, {
146
+ field: field
147
+ }));
148
+ });
149
+ const SettingFieldGroup = mobxReact.observer(({
150
+ field
151
+ }) => {
152
+ const {
153
+ customFieldGroup
154
+ } = useSettingRendererContext();
155
+ if (customFieldGroup) {
156
+ return customFieldGroup(field, /*#__PURE__*/React.createElement(SettingSetter, {
157
+ field: field
158
+ }, field.items?.map(item => /*#__PURE__*/React.createElement(SettingFieldView, {
159
+ key: item.id,
160
+ field: item
161
+ }))));
162
+ }
163
+ return /*#__PURE__*/React.createElement(SettingSetter, {
164
+ field: field
165
+ }, field.items?.map(item => /*#__PURE__*/React.createElement(SettingFieldView, {
166
+ key: item.id,
167
+ field: item
168
+ })));
169
+ });
170
+ const SettingFieldView = ({
171
+ field
172
+ }) => {
173
+ if (field.isGroup) {
174
+ return /*#__PURE__*/React.createElement(SettingFieldGroup, {
175
+ field: field,
176
+ key: field.id
177
+ });
178
+ } else {
179
+ return /*#__PURE__*/React.createElement(SettingFieldItem, {
180
+ field: field,
181
+ key: field.id
182
+ });
183
+ }
184
+ };
185
+ const SettingRender = mobxReact.observer(props => {
186
+ const {
187
+ editor,
188
+ customFieldItem,
189
+ customFieldGroup
190
+ } = props;
191
+ const designer = editor.get('designer');
192
+ const setterManager = editor.get('setterManager');
193
+ const {
194
+ settingsManager
195
+ } = designer;
196
+ const {
197
+ settings
198
+ } = settingsManager;
199
+ const items = settings?.items;
200
+ const ctx = react.useMemo(() => {
201
+ const ctx = {};
202
+ ctx.setterManager = setterManager;
203
+ ctx.settingsManager = settingsManager;
204
+ ctx.customFieldItem = customFieldItem;
205
+ ctx.customFieldGroup = customFieldGroup;
206
+ return ctx;
207
+ }, [setterManager, settingsManager, customFieldItem, customFieldGroup]);
208
+ if (!settings) {
209
+ // 未选中节点,提示选中 或者 显示根节点设置
210
+ return /*#__PURE__*/React.createElement("div", {
211
+ className: "lc-settings-main"
212
+ }, /*#__PURE__*/React.createElement("div", {
213
+ className: "lc-settings-notice"
214
+ }, /*#__PURE__*/React.createElement("p", null, "Please select a node in canvas")));
215
+ }
216
+
217
+ // 当节点被锁定,且未开启锁定后容器可设置属性
218
+ if (settings.isLocked) {
219
+ return /*#__PURE__*/React.createElement("div", {
220
+ className: "lc-settings-main"
221
+ }, /*#__PURE__*/React.createElement("div", {
222
+ className: "lc-settings-notice"
223
+ }, /*#__PURE__*/React.createElement("p", null, "Current node is locked")));
224
+ }
225
+ if (Array.isArray(settings.items) && settings.items.length === 0) {
226
+ return /*#__PURE__*/React.createElement("div", {
227
+ className: "lc-settings-main"
228
+ }, /*#__PURE__*/React.createElement("div", {
229
+ className: "lc-settings-notice"
230
+ }, /*#__PURE__*/React.createElement("p", null, "No config found for this type of component")));
231
+ }
232
+ if (!settings.isSameComponent) {
233
+ // TODO: future support 获取设置项交集编辑
234
+ return /*#__PURE__*/React.createElement("div", {
235
+ className: "lc-settings-main"
236
+ }, /*#__PURE__*/React.createElement("div", {
237
+ className: "lc-settings-notice"
238
+ }, /*#__PURE__*/React.createElement("p", null, "Please select same kind of components")));
239
+ }
240
+ return /*#__PURE__*/React.createElement(SettingRendererContext.Provider, {
241
+ value: ctx
242
+ }, items?.map(item => /*#__PURE__*/React.createElement(SettingFieldView, {
243
+ key: item.id,
244
+ field: item
245
+ })));
246
+ });
247
+
248
+ class Adapter {
249
+ renderers = {};
250
+ setRenderers(renderers) {
251
+ this.renderers = renderers;
252
+ }
253
+ setBaseRenderer(BaseRenderer) {
254
+ this.renderers.BaseRenderer = BaseRenderer;
255
+ }
256
+ setPageRenderer(PageRenderer) {
257
+ this.renderers.PageRenderer = PageRenderer;
258
+ }
259
+ setComponentRenderer(ComponentRenderer) {
260
+ this.renderers.ComponentRenderer = ComponentRenderer;
261
+ }
262
+ getRenderers() {
263
+ return this.renderers || {};
264
+ }
265
+ }
266
+ const adapter = new Adapter();
267
+
268
+ const RendererContext = /*#__PURE__*/react.createContext({});
269
+ const useRendererContext = () => {
270
+ try {
271
+ return react.useContext(RendererContext);
272
+ } catch (error) {
273
+ console.warn('useRendererContext must be used within a RendererContextProvider');
274
+ }
275
+ return {};
276
+ };
277
+ function contextFactory() {
278
+ let context = window.__appContext;
279
+ if (!context) {
280
+ context = /*#__PURE__*/react.createContext({});
281
+ window.__appContext = context;
282
+ }
283
+ return context;
284
+ }
285
+
286
+ const classnames = (...args) => {
287
+ return args.filter(Boolean).join(' ');
288
+ };
289
+
290
+ const logger = core.createLogger('Renderer');
291
+
292
+ const PropTypes2 = true;
293
+ function inSameDomain() {
294
+ try {
295
+ return window.parent !== window && window.parent.location.host === window.location.host;
296
+ } catch (e) {
297
+ return false;
298
+ }
299
+ }
300
+ function getFileCssName(fileName) {
301
+ if (!fileName) {
302
+ return;
303
+ }
304
+ const name = fileName.replace(/([A-Z])/g, '-$1').toLowerCase();
305
+ return `lce-${name}`.split('-').filter(p => !!p).join('-');
306
+ }
307
+ const isSchema = schema => {
308
+ if (!schema) {
309
+ return false;
310
+ }
311
+ if (schema.componentName === 'Leaf' || schema.componentName === 'Slot') {
312
+ return true;
313
+ }
314
+ if (Array.isArray(schema)) {
315
+ return schema.every(item => isSchema(item));
316
+ }
317
+ const isValidProps = props => {
318
+ if (!props) {
319
+ return false;
320
+ }
321
+ return typeof schema.props === 'object' && !Array.isArray(props);
322
+ };
323
+ return !!(schema.componentName && isValidProps(schema.props));
324
+ };
325
+ const getValue = (obj, path, defaultValue = {}) => {
326
+ if (Array.isArray(obj)) {
327
+ return defaultValue;
328
+ }
329
+ if (!obj || typeof obj !== 'object') {
330
+ return defaultValue;
331
+ }
332
+ const res = path.split('.').reduce((pre, cur) => {
333
+ return pre && pre[cur];
334
+ }, obj);
335
+ if (res === undefined) {
336
+ return defaultValue;
337
+ }
338
+ return res;
339
+ };
340
+ function transformArrayToMap(arr, key, overwrite = true) {
341
+ if (!arr || !Array.isArray(arr)) {
342
+ return {};
343
+ }
344
+ const res = {};
345
+ arr.forEach(item => {
346
+ const curKey = item[key];
347
+ if (item[key] === undefined) {
348
+ return;
349
+ }
350
+ if (res[curKey] && !overwrite) {
351
+ return;
352
+ }
353
+ res[curKey] = item;
354
+ });
355
+ return res;
356
+ }
357
+ const parseData = (schema, self, options = {}) => {
358
+ if (core.isJSExpression(schema)) {
359
+ return parseExpression({
360
+ str: schema,
361
+ self,
362
+ thisRequired: options.thisRequiredInJSE,
363
+ logScope: options.logScope
364
+ });
365
+ }
366
+ if (typeof schema === 'string') {
367
+ return schema.trim();
368
+ } else if (Array.isArray(schema)) {
369
+ return schema.map(item => parseData(item, self, options));
370
+ } else if (typeof schema === 'function') {
371
+ return schema.bind(self);
372
+ } else if (typeof schema === 'object') {
373
+ if (!schema) {
374
+ return schema;
375
+ }
376
+ const res = {};
377
+ Object.entries(schema).forEach(([key, val]) => {
378
+ if (key.startsWith('__')) {
379
+ return;
380
+ }
381
+ res[key] = parseData(val, self, options);
382
+ });
383
+ return res;
384
+ }
385
+ return schema;
386
+ };
387
+ const isUseLoop = (loop, isDesignMode) => {
388
+ if (!isDesignMode) {
389
+ return true;
390
+ }
391
+ if (!Array.isArray(loop)) {
392
+ return false;
393
+ }
394
+ return loop.length > 0;
395
+ };
396
+ function checkPropTypes(value, name, rule, componentName) {
397
+ let ruleFunction = rule;
398
+ if (typeof rule === 'string') {
399
+ ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${rule}`)(PropTypes2);
400
+ }
401
+ if (!ruleFunction || typeof ruleFunction !== 'function') {
402
+ logger.warn('checkPropTypes should have a function type rule argument');
403
+ return true;
404
+ }
405
+ const err = ruleFunction({
406
+ [name]: value
407
+ }, name, componentName, 'prop', null
408
+ );
409
+ if (err) {
410
+ logger.warn(err);
411
+ }
412
+ return !err;
413
+ }
414
+ function transformStringToFunction(str) {
415
+ if (typeof str !== 'string') {
416
+ return str;
417
+ }
418
+ if (inSameDomain() && window.parent.__newFunc) {
419
+ return window.parent.__newFunc(`"use strict"; return ${str}`)();
420
+ } else {
421
+ return new Function(`"use strict"; return ${str}`)();
422
+ }
423
+ }
424
+ function parseExpression(a, b, c = false) {
425
+ let str;
426
+ let self;
427
+ let thisRequired;
428
+ let logScope;
429
+ if (typeof a === 'object' && b === undefined) {
430
+ str = a.str;
431
+ self = a.self;
432
+ thisRequired = a.thisRequired;
433
+ logScope = a.logScope;
434
+ } else {
435
+ str = a;
436
+ self = b;
437
+ thisRequired = c;
438
+ }
439
+ try {
440
+ const contextArr = ['"use strict";', 'var __self = arguments[0];'];
441
+ contextArr.push('return ');
442
+ let tarStr;
443
+ tarStr = (str.value || '').trim();
444
+ tarStr = tarStr.replace(/this(\W|$)/g, (_a, b) => `__self${b}`);
445
+ tarStr = contextArr.join('\n') + tarStr;
446
+ if (inSameDomain() && window.parent.__newFunc) {
447
+ return window.parent.__newFunc(tarStr)(self);
448
+ }
449
+ const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`;
450
+ return new Function('$scope', code)(self);
451
+ } catch (err) {
452
+ logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self);
453
+ return undefined;
454
+ }
455
+ }
456
+ function parseThisRequiredExpression(str, self) {
457
+ return parseExpression(str, self, true);
458
+ }
459
+ function isString(str) {
460
+ return {}.toString.call(str) === '[object String]';
461
+ }
462
+ function capitalizeFirstLetter(word) {
463
+ if (!word || !isString(word) || word.length === 0) {
464
+ return word;
465
+ }
466
+ return word[0].toUpperCase() + word.slice(1);
467
+ }
468
+ const isReactClass = obj => {
469
+ return obj && obj.prototype && (obj.prototype.isReactComponent || obj.prototype instanceof react.Component);
470
+ };
471
+ function isReactComponent(obj) {
472
+ return obj && (isReactClass(obj) || typeof obj === 'function');
473
+ }
474
+
475
+ const excludePropertyNames = ['$$typeof', 'render', 'defaultProps', 'props', 'length', 'prototype', 'name', 'caller', 'callee', 'arguments'];
476
+ const cloneEnumerableProperty = (target, origin, excludes = excludePropertyNames) => {
477
+ const compExtraPropertyNames = Object.keys(origin).filter(d => !excludes.includes(d));
478
+ compExtraPropertyNames.forEach(d => {
479
+ target[d] = origin[d];
480
+ });
481
+ return target;
482
+ };
483
+ const createForwardRefHocElement = (Wrapper, Comp) => {
484
+ const WrapperComponent = cloneEnumerableProperty(/*#__PURE__*/react.forwardRef((props, ref) => {
485
+ return /*#__PURE__*/react.createElement(Wrapper, {
486
+ ...props,
487
+ forwardRef: ref
488
+ });
489
+ }), Comp);
490
+ WrapperComponent.displayName = Comp.displayName;
491
+ return WrapperComponent;
492
+ };
493
+
494
+ const patchDidCatch = (Comp, {
495
+ baseRenderer
496
+ }) => {
497
+ if (Comp.patchedCatch) {
498
+ return;
499
+ }
500
+ Comp.patchedCatch = true;
501
+ const originalDidCatch = Comp.prototype.componentDidCatch;
502
+ Comp.prototype.componentDidCatch = function didCatch(error, errorInfo) {
503
+ this.setState({
504
+ engineRenderError: true,
505
+ error
506
+ });
507
+ if (originalDidCatch && typeof originalDidCatch === 'function') {
508
+ originalDidCatch.call(this, error, errorInfo);
509
+ }
510
+ };
511
+ const {
512
+ engine
513
+ } = baseRenderer.context;
514
+ const originRender = Comp.prototype.render;
515
+ Comp.prototype.render = function () {
516
+ if (this.state && this.state.engineRenderError) {
517
+ this.state.engineRenderError = false;
518
+ return engine.createElement(engine.getFaultComponent(), {
519
+ ...this.props,
520
+ error: this.state.error,
521
+ componentName: this.props._componentName
522
+ });
523
+ }
524
+ return originRender.call(this);
525
+ };
526
+ if (!(Comp.prototype instanceof react.PureComponent)) {
527
+ const originShouldComponentUpdate = Comp.prototype.shouldComponentUpdate;
528
+ Comp.prototype.shouldComponentUpdate = function (nextProps, nextState) {
529
+ if (nextState && nextState.engineRenderError) {
530
+ return true;
531
+ }
532
+ return originShouldComponentUpdate ? originShouldComponentUpdate.call(this, nextProps, nextState) : true;
533
+ };
534
+ }
535
+ };
536
+ const cache$1 = new Map();
537
+ const compWrapper = (Comp, info) => {
538
+ if (Comp?.prototype?.isReactComponent || Comp?.prototype instanceof react.Component) {
539
+ patchDidCatch(Comp, info);
540
+ return Comp;
541
+ }
542
+ if (info.schema.id && cache$1.has(info.schema.id) && cache$1.get(info.schema.id)?.Comp === Comp) {
543
+ return cache$1.get(info.schema.id)?.WrapperComponent;
544
+ }
545
+ class Wrapper extends react.Component {
546
+ static displayName = Comp.displayName;
547
+ render() {
548
+ const {
549
+ forwardRef,
550
+ ...rest
551
+ } = this.props;
552
+ // @ts-ignore
553
+ return /*#__PURE__*/react.createElement(Comp, {
554
+ ...rest,
555
+ ref: forwardRef
556
+ });
557
+ }
558
+ }
559
+ patchDidCatch(Wrapper, info);
560
+ const WrapperComponent = createForwardRefHocElement(Wrapper, Comp);
561
+ info.schema.id && cache$1.set(info.schema.id, {
562
+ WrapperComponent,
563
+ Comp
564
+ });
565
+ return WrapperComponent;
566
+ };
567
+
568
+ var RerenderType = /*#__PURE__*/function (RerenderType) {
569
+ RerenderType["All"] = "All";
570
+ RerenderType["ChildChanged"] = "ChildChanged";
571
+ RerenderType["PropsChanged"] = "PropsChanged";
572
+ RerenderType["VisibleChanged"] = "VisibleChanged";
573
+ RerenderType["MinimalRenderUnit"] = "MinimalRenderUnit";
574
+ return RerenderType;
575
+ }(RerenderType || {}); // 缓存 Leaf 层组件,防止重新渲染问题
576
+ class LeafCache {
577
+ /** 组件缓存 */
578
+ component = new Map();
579
+
580
+ /**
581
+ * 状态缓存,场景:属性变化后,改组件被销毁,state 为空,没有展示修改后的属性
582
+ */
583
+ state = new Map();
584
+
585
+ /**
586
+ * 订阅事件缓存,导致 rerender 的订阅事件
587
+ */
588
+ event = new Map();
589
+ ref = new Map();
590
+ constructor(documentId, device) {
591
+ this.documentId = documentId;
592
+ this.device = device;
593
+ }
594
+ }
595
+ let cache;
596
+
597
+ /** 部分没有渲染的 node 节点进行兜底处理 or 渲染方式没有渲染 LeafWrapper */
598
+ const initRerenderEvent = ({
599
+ schema,
600
+ container,
601
+ getNode
602
+ }) => {
603
+ const leaf = getNode?.(schema.id);
604
+ if (!leaf || cache.event.get(schema.id)?.clear || leaf === cache.event.get(schema.id)) {
605
+ return;
606
+ }
607
+ cache.event.get(schema.id)?.dispose.forEach(disposeFn => disposeFn && disposeFn());
608
+ const debounceRerender = lodashEs.debounce(() => {
609
+ container.rerender();
610
+ }, 20);
611
+ cache.event.set(schema.id, {
612
+ clear: false,
613
+ leaf,
614
+ dispose: [leaf?.onPropChange?.(() => {
615
+ if (!container.autoRepaintNode) {
616
+ return;
617
+ }
618
+ logger.log(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onPropsChange make rerender`);
619
+ debounceRerender();
620
+ }), leaf?.onChildrenChange?.(() => {
621
+ if (!container.autoRepaintNode) {
622
+ return;
623
+ }
624
+ logger.log(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onChildrenChange make rerender`);
625
+ debounceRerender();
626
+ }), leaf?.onVisibleChange?.(() => {
627
+ if (!container.autoRepaintNode) {
628
+ return;
629
+ }
630
+ logger.log(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onVisibleChange make rerender`);
631
+ debounceRerender();
632
+ })]
633
+ });
634
+ };
635
+
636
+ /** 渲染的 node 节点全局注册事件清除 */
637
+ const clearRerenderEvent = id => {
638
+ if (cache.event.get(id)?.clear) {
639
+ return;
640
+ }
641
+ cache.event.get(id)?.dispose?.forEach(disposeFn => disposeFn && disposeFn());
642
+ cache.event.set(id, {
643
+ clear: true,
644
+ dispose: []
645
+ });
646
+ };
647
+
648
+ // 给每个组件包裹一个 HOC Leaf,支持组件内部属性变化,自响应渲染
649
+ const leafWrapper = (Comp, {
650
+ schema,
651
+ baseRenderer,
652
+ componentInfo,
653
+ scope
654
+ }) => {
655
+ const {
656
+ __getComponentProps: getProps,
657
+ __getSchemaChildrenVirtualDom: getChildren,
658
+ __parseData
659
+ } = baseRenderer;
660
+ const {
661
+ engine
662
+ } = baseRenderer.context;
663
+ const host = baseRenderer.props?.__host;
664
+ const curDocumentId = baseRenderer.props?.documentId ?? '';
665
+ const curDevice = baseRenderer.props?.device ?? '';
666
+ const getNode = baseRenderer.props?.getNode;
667
+ const container = baseRenderer.props?.__container;
668
+ const setSchemaChangedSymbol = baseRenderer.props?.setSchemaChangedSymbol;
669
+ const designer = host?.designer;
670
+ const componentCacheId = schema.id;
671
+ if (!cache || curDocumentId && curDocumentId !== cache.documentId || curDevice && curDevice !== cache.device) {
672
+ cache?.event.forEach(event => {
673
+ event.dispose?.forEach(disposeFn => disposeFn && disposeFn());
674
+ });
675
+ cache = new LeafCache(curDocumentId, curDevice);
676
+ }
677
+
678
+ // if (!isReactComponent(Comp)) {
679
+ // logger.error(`${schema.componentName} component may be has errors: `, Comp)
680
+ // }
681
+
682
+ initRerenderEvent({
683
+ schema,
684
+ container,
685
+ getNode
686
+ });
687
+ if (curDocumentId && cache.component.has(componentCacheId) && cache.component.get(componentCacheId).Comp === Comp) {
688
+ return cache.component.get(componentCacheId).LeafWrapper;
689
+ }
690
+ class LeafHoc extends react.Component {
691
+ recordInfo = {};
692
+ curEventLeaf;
693
+ static displayName = schema.componentName;
694
+ disposeFunctions = [];
695
+ __component_tag = 'leafWrapper';
696
+ renderUnitInfo;
697
+
698
+ // 最小渲染单元做防抖处理
699
+ makeUnitRenderDebounced = lodashEs.debounce(() => {
700
+ this.beforeRender(RerenderType.MinimalRenderUnit);
701
+ const schema = this.leaf?.export?.(core.TRANSFORM_STAGE.RENDER);
702
+ if (!schema) {
703
+ return;
704
+ }
705
+ const nextProps = getProps(schema, scope, Comp, componentInfo);
706
+ const children = getChildren(schema, scope, Comp);
707
+ const nextState = {
708
+ nodeProps: nextProps,
709
+ nodeChildren: children,
710
+ childrenInState: true
711
+ };
712
+ if ('children' in nextProps) {
713
+ nextState.nodeChildren = nextProps.children;
714
+ }
715
+ logger.log(`${this.leaf?.componentName}(${this.leaf?.id}) MinimalRenderUnit Render!`);
716
+ this.setState(nextState);
717
+ }, 20);
718
+ constructor(props) {
719
+ super(props);
720
+ // 监听以下事件,当变化时更新自己
721
+ logger.log(`${schema.componentName}[${this.leaf?.id}] leaf render in SimulatorRendererView`);
722
+ componentCacheId && clearRerenderEvent(componentCacheId);
723
+ this.curEventLeaf = this.leaf;
724
+ cache.ref.set(componentCacheId, {
725
+ makeUnitRender: this.makeUnitRender
726
+ });
727
+ let cacheState = cache.state.get(componentCacheId);
728
+ if (!cacheState || cacheState.__tag !== props.__tag) {
729
+ cacheState = this.getDefaultState(props);
730
+ }
731
+ this.state = cacheState;
732
+ }
733
+ recordTime = () => {
734
+ if (!this.recordInfo.startTime) {
735
+ return;
736
+ }
737
+ const endTime = Date.now();
738
+ const nodeCount = host?.designer?.currentDocument?.getNodeCount?.();
739
+ const componentName = this.recordInfo.node?.componentName || this.leaf?.componentName || 'UnknownComponent';
740
+ designer?.postEvent(core.DESIGNER_EVENT.NODE_RENDER, {
741
+ componentName,
742
+ time: endTime - this.recordInfo.startTime,
743
+ type: this.recordInfo.type,
744
+ nodeCount
745
+ });
746
+ this.recordInfo.startTime = null;
747
+ };
748
+ makeUnitRender = () => {
749
+ this.makeUnitRenderDebounced();
750
+ };
751
+ get autoRepaintNode() {
752
+ return container?.autoRepaintNode;
753
+ }
754
+ componentDidUpdate() {
755
+ this.recordTime();
756
+ }
757
+ componentDidMount() {
758
+ const _leaf = this.leaf;
759
+ this.initOnPropsChangeEvent(_leaf);
760
+ this.initOnChildrenChangeEvent(_leaf);
761
+ this.initOnVisibleChangeEvent(_leaf);
762
+ this.recordTime();
763
+ }
764
+ getDefaultState(nextProps) {
765
+ const {
766
+ hidden = false,
767
+ condition = true
768
+ } = nextProps.__inner__ || this.leaf?.export?.(core.TRANSFORM_STAGE.RENDER) || {};
769
+ return {
770
+ nodeChildren: null,
771
+ childrenInState: false,
772
+ visible: !hidden,
773
+ condition: __parseData?.(condition, scope),
774
+ nodeCacheProps: {},
775
+ nodeProps: {}
776
+ };
777
+ }
778
+ setState(state) {
779
+ cache.state.set(componentCacheId, {
780
+ ...this.state,
781
+ ...state,
782
+ __tag: this.props.__tag
783
+ });
784
+ super.setState(state);
785
+ }
786
+
787
+ /** 由于内部属性变化,在触发渲染前,会执行该函数 */
788
+ beforeRender(type, node) {
789
+ this.recordInfo.startTime = Date.now();
790
+ this.recordInfo.type = type;
791
+ this.recordInfo.node = node;
792
+ setSchemaChangedSymbol?.(true);
793
+ }
794
+ judgeMiniUnitRender() {
795
+ if (!this.renderUnitInfo) {
796
+ this.getRenderUnitInfo();
797
+ }
798
+ const renderUnitInfo = this.renderUnitInfo || {
799
+ singleRender: true
800
+ };
801
+ if (renderUnitInfo.singleRender) {
802
+ return;
803
+ }
804
+ const ref = cache.ref.get(renderUnitInfo.minimalUnitId);
805
+ if (!ref) {
806
+ logger.log('Cant find minimalRenderUnit ref! This make rerender!');
807
+ container?.rerender();
808
+ return;
809
+ }
810
+ logger.log(`${this.leaf?.componentName}(${this.leaf?.id}) need render, make its minimalRenderUnit ${renderUnitInfo.minimalUnitName}(${renderUnitInfo.minimalUnitId})`);
811
+ ref.makeUnitRender();
812
+ }
813
+ getRenderUnitInfo(leaf = this.leaf) {
814
+ // leaf 在低代码组件中存在 mock 的情况,退出最小渲染单元判断
815
+ if (!leaf || typeof leaf.isRoot !== 'function') {
816
+ return;
817
+ }
818
+ if (leaf.isRoot) {
819
+ this.renderUnitInfo = {
820
+ singleRender: true,
821
+ ...(this.renderUnitInfo || {})
822
+ };
823
+ }
824
+ if (leaf.componentMeta.isMinimalRenderUnit) {
825
+ this.renderUnitInfo = {
826
+ minimalUnitId: leaf.id,
827
+ minimalUnitName: leaf.componentName,
828
+ singleRender: false
829
+ };
830
+ }
831
+ if (leaf.hasLoop()) {
832
+ // 含有循环配置的元素,父元素是最小渲染单元
833
+ this.renderUnitInfo = {
834
+ minimalUnitId: leaf?.parent?.id,
835
+ minimalUnitName: leaf?.parent?.componentName,
836
+ singleRender: false
837
+ };
838
+ }
839
+ if (leaf.parent) {
840
+ this.getRenderUnitInfo(leaf.parent);
841
+ }
842
+ }
843
+ UNSAFE_componentWillReceiveProps(nextProps) {
844
+ const {
845
+ componentId
846
+ } = nextProps;
847
+ if (nextProps.__tag === this.props.__tag) {
848
+ return null;
849
+ }
850
+ const _leaf = getNode?.(componentId);
851
+ if (_leaf && this.curEventLeaf && _leaf !== this.curEventLeaf) {
852
+ this.disposeFunctions.forEach(fn => fn());
853
+ this.disposeFunctions = [];
854
+ this.initOnChildrenChangeEvent(_leaf);
855
+ this.initOnPropsChangeEvent(_leaf);
856
+ this.initOnVisibleChangeEvent(_leaf);
857
+ this.curEventLeaf = _leaf;
858
+ }
859
+ const {
860
+ visible,
861
+ ...resetState
862
+ } = this.getDefaultState(nextProps);
863
+ this.setState(resetState);
864
+ }
865
+
866
+ /** 监听参数变化 */
867
+ initOnPropsChangeEvent(leaf = this.leaf) {
868
+ // const handlePropsChange = debounce((propChangeInfo: PropChangeInfo) => {
869
+ const handlePropsChange = lodashEs.debounce(propChangeInfo => {
870
+ const {
871
+ key,
872
+ newValue = null
873
+ } = propChangeInfo;
874
+ const node = leaf;
875
+ if (key === '___condition___') {
876
+ const {
877
+ condition = true
878
+ } = this.leaf?.export(core.TRANSFORM_STAGE.RENDER) || {};
879
+ const conditionValue = __parseData?.(condition, scope);
880
+ logger.log(`key is ___condition___, change condition value to [${condition}]`);
881
+ // 条件表达式改变
882
+ this.setState({
883
+ condition: conditionValue
884
+ });
885
+ return;
886
+ }
887
+
888
+ // 如果循坏条件变化,从根节点重新渲染
889
+ // 目前多层循坏无法判断需要从哪一层开始渲染,故先粗暴解决
890
+ if (key === '___loop___') {
891
+ logger.log('key is ___loop___, render a page!');
892
+ container?.rerender();
893
+ // 由于 scope 变化,需要清空缓存,使用新的 scope
894
+ cache.component.delete(componentCacheId);
895
+ return;
896
+ }
897
+ this.beforeRender(RerenderType.PropsChanged);
898
+ const {
899
+ state
900
+ } = this;
901
+ const {
902
+ nodeCacheProps
903
+ } = state;
904
+ const nodeProps = getProps(node?.export?.(core.TRANSFORM_STAGE.RENDER), scope, Comp, componentInfo);
905
+ if (key && !(key in nodeProps) && key in this.props) {
906
+ // 当 key 在 this.props 中时,且不存在在计算值中,需要用 newValue 覆盖掉 this.props 的取值
907
+ nodeCacheProps[key] = newValue;
908
+ }
909
+ logger.log(`${leaf?.componentName}[${this.leaf?.id}] component trigger onPropsChange!`, nodeProps, nodeCacheProps, key, newValue);
910
+ this.setState('children' in nodeProps ? {
911
+ nodeChildren: nodeProps.children,
912
+ nodeProps,
913
+ childrenInState: true,
914
+ nodeCacheProps
915
+ } : {
916
+ nodeProps,
917
+ nodeCacheProps
918
+ });
919
+ this.judgeMiniUnitRender();
920
+ });
921
+ // const dispose = leaf?.onPropChange?.((propChangeInfo: IPublicTypePropChangeOptions) => {
922
+ const dispose = leaf?.onPropChange?.(propChangeInfo => {
923
+ if (!this.autoRepaintNode) {
924
+ return;
925
+ }
926
+ handlePropsChange(propChangeInfo);
927
+ });
928
+ dispose && this.disposeFunctions.push(dispose);
929
+ }
930
+
931
+ /**
932
+ * 监听显隐变化
933
+ */
934
+ initOnVisibleChangeEvent(leaf = this.leaf) {
935
+ const dispose = leaf?.onVisibleChange?.(flag => {
936
+ if (!this.autoRepaintNode) {
937
+ return;
938
+ }
939
+ if (this.state.visible === flag) {
940
+ return;
941
+ }
942
+ logger.log(`${leaf?.componentName}[${this.leaf?.id}] component trigger onVisibleChange(${flag}) event`);
943
+ this.beforeRender(RerenderType.VisibleChanged);
944
+ this.setState({
945
+ visible: flag
946
+ });
947
+ this.judgeMiniUnitRender();
948
+ });
949
+ dispose && this.disposeFunctions.push(dispose);
950
+ }
951
+
952
+ /**
953
+ * 监听子元素变化(拖拽,删除...)
954
+ */
955
+ initOnChildrenChangeEvent(leaf = this.leaf) {
956
+ const dispose = leaf?.onChildrenChange?.(param => {
957
+ if (!this.autoRepaintNode) {
958
+ return;
959
+ }
960
+ const {
961
+ type,
962
+ node
963
+ } = param || {};
964
+ this.beforeRender(`${RerenderType.ChildChanged}-${type}`, node);
965
+ // TODO: 缓存同级其他元素的 children。
966
+ // 缓存二级 children Next 查询筛选组件有问题
967
+ // 缓存一级 children Next Tab 组件有问题
968
+ const nextChild = getChildren(leaf?.export?.(core.TRANSFORM_STAGE.RENDER), scope, Comp);
969
+ logger.log(`${schema.componentName}[${this.leaf?.id}] component trigger onChildrenChange event`, nextChild);
970
+ this.setState({
971
+ nodeChildren: nextChild,
972
+ childrenInState: true
973
+ });
974
+ this.judgeMiniUnitRender();
975
+ });
976
+ dispose && this.disposeFunctions.push(dispose);
977
+ }
978
+ componentWillUnmount() {
979
+ this.disposeFunctions.forEach(fn => fn());
980
+ }
981
+ get hasChildren() {
982
+ if (!this.state.childrenInState) {
983
+ return 'children' in this.props;
984
+ }
985
+ return true;
986
+ }
987
+ get children() {
988
+ if (this.state.childrenInState) {
989
+ return this.state.nodeChildren;
990
+ }
991
+ if (this.props.children && !Array.isArray(this.props.children)) {
992
+ return [this.props.children];
993
+ }
994
+ if (this.props.children && this.props.children.length) {
995
+ return this.props.children;
996
+ }
997
+ return this.props.children;
998
+ }
999
+ get leaf() {
1000
+ // if (this.props._leaf?.isMock) {
1001
+ // // 低代码组件作为一个整体更新,其内部的组件不需要监听相关事件
1002
+ // return undefined
1003
+ // }
1004
+
1005
+ return getNode?.(componentCacheId);
1006
+ }
1007
+ render() {
1008
+ if (!this.state.visible || !this.state.condition) {
1009
+ return null;
1010
+ }
1011
+ const {
1012
+ forwardRef,
1013
+ ...rest
1014
+ } = this.props;
1015
+ const compProps = {
1016
+ ...rest,
1017
+ ...(this.state.nodeCacheProps || {}),
1018
+ ...(this.state.nodeProps || {}),
1019
+ children: [],
1020
+ __id: this.leaf?.id,
1021
+ ref: forwardRef
1022
+ };
1023
+ delete compProps.__inner__;
1024
+ if (this.hasChildren) {
1025
+ return engine.createElement(Comp, compProps, this.children);
1026
+ }
1027
+ return engine.createElement(Comp, compProps);
1028
+ }
1029
+ }
1030
+ const LeafWrapper = createForwardRefHocElement(LeafHoc, Comp);
1031
+ cache.component.set(componentCacheId, {
1032
+ LeafWrapper,
1033
+ Comp
1034
+ });
1035
+ return LeafWrapper;
1036
+ };
1037
+
1038
+ /**
1039
+ * execute method in schema.lifeCycles with context
1040
+ */
1041
+ function executeLifeCycleMethod(context, schema, method, args, thisRequiredInJSE) {
1042
+ if (!context || !isSchema(schema) || !method) {
1043
+ return;
1044
+ }
1045
+ const lifeCycleMethods = getValue(schema, 'lifeCycles', {});
1046
+ let fn = lifeCycleMethods[method];
1047
+ if (!fn) {
1048
+ return;
1049
+ }
1050
+
1051
+ // TODO: cache
1052
+ if (core.isJSExpression(fn) || core.isJSFunction(fn)) {
1053
+ fn = thisRequiredInJSE ? parseThisRequiredExpression(fn, context) : parseExpression(fn, context);
1054
+ }
1055
+ if (typeof fn !== 'function') {
1056
+ logger.error(`生命周期${method}类型不符`, fn);
1057
+ return;
1058
+ }
1059
+ try {
1060
+ return fn.apply(context, args);
1061
+ } catch (e) {
1062
+ logger.error(`[${schema.componentName}]生命周期${method}出错`, e);
1063
+ }
1064
+ }
1065
+
1066
+ /**
1067
+ * get children from a node schema
1068
+ */
1069
+ function getSchemaChildren(schema) {
1070
+ if (!schema) {
1071
+ return;
1072
+ }
1073
+ return schema.children;
1074
+ }
1075
+ function baseRendererFactory() {
1076
+ const {
1077
+ BaseRenderer: customBaseRenderer
1078
+ } = adapter.getRenderers();
1079
+ if (customBaseRenderer) {
1080
+ return customBaseRenderer;
1081
+ }
1082
+ const DEFAULT_LOOP_ARG_ITEM = 'item';
1083
+ const DEFAULT_LOOP_ARG_INDEX = 'index';
1084
+ // const scopeIdx = 0
1085
+
1086
+ return class BaseRenderer extends react.Component {
1087
+ static displayName = 'BaseRenderer';
1088
+ static defaultProps = {
1089
+ __schema: {}
1090
+ };
1091
+ static contextType = RendererContext;
1092
+ dataSourceMap = {};
1093
+ __namespace = 'base';
1094
+ __compScopes = {};
1095
+ __instanceMap = {};
1096
+ __dataHelper;
1097
+
1098
+ /**
1099
+ * keep track of customMethods added to this context
1100
+ *
1101
+ * @type {any}
1102
+ */
1103
+ __customMethodsList = [];
1104
+ __parseExpression;
1105
+ __ref;
1106
+
1107
+ /**
1108
+ * reference of style element contains schema.css
1109
+ *
1110
+ * @type {any}
1111
+ */
1112
+ __styleElement;
1113
+ constructor(props) {
1114
+ super(props);
1115
+ this.__parseExpression = (str, self) => {
1116
+ return parseExpression({
1117
+ str,
1118
+ self,
1119
+ thisRequired: props?.thisRequiredInJSE,
1120
+ logScope: props.componentName
1121
+ });
1122
+ };
1123
+ this.__beforeInit(props);
1124
+ this.__init(props);
1125
+ this.__afterInit(props);
1126
+ logger.log(`constructor - ${props?.__schema?.fileName}`);
1127
+ }
1128
+ __beforeInit(props) {}
1129
+ __init(props) {
1130
+ this.__compScopes = {};
1131
+ this.__instanceMap = {};
1132
+ this.__bindCustomMethods(props);
1133
+ }
1134
+ __afterInit(props) {}
1135
+ static getDerivedStateFromProps(props, state) {
1136
+ const result = executeLifeCycleMethod(this, props?.__schema, 'getDerivedStateFromProps', [props, state], props.thisRequiredInJSE);
1137
+ return result === undefined ? null : result;
1138
+ }
1139
+ async getSnapshotBeforeUpdate(...args) {
1140
+ this.__executeLifeCycleMethod('getSnapshotBeforeUpdate', args);
1141
+ logger.log(`getSnapshotBeforeUpdate - ${this.props?.__schema?.componentName}`);
1142
+ }
1143
+ async componentDidMount(...args) {
1144
+ this.reloadDataSource();
1145
+ this.__executeLifeCycleMethod('componentDidMount', args);
1146
+ logger.log(`componentDidMount - ${this.props?.__schema?.componentName}`);
1147
+ }
1148
+ async componentDidUpdate(...args) {
1149
+ this.__executeLifeCycleMethod('componentDidUpdate', args);
1150
+ logger.log(`componentDidUpdate - ${this.props.__schema.componentName}`);
1151
+ }
1152
+ async componentWillUnmount(...args) {
1153
+ this.__executeLifeCycleMethod('componentWillUnmount', args);
1154
+ logger.log(`componentWillUnmount - ${this.props?.__schema?.componentName}`);
1155
+ }
1156
+ async componentDidCatch(...args) {
1157
+ this.__executeLifeCycleMethod('componentDidCatch', args);
1158
+ logger.warn(args);
1159
+ }
1160
+ reloadDataSource = () => new Promise((resolve, reject) => {
1161
+ logger.log('reload data source');
1162
+ if (!this.__dataHelper) {
1163
+ return resolve({});
1164
+ }
1165
+ this.__dataHelper.getInitData().then(res => {
1166
+ if (lodashEs.isEmpty(res)) {
1167
+ this.forceUpdate();
1168
+ return resolve({});
1169
+ }
1170
+ this.setState(res, resolve);
1171
+ }).catch(err => {
1172
+ reject(err);
1173
+ });
1174
+ });
1175
+ shouldComponentUpdate() {
1176
+ if (this.props.getSchemaChangedSymbol?.() && this.props.__container?.rerender) {
1177
+ this.props.__container?.rerender();
1178
+ return false;
1179
+ }
1180
+ return true;
1181
+ }
1182
+ forceUpdate() {
1183
+ if (this.shouldComponentUpdate()) {
1184
+ super.forceUpdate();
1185
+ }
1186
+ }
1187
+
1188
+ /**
1189
+ * execute method in schema.lifeCycles
1190
+ */
1191
+ __executeLifeCycleMethod = (method, args) => {
1192
+ executeLifeCycleMethod(this, this.props.__schema, method, args, this.props.thisRequiredInJSE);
1193
+ };
1194
+
1195
+ /**
1196
+ * this method is for legacy purpose only, which used _ prefix instead of __ as private for some historical reasons
1197
+ */
1198
+ __getComponentView = () => {
1199
+ const {
1200
+ __components,
1201
+ __schema
1202
+ } = this.props;
1203
+ if (!__components) {
1204
+ return;
1205
+ }
1206
+ return __components[__schema.componentName];
1207
+ };
1208
+ __bindCustomMethods = props => {
1209
+ const {
1210
+ __schema
1211
+ } = props;
1212
+ const customMethodsList = Object.keys(__schema.methods || {}) || [];
1213
+ (this.__customMethodsList || []).forEach(item => {
1214
+ if (!customMethodsList.includes(item)) {
1215
+ delete this[item];
1216
+ }
1217
+ });
1218
+ this.__customMethodsList = customMethodsList;
1219
+ lodashEs.forEach(__schema.methods, (val, key) => {
1220
+ let value = val;
1221
+ if (core.isJSExpression(value) || core.isJSFunction(value)) {
1222
+ value = this.__parseExpression(value, this);
1223
+ }
1224
+ if (typeof value !== 'function') {
1225
+ logger.error(`custom method ${key} can not be parsed to a valid function`, value);
1226
+ return;
1227
+ }
1228
+ this[key] = value.bind(this);
1229
+ });
1230
+ };
1231
+ __generateCtx = ctx => {
1232
+ const {
1233
+ pageContext,
1234
+ compContext
1235
+ } = this.context;
1236
+ const obj = {
1237
+ page: pageContext,
1238
+ component: compContext,
1239
+ ...ctx
1240
+ };
1241
+ lodashEs.forEach(obj, (val, key) => {
1242
+ this[key] = val;
1243
+ });
1244
+ };
1245
+ __parseData = (data, ctx) => {
1246
+ const {
1247
+ __ctx,
1248
+ thisRequiredInJSE,
1249
+ componentName
1250
+ } = this.props;
1251
+ return parseData(data, ctx || __ctx || this, {
1252
+ thisRequiredInJSE,
1253
+ logScope: componentName
1254
+ });
1255
+ };
1256
+ __initDataSource = props => {
1257
+ if (!props) {
1258
+ return;
1259
+ }
1260
+ // TODO: 数据源引擎方案
1261
+ // const schema = props.__schema || {}
1262
+ // const defaultDataSource: DataSource = {
1263
+ // list: [],
1264
+ // }
1265
+ // const dataSource = schema.dataSource || defaultDataSource
1266
+ // // requestHandlersMap 存在才走数据源引擎方案
1267
+ // // TODO: 下面if else 抽成独立函数
1268
+ // const useDataSourceEngine = !!props.__appHelper?.requestHandlersMap
1269
+ // if (useDataSourceEngine) {
1270
+ // this.__dataHelper = {
1271
+ // updateConfig: (updateDataSource: any) => {
1272
+ // const { dataSourceMap, reloadDataSource } = createDataSourceEngine(
1273
+ // updateDataSource ?? {},
1274
+ // this,
1275
+ // props.__appHelper.requestHandlersMap
1276
+ // ? { requestHandlersMap: props.__appHelper.requestHandlersMap }
1277
+ // : undefined,
1278
+ // )
1279
+
1280
+ // this.reloadDataSource = () =>
1281
+ // new Promise(resolve => {
1282
+ // logger.log('reload data source')
1283
+ // reloadDataSource().then(() => {
1284
+ // resolve({})
1285
+ // })
1286
+ // })
1287
+ // return dataSourceMap
1288
+ // },
1289
+ // }
1290
+ // this.dataSourceMap = this.__dataHelper.updateConfig(dataSource)
1291
+ // } else {
1292
+ // const appHelper = props.__appHelper
1293
+ // this.__dataHelper = new DataHelper(this, dataSource, appHelper, (config: any) => this.__parseData(config))
1294
+ // this.dataSourceMap = this.__dataHelper.dataSourceMap
1295
+ // this.reloadDataSource = () =>
1296
+ // new Promise((resolve, reject) => {
1297
+ // logger.log('reload data source')
1298
+ // if (!this.__dataHelper) {
1299
+ // return resolve({})
1300
+ // }
1301
+ // this.__dataHelper
1302
+ // .getInitData()
1303
+ // .then((res: any) => {
1304
+ // if (isEmpty(res)) {
1305
+ // return resolve({})
1306
+ // }
1307
+ // this.setState(res, resolve as () => void)
1308
+ // })
1309
+ // .catch((err: Error) => {
1310
+ // reject(err)
1311
+ // })
1312
+ // })
1313
+ // }
1314
+ };
1315
+
1316
+ /**
1317
+ * write props.__schema.css to document as a style element,
1318
+ * which will be added once and only once.
1319
+ * @PRIVATE
1320
+ */
1321
+ __writeCss = props => {
1322
+ const css = getValue(props.__schema, 'css', '');
1323
+ logger.log('create this.styleElement with css', css);
1324
+ let style = this.__styleElement;
1325
+ if (!this.__styleElement) {
1326
+ style = document.createElement('style');
1327
+ style.type = 'text/css';
1328
+ style.setAttribute('from', 'style-sheet');
1329
+ const head = document.head || document.getElementsByTagName('head')[0];
1330
+ head.appendChild(style);
1331
+ this.__styleElement = style;
1332
+ logger.log('this.styleElement is created', this.__styleElement);
1333
+ }
1334
+ if (style.innerHTML === css) {
1335
+ return;
1336
+ }
1337
+ style.innerHTML = css;
1338
+ };
1339
+ __render = () => {
1340
+ const schema = this.props.__schema;
1341
+ this.__executeLifeCycleMethod('render');
1342
+ this.__writeCss(this.props);
1343
+ const {
1344
+ engine
1345
+ } = this.context;
1346
+ if (engine) {
1347
+ engine.props.onCompGetCtx(schema, this);
1348
+ // 画布场景才需要每次渲染bind自定义方法
1349
+ if (this.__designModeIsDesign) {
1350
+ this.__bindCustomMethods(this.props);
1351
+ this.dataSourceMap = this.__dataHelper?.updateConfig(schema.dataSource);
1352
+ }
1353
+ }
1354
+ };
1355
+ __getRef = ref => {
1356
+ const {
1357
+ engine
1358
+ } = this.context;
1359
+ const {
1360
+ __schema
1361
+ } = this.props;
1362
+ // ref && engine?.props?.onCompGetRef(__schema, ref)
1363
+ // TODO: 只在 ref 存在执行,会影响 documentInstance 的卸载
1364
+ engine?.props?.onCompGetRef(__schema, ref);
1365
+ this.__ref = ref;
1366
+ };
1367
+ __createDom = () => {
1368
+ const {
1369
+ __schema,
1370
+ __ctx
1371
+ } = this.props;
1372
+ // merge defaultProps
1373
+ const scopeProps = {
1374
+ ...__schema.defaultProps,
1375
+ ...this.props
1376
+ };
1377
+ const scope = {
1378
+ props: scopeProps
1379
+ };
1380
+ scope.__proto__ = __ctx || this;
1381
+ const _children = getSchemaChildren(__schema);
1382
+ const Comp = this.__getComponentView();
1383
+ if (!Comp) {
1384
+ logger.log(`${__schema.componentName} is invalid!`);
1385
+ }
1386
+ const parentNodeInfo = {
1387
+ schema: __schema,
1388
+ Comp: this.__getHOCWrappedComponent(Comp, {
1389
+ schema: __schema,
1390
+ scope
1391
+ })
1392
+ };
1393
+ return this.__createVirtualDom(_children, scope, parentNodeInfo);
1394
+ };
1395
+
1396
+ /**
1397
+ * 将模型结构转换成react Element
1398
+ * @param originalSchema schema
1399
+ * @param originalScope scope
1400
+ * @param parentInfo 父组件的信息,包含schema和Comp
1401
+ * @param idx 为循环渲染的循环Index
1402
+ */
1403
+ __createVirtualDom = (originalSchema, originalScope, parentInfo, idx = '') => {
1404
+ if (originalSchema === null || originalSchema === undefined) {
1405
+ return null;
1406
+ }
1407
+ const scope = originalScope;
1408
+ const schema = originalSchema;
1409
+ const {
1410
+ engine
1411
+ } = this.context || {};
1412
+ if (!engine) {
1413
+ logger.log('this.context.engine is invalid!');
1414
+ return null;
1415
+ }
1416
+ try {
1417
+ const {
1418
+ __appHelper: appHelper,
1419
+ __components: components = {}
1420
+ } = this.props || {};
1421
+ if (core.isJSExpression(schema)) {
1422
+ return this.__parseExpression(schema, scope);
1423
+ }
1424
+ if (typeof schema === 'string') {
1425
+ return schema;
1426
+ }
1427
+ if (typeof schema === 'number' || typeof schema === 'boolean') {
1428
+ return String(schema);
1429
+ }
1430
+ if (Array.isArray(schema)) {
1431
+ if (schema.length === 1) {
1432
+ return this.__createVirtualDom(schema[0], scope, parentInfo);
1433
+ }
1434
+ return schema.map((item, idy) => this.__createVirtualDom(item, scope, parentInfo, item?.__ctx?.lceKey ? '' : String(idy)));
1435
+ }
1436
+ if (schema.$$typeof) {
1437
+ return schema;
1438
+ }
1439
+ if (!schema.componentName) {
1440
+ logger.error('The componentName in the schema is invalid, please check the schema: ', schema);
1441
+ return;
1442
+ }
1443
+ if (!isSchema(schema)) {
1444
+ return null;
1445
+ }
1446
+ let Comp = components[schema.componentName] || this.props.__container?.components?.[schema.componentName];
1447
+
1448
+ // 容器类组件的上下文通过props传递,避免context传递带来的嵌套问题
1449
+ const otherProps = isSchema(schema) ? {
1450
+ __schema: schema,
1451
+ __appHelper: appHelper,
1452
+ __components: components
1453
+ } : {};
1454
+ if (!Comp) {
1455
+ logger.error(`${schema.componentName} component is not found in components list! component list is:`, components || this.props.__container?.components);
1456
+ return engine.createElement(engine.getNotFoundComponent(), {
1457
+ componentName: schema.componentName,
1458
+ componentId: schema.id,
1459
+ enableStrictNotFoundMode: engine.props.enableStrictNotFoundMode,
1460
+ ref: ref => {
1461
+ ref && engine.props?.onCompGetRef(schema, ref);
1462
+ }
1463
+ }, this.__getSchemaChildrenVirtualDom(schema, scope, Comp));
1464
+ }
1465
+ if (schema.loop != null) {
1466
+ const loop = this.__parseData(schema.loop, scope);
1467
+ if (Array.isArray(loop) && loop.length === 0) return null;
1468
+ const useLoop = isUseLoop(loop, this.__designModeIsDesign);
1469
+ if (useLoop) {
1470
+ return this.__createLoopVirtualDom({
1471
+ ...schema,
1472
+ loop
1473
+ }, scope, parentInfo, idx);
1474
+ }
1475
+ }
1476
+ const condition = schema.condition == null ? true : this.__parseData(schema.condition, scope);
1477
+
1478
+ // DesignMode 为 design 情况下,需要进入 leaf Hoc,进行相关事件注册
1479
+ const displayInHook = this.__designModeIsDesign;
1480
+ if (!condition && !displayInHook) {
1481
+ return null;
1482
+ }
1483
+
1484
+ // TODO: scope
1485
+ // let scopeKey = ''
1486
+ // // 判断组件是否需要生成scope,且只生成一次,挂在this.__compScopes上
1487
+ // if (Comp.generateScope) {
1488
+ // const key = this.__parseExpression(schema.props?.key, scope)
1489
+ // if (key) {
1490
+ // // 如果组件自己设置key则使用组件自己的key
1491
+ // scopeKey = key
1492
+ // } else if (schema.__ctx) {
1493
+ // // 需要判断循环的情况
1494
+ // scopeKey = schema.__ctx.lceKey + (idx !== undefined ? `_${idx}` : '')
1495
+ // } else {
1496
+ // // 在生产环境schema没有__ctx上下文,需要手动生成一个lceKey
1497
+ // schema.__ctx = {
1498
+ // lceKey: `lce${++scopeIdx}`,
1499
+ // }
1500
+ // scopeKey = schema.__ctx.lceKey
1501
+ // }
1502
+ // if (!this.__compScopes[scopeKey]) {
1503
+ // this.__compScopes[scopeKey] = Comp.generateScope(this, schema)
1504
+ // }
1505
+ // }
1506
+ // // 如果组件有设置scope,需要为组件生成一个新的scope上下文
1507
+ // if (scopeKey && this.__compScopes[scopeKey]) {
1508
+ // const compSelf = { ...this.__compScopes[scopeKey] }
1509
+ // compSelf.__proto__ = scope
1510
+ // scope = compSelf
1511
+ // }
1512
+
1513
+ if (engine.props?.designMode) {
1514
+ otherProps.__designMode = engine.props.designMode;
1515
+ }
1516
+ if (this.__designModeIsDesign) {
1517
+ otherProps.__tag = Math.random();
1518
+ }
1519
+ const componentInfo = {};
1520
+ const props = this.__getComponentProps(schema, scope, Comp, {
1521
+ ...componentInfo,
1522
+ props: transformArrayToMap(componentInfo.props, 'name')
1523
+ }) || {};
1524
+ Comp = this.__getHOCWrappedComponent(Comp, {
1525
+ schema,
1526
+ componentInfo,
1527
+ baseRenderer: this,
1528
+ scope
1529
+ });
1530
+ otherProps.ref = ref => {
1531
+ this.$(schema.id || props.ref, ref); // 收集ref
1532
+ const refProps = props.ref;
1533
+ if (refProps && typeof refProps === 'string') {
1534
+ this[refProps] = ref;
1535
+ }
1536
+ ref && engine.props?.onCompGetRef(schema, ref);
1537
+ };
1538
+
1539
+ // scope需要传入到组件上
1540
+ // if (scopeKey && this.__compScopes[scopeKey]) {
1541
+ // props.__scope = this.__compScopes[scopeKey]
1542
+ // }
1543
+ if (schema?.__ctx?.lceKey) {
1544
+ if (!isSchema(schema)) {
1545
+ engine.props?.onCompGetCtx(schema, scope);
1546
+ }
1547
+ props.key = props.key || `${schema.__ctx.lceKey}_${schema.__ctx.idx || 0}_${idx !== undefined ? idx : ''}`;
1548
+ } else if ((typeof idx === 'number' || typeof idx === 'string') && !props.key) {
1549
+ // 仅当循环场景走这里
1550
+ props.key = idx;
1551
+ }
1552
+ props.__id = schema.id;
1553
+ if (!props.key) {
1554
+ props.key = props.__id;
1555
+ }
1556
+ return engine.createElement(Comp, {
1557
+ ...props,
1558
+ ...otherProps,
1559
+ // TODO: 看看这里需要怎么处理简洁
1560
+ __inner__: {
1561
+ hidden: schema.hidden,
1562
+ condition
1563
+ }
1564
+ }, this.__getSchemaChildrenVirtualDom(schema, scope, Comp, condition));
1565
+ } catch (e) {
1566
+ return engine.createElement(engine.getFaultComponent(), {
1567
+ error: e,
1568
+ schema,
1569
+ self: scope,
1570
+ parentInfo,
1571
+ idx
1572
+ });
1573
+ }
1574
+ };
1575
+
1576
+ /**
1577
+ * get Component HOCs
1578
+ *
1579
+ * @readonly
1580
+ * @type {ComponentConstruct[]}
1581
+ */
1582
+ get __componentHOCs() {
1583
+ if (this.__designModeIsDesign) {
1584
+ return [leafWrapper, compWrapper];
1585
+ }
1586
+ return [compWrapper];
1587
+ }
1588
+ __getSchemaChildrenVirtualDom = (schema, scope, Comp, condition = true) => {
1589
+ let children = condition ? getSchemaChildren(schema) : null;
1590
+ const result = [];
1591
+ if (children) {
1592
+ if (!Array.isArray(children)) {
1593
+ children = [children];
1594
+ }
1595
+ children.forEach(child => {
1596
+ const childVirtualDom = this.__createVirtualDom(core.isJSExpression(child) ? this.__parseExpression(child, scope) : child, scope, {
1597
+ schema,
1598
+ Comp
1599
+ });
1600
+ result.push(childVirtualDom);
1601
+ });
1602
+ }
1603
+ if (result && result.length > 0) {
1604
+ return result;
1605
+ }
1606
+ return null;
1607
+ };
1608
+ __getComponentProps = (schema, scope, Comp, componentInfo) => {
1609
+ if (!schema) {
1610
+ return {};
1611
+ }
1612
+ return this.__parseProps(schema?.props, scope, '', {
1613
+ schema,
1614
+ Comp,
1615
+ componentInfo: {
1616
+ ...(componentInfo || {}),
1617
+ props: transformArrayToMap((componentInfo || {}).props, 'name')
1618
+ }
1619
+ }) || {};
1620
+ };
1621
+ __createLoopVirtualDom = (schema, scope, parentInfo, idx) => {
1622
+ // TODO
1623
+ // if (isSchema(schema)) {
1624
+ // logger.warn('file type not support Loop')
1625
+ // return null
1626
+ // }
1627
+ if (!Array.isArray(schema.loop)) {
1628
+ return null;
1629
+ }
1630
+ const itemArg = schema.loopArgs && schema.loopArgs[0] || DEFAULT_LOOP_ARG_ITEM;
1631
+ const indexArg = schema.loopArgs && schema.loopArgs[1] || DEFAULT_LOOP_ARG_INDEX;
1632
+ const {
1633
+ loop
1634
+ } = schema;
1635
+ return loop.map((item, i) => {
1636
+ const loopSelf = {
1637
+ [itemArg]: item,
1638
+ [indexArg]: i
1639
+ };
1640
+ loopSelf.__proto__ = scope;
1641
+ return this.__createVirtualDom({
1642
+ ...schema,
1643
+ loop: undefined,
1644
+ props: {
1645
+ ...schema.props,
1646
+ // 循环下 key 不能为常量,这样会造成 key 值重复,渲染异常
1647
+ key: core.isJSExpression(schema.props?.key) ? schema.props?.key : null
1648
+ }
1649
+ }, loopSelf, parentInfo, idx ? `${idx}_${i}` : i);
1650
+ });
1651
+ };
1652
+ get __designModeIsDesign() {
1653
+ const {
1654
+ engine
1655
+ } = this.context || {};
1656
+ return engine?.props?.designMode === 'design';
1657
+ }
1658
+ __parseProps = (originalProps, scope, path, info) => {
1659
+ let props = originalProps;
1660
+ const {
1661
+ schema,
1662
+ Comp,
1663
+ componentInfo = {}
1664
+ } = info;
1665
+ const propInfo = getValue(componentInfo.props, path);
1666
+ // FIXME: 将这行逻辑外置,解耦,线上环境不要验证参数,调试环境可以有,通过传参自定义
1667
+ const propType = propInfo?.extra?.propType;
1668
+ const checkProps = value => {
1669
+ if (!propType) {
1670
+ return value;
1671
+ }
1672
+ return checkPropTypes(value, path, propType, componentInfo.name) ? value : undefined;
1673
+ };
1674
+ const parseReactNode = (data, params) => {
1675
+ if (lodashEs.isEmpty(params)) {
1676
+ const virtualDom = this.__createVirtualDom(data, scope, {
1677
+ schema,
1678
+ Comp
1679
+ });
1680
+ return checkProps(virtualDom);
1681
+ }
1682
+ return checkProps((...argValues) => {
1683
+ const args = {};
1684
+ if (Array.isArray(params) && params.length) {
1685
+ params.forEach((item, idx) => {
1686
+ if (typeof item === 'string') {
1687
+ args[item] = argValues[idx];
1688
+ } else if (item && typeof item === 'object') {
1689
+ args[item.name] = argValues[idx];
1690
+ }
1691
+ });
1692
+ }
1693
+ args.__proto__ = scope;
1694
+ return scope.__createVirtualDom(data, args, {
1695
+ schema,
1696
+ Comp
1697
+ });
1698
+ });
1699
+ };
1700
+ if (core.isJSExpression(props)) {
1701
+ props = this.__parseExpression(props, scope);
1702
+ // 只有当变量解析出来为模型结构的时候才会继续解析
1703
+ if (!isSchema(props)) {
1704
+ return checkProps(props);
1705
+ }
1706
+ }
1707
+ if (core.isJSFunction(props)) {
1708
+ props = transformStringToFunction(props.value);
1709
+ }
1710
+
1711
+ // 兼容通过componentInfo判断的情况
1712
+ if (isSchema(props)) {
1713
+ const isReactNodeFunction = !!(propInfo?.type === 'ReactNode' && propInfo?.props?.type === 'function');
1714
+ const isMixinReactNodeFunction = !!(propInfo?.type === 'Mixin' && propInfo?.props?.types?.indexOf('ReactNode') > -1 && propInfo?.props?.reactNodeProps?.type === 'function');
1715
+ let params = null;
1716
+ if (isReactNodeFunction) {
1717
+ params = propInfo?.props?.params;
1718
+ } else if (isMixinReactNodeFunction) {
1719
+ params = propInfo?.props?.reactNodeProps?.params;
1720
+ }
1721
+ return parseReactNode(props, params);
1722
+ }
1723
+ if (Array.isArray(props)) {
1724
+ return checkProps(props.map((item, idx) => this.__parseProps(item, scope, path ? `${path}.${idx}` : `${idx}`, info)));
1725
+ }
1726
+ if (typeof props === 'function') {
1727
+ return checkProps(props.bind(scope));
1728
+ }
1729
+ if (props && typeof props === 'object') {
1730
+ if (props.$$typeof) {
1731
+ return checkProps(props);
1732
+ }
1733
+ const res = {};
1734
+ lodashEs.forEach(props, (val, key) => {
1735
+ if (key.startsWith('__')) {
1736
+ res[key] = val;
1737
+ return;
1738
+ }
1739
+ res[key] = this.__parseProps(val, scope, path ? `${path}.${key}` : key, info);
1740
+ });
1741
+ return checkProps(res);
1742
+ }
1743
+ return checkProps(props);
1744
+ };
1745
+ $(id, instance) {
1746
+ this.__instanceMap = this.__instanceMap || {};
1747
+ if (!id || typeof id !== 'string') {
1748
+ return this.__instanceMap;
1749
+ }
1750
+ if (instance) {
1751
+ this.__instanceMap[id] = instance;
1752
+ }
1753
+ return this.__instanceMap[id];
1754
+ }
1755
+ __renderContextProvider = (customProps, children) => {
1756
+ return /*#__PURE__*/React.createElement(RendererContext.Provider, {
1757
+ value: {
1758
+ ...this.context,
1759
+ blockContext: this,
1760
+ ...(customProps || {})
1761
+ }
1762
+ }, children || this.__createDom());
1763
+ };
1764
+ __renderContextConsumer = children => {
1765
+ return /*#__PURE__*/React.createElement(RendererContext.Consumer, null, children);
1766
+ };
1767
+ __getHOCWrappedComponent(OriginalComp, info) {
1768
+ let Comp = OriginalComp;
1769
+ this.__componentHOCs.forEach(ComponentConstruct => {
1770
+ Comp = ComponentConstruct(Comp, {
1771
+ componentInfo: {},
1772
+ baseRenderer: this,
1773
+ ...info
1774
+ });
1775
+ });
1776
+ return Comp;
1777
+ }
1778
+ __renderComp(OriginalComp, ctxProps) {
1779
+ let Comp = OriginalComp;
1780
+ const {
1781
+ __schema,
1782
+ __ctx
1783
+ } = this.props;
1784
+ const scope = {};
1785
+ scope.__proto__ = __ctx || this;
1786
+ Comp = this.__getHOCWrappedComponent(Comp, {
1787
+ schema: __schema,
1788
+ scope
1789
+ });
1790
+ const data = this.__parseProps(__schema?.props, scope, '', {
1791
+ schema: __schema,
1792
+ Comp,
1793
+ componentInfo: {}
1794
+ });
1795
+ const {
1796
+ className
1797
+ } = data;
1798
+ const otherProps = {};
1799
+ const {
1800
+ engine
1801
+ } = this.context || {};
1802
+ if (!engine) {
1803
+ return null;
1804
+ }
1805
+ if (this.__designModeIsDesign) {
1806
+ otherProps.__tag = Math.random();
1807
+ }
1808
+ const child = engine.createElement(Comp, {
1809
+ ...data,
1810
+ ...this.props,
1811
+ ref: this.__getRef,
1812
+ className: classnames(__schema?.fileName && getFileCssName(__schema.fileName), className, this.props.className),
1813
+ __id: __schema?.id,
1814
+ ...otherProps
1815
+ }, this.__createDom());
1816
+ return this.__renderContextProvider(ctxProps, child);
1817
+ }
1818
+ __renderContent(children) {
1819
+ const {
1820
+ __schema
1821
+ } = this.props;
1822
+ const parsedProps = this.__parseData(__schema.props);
1823
+ const className = classnames(`lce-${this.__namespace}`, __schema?.fileName && getFileCssName(__schema.fileName), parsedProps.className, this.props.className);
1824
+ const style = {
1825
+ ...(parsedProps.style || {}),
1826
+ ...(typeof this.props.style === 'object' ? this.props.style : {})
1827
+ };
1828
+ const id = this.props.id || parsedProps.id;
1829
+ return /*#__PURE__*/React.createElement("div", {
1830
+ ref: this.__getRef,
1831
+ className: className,
1832
+ id: id,
1833
+ style: style
1834
+ }, children);
1835
+ }
1836
+ __checkSchema = (schema, originalExtraComponents = []) => {
1837
+ let extraComponents = originalExtraComponents;
1838
+ if (typeof extraComponents === 'string') {
1839
+ extraComponents = [extraComponents];
1840
+ }
1841
+
1842
+ // const builtin = capitalizeFirstLetter(this.__namespace)
1843
+ // const componentNames = [builtin, ...extraComponents]
1844
+ const componentNames = [...Object.keys(this.props.__components), ...extraComponents];
1845
+ return !isSchema(schema) || !componentNames.includes(schema?.componentName ?? '');
1846
+ };
1847
+ get appHelper() {
1848
+ return this.props.__appHelper;
1849
+ }
1850
+ get requestHandlersMap() {
1851
+ return this.appHelper?.requestHandlersMap;
1852
+ }
1853
+ get utils() {
1854
+ return this.appHelper?.utils;
1855
+ }
1856
+ get constants() {
1857
+ return this.appHelper?.constants;
1858
+ }
1859
+
1860
+ // render() {
1861
+ // return null
1862
+ // }
1863
+ };
1864
+ }
1865
+
1866
+ function componentRendererFactory() {
1867
+ const BaseRenderer = baseRendererFactory();
1868
+ return class CompRenderer extends BaseRenderer {
1869
+ static displayName = 'CompRenderer';
1870
+ __namespace = 'component';
1871
+ __afterInit(props, ...rest) {
1872
+ this.__generateCtx({
1873
+ component: this
1874
+ });
1875
+ const schema = props.__schema || {};
1876
+ this.state = this.__parseData(schema.state || {});
1877
+ this.__initDataSource(props);
1878
+ this.__executeLifeCycleMethod('constructor', [props, ...rest]);
1879
+ }
1880
+ render() {
1881
+ const {
1882
+ __schema
1883
+ } = this.props;
1884
+ if (this.__checkSchema(__schema)) {
1885
+ return '自定义组件 schema 结构异常!';
1886
+ }
1887
+ logger.log(`${CompRenderer.displayName} render - ${__schema.componentName}`);
1888
+ this.__generateCtx({
1889
+ component: this
1890
+ });
1891
+ this.__render();
1892
+ const noContainer = this.__parseData(__schema.props?.noContainer);
1893
+ this.__bindCustomMethods(this.props);
1894
+ if (noContainer) {
1895
+ return this.__renderContextProvider({
1896
+ compContext: this
1897
+ });
1898
+ }
1899
+ const Comp = this.__getComponentView();
1900
+ if (!Comp) {
1901
+ return this.__renderContent(this.__renderContextProvider({
1902
+ compContext: this
1903
+ }));
1904
+ }
1905
+ return this.__renderComp(Comp, {
1906
+ compContext: this
1907
+ });
1908
+ }
1909
+ };
1910
+ }
1911
+
1912
+ function pageRendererFactory() {
1913
+ const BaseRenderer = baseRendererFactory();
1914
+ return class PageRenderer extends BaseRenderer {
1915
+ static displayName = 'PageRenderer';
1916
+ __namespace = 'page';
1917
+ __afterInit(props, ...rest) {
1918
+ const schema = props.__schema || {};
1919
+ this.state = this.__parseData(schema.state || {});
1920
+ this.__initDataSource(props);
1921
+ this.__executeLifeCycleMethod('constructor', [props, ...rest]);
1922
+ }
1923
+ async componentDidUpdate(prevProps, _prevState, snapshot) {
1924
+ const {
1925
+ __ctx
1926
+ } = this.props;
1927
+ // 当编排的时候修改 schema.state 值,需要将最新 schema.state 值 setState
1928
+ if (JSON.stringify(prevProps.__schema.state) !== JSON.stringify(this.props.__schema.state)) {
1929
+ const newState = this.__parseData(this.props.__schema.state, __ctx);
1930
+ this.setState(newState);
1931
+ }
1932
+ super.componentDidUpdate?.(prevProps, _prevState, snapshot);
1933
+ }
1934
+ setState(state, callback) {
1935
+ logger.log('page set state', state);
1936
+ super.setState(state, callback);
1937
+ }
1938
+ render() {
1939
+ const {
1940
+ __schema
1941
+ } = this.props;
1942
+ if (this.__checkSchema(__schema)) {
1943
+ return '页面schema结构异常!';
1944
+ }
1945
+ logger.log(`${PageRenderer.displayName} render - ${__schema.componentName}`);
1946
+ this.__bindCustomMethods(this.props);
1947
+ this.__initDataSource(this.props);
1948
+ this.__generateCtx({
1949
+ page: this
1950
+ });
1951
+ this.__render();
1952
+ const Comp = this.__getComponentView();
1953
+ if (!Comp) {
1954
+ return this.__renderContent(this.__renderContextProvider({
1955
+ pageContext: this
1956
+ }));
1957
+ }
1958
+ return this.__renderComp(Comp, {
1959
+ pageContext: this
1960
+ });
1961
+ }
1962
+ };
1963
+ }
1964
+
1965
+ const FaultComponent = ({
1966
+ componentName = '',
1967
+ error
1968
+ }) => {
1969
+ logger.error(`${componentName} 组件渲染异常, 异常原因: ${error?.message || error || '未知'}`);
1970
+ return /*#__PURE__*/React.createElement("div", {
1971
+ role: "alert",
1972
+ "aria-label": `${componentName} 组件渲染异常`,
1973
+ style: {
1974
+ width: '100%',
1975
+ height: '50px',
1976
+ lineHeight: '50px',
1977
+ textAlign: 'center',
1978
+ fontSize: '15px',
1979
+ color: '#ef4444',
1980
+ border: '2px solid #ef4444'
1981
+ }
1982
+ }, componentName, " \u7EC4\u4EF6\u6E32\u67D3\u5F02\u5E38\uFF0C\u8BF7\u67E5\u770B\u63A7\u5236\u53F0\u65E5\u5FD7");
1983
+ };
1984
+
1985
+ const NotFoundComponent = ({
1986
+ componentName = '',
1987
+ enableStrictNotFoundMode,
1988
+ children
1989
+ }) => {
1990
+ logger.warn(`Component ${componentName} not found`);
1991
+ if (enableStrictNotFoundMode) {
1992
+ return /*#__PURE__*/React.createElement(React.Fragment, null, `${componentName} Component Not Found`);
1993
+ }
1994
+ return /*#__PURE__*/React.createElement("div", {
1995
+ role: "alert",
1996
+ "aria-label": `${componentName} component not found`,
1997
+ style: {
1998
+ width: '100%',
1999
+ height: '50px',
2000
+ lineHeight: '50px',
2001
+ textAlign: 'center',
2002
+ fontSize: '15px',
2003
+ color: '#eab308',
2004
+ border: '2px solid #eab308'
2005
+ }
2006
+ }, `${componentName} Component Not Found`);
2007
+ };
2008
+
2009
+ function rendererFactory() {
2010
+ const RENDERER_COMPS = adapter.getRenderers();
2011
+ return class Renderer extends react.Component {
2012
+ static displayName = 'Renderer';
2013
+ state = {};
2014
+ __ref;
2015
+ static defaultProps = {
2016
+ appHelper: undefined,
2017
+ components: {},
2018
+ designMode: 'live',
2019
+ suspended: false,
2020
+ schema: {},
2021
+ onCompGetRef: () => {},
2022
+ onCompGetCtx: () => {},
2023
+ thisRequiredInJSE: true
2024
+ };
2025
+ constructor(props) {
2026
+ super(props);
2027
+ this.state = {};
2028
+ core.logger.log(`entry.constructor - ${props?.schema?.componentName}`);
2029
+ }
2030
+ async componentDidMount() {
2031
+ core.logger.log(`entry.componentDidMount - ${this.props.schema && this.props.schema.componentName}`);
2032
+ }
2033
+ async componentDidUpdate() {
2034
+ core.logger.log(`entry.componentDidUpdate - ${this.props?.schema?.componentName}`);
2035
+ }
2036
+ async componentWillUnmount() {
2037
+ core.logger.log(`entry.componentWillUnmount - ${this.props?.schema?.componentName}`);
2038
+ }
2039
+ componentDidCatch(error) {
2040
+ this.state.engineRenderError = true;
2041
+ this.state.error = error;
2042
+ }
2043
+ shouldComponentUpdate(nextProps) {
2044
+ return !nextProps.suspended;
2045
+ }
2046
+ __getRef = ref => {
2047
+ this.__ref = ref;
2048
+ if (ref) {
2049
+ this.props.onCompGetRef?.(this.props.schema, ref);
2050
+ }
2051
+ };
2052
+ isValidComponent(SetComponent) {
2053
+ return SetComponent;
2054
+ }
2055
+ createElement(SetComponent, props, children) {
2056
+ return (this.props.customCreateElement || react.createElement)(SetComponent, props, children);
2057
+ }
2058
+ getNotFoundComponent() {
2059
+ return this.props.notFoundComponent || NotFoundComponent;
2060
+ }
2061
+ getFaultComponent() {
2062
+ return this.props.faultComponent || FaultComponent;
2063
+ }
2064
+ render() {
2065
+ const {
2066
+ schema,
2067
+ designMode,
2068
+ appHelper,
2069
+ components
2070
+ } = this.props;
2071
+ if (lodashEs.isEmpty(schema)) {
2072
+ return null;
2073
+ }
2074
+ if (!isSchema(schema)) {
2075
+ core.logger.error('The root component name needs to be one of Page、Block、Component, please check the schema: ', schema);
2076
+ return '模型结构异常';
2077
+ }
2078
+ core.logger.log('entry.render');
2079
+ const allComponents = {
2080
+ ...components,
2081
+ ...RENDERER_COMPS
2082
+ };
2083
+ // TODO: 默认最顶层使用 PageRenderer
2084
+ const Comp = allComponents.PageRenderer;
2085
+ if (this.state && this.state.engineRenderError) {
2086
+ return /*#__PURE__*/react.createElement(this.getFaultComponent(), {
2087
+ componentName: schema.componentName,
2088
+ error: this.state.error
2089
+ });
2090
+ }
2091
+ if (!Comp) {
2092
+ return null;
2093
+ }
2094
+ return /*#__PURE__*/React.createElement(RendererContext.Provider, {
2095
+ value: {
2096
+ appHelper,
2097
+ components: allComponents,
2098
+ engine: this
2099
+ }
2100
+ }, /*#__PURE__*/React.createElement(Comp, _extends({
2101
+ key: schema.__ctx && `${schema.__ctx.lceKey}_${schema.__ctx.idx || '0'}`
2102
+ // ref={this.__getRef}
2103
+ ,
2104
+ __appHelper: appHelper,
2105
+ __components: allComponents,
2106
+ __schema: schema,
2107
+ __designMode: designMode
2108
+ }, this.props)));
2109
+ }
2110
+ };
2111
+ }
2112
+
2113
+ const DEV = '_EASY_EDITOR_DEV_';
2114
+
2115
+ exports.DEV = DEV;
2116
+ exports.RendererContext = RendererContext;
2117
+ exports.SettingFieldView = SettingFieldView;
2118
+ exports.SettingRender = SettingRender;
2119
+ exports.adapter = adapter;
2120
+ exports.baseRendererFactory = baseRendererFactory;
2121
+ exports.capitalizeFirstLetter = capitalizeFirstLetter;
2122
+ exports.checkPropTypes = checkPropTypes;
2123
+ exports.classnames = classnames;
2124
+ exports.cloneEnumerableProperty = cloneEnumerableProperty;
2125
+ exports.compWrapper = compWrapper;
2126
+ exports.componentRendererFactory = componentRendererFactory;
2127
+ exports.contextFactory = contextFactory;
2128
+ exports.createForwardRefHocElement = createForwardRefHocElement;
2129
+ exports.executeLifeCycleMethod = executeLifeCycleMethod;
2130
+ exports.getFileCssName = getFileCssName;
2131
+ exports.getSchemaChildren = getSchemaChildren;
2132
+ exports.getValue = getValue;
2133
+ exports.inSameDomain = inSameDomain;
2134
+ exports.isReactClass = isReactClass;
2135
+ exports.isReactComponent = isReactComponent;
2136
+ exports.isSchema = isSchema;
2137
+ exports.isString = isString;
2138
+ exports.isUseLoop = isUseLoop;
2139
+ exports.leafWrapper = leafWrapper;
2140
+ exports.logger = logger;
2141
+ exports.pageRendererFactory = pageRendererFactory;
2142
+ exports.parseData = parseData;
2143
+ exports.parseExpression = parseExpression;
2144
+ exports.parseThisRequiredExpression = parseThisRequiredExpression;
2145
+ exports.rendererFactory = rendererFactory;
2146
+ exports.transformArrayToMap = transformArrayToMap;
2147
+ exports.transformStringToFunction = transformStringToFunction;
2148
+ exports.useRendererContext = useRendererContext;
2149
+ //# sourceMappingURL=index.development.js.map