@tmagic/editor 1.8.2 → 1.8.4-beta.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 (110) hide show
  1. package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +23 -25
  2. package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +2 -2
  3. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +1 -1
  4. package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +1 -1
  5. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +1 -1
  6. package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +1 -1
  7. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +1 -1
  8. package/dist/es/fields/StyleSetter/configs.js +1 -1
  9. package/dist/es/fields/configs/codeSelect.js +5 -7
  10. package/dist/es/fields/configs/displayConds.js +8 -9
  11. package/dist/es/fields/configs/eventSelect.js +30 -27
  12. package/dist/es/headless.js +1 -2
  13. package/dist/es/hooks/use-stage.js +19 -18
  14. package/dist/es/index.js +9 -16
  15. package/dist/es/initService.js +33 -106
  16. package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +1 -1
  17. package/dist/es/layouts/NavMenuColumn.vue_vue_type_script_setup_true_lang.js +12 -2
  18. package/dist/es/layouts/page-bar/AddButton.vue_vue_type_script_setup_true_lang.js +4 -6
  19. package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +2 -2
  20. package/dist/es/layouts/props-panel/use-style-panel.js +1 -1
  21. package/dist/es/layouts/sidebar/ComponentListPanel.vue_vue_type_script_setup_true_lang.js +5 -1
  22. package/dist/es/layouts/workspace/viewer/Stage.vue_vue_type_script_setup_true_lang.js +3 -0
  23. package/dist/es/sdk/index.js +151 -0
  24. package/dist/es/{services → sdk/services}/BaseService.js +1 -1
  25. package/dist/es/{services/codeBlock.js → sdk/services/CodeBlock.js} +37 -40
  26. package/dist/es/{services/componentList.js → sdk/services/ComponentList.js} +5 -6
  27. package/dist/es/{services/dataSource.js → sdk/services/DataSource.js} +38 -37
  28. package/dist/es/{services/dep.js → sdk/services/Dep.js} +27 -17
  29. package/dist/es/{services/editor.js → sdk/services/Editor.js} +124 -87
  30. package/dist/es/{services/events.js → sdk/services/Events.js} +13 -13
  31. package/dist/es/{services/history.js → sdk/services/History.js} +86 -33
  32. package/dist/es/{services/props.js → sdk/services/Props.js} +25 -26
  33. package/dist/es/{services/storage.js → sdk/services/WebStorage.js} +31 -8
  34. package/dist/es/sdk/utils/dep.js +100 -0
  35. package/dist/es/sdk/utils/operator.js +70 -0
  36. package/dist/es/sdk/utils/position.js +8 -0
  37. package/dist/es/sdk/utils/props.js +281 -0
  38. package/dist/es/sdk/validate/index.js +286 -0
  39. package/dist/es/sdk/wiring.js +163 -0
  40. package/dist/es/sdk.js +15 -0
  41. package/dist/es/services/keybinding.js +21 -21
  42. package/dist/es/services/sdk-instance.js +37 -0
  43. package/dist/es/services/stageOverlay.js +14 -14
  44. package/dist/es/services/ui.js +6 -6
  45. package/dist/es/style.css +31 -6
  46. package/dist/es/utils/data-source/formConfigs/base.js +1 -1
  47. package/dist/es/utils/data-source/formConfigs/http.js +1 -1
  48. package/dist/es/utils/dep/worker.js +1 -1
  49. package/dist/es/utils/editor.js +2 -2
  50. package/dist/es/utils/event.js +25 -21
  51. package/dist/es/utils/operator.js +11 -57
  52. package/dist/es/utils/props.js +12 -282
  53. package/dist/es/utils/type-match-rules.js +10 -12
  54. package/dist/style.css +31 -6
  55. package/dist/themes/magic-admin.css +31 -6
  56. package/dist/tmagic-editor-headless.umd.cjs +4690 -2891
  57. package/dist/tmagic-editor-sdk.umd.cjs +9911 -0
  58. package/dist/tmagic-editor.umd.cjs +14155 -13319
  59. package/package.json +13 -8
  60. package/src/Editor.vue +14 -9
  61. package/src/editorProps.ts +23 -0
  62. package/src/fields/configs/codeSelect.ts +6 -9
  63. package/src/fields/configs/displayConds.ts +10 -9
  64. package/src/fields/configs/eventSelect.ts +40 -25
  65. package/src/hooks/use-stage.ts +2 -1
  66. package/src/index.ts +11 -9
  67. package/src/initService.ts +59 -188
  68. package/src/layouts/Framework.vue +1 -1
  69. package/src/layouts/NavMenuColumn.vue +16 -4
  70. package/src/layouts/page-bar/AddButton.vue +5 -6
  71. package/src/layouts/props-panel/PropsPanel.vue +1 -1
  72. package/src/layouts/props-panel/use-style-panel.ts +1 -1
  73. package/src/layouts/sidebar/ComponentListPanel.vue +7 -1
  74. package/src/layouts/workspace/viewer/Stage.vue +5 -0
  75. package/src/sdk/index.ts +264 -0
  76. package/src/{services/codeBlock.ts → sdk/services/CodeBlock.ts} +43 -38
  77. package/src/{services/componentList.ts → sdk/services/ComponentList.ts} +8 -7
  78. package/src/{services/dataSource.ts → sdk/services/DataSource.ts} +38 -32
  79. package/src/{services/dep.ts → sdk/services/Dep.ts} +29 -12
  80. package/src/{services/editor.ts → sdk/services/Editor.ts} +175 -86
  81. package/src/{services/events.ts → sdk/services/Events.ts} +15 -12
  82. package/src/{services/history.ts → sdk/services/History.ts} +100 -28
  83. package/src/{services/props.ts → sdk/services/Props.ts} +25 -25
  84. package/src/{services/storage.ts → sdk/services/WebStorage.ts} +41 -7
  85. package/src/sdk/type.ts +191 -0
  86. package/src/sdk/utils/dep.ts +196 -0
  87. package/src/sdk/utils/operator.ts +114 -0
  88. package/src/sdk/utils/position.ts +23 -0
  89. package/src/sdk/utils/props.ts +368 -0
  90. package/src/sdk/validate/index.ts +501 -0
  91. package/src/sdk/wiring.ts +211 -0
  92. package/src/sdk.ts +19 -0
  93. package/src/services/keybinding.ts +2 -2
  94. package/src/services/sdk-instance.ts +66 -0
  95. package/src/services/stageOverlay.ts +2 -2
  96. package/src/services/ui.ts +3 -3
  97. package/src/theme/nav-menu.scss +29 -2
  98. package/src/type.ts +33 -9
  99. package/src/utils/data-source/formConfigs/base.ts +1 -1
  100. package/src/utils/data-source/formConfigs/http.ts +1 -1
  101. package/src/utils/dep/collect-worker-client.ts +9 -6
  102. package/src/utils/dep/worker.ts +5 -0
  103. package/src/utils/editor.ts +2 -1
  104. package/src/utils/event.ts +36 -18
  105. package/src/utils/operator.ts +21 -87
  106. package/src/utils/props.ts +17 -332
  107. package/src/utils/type-match-rules.ts +1 -3
  108. package/types/index.d.ts +797 -540
  109. package/types/sdk.d.ts +2594 -0
  110. /package/src/{services → sdk/services}/BaseService.ts +0 -0
