@cloudbase/lowcode-builder 1.7.1 → 1.7.3

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.
package/lib/test.js DELETED
@@ -1,717 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.generateWidgetAPIContext = exports.createInitData = exports.disposeWidget = exports.getWidget = exports.ID_SEPARATOR = exports.generateForContextOfWidget = exports.createWidgets = exports.resolveWidgetData = void 0;
7
- const mobx_1 = require("mobx");
8
- const style_1 = require("./style");
9
- const util_1 = require("./util");
10
- const weapp_component_1 = require("./weapp-component");
11
- const event_emitter_1 = __importDefault(require("./event-emitter"));
12
- const lodash_set_1 = __importDefault(require("lodash.set"));
13
- const constant_1 = require("./constant");
14
- /**
15
- * convert widget prop to data for wxml
16
- * @param {*} props
17
- */
18
- const EXTRA_PROPS_MAP = [
19
- /**
20
- * create widgets
21
- */
22
- 'id',
23
- 'widgetType',
24
- 'parent',
25
- 'children',
26
- '_scope',
27
- '_disposers',
28
- '_eventListeners',
29
- /**
30
- * mount widget api
31
- */
32
- 'findWidgets',
33
- 'getWidgetsByType',
34
- 'getOwnerWidget',
35
- 'getDom',
36
- 'on',
37
- 'off',
38
- 'getConfig',
39
- '_getInstanceRef',
40
- '_methods',
41
- '_userWidget',
42
- /**
43
- * 其他挂载
44
- */
45
- '_descendants',
46
- '_forContext',
47
- /**
48
- * widgetProps 附带值
49
- */
50
- '_id',
51
- '_order',
52
- 'classList',
53
- ].reduce((map, key) => {
54
- map[key] = true;
55
- return map;
56
- }, {});
57
- function resolveWidgetProp(props) {
58
- let { classList = [], ...restProps } = props;
59
- const data = {};
60
- Object.keys(restProps).forEach((key) => {
61
- if (EXTRA_PROPS_MAP[key]) {
62
- return;
63
- }
64
- if (restProps[key] instanceof Function || restProps[key] === undefined) {
65
- return;
66
- }
67
- data[key] = restProps[key];
68
- });
69
- data.style = (0, style_1.styleToCss)(restProps.style);
70
- data.className = classList.join ? classList.join(' ') : classList;
71
- return data;
72
- }
73
- // widget prop -> wxml data
74
- function resolveWidgetData(props) {
75
- if (!Array.isArray(props)) {
76
- return resolveWidgetProp(props);
77
- }
78
- return props.map(resolveWidgetData);
79
- }
80
- exports.resolveWidgetData = resolveWidgetData;
81
- function createWidgets(widgetProps, dataBinds, ownerMpInst) {
82
- const rootNode = createWidgetDataTree(widgetProps, dataBinds);
83
- const failedBinds = [];
84
- const result = createSubWidgetTree({ ownerForWidgetHolder: {} }, rootNode, dataBinds, ownerMpInst, {}, failedBinds, undefined);
85
- retryFailedBinds(failedBinds, true);
86
- return result;
87
- }
88
- exports.createWidgets = createWidgets;
89
- /**
90
- * @param ownerMpInst The MP page or component instance the widgets belongs to
91
- * @param ownerForWidgetHolder null for the virtual root node(first run)
92
- * @param curForNode a component node or a virtual root tree node
93
- * @returns {widgets: {id1:[], id2}, rootWidget: {children: [], _disposers: [], ...otherProps}}
94
- */
95
- function createSubWidgetTree(ctx, curForNode, dataBinds, ownerMpInst, forContext = {}, failedBinds = [], parentWidget = { children: (0, mobx_1.observable)([]), _disposers: [] }) {
96
- var _a;
97
- const { ownerForWidgetHolder = {}, existingWidgetMap = {} } = ctx;
98
- /**
99
- * 不能类似web实现记录额外的 _disposers
100
- * const widgetHolder = { _disposers: [] }
101
- * 因为 merger-render.initMergeRenderer 里面没有特殊处理
102
- */
103
- const widgetHolder = {};
104
- const { lists: forLists = [] } = forContext;
105
- const currentIndex = (_a = forLists[0]) === null || _a === void 0 ? void 0 : _a.currentIndex;
106
- const indexPostfix = (forContext.lists || [])
107
- .slice()
108
- .reverse()
109
- .map(({ currentIndex }) => exports.ID_SEPARATOR + currentIndex)
110
- .join('');
111
- // traverse down the tree to set up all widgets
112
- dfsTree(curForNode, (node, parentNode, cache) => {
113
- var _a, _b, _c, _d;
114
- const parentForWidgetArr = ownerForWidgetHolder[node.id] || [];
115
- const { _waForKey } = node.value;
116
- const key = (_b = (_a = forContext.forItems) === null || _a === void 0 ? void 0 : _a[node.id]) === null || _b === void 0 ? void 0 : _b[_waForKey];
117
- let forExistingWidgetMap = {};
118
- let forExsitWidget;
119
- /**
120
- * for 起始节点,根据 existingWidgetMap 判断复用
121
- */
122
- if (node.id === curForNode.id) {
123
- if (existingWidgetMap[key] && existingWidgetMap[key].index === currentIndex) {
124
- forExistingWidgetMap = existingWidgetMap[key].widgets || {};
125
- forExsitWidget = forExistingWidgetMap[node.id];
126
- if (forExsitWidget) {
127
- cache[node.id] = {
128
- widgets: forExistingWidgetMap,
129
- };
130
- }
131
- }
132
- }
133
- else if (cache[parentNode === null || parentNode === void 0 ? void 0 : parentNode.id]) {
134
- forExsitWidget = cache[parentNode === null || parentNode === void 0 ? void 0 : parentNode.id].widgets[node.id] || null;
135
- if (forExsitWidget) {
136
- cache[node.id] = cache[parentNode === null || parentNode === void 0 ? void 0 : parentNode.id];
137
- }
138
- }
139
- const existedWidget = forExsitWidget || null;
140
- if (node.forCount === curForNode.forCount) {
141
- /**
142
- * 同一层循环作用域内,当前节点与 curForNode(循环根节点)在同一级循环作用域中
143
- * 即没有再开辟新级别的 for 循环
144
- * Leaf node
145
- */
146
- let w = existedWidget;
147
- if (!existedWidget) {
148
- const parentNode = node.parent;
149
- let parent = parentNode ? widgetHolder[parentNode.id] || ownerForWidgetHolder[parentNode.id] : null;
150
- w = createWidget(node.value, node.id, indexPostfix, parent, ownerMpInst, (_c = forContext.forItems) === null || _c === void 0 ? void 0 : _c[node.id]);
151
- w._key = key;
152
- if (!parent) {
153
- parentWidget.children.push(w);
154
- }
155
- }
156
- else {
157
- w.id = `${node.id}${indexPostfix}`;
158
- disposeWidget(existedWidget, true);
159
- }
160
- (_d = parentForWidgetArr.push) === null || _d === void 0 ? void 0 : _d.call(parentForWidgetArr, w);
161
- setUpWidgetDataBinds(w, dataBinds[node.id], forContext, failedBinds, ownerMpInst._getInstance());
162
- widgetHolder[node.id] = w;
163
- if (widgetHolder === null || widgetHolder === void 0 ? void 0 : widgetHolder[node._ancestorId]) {
164
- // 在虚拟项 RepeaterItem 下挂载所有的子孙 __descendants
165
- // 这里不要用 lodashSet,否则会出现刷新几次,出现 Error: [mobx.array] Index out of bounds 的错误,原因暂不明
166
- widgetHolder[node._ancestorId]._descendants = widgetHolder[node._ancestorId]._descendants || {};
167
- widgetHolder[node._ancestorId]._descendants[node.id] = widgetHolder[node.id];
168
- }
169
- }
170
- else {
171
- if (!existedWidget) {
172
- widgetHolder[node.id] = (0, mobx_1.observable)([]);
173
- }
174
- else {
175
- // Reuse existed for widget array
176
- widgetHolder[node.id] = existedWidget;
177
- }
178
- if (parentForWidgetArr) {
179
- parentForWidgetArr.push(widgetHolder[node.id]);
180
- }
181
- }
182
- }, undefined);
183
- // run for of next level
184
- dfsTree(curForNode, (node) => {
185
- if (node.forCount === curForNode.forCount + 1 && dataBinds[node.id] && dataBinds[node.id]._waFor) {
186
- // find the node bound with next level for
187
- const parent = getNodeParentWidget(node, widgetHolder, parentWidget);
188
- const dispose = runFor(node, dataBinds, ownerMpInst, forContext, widgetHolder, failedBinds, parent);
189
- parent._disposers.push(dispose); // Add the for bind dispose to the parent node of forNode
190
- }
191
- }, undefined);
192
- retryFailedBinds(failedBinds);
193
- return { widgets: widgetHolder, rootWidget: widgetHolder[curForNode.id] || parentWidget };
194
- }
195
- // Retry failed databinds
196
- function retryFailedBinds(failedBinds, finalTry) {
197
- const len = failedBinds.length;
198
- for (let i = 0; i < len; i++) {
199
- const setUpDataBind = failedBinds.shift();
200
- setUpDataBind(finalTry);
201
- }
202
- }
203
- /**
204
- *
205
- * @param {*} curForNode
206
- * @param {*} forContext
207
- * @param {*} parentForWidgets
208
- * @param {*} parentWidget
209
- * @returns top level widgets or for dispose
210
- */
211
- const _FOR_ERROR_CACHE_MAP = {};
212
- function runFor(curForNode, dataBinds, ownerMpInst, forContext, ownerForWidgetHolder, failedBinds, parentWidget) {
213
- const nodeId = curForNode.id;
214
- const { _waForKey } = curForNode.value;
215
- const dispose = (0, mobx_1.autorun)(() => {
216
- let forList = [];
217
- try {
218
- clearTimeout(_FOR_ERROR_CACHE_MAP[nodeId]);
219
- const $instance = ownerMpInst._getInstance();
220
- const dataContext = (0, mobx_1.untracked)(() => (0, util_1.generateDataContext)(parentWidget));
221
- const $w = (0, mobx_1.untracked)(() => { var _a; return generateWidgetAPIContext((_a = $instance === null || $instance === void 0 ? void 0 : $instance.__internal__) === null || _a === void 0 ? void 0 : _a.$w, parentWidget, forContext); });
222
- forList = dataBinds[nodeId]._waFor.call($instance, $instance, forContext.lists, forContext.forItems, undefined, dataContext, $w);
223
- if (!Array.isArray(forList)) {
224
- forList = [];
225
- }
226
- }
227
- catch (e) {
228
- forList = [];
229
- _FOR_ERROR_CACHE_MAP[nodeId] = setTimeout(() => {
230
- console.warn('For binding error', nodeId, e);
231
- }, 1000);
232
- }
233
- // Track list change (e.g. push)
234
- forList.forEach((e) => { });
235
- (0, mobx_1.untracked)(() => {
236
- // dispose widgets before reused instead
237
- // disposeWidgets(parentForWidgets[curForNode.id])
238
- const exsitMap = forList.reduce((map, item, index) => {
239
- const cache = item === null || item === void 0 ? void 0 : item[_waForKey];
240
- if (map[cache] === undefined) {
241
- map[cache] = index;
242
- }
243
- return map;
244
- }, {});
245
- const forWidgets = ownerForWidgetHolder[nodeId];
246
- const existingWidgetMap = {};
247
- const existingWidgetIndexMap = {};
248
- const extraWidgetsIndexMap = {};
249
- forWidgets.forEach((widget, index) => {
250
- var _a;
251
- if (exsitMap[widget._key] !== undefined) {
252
- const nodeId = (_a = widget.id) === null || _a === void 0 ? void 0 : _a.split(exports.ID_SEPARATOR)[0];
253
- existingWidgetMap[widget._key] = { index: exsitMap[widget._key], widgets: { [nodeId]: widget } };
254
- /**
255
- * 此处依赖了 existingWidgetMap[widget._key].widgets 的引用,
256
- * 为了直接可以通过 index 访问到 existingWidgetMap 里的值进行编辑
257
- * 但是依赖引用关系,存在维护风险
258
- */
259
- existingWidgetIndexMap[index] = existingWidgetMap[widget._key].widgets;
260
- // need to use uqique key
261
- exsitMap[widget._key] = undefined;
262
- }
263
- else {
264
- extraWidgetsIndexMap[index] = widget;
265
- }
266
- });
267
- // clean extra widgets of previous for run
268
- dfsTree(curForNode, (node) => {
269
- const arr = ownerForWidgetHolder[node.id] || [];
270
- /**
271
- * clone 上次 for 已有的 widgets
272
- */
273
- if (node.id !== curForNode.id) {
274
- arr.forEach((item, index) => {
275
- if (existingWidgetIndexMap[index]) {
276
- existingWidgetIndexMap[index][node.id] = item;
277
- }
278
- });
279
- }
280
- /**
281
- * 重头开始生成
282
- * 清空原有 arr 并保持引用不变
283
- */
284
- arr.splice(0, arr.length);
285
- }, undefined);
286
- /**
287
- * 明确已经不会复用的节点,清除 mobx observer
288
- * 并递归清理子节点
289
- */
290
- for (const index in extraWidgetsIndexMap) {
291
- const w = extraWidgetsIndexMap[index];
292
- disposeWidget(w);
293
- const { children } = w.parent || parentWidget;
294
- children.remove(w);
295
- // w.parent = null
296
- }
297
- const isInRepeaterChild = (parentWidget === null || parentWidget === void 0 ? void 0 : parentWidget.widgetType) === `${constant_1.REPEATER.MODULE_NAME}:${constant_1.REPEATER.REPEATER_NAME}`;
298
- forList.forEach((item, index) => {
299
- let forContextListAlias;
300
- let { lists = [], forItems = {} } = forContext;
301
- const listMeta = { currentItem: item, currentIndex: index };
302
- if (isInRepeaterChild) {
303
- forContextListAlias = {
304
- [`${parentWidget.forIndex}` || 'currentIndex']: listMeta.currentIndex,
305
- [`${parentWidget.forItem}` || 'currentItem']: listMeta.currentItem,
306
- };
307
- }
308
- const _forContext = {
309
- lists: [{ ...listMeta, alias: forContextListAlias }, ...lists],
310
- forItems: { ...forItems, [nodeId]: item },
311
- };
312
- const { rootWidget } = createSubWidgetTree({ ownerForWidgetHolder, existingWidgetMap }, curForNode, dataBinds, ownerMpInst, _forContext, failedBinds, parentWidget);
313
- rootWidget._forContext = _forContext;
314
- });
315
- });
316
- });
317
- return dispose;
318
- }
319
- function createWidget(props, nodeId, indexPostfix, parent, ownerMpInst, forContext) {
320
- const { widgetType, _parentId, ...restProps } = props;
321
- const w = (0, mobx_1.observable)(restProps);
322
- const id = `${nodeId}${indexPostfix}`;
323
- // Builtin props
324
- w.id = id; // 重用之后要修改id
325
- // Object.defineProperty(w, 'id', { value: id });
326
- Object.defineProperty(w, 'widgetType', { value: widgetType });
327
- Object.defineProperty(w, '_scope', {
328
- value: (0, mobx_1.observable)({
329
- id: nodeId,
330
- dataContext: {},
331
- /**
332
- * 当前节点的 for currentItem 对象
333
- */
334
- forContext,
335
- }),
336
- });
337
- // w._disposers = []
338
- // w.children = []
339
- Object.defineProperty(w, 'children', { value: (0, mobx_1.observable)([]) });
340
- Object.defineProperty(w, '_disposers', { value: (0, mobx_1.observable)([]) });
341
- Object.defineProperty(w, '_eventListeners', { value: new event_emitter_1.default() });
342
- if (parent) {
343
- // w.parent = parent
344
- Object.defineProperty(w, 'parent', { value: parent });
345
- parent.children.push(w);
346
- }
347
- switch (widgetType) {
348
- case `${constant_1.REPEATER.MODULE_NAME}:${constant_1.REPEATER.REPEATER_NAME}`: {
349
- if (!w.items) {
350
- Object.defineProperty(w, 'items', {
351
- get() {
352
- return (w.children || []).map((item) => {
353
- const descendants = {};
354
- Object.keys((item === null || item === void 0 ? void 0 : item._descendants) || {}).forEach((key) => {
355
- descendants[key] = item._descendants[key]._userWidget;
356
- });
357
- return descendants;
358
- });
359
- },
360
- });
361
- }
362
- break;
363
- }
364
- }
365
- mountBuiltinWigetsAPI(w, ownerMpInst);
366
- return w;
367
- }
368
- function setUpWidgetDataBinds(w, dataBinds, forContext, failedBinds, ctx) {
369
- Object.keys(dataBinds || {})
370
- .sort((a, b) => {
371
- return a.length - b.length > 0 ? 1 : -1;
372
- })
373
- .map((prop) => {
374
- if (prop === '_waFor') {
375
- return;
376
- }
377
- let timer = null;
378
- const setUpDataBind = (isFinalTry) => {
379
- let ran = false;
380
- const dispose = (0, mobx_1.autorun)((reaction) => {
381
- try {
382
- clearTimeout(timer);
383
- const dataContext = (0, mobx_1.untracked)(() => (0, util_1.generateDataContext)(w));
384
- const $w = (0, mobx_1.untracked)(() => { var _a; return generateWidgetAPIContext((_a = ctx === null || ctx === void 0 ? void 0 : ctx.__internal__) === null || _a === void 0 ? void 0 : _a.$w, w, forContext); });
385
- // Computed data bind in the next tick since data bind may read widgets data
386
- const value = dataBinds[prop].call(ctx, ctx, forContext.lists, forContext.forItems, undefined, dataContext, $w);
387
- const paths = prop.split('.').filter((key) => !!key);
388
- if (paths.length > 1) {
389
- // 一定要 untracked 不然爆栈了
390
- (0, mobx_1.untracked)(() => (0, lodash_set_1.default)(w, prop, value));
391
- }
392
- else {
393
- // 普通 key 直接赋值
394
- w[prop] = value;
395
- }
396
- }
397
- catch (e) {
398
- if (prop === '_waIf') {
399
- w[prop] = false;
400
- }
401
- if (isFinalTry || ran) {
402
- timer = setTimeout(() => {
403
- console.warn(`Error computing data bind ${w.id}.${prop}`, e);
404
- }, 1000);
405
- }
406
- else {
407
- failedBinds.push((...args) => {
408
- reaction.dispose();
409
- return setUpDataBind(...args);
410
- });
411
- }
412
- ran = true;
413
- }
414
- });
415
- w._disposers.push(dispose);
416
- };
417
- setUpDataBind();
418
- });
419
- }
420
- function generateForContextOfWidget(widget) {
421
- const forContext = widget._forContext;
422
- if (forContext)
423
- return forContext;
424
- if (widget.parent)
425
- return generateForContextOfWidget(widget.parent);
426
- }
427
- exports.generateForContextOfWidget = generateForContextOfWidget;
428
- exports.ID_SEPARATOR = '-';
429
- function getWidget(widgets, id) {
430
- return (0, util_1.getDeep)(widgets, id, exports.ID_SEPARATOR);
431
- }
432
- exports.getWidget = getWidget;
433
- function getNodeParentWidget(node, widgets, defaultParent = { children: (0, mobx_1.observable)([]), _disposers: [] }) {
434
- var _a;
435
- return (((_a = node === null || node === void 0 ? void 0 : node.parent) === null || _a === void 0 ? void 0 : _a.id) && (widgets === null || widgets === void 0 ? void 0 : widgets[node.parent.id])) || defaultParent;
436
- }
437
- /**
438
- * Add parent, children to widget
439
- */
440
- function createWidgetDataTree(widgets, dataBinds) {
441
- const virtualRoot = { children: [], forCount: 0 };
442
- const nodes = Object.keys(widgets).reduce((result, id) => {
443
- const w = widgets[id];
444
- result[id] = { id, value: w, _order: w._order, children: [], parent: null, forCount: 0 };
445
- return result;
446
- }, {});
447
- // Create widgets tree API
448
- Object.keys(nodes).map((id) => {
449
- const curNode = nodes[id];
450
- const parent = nodes[widgets[id]._parentId];
451
- // delete widgets[id]._parentId
452
- if (!parent) {
453
- virtualRoot.children.push(curNode);
454
- return;
455
- }
456
- curNode.parent = parent;
457
- parent.children.push(curNode);
458
- });
459
- // Sort children
460
- Object.keys(nodes).map((id) => {
461
- nodes[id].children.sort((a, b) => a._order - b._order);
462
- });
463
- virtualRoot.children.map(addForCount);
464
- // dfs, add forCount
465
- function addForCount(node) {
466
- var _a, _b;
467
- if (node.parent) {
468
- node.forCount = node.parent.forCount;
469
- const { widgetType } = node.parent.value;
470
- if (widgetType === `${constant_1.REPEATER.MODULE_NAME}:${constant_1.REPEATER.REPEATER_ITEM_NAME}`) {
471
- node._ancestorId = node.parent.id;
472
- }
473
- if ((_a = node.parent) === null || _a === void 0 ? void 0 : _a._ancestorId) {
474
- // Repeater 作用域内的所有子孙(排除里面的 Repeater 组件,它本身又产生深一层的作用域)继承父级的循环信息
475
- if (!node._ancestorId) {
476
- node._ancestorId = node.parent._ancestorId;
477
- }
478
- }
479
- }
480
- if ((_b = dataBinds[node.id]) === null || _b === void 0 ? void 0 : _b._waFor) {
481
- node.forCount += 1;
482
- }
483
- node.children.map(addForCount);
484
- }
485
- return virtualRoot;
486
- }
487
- function dfsTree(node, fn, parent, cache = {}) {
488
- node.value && fn(node, parent, cache);
489
- node.children.map((e) => dfsTree(e, fn, node.value ? node : null, cache));
490
- }
491
- // dispose autorun, widget can be the virtual root widget
492
- function disposeWidget(widget, noRecursive = false) {
493
- const disposers = widget._disposers;
494
- disposers.map((dispose) => dispose());
495
- disposers.splice(0, disposers.length);
496
- !noRecursive && widget.children.forEach((w) => disposeWidget(w));
497
- }
498
- exports.disposeWidget = disposeWidget;
499
- function createInitData(widgets, dataBinds, keyPrefix = '') {
500
- return Object.keys(widgets).reduce((result, id) => {
501
- if (!isWidgetInFor(id, widgets, dataBinds)) {
502
- result[keyPrefix + id] = resolveWidgetData(widgets[id]);
503
- }
504
- else {
505
- result[keyPrefix + id] = [];
506
- }
507
- return result;
508
- }, {});
509
- }
510
- exports.createInitData = createInitData;
511
- function isWidgetInFor(id, widgets, dataBinds) {
512
- var _a;
513
- let curNode = widgets[id];
514
- let nodeId = id;
515
- while (curNode) {
516
- if ((_a = dataBinds[nodeId]) === null || _a === void 0 ? void 0 : _a._waFor) {
517
- return true;
518
- }
519
- nodeId = curNode._parentId;
520
- curNode = widgets[nodeId];
521
- }
522
- }
523
- function mountBuiltinWigetsAPI(widget, owner) {
524
- // #1 builtin APIs
525
- widget.findWidgets = function (filter, includeInvisibleDescendants) {
526
- let { children = [] } = this;
527
- if (!includeInvisibleDescendants) {
528
- // include visible widgets only by default
529
- children = children.filter((e) => e._waIf !== false);
530
- }
531
- const matched = [];
532
- children.forEach((w) => {
533
- if (filter(w)) {
534
- matched.push(w);
535
- }
536
- matched.push(...w.findWidgets(filter, includeInvisibleDescendants));
537
- });
538
- return matched;
539
- };
540
- widget.getWidgetsByType = function (type, includeInvisibleDescendants) {
541
- return this.findWidgets((w) => w.widgetType === type, includeInvisibleDescendants);
542
- };
543
- /**
544
- * Similar to selectOwnerComponent of WX MP: https://developers.weixin.qq.com/miniprogram/dev/reference/api/Component.html
545
- */
546
- widget.getOwnerWidget = function () {
547
- var _a, _b;
548
- return (_b = (_a = owner === null || owner === void 0 ? void 0 : owner._getInstance) === null || _a === void 0 ? void 0 : _a.call(owner)) === null || _b === void 0 ? void 0 : _b.node;
549
- };
550
- // Will be overwritten by composited component
551
- widget.getDom = function (fields) {
552
- return new Promise((resolve, reject) => {
553
- const query = (owner || wx).createSelectorQuery();
554
- query
555
- .select(`#${this.id}`)
556
- .fields(fields, (res) => {
557
- resolve(res);
558
- })
559
- .exec();
560
- });
561
- };
562
- widget.on = function (type, listener) {
563
- this._eventListeners.on(type, listener);
564
- };
565
- widget.off = function (type, listener) {
566
- this._eventListeners.off(type, listener);
567
- };
568
- widget.getConfig = () => ({});
569
- const lowcode = weapp_component_1.compLowcodes[widget.widgetType];
570
- if (lowcode) {
571
- const { config } = lowcode;
572
- widget.getConfig = () => config;
573
- }
574
- widget._getInstanceRef = () => null;
575
- /**
576
- * @deprecated
577
- */
578
- widget._methods = {};
579
- Object.defineProperty(widget, '_userWidget', {
580
- value: (0, mobx_1.untracked)(() => {
581
- return new UserWidget(widget);
582
- }),
583
- });
584
- }
585
- /**
586
- * 对外(用户)的 Widget
587
- */
588
- class UserWidget {
589
- constructor(widget) {
590
- this._widget = null;
591
- this._widget = widget;
592
- return new Proxy(this, {
593
- get(target, prop) {
594
- if (prop in target) {
595
- return target[prop];
596
- }
597
- // 优先 custom
598
- if (target.custom[prop]) {
599
- return target.custom[prop];
600
- }
601
- // 再尝试内置的方法和属性
602
- const buildinMember = target.sys[prop];
603
- if (buildinMember)
604
- return buildinMember;
605
- // 兼容原来的 widget。最后直接代理到 widget 上,主要是访问组件的属性
606
- return target._widget[prop];
607
- },
608
- });
609
- }
610
- get description() {
611
- const { id } = this._widget;
612
- return `
613
- 使用说明:
614
- 1. w 命名空间下为内置方法,可通过 $w.${id}.w.<成员> 或 $w.${id}.<成员> 访问。
615
- 访问 id 示例: $w.${id}.id 或 $w.${id}.w.id
616
- 2. custom 命名空间下为用户自定义对外暴露的成员,可通过 $w.${id}.custom.<成员> 或 $w.${id}.<成员> 访问。
617
- 访问自定义方法 hello 示例: $w.${id}.hello() 或 $w.${id}.custom.hello()
618
- 3. 通过 $w.${id}.<成员> 访问时,如果自定义成员与内置成员重名,则自定义成员覆盖内置成员。
619
- 4. [注意]: 请不要直接访问 _widget,它里面的成员为内部实现,随时可能进行调整
620
- `;
621
- }
622
- // 内置属性和方法命名空间
623
- get sys() {
624
- var _a, _b;
625
- const widget = this._widget;
626
- const [module, component] = ((_b = (_a = widget.widgetType) === null || _a === void 0 ? void 0 : _a.split) === null || _b === void 0 ? void 0 : _b.call(_a, ':')) || [];
627
- return {
628
- /**
629
- * 内置属性
630
- */
631
- get id() {
632
- return widget.id;
633
- },
634
- get module() {
635
- return module;
636
- },
637
- get component() {
638
- return component;
639
- },
640
- get name() {
641
- return widget.widgetType;
642
- },
643
- get parent() {
644
- var _a;
645
- return (_a = widget.parent) === null || _a === void 0 ? void 0 : _a._userWidget;
646
- },
647
- get children() {
648
- var _a;
649
- return ((_a = widget.children) === null || _a === void 0 ? void 0 : _a.map((item) => item._userWidget)) || [];
650
- },
651
- /**
652
- * 内置方法
653
- */
654
- closest(filter) {
655
- let { parent } = this;
656
- if (!filter)
657
- return parent;
658
- while (parent) {
659
- const matched = filter(parent);
660
- if (matched)
661
- return parent;
662
- parent = parent.parent;
663
- }
664
- return null;
665
- },
666
- /**
667
- * 为了兼容 2.17+ button 组件,使用 $node.parent.getConfig
668
- * 挂载代理
669
- */
670
- getConfig(...args) {
671
- var _a;
672
- return (_a = widget.getConfig) === null || _a === void 0 ? void 0 : _a.call(widget, ...args);
673
- },
674
- };
675
- }
676
- get custom() {
677
- var _a, _b, _c;
678
- const { methods = {}, ...restInstance } = ((_c = (_b = (_a = this._widget)._getInstanceRef) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.current) || {};
679
- const userCustomMember = {
680
- ...this._widget._methods,
681
- ...restInstance,
682
- ...methods,
683
- };
684
- return userCustomMember;
685
- }
686
- }
687
- function generateWidgetAPIContext($w = {}, widget, forContext) {
688
- return new Proxy($w, {
689
- get(target, prop) {
690
- // debugger;
691
- var _a;
692
- /**
693
- * for context 变量优先
694
- */
695
- const { lists = [] } = forContext;
696
- for (const meta of lists) {
697
- const map = meta.alias || {};
698
- if (prop in map) {
699
- return map[prop];
700
- }
701
- }
702
- // 尝试代理同级 widget
703
- if (widget) {
704
- let { parent } = widget;
705
- while (parent) {
706
- if ((_a = parent._descendants) === null || _a === void 0 ? void 0 : _a[prop]) {
707
- return parent._descendants[prop]._userWidget;
708
- }
709
- parent = parent.parent;
710
- }
711
- }
712
- // 尝试代理到全局的 $w
713
- return target[prop];
714
- },
715
- });
716
- }
717
- exports.generateWidgetAPIContext = generateWidgetAPIContext;