@cloudbase/lowcode-builder 1.2.4 → 1.3.0

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.
@@ -1,11 +1,16 @@
1
1
  import { observable } from 'mobx';
2
2
  import { createComputed, createEventHandlers, checkAuth } from './util';
3
- import { createWidgets, createInitData, disposeWidget, ID_SEPARATOR } from './widget';
3
+ import { createWidgets, createInitData, disposeWidget, ID_SEPARATOR, getWidget } from './widget';
4
4
  import mergeRenderer from './merge-renderer';
5
- import { createDataset, EXTRA_API, createStateDataSourceVar, generateParamsParser, setConfig } from '../datasources/index';
6
- import { runWatchers } from './watch'
7
- import lodashGet from 'lodash.get';
8
- import { getMpEventHandlerName } from "./util";
5
+ import {
6
+ createDataset,
7
+ EXTRA_API,
8
+ createStateDataSourceVar,
9
+ generateParamsParser,
10
+ setConfig,
11
+ } from '../datasources/index';
12
+ import { runWatchers } from './watch';
13
+ import { getMpEventHandlerName } from './util';
9
14
 
10
15
  export function createPage(
11
16
  id,
@@ -20,31 +25,32 @@ export function createPage(
20
25
  app,
21
26
  handler,
22
27
  pageContext = {},
23
- pageAttributes
28
+ pageAttributes,
24
29
  ) {
25
30
  const evtHandlers = createEventHandlers(evtListeners);
26
31
 
27
32
  function extractLifecycles() {
28
33
  const result = {};
29
- Object.keys(lifecycle).map(name => {
34
+ Object.keys(lifecycle).map((name) => {
30
35
  result[name] = function () {
31
- return lifecycle[name].apply(this.getWeAppInst(), arguments)
32
- }
33
- })
36
+ return lifecycle[name].apply(this._getInstance(), arguments);
37
+ };
38
+ });
34
39
 
35
40
  result['onShareAppMessage'] = (res) => {
36
41
  if (res?.from === 'button' && res?.target?.dataset?.weda_share_info) {
37
42
  return res?.target?.dataset?.weda_share_info;
38
- }
39
- else if (res?.from === 'menu' && pageAttributes?.appShareMessage) {
43
+ } else if (res?.from === 'menu' && pageAttributes?.appShareMessage) {
40
44
  let { enable, pageId, params, imageUrl, title } = pageAttributes.appShareMessage;
41
45
  if (enable) {
42
46
  pageId = pageId ? pageId.replace(/^(\.)?\//, '') : pageId;
43
- let realPath = `/pages/${pageId}/index` + (params ? '?' + params.map(pair => pair.key + '=' + pair.value).join('&') : '');
47
+ let realPath =
48
+ `/pages/${pageId}/index` +
49
+ (params ? '?' + params.map((pair) => pair.key + '=' + pair.value).join('&') : '');
44
50
  return {
45
51
  path: realPath,
46
52
  imageUrl,
47
- title
53
+ title,
48
54
  };
49
55
  }
50
56
  }
@@ -54,7 +60,7 @@ export function createPage(
54
60
  console.log(error);
55
61
  return {};
56
62
  }
57
- }
63
+ };
58
64
  return result;
59
65
  }
60
66
 
@@ -63,45 +69,37 @@ export function createPage(
63
69
  data: createInitData(widgetProps, dataBinds),
64
70
  lifetimes: {
65
71
  attached() {
66
- const $page = this.getWeAppInst()
72
+ const $page = this._getInstance();
67
73
  this._pageActive = true;
74
+ /**
75
+ * @deprecated
76
+ * 使用 behavior 的方式挂载methods
77
+ */
68
78
  this.__mnt__ = (e) => {
69
- if (!$page.componentMethods) {
70
- $page.componentMethods = {};
71
- }
72
- const [id, index = 0] = e.target.id.split(ID_SEPARATOR);
73
- if (!$page.componentMethods[id]) {
74
- $page.componentMethods[id] = [];
75
- }
76
- $page.componentMethods[id][index] = e.detail.methods;
79
+ const widget = getWidget($page.widgets, e.target.id);
80
+ widget._methods = e.detail.methods;
77
81
  };
82
+ /**
83
+ * @deprecated
84
+ * 使用 behavior 的方式挂载methods
85
+ */
78
86
  this.__unmnt__ = (e) => {
79
- const [id, index = 0] = e.target.id.split(ID_SEPARATOR);
80
- if ($page.componentMethods) {
81
- const components = $page.componentMethods[id];
82
- if (components) {
83
- components[index] = undefined;
84
- if (components.filter(com => com !== undefined).length === 0) {
85
- delete $page.componentMethods[id];
86
- }
87
- }
88
- }
89
- }
87
+ const widget = getWidget($page.widgets, e.target.id);
88
+ widget._methods = {};
89
+ };
90
90
  this._disposers = this.initMergeRenderer($page.widgets);
91
91
  },
92
92
  detached() {
93
- const $page = this.getWeAppInst()
94
- this._pageActive = false
95
- disposeWidget($page._rootWidget)
96
- this._disposers.forEach(dispose => dispose())
93
+ const $page = this._getInstance();
94
+ this._pageActive = false;
95
+ disposeWidget($page._rootWidget);
96
+ this._disposers.forEach((dispose) => dispose());
97
97
  },
98
98
  },
99
99
  pageLifetimes: {
100
100
  // 组件所在页面的生命周期函数,定义下给运营平台上报用
101
- show: function () {
102
- },
103
- hide: function () {
104
- },
101
+ show: function () {},
102
+ hide: function () {},
105
103
  },
106
104
  methods: {
107
105
  _pageActive: true,
@@ -110,7 +108,7 @@ export function createPage(
110
108
  ...evtHandlers,
111
109
  ...mergeRenderer,
112
110
  onLoad(options) {
113
- const $page = this.getWeAppInst()
111
+ const $page = this._getInstance();
114
112
  setConfig({ currentPageId: $page.uuid });
115
113
  app.__internal__.activePage = $page;
116
114
  this._pageActive = true;
@@ -120,36 +118,36 @@ export function createPage(
120
118
  createStateDataSourceVar($page.uuid, generateParamsParser({ app, $page }));
121
119
 
122
120
  const hook = lifecycle.onLoad || lifecycle.onPageLoad;
123
- hook && hook.call($page, query);
121
+ hook?.call?.($page, query);
124
122
 
125
- this.invokeEventHandler(id, 'load', {detail: {query}});
123
+ this.invokeEventHandler(id, 'load', { detail: { query } });
126
124
  },
127
125
  onReady() {
128
- const $page = this.getWeAppInst();
126
+ const $page = this._getInstance();
129
127
 
130
128
  this._disposers.push(...runWatchers(index, this));
131
129
 
132
130
  const hook = lifecycle.onReady || lifecycle.onPageReady;
133
- hook && hook.call($page);
131
+ hook?.call?.($page);
134
132
 
135
133
  this.invokeEventHandler(id, 'ready');
136
134
  },
137
135
  onUnload() {
138
- const $page = this.getWeAppInst()
136
+ const $page = this._getInstance();
139
137
 
140
138
  const hook = lifecycle.onUnload || lifecycle.onPageUnload;
141
- hook && hook.call($page);
139
+ hook?.call?.($page);
142
140
 
143
141
  this.invokeEventHandler(id, 'unload');
144
142
  },
145
143
  async onShow() {
146
- const $page = this.getWeAppInst()
144
+ const $page = this._getInstance();
147
145
  setConfig({ currentPageId: $page.uuid });
148
146
  app.__internal__.activePage = $page;
149
147
  this._pageActive = true;
150
148
 
151
149
  const hook = lifecycle.onShow || lifecycle.onPageShow;
152
- hook && hook.call($page);
150
+ hook?.call?.($page);
153
151
 
154
152
  // 权限检查
155
153
  if (await checkAuth(app, app.id, $page)) {
@@ -161,9 +159,9 @@ export function createPage(
161
159
  this.invokeEventHandler(id, 'show');
162
160
  },
163
161
  onHide() {
164
- const $page = this.getWeAppInst()
162
+ const $page = this._getInstance();
165
163
  const hook = lifecycle.onHide || lifecycle.onPageHide;
166
- hook && hook.call($page);
164
+ hook?.call?.($page);
167
165
  this._pageActive = false;
168
166
 
169
167
  // 触发页面节点事件
@@ -173,22 +171,24 @@ export function createPage(
173
171
  const keyName = getMpEventHandlerName(id, triggerName);
174
172
  typeof evtHandlers?.[keyName] === 'function' && evtHandlers[keyName].call(this, params);
175
173
  },
176
- getWeAppInst() {
177
- let $page = this.$WEAPPS_PAGE
178
- if ($page) { return $page }
174
+ _getInstance() {
175
+ let $page = this.$WEAPPS_PAGE;
176
+ if ($page) {
177
+ return $page;
178
+ }
179
179
  Object.assign(pageContext, {
180
180
  id,
181
181
  uuid,
182
182
  state: observable(pageState),
183
- widgets: {}
184
- })
183
+ widgets: {},
184
+ });
185
185
  $page = pageContext;
186
- this.$WEAPPS_PAGE = $page
186
+ this.$WEAPPS_PAGE = $page;
187
187
  $page.handler = Object.keys(handler).reduce((result, key) => {
188
- result[key] = handler[key].bind($page)
189
- return result
190
- }, {})
191
- $page.computed = createComputed(pageComputed, $page)
188
+ result[key] = handler[key].bind($page);
189
+ return result;
190
+ }, {});
191
+ $page.computed = createComputed(pageComputed, $page);
192
192
  let dataset = createDataset($page.uuid);
193
193
  $page.dataset = dataset;
194
194
  $page.state.dataset = dataset;
@@ -198,40 +198,45 @@ export function createPage(
198
198
  });
199
199
  };
200
200
 
201
- const { rootWidget } = createWidgets(widgetProps, dataBinds, $page.widgets, this)
202
- $page._rootWidget = rootWidget
201
+ const { rootWidget } = createWidgets(widgetProps, dataBinds, $page.widgets, this);
202
+ $page._rootWidget = rootWidget;
203
203
 
204
204
  $page.invokeComponentMethod = ({ component, method, params }) => {
205
- const components = lodashGet($page.componentMethods, component);
206
- let componentInstance = undefined
205
+ const components = $page.widgets[component];
206
+ let componentInstance = undefined;
207
207
  if (Array.isArray(components)) {
208
- if(components.length > 1){
209
- throw new Error('调用方法失败:id为'+component+'的组件拥有多个实例')
208
+ if (components.length > 1) {
209
+ throw new Error(`调用方法失败:id为${component}的组件拥有多个实例`);
210
210
  }
211
- componentInstance = components[0]
212
- // components.forEach(component => {
213
- // component[method](params);
214
- // })
211
+ componentInstance = components[0];
215
212
  } else {
216
- componentInstance = components
213
+ componentInstance = components;
217
214
  }
218
215
 
219
- if(!componentInstance?.[method] || typeof componentInstance[method]!= 'function') {
220
- throw new Error('调用方法失败:未找到id为'+component+'下的方法'+method)
216
+ const currentInstanceRef = componentInstance?._getInstanceRef()?.current;
217
+ const { methods = {}, ...restInstanceRef } = currentInstanceRef;
218
+
219
+ const readonlyMap = {
220
+ ...componentInstance?._methods,
221
+ ...restInstanceRef,
222
+ ...methods,
223
+ };
224
+
225
+ if (typeof readonlyMap[method] !== 'function') {
226
+ throw new Error(`调用方法失败:未找到id为${component}下的方法${method}`);
221
227
  }
222
- return componentInstance[method](params);
228
+ return readonlyMap[method](params);
223
229
  };
224
230
 
225
- return $page
231
+ return $page;
226
232
  },
227
233
  },
228
234
  });
229
235
  }
230
236
 
231
237
  function decodePageQuery(query) {
232
- return Object.keys(query)
233
- .reduce((decoded, key) => {
234
- decoded[key] = decodeURIComponent(query[key]);
235
- return decoded;
236
- }, {});
238
+ return Object.keys(query).reduce((decoded, key) => {
239
+ decoded[key] = decodeURIComponent(query[key]);
240
+ return decoded;
241
+ }, {});
237
242
  }