@@ -1,16 +1,12 @@
1
- import { CODE_DRAFT_STORAGE_KEY } from "../type.js";
2
- import { getEditorConfig } from "../utils/config.js";
1
+ import { CODE_DRAFT_STORAGE_KEY } from "../../type.js";
2
+ import { COPY_CODE_STORAGE_KEY } from "../../utils/editor.js";
3
+ import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../../utils/history.js";
3
4
  import BaseService from "./BaseService.js";
4
- import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../utils/history.js";
5
- import history_default from "./history.js";
6
- import storage_default, { Protocol } from "./storage.js";
7
- import { COPY_CODE_STORAGE_KEY } from "../utils/editor.js";
8
- import editor_default from "./editor.js";
5
+ import { Protocol } from "./WebStorage.js";
9
6
  import { getValueByKeyPath, setValueByKeyPath } from "@tmagic/utils";
10
7
  import { Target, Watcher } from "@tmagic/core";
11
- import { reactive } from "vue";
12
8
  import { cloneDeep as cloneDeep$1, get, keys, pick } from "lodash-es";
13
- //#region packages/editor/src/services/codeBlock.ts
9
+ //#region packages/editor/src/sdk/services/CodeBlock.ts
14
10
  var canUsePluginMethods = {
15
11
  async: [
16
12
  "setCodeDslById",
@@ -22,13 +18,8 @@ var canUsePluginMethods = {
22
18
  sync: ["setCodeDslByIdSync"]
23
19
  };
24
20
  var CodeBlock = class extends BaseService {
25
- state = reactive({
26
- codeDsl: null,
27
- editable: true,
28
- combineIds: [],
29
- undeletableList: [],
30
- paramsColConfig: void 0
31
- });
21
+ ctx;
22
+ state;
32
23
  /**
33
24
  * 最近一次写入历史栈的代码块历史记录 uuid(单条写入场景:新增 / 更新)。
34
25
  * 供 setCodeDslById(Sync)AndGetHistoryId 取回,普通方法不读取它。
@@ -39,7 +30,7 @@ var CodeBlock = class extends BaseService {
39
30
  * 在 deleteCodeDslByIds 入口处重置,供 deleteCodeDslByIdsAndGetHistoryId 取回。
40
31
  */
41
32
  lastDeletedHistoryIds = [];
42
- constructor() {
33
+ constructor(ctx) {
43
34
  super([...canUsePluginMethods.async.map((methodName) => ({
44
35
  name: methodName,
45
36
  isAsync: true
@@ -47,6 +38,14 @@ var CodeBlock = class extends BaseService {
47
38
  name: methodName,
48
39
  isAsync: false
49
40
  }))]);
41
+ this.ctx = ctx;
42
+ this.state = ctx.reactivity.reactive({
43
+ codeDsl: null,
44
+ editable: true,
45
+ combineIds: [],
46
+ undeletableList: [],
47
+ paramsColConfig: void 0
48
+ });
50
49
  }
51
50
  /**
52
51
  * 设置活动的代码块dsl数据源
@@ -113,8 +112,7 @@ var CodeBlock = class extends BaseService {
113
112
  if (codeDsl[id] && !force) return;
114
113
  const codeConfigProcessed = cloneDeep$1(codeConfig);
115
114
  if (codeConfigProcessed.content) {
116
- const parseDSL = getEditorConfig("parseDSL");
117
- if (typeof codeConfigProcessed.content === "string") codeConfigProcessed.content = parseDSL(codeConfigProcessed.content);
115
+ if (typeof codeConfigProcessed.content === "string" && this.ctx.options.parseDSL) codeConfigProcessed.content = this.ctx.parseDSL(codeConfigProcessed.content);
118
116
  }
119
117
  const oldContent = codeDsl[id] ? cloneDeep$1(codeDsl[id]) : null;
120
118
  codeDsl[id] = {
@@ -130,7 +128,7 @@ var CodeBlock = class extends BaseService {
130
128
  historyDescription,
131
129
  source: historySource
132
130
  });
133
- this.lastPushedHistoryId = (step ? history_default.push("codeBlock", step, id) : null)?.uuid ?? null;
131
+ this.lastPushedHistoryId = (step ? this.ctx.history.push("codeBlock", step, id) : null)?.uuid ?? null;
134
132
  }
135
133
  this.emit("addOrUpdate", id, codeDsl[id]);
136
134
  this.emit("change", this.getCodeDsl());
@@ -193,19 +191,19 @@ var CodeBlock = class extends BaseService {
193
191
  * 设置代码草稿
194
192
  */
195
193
  setCodeDraft(codeId, content) {
196
- globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
194
+ this.ctx.storage.getStorage().setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
197
195
  }
198
196
  /**
199
197
  * 获取代码草稿
200
198
  */
201
199
  getCodeDraft(codeId) {
202
- return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
200
+ return this.ctx.storage.getStorage().getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
203
201
  }
204
202
  /**
205
203
  * 删除代码草稿
206
204
  */
207
205
  removeCodeDraft(codeId) {
208
- globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
206
+ this.ctx.storage.getStorage().removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
209
207
  }
210
208
  /**
211
209
  * 在dsl数据源中删除指定id的代码块
@@ -227,7 +225,7 @@ var CodeBlock = class extends BaseService {
227
225
  historyDescription,
228
226
  source: historySource
229
227
  });
230
- const uuid = step ? history_default.push("codeBlock", step, id)?.uuid : void 0;
228
+ const uuid = step ? this.ctx.history.push("codeBlock", step, id)?.uuid : void 0;
231
229
  if (uuid) this.lastDeletedHistoryIds.push(uuid);
232
230
  }
233
231
  this.emit("remove", id);
@@ -289,7 +287,7 @@ var CodeBlock = class extends BaseService {
289
287
  * @returns 撤销的 step;栈不存在或已无可撤销时返回 null
290
288
  */
291
289
  async undo(id) {
292
- const step = history_default.undo("codeBlock", id);
290
+ const step = this.ctx.history.undo("codeBlock", id);
293
291
  if (!step) return null;
294
292
  await this.applyHistoryStep(step, true);
295
293
  return step;
@@ -300,18 +298,18 @@ var CodeBlock = class extends BaseService {
300
298
  * @returns 重做的 step;栈不存在或已无可重做时返回 null
301
299
  */
302
300
  async redo(id) {
303
- const step = history_default.redo("codeBlock", id);
301
+ const step = this.ctx.history.redo("codeBlock", id);
304
302
  if (!step) return null;
305
303
  await this.applyHistoryStep(step, false);
306
304
  return step;
307
305
  }
308
306
  /** 是否可对指定代码块撤销。 */
309
307
  canUndo(id) {
310
- return history_default.canUndo("codeBlock", id);
308
+ return this.ctx.history.canUndo("codeBlock", id);
311
309
  }
312
310
  /** 是否可对指定代码块重做。 */
313
311
  canRedo(id) {
314
- return history_default.canRedo("codeBlock", id);
312
+ return this.ctx.history.canRedo("codeBlock", id);
315
313
  }
316
314
  /**
317
315
  * 跳转指定代码块的历史栈到目标游标。语义同 editor.gotoPageStep。
@@ -321,7 +319,7 @@ var CodeBlock = class extends BaseService {
321
319
  * @returns 实际移动到的最终游标位置
322
320
  */
323
321
  async goto(id, targetCursor) {
324
- let cursor = history_default.getCursor("codeBlock", id);
322
+ let cursor = this.ctx.history.getCursor("codeBlock", id);
325
323
  const target = Math.max(0, targetCursor);
326
324
  while (cursor > target) {
327
325
  if (!await this.undo(id)) break;
@@ -344,7 +342,7 @@ var CodeBlock = class extends BaseService {
344
342
  * @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
345
343
  */
346
344
  async revert(id, index) {
347
- const entry = history_default.getStepList("codeBlock", id)[index];
345
+ const entry = this.ctx.history.getStepList("codeBlock", id)[index];
348
346
  if (!entry?.applied) return null;
349
347
  const { oldSchema, newSchema, changeRecords } = entry.step.diff?.[0] ?? {};
350
348
  if (oldSchema && newSchema && !changeRecords?.length) return null;
@@ -361,7 +359,7 @@ var CodeBlock = class extends BaseService {
361
359
  async revertById(uuids) {
362
360
  const results = [];
363
361
  for (const uuid of uuids) {
364
- const location = history_default.findStepLocationByUuid("codeBlock", uuid);
362
+ const location = this.ctx.history.findStepLocationByUuid("codeBlock", uuid);
365
363
  results.push(location ? await this.revert(location.id, location.index) : null);
366
364
  }
367
365
  return results;
@@ -390,7 +388,7 @@ var CodeBlock = class extends BaseService {
390
388
  coperWatcher.addTarget(customTarget);
391
389
  coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
392
390
  Object.keys(customTarget.deps).forEach((nodeId) => {
393
- const node = editor_default.getNodeById(nodeId);
391
+ const node = this.ctx.editor.getNodeById(nodeId);
394
392
  if (!node) return;
395
393
  customTarget.deps[nodeId].keys.forEach((key) => {
396
394
  const relateCodeId = get(node, key);
@@ -401,14 +399,14 @@ var CodeBlock = class extends BaseService {
401
399
  });
402
400
  });
403
401
  }
404
- storage_default.setItem(COPY_CODE_STORAGE_KEY, copyData, { protocol: Protocol.OBJECT });
402
+ this.ctx.storage.setItem(COPY_CODE_STORAGE_KEY, copyData, { protocol: Protocol.OBJECT });
405
403
  }
406
404
  /**
407
405
  * 粘贴代码块
408
406
  * @returns
409
407
  */
410
408
  paste() {
411
- const codeDsl = storage_default.getItem(COPY_CODE_STORAGE_KEY);
409
+ const codeDsl = this.ctx.storage.getItem(COPY_CODE_STORAGE_KEY);
412
410
  Object.keys(codeDsl).forEach((codeId) => {
413
411
  this.setCodeDslByIdSync(codeId, codeDsl[codeId], false);
414
412
  });
@@ -439,14 +437,14 @@ var CodeBlock = class extends BaseService {
439
437
  historyDescription,
440
438
  historySource: "rollback"
441
439
  });
442
- return history_default.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
440
+ return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
443
441
  }
444
442
  if (oldSchema && !newSchema) {
445
443
  this.setCodeDslByIdSync(id, cloneDeep$1(oldSchema), true, {
446
444
  historyDescription,
447
445
  historySource: "rollback"
448
446
  });
449
- return history_default.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
447
+ return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
450
448
  }
451
449
  if (!oldSchema || !newSchema) return null;
452
450
  if (changeRecords?.length) {
@@ -467,13 +465,13 @@ var CodeBlock = class extends BaseService {
467
465
  historyDescription,
468
466
  historySource: "rollback"
469
467
  });
470
- return history_default.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
468
+ return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
471
469
  }
472
470
  this.setCodeDslByIdSync(id, cloneDeep$1(oldSchema), true, {
473
471
  historyDescription,
474
472
  historySource: "rollback"
475
473
  });
476
- return history_default.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
474
+ return this.ctx.history.getStepList("codeBlock", id).slice(-1)[0]?.step ?? null;
477
475
  }
478
476
  /**
479
477
  * 把一条历史 step 应用到当前代码块服务上。
@@ -526,6 +524,5 @@ var CodeBlock = class extends BaseService {
526
524
  this.setCodeDslByIdSync(id, cloneDeep$1(sourceForValues), true, { doNotPushHistory: true });
527
525
  }
528
526
  };
529
- var codeBlock_default = new CodeBlock();
530
527
  //#endregion
531
- export { codeBlock_default as default };
528
+ export { CodeBlock as default };
@@ -1,10 +1,10 @@
1
1
  import BaseService from "./BaseService.js";
2
- import { shallowReactive } from "vue";
3
- //#region packages/editor/src/services/componentList.ts
2
+ //#region packages/editor/src/sdk/services/ComponentList.ts
4
3
  var ComponentList = class extends BaseService {
5
- state = shallowReactive({ list: [] });
6
- constructor() {
4
+ state;
5
+ constructor(ctx) {
7
6
  super([]);
7
+ this.state = ctx.reactivity.shallowReactive({ list: [] });
8
8
  }
9
9
  /**
10
10
  * @param componentGroupList 组件列表配置
@@ -24,6 +24,5 @@ var ComponentList = class extends BaseService {
24
24
  this.removeAllPlugins();
25
25
  }
26
26
  };
27
- var componentList_default = new ComponentList();
28
27
  //#endregion
29
- export { componentList_default as default };
28
+ export { ComponentList as default };
@@ -1,15 +1,12 @@
1
+ import { COPY_DS_STORAGE_KEY } from "../../utils/editor.js";
2
+ import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../../utils/history.js";
1
3
  import BaseService from "./BaseService.js";
2
- import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from "../utils/history.js";
3
- import history_default from "./history.js";
4
- import storage_default, { Protocol } from "./storage.js";
5
- import { COPY_DS_STORAGE_KEY } from "../utils/editor.js";
6
- import editor_default from "./editor.js";
7
- import { getFormConfig, getFormValue } from "../utils/data-source/index.js";
4
+ import { Protocol } from "./WebStorage.js";
5
+ import { getFormConfig, getFormValue } from "../../utils/data-source/index.js";
8
6
  import { getValueByKeyPath, guid, setValueByKeyPath, toLine } from "@tmagic/utils";
9
7
  import { Target, Watcher } from "@tmagic/core";
10
- import { reactive } from "vue";
11
8
  import { cloneDeep as cloneDeep$1, get } from "lodash-es";
12
- //#region packages/editor/src/services/dataSource.ts
9
+ //#region packages/editor/src/sdk/services/DataSource.ts
13
10
  var canUsePluginMethods = {
14
11
  async: [],
15
12
  sync: [
@@ -28,26 +25,29 @@ var canUsePluginMethods = {
28
25
  ]
29
26
  };
30
27
  var DataSource = class extends BaseService {
31
- state = reactive({
32
- datasourceTypeList: [],
33
- dataSources: [],
34
- editable: true,
35
- configs: {},
36
- values: {},
37
- events: {},
38
- methods: {}
39
- });
28
+ ctx;
29
+ state;
40
30
  /**
41
31
  * 最近一次写入历史栈的数据源历史记录 uuid。
42
32
  * 供 *AndGetHistoryId 系列方法在调用 add / update / remove 后取回本次产生的历史记录 id;
43
33
  * 普通方法不读取它,调用前由 *AndGetHistoryId 重置为 null。
44
34
  */
45
35
  lastPushedHistoryId = null;
46
- constructor() {
36
+ constructor(ctx) {
47
37
  super(canUsePluginMethods.sync.map((methodName) => ({
48
38
  name: methodName,
49
39
  isAsync: false
50
40
  })));
41
+ this.ctx = ctx;
42
+ this.state = ctx.reactivity.reactive({
43
+ datasourceTypeList: [],
44
+ dataSources: [],
45
+ editable: true,
46
+ configs: {},
47
+ values: {},
48
+ events: {},
49
+ methods: {}
50
+ });
51
51
  }
52
52
  set(name, value) {
53
53
  this.state[name] = value;
@@ -87,6 +87,7 @@ var DataSource = class extends BaseService {
87
87
  * @param options.historyDescription 入栈时附带的人类可读描述,用于历史面板展示
88
88
  */
89
89
  add(config, { doNotPushHistory = false, historyDescription, historySource } = {}) {
90
+ this.ctx.validator.guardDataSource(config, true);
90
91
  const newConfig = {
91
92
  ...config,
92
93
  id: config.id && !this.getDataSourceById(config.id) ? config.id : this.createId()
@@ -99,7 +100,7 @@ var DataSource = class extends BaseService {
99
100
  historyDescription,
100
101
  source: historySource
101
102
  });
102
- this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
103
+ this.lastPushedHistoryId = (step ? this.ctx.history.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
103
104
  }
104
105
  this.emit("add", newConfig);
105
106
  this.emit("change", this.get("dataSources"));
@@ -114,6 +115,7 @@ var DataSource = class extends BaseService {
114
115
  * @param data.historyDescription 入栈时附带的人类可读描述,用于历史面板展示
115
116
  */
116
117
  update(config, { changeRecords = [], doNotPushHistory = false, historyDescription, historySource } = {}) {
118
+ this.ctx.validator.guardDataSource(config, false);
117
119
  const dataSources = this.get("dataSources");
118
120
  const index = dataSources.findIndex((ds) => ds.id === config.id);
119
121
  const oldConfig = dataSources[index];
@@ -127,7 +129,7 @@ var DataSource = class extends BaseService {
127
129
  historyDescription,
128
130
  source: historySource
129
131
  });
130
- this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
132
+ this.lastPushedHistoryId = (step ? this.ctx.history.push("dataSource", step, newConfig.id) : null)?.uuid ?? null;
131
133
  }
132
134
  this.emit("update", newConfig, {
133
135
  oldConfig,
@@ -155,7 +157,7 @@ var DataSource = class extends BaseService {
155
157
  historyDescription,
156
158
  source: historySource
157
159
  });
158
- this.lastPushedHistoryId = (step ? history_default.push("dataSource", step, id) : null)?.uuid ?? null;
160
+ this.lastPushedHistoryId = (step ? this.ctx.history.push("dataSource", step, id) : null)?.uuid ?? null;
159
161
  }
160
162
  this.emit("remove", id);
161
163
  this.emit("change", this.get("dataSources"));
@@ -203,7 +205,7 @@ var DataSource = class extends BaseService {
203
205
  * @returns 撤销的 step;栈不存在或已无可撤销时返回 null
204
206
  */
205
207
  undo(id) {
206
- const step = history_default.undo("dataSource", id);
208
+ const step = this.ctx.history.undo("dataSource", id);
207
209
  if (!step) return null;
208
210
  this.applyHistoryStep(step, true);
209
211
  return step;
@@ -214,18 +216,18 @@ var DataSource = class extends BaseService {
214
216
  * @returns 重做的 step;栈不存在或已无可重做时返回 null
215
217
  */
216
218
  redo(id) {
217
- const step = history_default.redo("dataSource", id);
219
+ const step = this.ctx.history.redo("dataSource", id);
218
220
  if (!step) return null;
219
221
  this.applyHistoryStep(step, false);
220
222
  return step;
221
223
  }
222
224
  /** 是否可对指定数据源撤销。 */
223
225
  canUndo(id) {
224
- return history_default.canUndo("dataSource", id);
226
+ return this.ctx.history.canUndo("dataSource", id);
225
227
  }
226
228
  /** 是否可对指定数据源重做。 */
227
229
  canRedo(id) {
228
- return history_default.canRedo("dataSource", id);
230
+ return this.ctx.history.canRedo("dataSource", id);
229
231
  }
230
232
  /**
231
233
  * 跳转指定数据源的历史栈到目标游标。语义同 editor.gotoPageStep。
@@ -235,7 +237,7 @@ var DataSource = class extends BaseService {
235
237
  * @returns 实际移动到的最终游标位置
236
238
  */
237
239
  goto(id, targetCursor) {
238
- let cursor = history_default.getCursor("dataSource", id);
240
+ let cursor = this.ctx.history.getCursor("dataSource", id);
239
241
  const target = Math.max(0, targetCursor);
240
242
  while (cursor > target) {
241
243
  if (!this.undo(id)) break;
@@ -258,7 +260,7 @@ var DataSource = class extends BaseService {
258
260
  * @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
259
261
  */
260
262
  revert(id, index) {
261
- const entry = history_default.getStepList("dataSource", id)[index];
263
+ const entry = this.ctx.history.getStepList("dataSource", id)[index];
262
264
  if (!entry?.applied) return null;
263
265
  const { oldSchema, newSchema, changeRecords } = entry.step.diff?.[0] ?? {};
264
266
  if (oldSchema && newSchema && !changeRecords?.length) return null;
@@ -274,7 +276,7 @@ var DataSource = class extends BaseService {
274
276
  */
275
277
  revertById(uuids) {
276
278
  return uuids.map((uuid) => {
277
- const location = history_default.findStepLocationByUuid("dataSource", uuid);
279
+ const location = this.ctx.history.findStepLocationByUuid("dataSource", uuid);
278
280
  if (!location) return null;
279
281
  return this.revert(location.id, location.index);
280
282
  });
@@ -310,7 +312,7 @@ var DataSource = class extends BaseService {
310
312
  coperWatcher.addTarget(customTarget);
311
313
  coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
312
314
  Object.keys(customTarget.deps).forEach((nodeId) => {
313
- const node = editor_default.getNodeById(nodeId);
315
+ const node = this.ctx.editor.getNodeById(nodeId);
314
316
  if (!node) return;
315
317
  customTarget.deps[nodeId].keys.forEach((key) => {
316
318
  const [relateDsId] = get(node, key);
@@ -321,14 +323,14 @@ var DataSource = class extends BaseService {
321
323
  });
322
324
  });
323
325
  }
324
- storage_default.setItem(COPY_DS_STORAGE_KEY, copyData, { protocol: Protocol.OBJECT });
326
+ this.ctx.storage.setItem(COPY_DS_STORAGE_KEY, copyData, { protocol: Protocol.OBJECT });
325
327
  }
326
328
  /**
327
329
  * 粘贴数据源
328
330
  * @returns
329
331
  */
330
332
  paste() {
331
- storage_default.getItem(COPY_DS_STORAGE_KEY).forEach((item) => {
333
+ this.ctx.storage.getItem(COPY_DS_STORAGE_KEY).forEach((item) => {
332
334
  if (!this.getDataSourceById(item.id)) this.add(item);
333
335
  });
334
336
  }
@@ -344,14 +346,14 @@ var DataSource = class extends BaseService {
344
346
  historyDescription,
345
347
  historySource: "rollback"
346
348
  });
347
- return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
349
+ return this.ctx.history.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
348
350
  }
349
351
  if (oldSchema && !newSchema) {
350
352
  this.add(cloneDeep$1(oldSchema), {
351
353
  historyDescription,
352
354
  historySource: "rollback"
353
355
  });
354
- return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
356
+ return this.ctx.history.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
355
357
  }
356
358
  if (!oldSchema || !newSchema) return null;
357
359
  if (changeRecords?.length) {
@@ -372,13 +374,13 @@ var DataSource = class extends BaseService {
372
374
  historyDescription,
373
375
  historySource: "rollback"
374
376
  });
375
- return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
377
+ return this.ctx.history.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
376
378
  }
377
379
  this.update(cloneDeep$1(oldSchema), {
378
380
  historyDescription,
379
381
  historySource: "rollback"
380
382
  });
381
- return history_default.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
383
+ return this.ctx.history.getStepList("dataSource", id).slice(-1)[0]?.step ?? null;
382
384
  }
383
385
  /**
384
386
  * 把一条历史 step 应用到当前数据源服务上。
@@ -431,6 +433,5 @@ var DataSource = class extends BaseService {
431
433
  this.update(cloneDeep$1(sourceForValues), { doNotPushHistory: true });
432
434
  }
433
435
  };
434
- var dataSource_default = new DataSource();
435
436
  //#endregion
436
- export { dataSource_default as default };
437
+ export { DataSource as default };
@@ -1,25 +1,36 @@
1
1
  import BaseService from "./BaseService.js";
2
- import { IdleTask } from "../utils/dep/idle-task.js";
3
- import { CollectWorkerClient } from "../utils/dep/collect-worker-client.js";
2
+ import { IdleTask } from "../../utils/dep/idle-task.js";
4
3
  import { isPage } from "@tmagic/utils";
5
4
  import { DepTargetType, Watcher, traverseTarget } from "@tmagic/core";
6
- import { reactive, shallowReactive } from "vue";
7
5
  import { throttle } from "lodash-es";
8
- //#region packages/editor/src/services/dep.ts
6
+ //#region packages/editor/src/sdk/services/Dep.ts
7
+ /** Node 下没有 worker 时使用:始终不可用,依赖收集退回主线程空闲队列 */
8
+ var noopCollectWorker = {
9
+ isSupported: false,
10
+ collectDsl: () => Promise.resolve(null),
11
+ collect: () => Promise.resolve(null),
12
+ terminate: () => {},
13
+ abort: () => {}
14
+ };
9
15
  var Dep = class extends BaseService {
10
- state = shallowReactive({
11
- collecting: false,
12
- taskLength: 0
13
- });
16
+ ctx;
17
+ state;
14
18
  idleTask = new IdleTask();
15
- collectWorker = new CollectWorkerClient();
16
- watcher = new Watcher({ initialTargets: reactive({}) });
19
+ collectWorker;
20
+ watcher;
17
21
  waitingWorker;
18
22
  resolveWaitingWorker;
19
23
  workerGeneration = 0;
20
24
  activeBatches = /* @__PURE__ */ new Set();
21
- constructor() {
25
+ constructor(ctx) {
22
26
  super();
27
+ this.ctx = ctx;
28
+ this.state = ctx.reactivity.shallowReactive({
29
+ collecting: false,
30
+ taskLength: 0
31
+ });
32
+ this.collectWorker = ctx.options.collectWorker ?? noopCollectWorker;
33
+ this.watcher = new Watcher({ initialTargets: ctx.reactivity.reactive({}) });
23
34
  this.idleTask.on("update-task-length", throttle(({ length }) => {
24
35
  this.set("taskLength", length);
25
36
  }, 1e3));
@@ -56,10 +67,10 @@ var Dep = class extends BaseService {
56
67
  collect(nodes, depExtendedData = {}, deep = false, type) {
57
68
  this.set("collecting", true);
58
69
  const targets = this.watcher.getCollectableTargets(type);
59
- if (targets.length) for (const node of nodes) {
60
- if (isPage(node)) for (const target of targets) this.removePageDep(target, depExtendedData);
70
+ if (targets.length) {
71
+ for (const node of nodes) if (isPage(node)) for (const target of targets) this.removePageDep(target, depExtendedData);
61
72
  else this.watcher.removeTargetsDep(targets, node);
62
- this.watcher.collectItems(node, targets, depExtendedData, deep);
73
+ for (const node of nodes) this.watcher.collectItems(node, targets, depExtendedData, deep);
63
74
  }
64
75
  this.set("collecting", false);
65
76
  this.emit("collected", nodes, deep);
@@ -115,7 +126,7 @@ var Dep = class extends BaseService {
115
126
  }
116
127
  traverseTarget(this.watcher.getTargetsList(), (target) => {
117
128
  if (depsData[target.type]?.[target.id]) {
118
- target.deps = reactive(depsData[target.type][target.id]);
129
+ target.deps = this.ctx.reactivity.reactive(depsData[target.type][target.id]);
119
130
  if (target.type === DepTargetType.DATA_SOURCE && dsl.dataSourceDeps) dsl.dataSourceDeps[target.id] = target.deps;
120
131
  else if (target.type === DepTargetType.DATA_SOURCE_COND && dsl.dataSourceCondDeps) dsl.dataSourceCondDeps[target.id] = target.deps;
121
132
  else if (target.type === DepTargetType.DATA_SOURCE_METHOD && dsl.dataSourceMethodDeps) dsl.dataSourceMethodDeps[target.id] = target.deps;
@@ -301,6 +312,5 @@ var Dep = class extends BaseService {
301
312
  this.set("collecting", this.activeBatches.size > 0);
302
313
  }
303
314
  };
304
- var dep_default = new Dep();
305
315
  //#endregion
306
- export { dep_default as default };
316
+ export { Dep as default };