@kissflow/lowcode-client-sdk 1.0.12 → 1.0.13

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,22 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  var __accessCheck = (obj, member, msg) => {
21
2
  if (!member.has(obj))
22
3
  throw TypeError("Cannot " + msg);
@@ -39,7 +20,7 @@ var __privateMethod = (obj, member, method) => {
39
20
  __accessCheck(obj, member, "access private method");
40
21
  return method;
41
22
  };
42
- var _listeners, _eventListeners, _addListener, addListener_fn, _appendEventListeners, appendEventListeners_fn, _checkEvents, checkEvents_fn, _onMessage, onMessage_fn, _csrfToken;
23
+ var _listeners, _registerComponentAPIs, registerComponentAPIs_fn, _csrfToken;
43
24
  let nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
44
25
  byte &= 63;
45
26
  if (byte < 36) {
@@ -55,7 +36,6 @@ let nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduce(
55
36
  }, "");
56
37
  const LISTENER_CMDS = {
57
38
  API: "API",
58
- PARAMS: "PARAMS",
59
39
  GET_CONTEXT: "GET_CONTEXT",
60
40
  RETURN: "RETURN",
61
41
  GET_FORM_FIELD: "GET_FORM_FIELD",
@@ -87,11 +67,23 @@ const LISTENER_CMDS = {
87
67
  GET_POPUP_PARAMS: "GET_POPUP_PARAMS",
88
68
  GET_ALL_POPUP_PARAMS: "GET_ALL_POPUP_PARAMS",
89
69
  CLOSE_POPUP: "CLOSE_POPUP",
90
- GET_COMPONENT: "GET_COMPONENT",
70
+ COMPONENT_GET: "COMPONENT_GET",
91
71
  COMPONENT_REFRESH: "COMPONENT_REFRESH",
92
72
  COMPONENT_SHOW: "COMPONENT_SHOW",
93
73
  COMPONENT_HIDE: "COMPONENT_HIDE",
94
- INITIALIZE_CUST_COMP: "INITIALIZE_CUST_COMP"
74
+ COMPONENT_ADD_EVENT_LISTENER: "COMPONENT_ADD_EVENT_LISTENER",
75
+ CC_INITIALIZE: "CC_INITIALIZE",
76
+ CC_WATCH_PARAMS: "CC_WATCH_PARAMS",
77
+ WINDOW_NDEF_READER_NEW: "WINDOW_NDEF_READER_NEW",
78
+ WINDOW_NDEF_READER_SCAN: "WINDOW_NDEF_READER_SCAN",
79
+ WINDOW_NDEF_READER_WRITE: "WINDOW_NDEF_READER_WRITE",
80
+ WINDOW_NDEF_READER_ADD_EVENT_LISTENER: "WINDOW_NDEF_READER_ADD_EVENT_LISTENER",
81
+ WINDOW_NDEF_READER_MAKE_READONLY: "WINDOW_NDEF_READER_MAKE_READONLY",
82
+ WINDOW_NDEF_READER_ABORT_SCAN: "WINDOW_NDEF_READER_ABORT_SCAN"
83
+ };
84
+ const EVENT_TYPES = {
85
+ COMPONENT_ON_MOUNT: "componentOnMount",
86
+ CC_ON_PARAMS_CHANGE: "onCustomComponentParamsChange"
95
87
  };
96
88
  const DEFAULTS = {
97
89
  POPUP_ID: "ACTIVE_POP_UP"
@@ -99,30 +91,87 @@ const DEFAULTS = {
99
91
  function generateId(prefix = "lcncsdk") {
100
92
  return `${prefix}-${nanoid()}`;
101
93
  }
94
+ const globalInstances = {};
102
95
  function postMessage(args) {
103
- if (self.parent && self.parent !== self) {
104
- self.parent.postMessage(args, "*");
96
+ if (globalThis.parent && globalThis.parent !== globalThis) {
97
+ globalThis.parent.postMessage(args, "*");
105
98
  } else {
106
- self.postMessage(args);
99
+ globalThis.postMessage(args);
107
100
  }
108
101
  }
109
- class BaseSDK {
110
- constructor(props) {
111
- __privateAdd(this, _addListener);
112
- __privateAdd(this, _appendEventListeners);
113
- __privateAdd(this, _checkEvents);
114
- __privateAdd(this, _onMessage);
102
+ function onMessage(event) {
103
+ if (event.origin !== globalThis.location.origin) {
104
+ const data = event.data;
105
+ if (data == null ? void 0 : data.isEvent) {
106
+ const { target, eventParams, eventName, eventConfig = {} } = data;
107
+ let targetInstance = globalInstances[target];
108
+ targetInstance._dispatchEvent(eventName, eventParams);
109
+ if (eventConfig.once) {
110
+ targetInstance._removeEventListener(eventName);
111
+ }
112
+ return;
113
+ }
114
+ let { _req: req, resp } = data;
115
+ if (req == null ? void 0 : req._id) {
116
+ let targetInstance = globalInstances[req._id];
117
+ targetInstance._dispatchMessageEvents(req, resp);
118
+ }
119
+ }
120
+ }
121
+ globalThis.addEventListener("message", onMessage);
122
+ class EventBase {
123
+ constructor() {
115
124
  __privateAdd(this, _listeners, void 0);
116
- __privateAdd(this, _eventListeners, void 0);
117
125
  __privateSet(this, _listeners, {});
118
- __privateSet(this, _eventListeners, {});
119
- self.addEventListener("message", __privateMethod(this, _onMessage, onMessage_fn).bind(this), false);
120
126
  }
127
+ _addEventListener(eventName, callBack) {
128
+ var _a;
129
+ __privateGet(this, _listeners)[eventName] = ((_a = __privateGet(this, _listeners)) == null ? void 0 : _a[eventName]) || [];
130
+ __privateGet(this, _listeners)[eventName].push(callBack);
131
+ }
132
+ _removeEventListener(eventName, callBack) {
133
+ if (callBack) {
134
+ let index = __privateGet(this, _listeners)[eventName].findIndex(callBack);
135
+ index > -1 && __privateGet(this, _listeners)[eventName].splice(index, 1);
136
+ return;
137
+ }
138
+ Reflect.deleteProperty(__privateGet(this, _listeners), eventName);
139
+ }
140
+ _dispatchEvent(eventName, eventParams) {
141
+ if (Array.isArray(__privateGet(this, _listeners)[eventName])) {
142
+ __privateGet(this, _listeners)[eventName].forEach(
143
+ (callBack) => callBack(eventParams)
144
+ );
145
+ return true;
146
+ }
147
+ return false;
148
+ }
149
+ _dispatchMessageEvents(req, resp) {
150
+ let target = req._id;
151
+ if (Array.isArray(__privateGet(this, _listeners)[target])) {
152
+ __privateGet(this, _listeners)[target].forEach((listener) => {
153
+ try {
154
+ if (resp && Object.keys(resp).length === 1 && req.command !== LISTENER_CMDS.API) {
155
+ listener(Object.values(resp)[0]);
156
+ } else {
157
+ listener(resp);
158
+ }
159
+ } catch (err) {
160
+ console.error("Message callback error: ", err);
161
+ }
162
+ });
163
+ this._removeEventListener(target);
164
+ }
165
+ }
166
+ }
167
+ _listeners = new WeakMap();
168
+ class BaseSDK extends EventBase {
121
169
  _postMessageAsync(command, args, hasCallBack, callBack) {
122
170
  return new Promise((resolve, reject) => {
123
171
  const _id = generateId(command.toLowerCase());
124
- postMessage(__spreadValues({ _id, command }, args));
125
- __privateMethod(this, _addListener, addListener_fn).call(this, _id, async (data) => {
172
+ postMessage({ _id, command, ...args });
173
+ globalInstances[_id] = this;
174
+ this._addEventListener(_id, async (data) => {
126
175
  if (data == null ? void 0 : data.errorMessage) {
127
176
  reject(data);
128
177
  } else {
@@ -134,66 +183,17 @@ class BaseSDK {
134
183
  });
135
184
  });
136
185
  }
137
- _postMessage(command, func, args = {}) {
186
+ _postMessage(command, args, callBack) {
138
187
  const _id = generateId(command.toLowerCase());
139
- postMessage(__spreadValues({ _id, command }, args));
140
- __privateMethod(this, _addListener, addListener_fn).call(this, _id, (data) => func(data));
141
- }
142
- _registerEventListener(_id, eventType, callback) {
143
- __privateMethod(this, _appendEventListeners, appendEventListeners_fn).call(this, _id, eventType, callback);
144
- }
145
- }
146
- _listeners = new WeakMap();
147
- _eventListeners = new WeakMap();
148
- _addListener = new WeakSet();
149
- addListener_fn = function(_id, callback) {
150
- __privateGet(this, _listeners)[_id] = __privateGet(this, _listeners)[_id] || [];
151
- __privateGet(this, _listeners)[_id].push(callback);
152
- };
153
- _appendEventListeners = new WeakSet();
154
- appendEventListeners_fn = function(_id, eventType, callback) {
155
- if (!__privateGet(this, _eventListeners)[_id]) {
156
- __privateGet(this, _eventListeners)[_id] = {};
157
- }
158
- __privateGet(this, _eventListeners)[_id][eventType] = callback;
159
- };
160
- _checkEvents = new WeakSet();
161
- checkEvents_fn = function(data) {
162
- const { _id, eventType, eventParams } = data;
163
- if (!_id || !eventType)
164
- return;
165
- const eventListener = __privateGet(this, _eventListeners)[_id] || {};
166
- if (eventListener[eventType]) {
167
- eventListener[eventType](eventParams || {});
168
- }
169
- };
170
- _onMessage = new WeakSet();
171
- onMessage_fn = function(event) {
172
- if (event.origin !== self.location.origin) {
173
- const data = event.data;
174
- if (data.isEvent) {
175
- return __privateMethod(this, _checkEvents, checkEvents_fn).call(this, data);
176
- }
177
- const _req = (data == null ? void 0 : data._req) || {};
178
- let listeners = __privateGet(this, _listeners)[_req == null ? void 0 : _req._id] || [];
179
- if (listeners) {
180
- listeners.forEach((listener) => {
181
- try {
182
- if (data.resp && Object.keys(data.resp).length === 1 && _req.command !== LISTENER_CMDS.API) {
183
- listener(Object.values(data.resp)[0]);
184
- } else {
185
- listener(data.resp);
186
- }
187
- } catch (err) {
188
- console.error("Message callback error: ", err);
189
- }
190
- });
188
+ postMessage({ _id, command, ...args });
189
+ if (callBack) {
190
+ this._addEventListener(args.eventName, callBack);
191
191
  }
192
192
  }
193
- };
193
+ }
194
194
  class Form extends BaseSDK {
195
195
  constructor(instanceId) {
196
- super({});
196
+ super();
197
197
  this.type = "Form";
198
198
  this.instanceId = instanceId;
199
199
  }
@@ -219,19 +219,27 @@ class Form extends BaseSDK {
219
219
  }
220
220
  class Table extends BaseSDK {
221
221
  constructor(instanceId, tableId) {
222
- super({});
222
+ super();
223
223
  this.tableId = tableId;
224
224
  this.instanceId = instanceId;
225
225
  }
226
226
  toJSON() {
227
- return this._postMessageAsync(LISTENER_CMDS.TO_JSON, {
228
- tableId: this.tableId
229
- });
227
+ return this._postMessageAsync(
228
+ LISTENER_CMDS.TO_JSON,
229
+ {
230
+ tableId: this.tableId
231
+ }
232
+ );
230
233
  }
231
234
  getRows() {
232
- return this._postMessageAsync(LISTENER_CMDS.GET_TABLE_ROWS, { tableId: this.tableId }, true, (data) => {
233
- return data.map((row) => new TableForm(this.instanceId, this.tableId, row.id));
234
- });
235
+ return this._postMessageAsync(
236
+ LISTENER_CMDS.GET_TABLE_ROWS,
237
+ { tableId: this.tableId },
238
+ true,
239
+ (data) => {
240
+ return data.map((row) => new TableForm(this.instanceId, this.tableId, row.id));
241
+ }
242
+ );
235
243
  }
236
244
  getRow(rowId) {
237
245
  return new TableForm(this.instanceId, this.tableId, rowId);
@@ -251,7 +259,7 @@ class Table extends BaseSDK {
251
259
  }
252
260
  class TableForm extends BaseSDK {
253
261
  constructor(instanceId, tableId, rowId) {
254
- super({});
262
+ super();
255
263
  this.instanceId = instanceId;
256
264
  this.type = "TabelForm";
257
265
  this.tableId = tableId;
@@ -331,9 +339,25 @@ class Formatter extends BaseSDK {
331
339
  }
332
340
  class Component extends BaseSDK {
333
341
  constructor(props) {
334
- super({});
342
+ super();
343
+ __privateAdd(this, _registerComponentAPIs);
335
344
  this._id = props.componentId;
336
345
  this.type = "Component";
346
+ globalInstances[this._id] = this;
347
+ __privateMethod(this, _registerComponentAPIs, registerComponentAPIs_fn).call(this, props.componentMethods);
348
+ }
349
+ onMount(callback) {
350
+ this._postMessage(
351
+ LISTENER_CMDS.COMPONENT_ADD_EVENT_LISTENER,
352
+ {
353
+ id: this._id,
354
+ eventName: EVENT_TYPES.COMPONENT_ON_MOUNT,
355
+ eventConfig: {
356
+ once: true
357
+ }
358
+ },
359
+ callback
360
+ );
337
361
  }
338
362
  refresh() {
339
363
  return this._postMessageAsync(LISTENER_CMDS.COMPONENT_REFRESH, {
@@ -351,9 +375,32 @@ class Component extends BaseSDK {
351
375
  });
352
376
  }
353
377
  }
378
+ _registerComponentAPIs = new WeakSet();
379
+ registerComponentAPIs_fn = function(componentAPIs) {
380
+ componentAPIs == null ? void 0 : componentAPIs.forEach((Api) => {
381
+ this[Api.name] = (...args) => {
382
+ if (Api.type === "method") {
383
+ return this._postMessageAsync(`COMPONENT_${Api.name}`, {
384
+ id: this._id,
385
+ parameters: args
386
+ });
387
+ } else if (Api.type === "event") {
388
+ this._postMessage(
389
+ LISTENER_CMDS.COMPONENT_ADD_EVENT_LISTENER,
390
+ {
391
+ id: this._id,
392
+ eventName: Api.name,
393
+ eventConfig: args[1]
394
+ },
395
+ args[0]
396
+ );
397
+ }
398
+ };
399
+ });
400
+ };
354
401
  class Popup extends BaseSDK {
355
402
  constructor(props) {
356
- super({});
403
+ super();
357
404
  this.type = "Popup";
358
405
  this._id = props.popupId || DEFAULTS.POPUP_ID;
359
406
  }
@@ -372,14 +419,19 @@ class Popup extends BaseSDK {
372
419
  return this._postMessageAsync(LISTENER_CMDS.CLOSE_POPUP, {});
373
420
  }
374
421
  getComponent(componentId) {
375
- return this._postMessageAsync(LISTENER_CMDS.GET_COMPONENT, { componentId }, true, (data) => {
376
- return new Component(data);
377
- });
422
+ return this._postMessageAsync(
423
+ LISTENER_CMDS.COMPONENT_GET,
424
+ { componentId },
425
+ true,
426
+ (data) => {
427
+ return new Component(data);
428
+ }
429
+ );
378
430
  }
379
431
  }
380
432
  class Page extends BaseSDK {
381
433
  constructor(props) {
382
- super({});
434
+ super();
383
435
  this.type = "Page";
384
436
  this.popup = new Popup({});
385
437
  this._id = props.pageId;
@@ -412,14 +464,17 @@ class Page extends BaseSDK {
412
464
  });
413
465
  }
414
466
  getComponent(componentId) {
415
- return this._postMessageAsync(LISTENER_CMDS.GET_COMPONENT, { componentId }, true, (data) => {
416
- return new Component(data);
417
- });
467
+ return this._postMessageAsync(
468
+ LISTENER_CMDS.COMPONENT_GET,
469
+ { componentId },
470
+ true,
471
+ (data) => new Component(data)
472
+ );
418
473
  }
419
474
  }
420
475
  class Application extends BaseSDK {
421
476
  constructor(props) {
422
- super({});
477
+ super();
423
478
  this._id = props.appId;
424
479
  this.page = new Page(props);
425
480
  }
@@ -441,12 +496,89 @@ class Application extends BaseSDK {
441
496
  });
442
497
  }
443
498
  }
499
+ class NDEFReader extends BaseSDK {
500
+ constructor() {
501
+ super();
502
+ this.id = generateId(LISTENER_CMDS.WINDOW_NDEF_READER_NEW);
503
+ globalInstances[this.id] = this;
504
+ this._postMessage(
505
+ LISTENER_CMDS.WINDOW_NDEF_READER_NEW,
506
+ {
507
+ id: this.id,
508
+ operation: "new"
509
+ },
510
+ (data) => {
511
+ }
512
+ );
513
+ }
514
+ scan() {
515
+ return new Promise((resolve, reject) => {
516
+ this._postMessage(
517
+ LISTENER_CMDS.WINDOW_NDEF_READER_SCAN,
518
+ { id: this.id, operation: "scan" },
519
+ ({ data, err }) => {
520
+ console.log("scan data from main window ", data);
521
+ resolve(data);
522
+ }
523
+ );
524
+ });
525
+ }
526
+ write(data) {
527
+ return new Promise((resolve, reject) => {
528
+ this._postMessage(
529
+ LISTENER_CMDS.WINDOW_NDEF_READER_WRITE,
530
+ { id: this.id, operation: "write", data },
531
+ ({ data: data2, err }) => {
532
+ }
533
+ );
534
+ });
535
+ }
536
+ addEventListener(eventName, cb) {
537
+ this._postMessage(
538
+ LISTENER_CMDS.WINDOW_NDEF_READER_ADD_EVENT_LISTENER,
539
+ {
540
+ id: this.id,
541
+ operation: "addEventListener",
542
+ eventName
543
+ },
544
+ () => {
545
+ }
546
+ );
547
+ }
548
+ makeReadOnly() {
549
+ return new Promise((resolve, reject) => {
550
+ this._postMessage(
551
+ LISTENER_CMDS.WINDOW_NDEF_READER_MAKE_READONLY,
552
+ { id: this.id, operation: "makeReadOnly" },
553
+ ({ data, err }) => {
554
+ }
555
+ );
556
+ });
557
+ }
558
+ abortScan() {
559
+ return new Promise((resolve, reject) => {
560
+ this._postMessage(
561
+ LISTENER_CMDS.WINDOW_NDEF_READER_ABORT_SCAN,
562
+ { id: this.id, operation: "abortScan" },
563
+ ({ data, err }) => {
564
+ }
565
+ );
566
+ });
567
+ }
568
+ }
569
+ const window = {
570
+ NDEFReader
571
+ };
444
572
  class LowcodeSDK extends BaseSDK {
445
573
  constructor(props) {
446
- super({});
574
+ super();
447
575
  __privateAdd(this, _csrfToken, void 0);
448
576
  if (props.tableId && props.tableRowId) {
449
- this.context = new TableForm(props.formInstanceId, props.tableId, props.tableRowId);
577
+ this.context = new TableForm(
578
+ props.formInstanceId,
579
+ props.tableId,
580
+ props.tableRowId
581
+ );
450
582
  } else if (props.formInstanceId) {
451
583
  this.context = new Form(props.formInstanceId);
452
584
  } else if (props.popupId) {
@@ -456,8 +588,8 @@ class LowcodeSDK extends BaseSDK {
456
588
  } else if (props.componentId) {
457
589
  this.context = new Component(props);
458
590
  }
459
- this.client = new Client({});
460
- this.formatter = new Formatter({});
591
+ this.client = new Client();
592
+ this.formatter = new Formatter();
461
593
  if (props.appId) {
462
594
  this.app = new Application(props);
463
595
  }
@@ -467,11 +599,13 @@ class LowcodeSDK extends BaseSDK {
467
599
  __privateSet(this, _csrfToken, props.csrfToken);
468
600
  }
469
601
  async api(url, args) {
470
- const response = await globalThis.fetch(url, __spreadProps(__spreadValues({}, args), {
471
- headers: __spreadProps(__spreadValues({}, (args == null ? void 0 : args.headers) || {}), {
602
+ const response = await globalThis.fetch(url, {
603
+ ...args,
604
+ headers: {
605
+ ...(args == null ? void 0 : args.headers) || {},
472
606
  "X-Csrf-Token": __privateGet(this, _csrfToken)
473
- })
474
- }));
607
+ }
608
+ });
475
609
  const contentType = response.headers.get("content-type");
476
610
  if (contentType && contentType.includes("application/json")) {
477
611
  return await response.json();
@@ -484,4 +618,4 @@ _csrfToken = new WeakMap();
484
618
  function initSDK(config) {
485
619
  return new LowcodeSDK(config);
486
620
  }
487
- export { initSDK as default };
621
+ export { initSDK as default, window };