@tmagic/editor 1.8.1 → 1.8.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.
Files changed (99) hide show
  1. package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +17 -25
  2. package/dist/es/editorProps.js +3 -0
  3. package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +2 -2
  4. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +1 -1
  5. package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +1 -1
  6. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +1 -1
  7. package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +1 -1
  8. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +1 -1
  9. package/dist/es/fields/StyleSetter/configs.js +1 -1
  10. package/dist/es/fields/configs/codeSelect.js +5 -7
  11. package/dist/es/fields/configs/displayConds.js +8 -9
  12. package/dist/es/fields/configs/eventSelect.js +30 -27
  13. package/dist/es/hooks/use-stage.js +18 -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/props-panel/FormPanel.vue_vue_type_script_setup_true_lang.js +8 -6
  18. package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +4 -4
  19. package/dist/es/layouts/props-panel/use-style-panel.js +1 -1
  20. package/dist/es/sdk/index.js +151 -0
  21. package/dist/es/{services → sdk/services}/BaseService.js +1 -1
  22. package/dist/es/{services/codeBlock.js → sdk/services/CodeBlock.js} +37 -40
  23. package/dist/es/{services/componentList.js → sdk/services/ComponentList.js} +5 -6
  24. package/dist/es/{services/dataSource.js → sdk/services/DataSource.js} +38 -37
  25. package/dist/es/{services/dep.js → sdk/services/Dep.js} +24 -14
  26. package/dist/es/{services/editor.js → sdk/services/Editor.js} +124 -87
  27. package/dist/es/{services/events.js → sdk/services/Events.js} +13 -13
  28. package/dist/es/{services/history.js → sdk/services/History.js} +86 -33
  29. package/dist/es/{services/props.js → sdk/services/Props.js} +25 -26
  30. package/dist/es/{services/storage.js → sdk/services/WebStorage.js} +31 -8
  31. package/dist/es/sdk/utils/dep.js +100 -0
  32. package/dist/es/sdk/utils/operator.js +70 -0
  33. package/dist/es/sdk/utils/position.js +8 -0
  34. package/dist/es/sdk/utils/props.js +281 -0
  35. package/dist/es/sdk/validate/index.js +286 -0
  36. package/dist/es/sdk/wiring.js +163 -0
  37. package/dist/es/sdk.js +15 -0
  38. package/dist/es/services/keybinding.js +21 -21
  39. package/dist/es/services/sdk-instance.js +37 -0
  40. package/dist/es/services/stageOverlay.js +14 -14
  41. package/dist/es/services/ui.js +6 -6
  42. package/dist/es/utils/data-source/formConfigs/base.js +1 -1
  43. package/dist/es/utils/data-source/formConfigs/http.js +1 -1
  44. package/dist/es/utils/editor.js +2 -2
  45. package/dist/es/utils/event.js +25 -21
  46. package/dist/es/utils/operator.js +11 -57
  47. package/dist/es/utils/props.js +12 -282
  48. package/dist/es/utils/type-match-rules.js +10 -12
  49. package/dist/tmagic-editor-headless.umd.cjs +4690 -2891
  50. package/dist/tmagic-editor-sdk.umd.cjs +9911 -0
  51. package/dist/tmagic-editor.umd.cjs +14200 -13381
  52. package/package.json +12 -7
  53. package/src/Editor.vue +17 -10
  54. package/src/editorProps.ts +5 -2
  55. package/src/fields/configs/codeSelect.ts +6 -9
  56. package/src/fields/configs/displayConds.ts +10 -9
  57. package/src/fields/configs/eventSelect.ts +40 -25
  58. package/src/hooks/use-stage.ts +1 -1
  59. package/src/index.ts +11 -9
  60. package/src/initService.ts +59 -188
  61. package/src/layouts/Framework.vue +1 -1
  62. package/src/layouts/props-panel/FormPanel.vue +8 -5
  63. package/src/layouts/props-panel/PropsPanel.vue +8 -3
  64. package/src/layouts/props-panel/use-style-panel.ts +1 -1
  65. package/src/sdk/index.ts +264 -0
  66. package/src/{services/codeBlock.ts → sdk/services/CodeBlock.ts} +43 -38
  67. package/src/{services/componentList.ts → sdk/services/ComponentList.ts} +8 -7
  68. package/src/{services/dataSource.ts → sdk/services/DataSource.ts} +38 -32
  69. package/src/{services/dep.ts → sdk/services/Dep.ts} +24 -11
  70. package/src/{services/editor.ts → sdk/services/Editor.ts} +175 -86
  71. package/src/{services/events.ts → sdk/services/Events.ts} +15 -12
  72. package/src/{services/history.ts → sdk/services/History.ts} +100 -28
  73. package/src/{services/props.ts → sdk/services/Props.ts} +25 -25
  74. package/src/{services/storage.ts → sdk/services/WebStorage.ts} +41 -7
  75. package/src/sdk/type.ts +191 -0
  76. package/src/sdk/utils/dep.ts +196 -0
  77. package/src/sdk/utils/operator.ts +114 -0
  78. package/src/sdk/utils/position.ts +23 -0
  79. package/src/sdk/utils/props.ts +368 -0
  80. package/src/sdk/validate/index.ts +501 -0
  81. package/src/sdk/wiring.ts +211 -0
  82. package/src/sdk.ts +19 -0
  83. package/src/services/keybinding.ts +2 -2
  84. package/src/services/sdk-instance.ts +66 -0
  85. package/src/services/stageOverlay.ts +2 -2
  86. package/src/services/ui.ts +3 -3
  87. package/src/type.ts +9 -9
  88. package/src/utils/data-source/formConfigs/base.ts +1 -1
  89. package/src/utils/data-source/formConfigs/http.ts +1 -1
  90. package/src/utils/dep/collect-worker-client.ts +9 -6
  91. package/src/utils/dep/worker.ts +5 -0
  92. package/src/utils/editor.ts +2 -1
  93. package/src/utils/event.ts +36 -18
  94. package/src/utils/operator.ts +21 -87
  95. package/src/utils/props.ts +17 -332
  96. package/src/utils/type-match-rules.ts +1 -3
  97. package/types/index.d.ts +742 -528
  98. package/types/sdk.d.ts +2572 -0
  99. /package/src/{services → sdk/services}/BaseService.ts +0 -0
@@ -16,7 +16,6 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { reactive } from 'vue';
20
19
  import { cloneDeep, get, keys, pick } from 'lodash-es';
21
20
  import type { Writable } from 'type-fest';
22
21
 
@@ -25,9 +24,6 @@ import { Target, Watcher } from '@tmagic/core';
25
24
  import type { TableColumnConfig } from '@tmagic/form';
26
25
  import { getValueByKeyPath, setValueByKeyPath } from '@tmagic/utils';
27
26
 
28
- import editorService from '@editor/services/editor';
29
- import historyService from '@editor/services/history';
30
- import storageService, { Protocol } from '@editor/services/storage';
31
27
  import type {
32
28
  AsyncHookPlugin,
33
29
  CodeBlockStepValue,
@@ -37,11 +33,13 @@ import type {
37
33
  HistoryOpOptionsWithChangeRecords,
38
34
  } from '@editor/type';
39
35
  import { CODE_DRAFT_STORAGE_KEY } from '@editor/type';
40
- import { getEditorConfig } from '@editor/utils/config';
41
36
  import { COPY_CODE_STORAGE_KEY } from '@editor/utils/editor';
42
37
  import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from '@editor/utils/history';
43
38
 
39
+ import type { SdkContext } from '../type';
40
+
44
41
  import BaseService from './BaseService';
42
+ import { Protocol } from './WebStorage';
45
43
 
46
44
  const canUsePluginMethods = {
47
45
  async: ['setCodeDslById', 'setEditStatus', 'setCombineIds', 'setUndeleteableList', 'deleteCodeDslByIds'] as const,
@@ -51,13 +49,7 @@ const canUsePluginMethods = {
51
49
  type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
52
50
 
53
51
  class CodeBlock extends BaseService {
54
- private state = reactive<CodeState>({
55
- codeDsl: null,
56
- editable: true,
57
- combineIds: [],
58
- undeletableList: [],
59
- paramsColConfig: undefined,
60
- });
52
+ private state: CodeState;
61
53
 
62
54
  /**
63
55
  * 最近一次写入历史栈的代码块历史记录 uuid(单条写入场景:新增 / 更新)。
@@ -70,11 +62,18 @@ class CodeBlock extends BaseService {
70
62
  */
71
63
  private lastDeletedHistoryIds: string[] = [];
72
64
 
73
- constructor() {
65
+ constructor(private ctx: SdkContext) {
74
66
  super([
75
67
  ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
76
68
  ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
77
69
  ]);
70
+ this.state = ctx.reactivity.reactive<CodeState>({
71
+ codeDsl: null,
72
+ editable: true,
73
+ combineIds: [],
74
+ undeletableList: [],
75
+ paramsColConfig: undefined,
76
+ });
78
77
  }
79
78
 
80
79
  /**
@@ -170,9 +169,9 @@ class CodeBlock extends BaseService {
170
169
  const codeConfigProcessed = cloneDeep(codeConfig);
171
170
  if (codeConfigProcessed.content) {
172
171
  // 在保存的时候转换代码内容
173
- const parseDSL = getEditorConfig('parseDSL');
174
- if (typeof codeConfigProcessed.content === 'string') {
175
- codeConfigProcessed.content = parseDSL<(...args: any[]) => any>(codeConfigProcessed.content);
172
+ // 没有注入 parseDSL(如 Node 默认)时保持源码字符串,不会执行代码,导出的 DSL 里仍是字符串
173
+ if (typeof codeConfigProcessed.content === 'string' && this.ctx.options.parseDSL) {
174
+ codeConfigProcessed.content = this.ctx.parseDSL(codeConfigProcessed.content) as (...args: any[]) => any;
176
175
  }
177
176
  }
178
177
 
@@ -196,7 +195,7 @@ class CodeBlock extends BaseService {
196
195
  historyDescription,
197
196
  source: historySource,
198
197
  });
199
- this.lastPushedHistoryId = (step ? historyService.push('codeBlock', step, id) : null)?.uuid ?? null;
198
+ this.lastPushedHistoryId = (step ? this.ctx.history.push('codeBlock', step, id) : null)?.uuid ?? null;
200
199
  }
201
200
 
202
201
  this.emit('addOrUpdate', id, codeDsl[id]);
@@ -268,21 +267,21 @@ class CodeBlock extends BaseService {
268
267
  * 设置代码草稿
269
268
  */
270
269
  public setCodeDraft(codeId: Id, content: string): void {
271
- globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
270
+ this.ctx.storage.getStorage().setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
272
271
  }
273
272
 
274
273
  /**
275
274
  * 获取代码草稿
276
275
  */
277
276
  public getCodeDraft(codeId: Id): string | null {
278
- return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
277
+ return this.ctx.storage.getStorage().getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
279
278
  }
280
279
 
281
280
  /**
282
281
  * 删除代码草稿
283
282
  */
284
283
  public removeCodeDraft(codeId: Id): void {
285
- globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
284
+ this.ctx.storage.getStorage().removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
286
285
  }
287
286
 
288
287
  /**
@@ -314,7 +313,7 @@ class CodeBlock extends BaseService {
314
313
  historyDescription,
315
314
  source: historySource,
316
315
  });
317
- const uuid = step ? historyService.push('codeBlock', step, id)?.uuid : undefined;
316
+ const uuid = step ? this.ctx.history.push('codeBlock', step, id)?.uuid : undefined;
318
317
  if (uuid) this.lastDeletedHistoryIds.push(uuid);
319
318
  }
320
319
 
@@ -390,7 +389,7 @@ class CodeBlock extends BaseService {
390
389
  * @returns 撤销的 step;栈不存在或已无可撤销时返回 null
391
390
  */
392
391
  public async undo(id: Id): Promise<CodeBlockStepValue | null> {
393
- const step = historyService.undo('codeBlock', id);
392
+ const step = this.ctx.history.undo('codeBlock', id);
394
393
  if (!step) return null;
395
394
  await this.applyHistoryStep(step, true);
396
395
  return step;
@@ -402,7 +401,7 @@ class CodeBlock extends BaseService {
402
401
  * @returns 重做的 step;栈不存在或已无可重做时返回 null
403
402
  */
404
403
  public async redo(id: Id): Promise<CodeBlockStepValue | null> {
405
- const step = historyService.redo('codeBlock', id);
404
+ const step = this.ctx.history.redo('codeBlock', id);
406
405
  if (!step) return null;
407
406
  await this.applyHistoryStep(step, false);
408
407
  return step;
@@ -410,12 +409,12 @@ class CodeBlock extends BaseService {
410
409
 
411
410
  /** 是否可对指定代码块撤销。 */
412
411
  public canUndo(id: Id): boolean {
413
- return historyService.canUndo('codeBlock', id);
412
+ return this.ctx.history.canUndo('codeBlock', id);
414
413
  }
415
414
 
416
415
  /** 是否可对指定代码块重做。 */
417
416
  public canRedo(id: Id): boolean {
418
- return historyService.canRedo('codeBlock', id);
417
+ return this.ctx.history.canRedo('codeBlock', id);
419
418
  }
420
419
 
421
420
  /**
@@ -426,7 +425,7 @@ class CodeBlock extends BaseService {
426
425
  * @returns 实际移动到的最终游标位置
427
426
  */
428
427
  public async goto(id: Id, targetCursor: number): Promise<number> {
429
- let cursor = historyService.getCursor('codeBlock', id);
428
+ let cursor = this.ctx.history.getCursor('codeBlock', id);
430
429
  const target = Math.max(0, targetCursor);
431
430
  while (cursor > target) {
432
431
  const step = await this.undo(id);
@@ -452,7 +451,7 @@ class CodeBlock extends BaseService {
452
451
  * @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
453
452
  */
454
453
  public async revert(id: Id, index: number): Promise<CodeBlockStepValue | null> {
455
- const list = historyService.getStepList('codeBlock', id);
454
+ const list = this.ctx.history.getStepList('codeBlock', id);
456
455
  const entry = list[index];
457
456
  if (!entry?.applied) return null;
458
457
  // 更新类步骤(前后 content 都存在)必须带 changeRecords 才支持回滚,否则只能整内容替换,会冲掉后续无关变更。
@@ -473,7 +472,7 @@ class CodeBlock extends BaseService {
473
472
  public async revertById(uuids: string[]): Promise<(CodeBlockStepValue | null)[]> {
474
473
  const results: (CodeBlockStepValue | null)[] = [];
475
474
  for (const uuid of uuids) {
476
- const location = historyService.findStepLocationByUuid('codeBlock', uuid);
475
+ const location = this.ctx.history.findStepLocationByUuid('codeBlock', uuid);
477
476
  results.push(location ? await this.revert(location.id, location.index) : null);
478
477
  }
479
478
  return results;
@@ -513,7 +512,7 @@ class CodeBlock extends BaseService {
513
512
  coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
514
513
 
515
514
  Object.keys(customTarget.deps).forEach((nodeId: Id) => {
516
- const node = editorService.getNodeById(nodeId);
515
+ const node = this.ctx.editor.getNodeById(nodeId);
517
516
  if (!node) return;
518
517
  customTarget!.deps[nodeId].keys.forEach((key) => {
519
518
  const relateCodeId = get(node, key);
@@ -527,7 +526,7 @@ class CodeBlock extends BaseService {
527
526
  });
528
527
  });
529
528
  }
530
- storageService.setItem(COPY_CODE_STORAGE_KEY, copyData, {
529
+ this.ctx.storage.setItem(COPY_CODE_STORAGE_KEY, copyData, {
531
530
  protocol: Protocol.OBJECT,
532
531
  });
533
532
  }
@@ -537,7 +536,7 @@ class CodeBlock extends BaseService {
537
536
  * @returns
538
537
  */
539
538
  public paste() {
540
- const codeDsl: CodeBlockDSL = storageService.getItem(COPY_CODE_STORAGE_KEY);
539
+ const codeDsl: CodeBlockDSL = this.ctx.storage.getItem(COPY_CODE_STORAGE_KEY);
541
540
  Object.keys(codeDsl).forEach((codeId: Id) => {
542
541
  // 不覆盖同样id的代码块
543
542
  this.setCodeDslByIdSync(codeId, codeDsl[codeId], false);
@@ -575,13 +574,16 @@ class CodeBlock extends BaseService {
575
574
  // 原本是新增 → revert 即删除
576
575
  if (!oldSchema && newSchema) {
577
576
  await this.deleteCodeDslByIds([id], { historyDescription, historySource: 'rollback' });
578
- return historyService.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
577
+ return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
579
578
  }
580
579
 
581
580
  // 原本是删除 → revert 即写回
582
581
  if (oldSchema && !newSchema) {
583
- this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, { historyDescription, historySource: 'rollback' });
584
- return historyService.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
582
+ this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
583
+ historyDescription,
584
+ historySource: 'rollback',
585
+ });
586
+ return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
585
587
  }
586
588
 
587
589
  if (!oldSchema || !newSchema) return null;
@@ -605,11 +607,14 @@ class CodeBlock extends BaseService {
605
607
  historyDescription,
606
608
  historySource: 'rollback',
607
609
  });
608
- return historyService.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
610
+ return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
609
611
  }
610
612
 
611
- this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, { historyDescription, historySource: 'rollback' });
612
- return historyService.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
613
+ this.setCodeDslByIdSync(id, cloneDeep(oldSchema), true, {
614
+ historyDescription,
615
+ historySource: 'rollback',
616
+ });
617
+ return this.ctx.history.getStepList('codeBlock', id).slice(-1)[0]?.step ?? null;
613
618
  }
614
619
 
615
620
  /**
@@ -680,4 +685,4 @@ class CodeBlock extends BaseService {
680
685
 
681
686
  export type CodeBlockService = CodeBlock;
682
687
 
683
- export default new CodeBlock();
688
+ export default CodeBlock;
@@ -16,19 +16,20 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { shallowReactive } from 'vue';
20
-
21
19
  import type { ComponentGroup, ComponentGroupState } from '@editor/type';
22
20
 
21
+ import type { SdkContext } from '../type';
22
+
23
23
  import BaseService from './BaseService';
24
24
 
25
25
  class ComponentList extends BaseService {
26
- private state = shallowReactive<ComponentGroupState>({
27
- list: [],
28
- });
26
+ private state: ComponentGroupState;
29
27
 
30
- constructor() {
28
+ constructor(ctx: SdkContext) {
31
29
  super([]);
30
+ this.state = ctx.reactivity.shallowReactive<ComponentGroupState>({
31
+ list: [],
32
+ });
32
33
  }
33
34
 
34
35
  /**
@@ -55,4 +56,4 @@ class ComponentList extends BaseService {
55
56
 
56
57
  export type ComponentListService = ComponentList;
57
58
 
58
- export default new ComponentList();
59
+ export default ComponentList;
@@ -1,4 +1,3 @@
1
- import { reactive } from 'vue';
2
1
  import { cloneDeep, get } from 'lodash-es';
3
2
  import type { Writable } from 'type-fest';
4
3
 
@@ -7,9 +6,6 @@ import { Target, Watcher } from '@tmagic/core';
7
6
  import type { FormConfig } from '@tmagic/form';
8
7
  import { getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils';
9
8
 
10
- import editorService from '@editor/services/editor';
11
- import historyService from '@editor/services/history';
12
- import storageService, { Protocol } from '@editor/services/storage';
13
9
  import type {
14
10
  DataSourceStepValue,
15
11
  DatasourceTypeOption,
@@ -22,7 +18,10 @@ import { getFormConfig, getFormValue } from '@editor/utils/data-source';
22
18
  import { COPY_DS_STORAGE_KEY } from '@editor/utils/editor';
23
19
  import { createStackStep, describeRevertStep, getLastPushedHistoryIds } from '@editor/utils/history';
24
20
 
21
+ import type { SdkContext } from '../type';
22
+
25
23
  import BaseService from './BaseService';
24
+ import { Protocol } from './WebStorage';
26
25
 
27
26
  interface State {
28
27
  datasourceTypeList: DatasourceTypeOption[];
@@ -57,15 +56,7 @@ const canUsePluginMethods = {
57
56
  type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
58
57
 
59
58
  class DataSource extends BaseService {
60
- private state = reactive<State>({
61
- datasourceTypeList: [],
62
- dataSources: [],
63
- editable: true,
64
- configs: {},
65
- values: {},
66
- events: {},
67
- methods: {},
68
- });
59
+ private state: State;
69
60
 
70
61
  /**
71
62
  * 最近一次写入历史栈的数据源历史记录 uuid。
@@ -74,8 +65,17 @@ class DataSource extends BaseService {
74
65
  */
75
66
  private lastPushedHistoryId: string | null = null;
76
67
 
77
- constructor() {
68
+ constructor(private ctx: SdkContext) {
78
69
  super(canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })));
70
+ this.state = ctx.reactivity.reactive<State>({
71
+ datasourceTypeList: [],
72
+ dataSources: [],
73
+ editable: true,
74
+ configs: {},
75
+ values: {},
76
+ events: {},
77
+ methods: {},
78
+ });
79
79
  }
80
80
 
81
81
  public set<K extends StateKey, T extends State[K]>(name: K, value: T) {
@@ -129,6 +129,10 @@ class DataSource extends BaseService {
129
129
  config: DataSourceSchema,
130
130
  { doNotPushHistory = false, historyDescription, historySource }: HistoryOpOptions = {},
131
131
  ) {
132
+ // 数据源的新增 / 更新是同步方法,这里只能做同步的结构校验(类型是否可用);
133
+ // 属性表单校验见 `sdk.validate.dataSource(id)`
134
+ this.ctx.validator.guardDataSource(config, true);
135
+
132
136
  const newConfig = {
133
137
  ...config,
134
138
  id: config.id && !this.getDataSourceById(config.id) ? config.id : this.createId(),
@@ -143,7 +147,7 @@ class DataSource extends BaseService {
143
147
  historyDescription,
144
148
  source: historySource,
145
149
  });
146
- this.lastPushedHistoryId = (step ? historyService.push('dataSource', step, newConfig.id) : null)?.uuid ?? null;
150
+ this.lastPushedHistoryId = (step ? this.ctx.history.push('dataSource', step, newConfig.id) : null)?.uuid ?? null;
147
151
  }
148
152
 
149
153
  this.emit('add', newConfig);
@@ -169,6 +173,8 @@ class DataSource extends BaseService {
169
173
  historySource,
170
174
  }: HistoryOpOptionsWithChangeRecords = {},
171
175
  ) {
176
+ this.ctx.validator.guardDataSource(config, false);
177
+
172
178
  const dataSources = this.get('dataSources');
173
179
 
174
180
  const index = dataSources.findIndex((ds) => ds.id === config.id);
@@ -186,7 +192,7 @@ class DataSource extends BaseService {
186
192
  historyDescription,
187
193
  source: historySource,
188
194
  });
189
- this.lastPushedHistoryId = (step ? historyService.push('dataSource', step, newConfig.id) : null)?.uuid ?? null;
195
+ this.lastPushedHistoryId = (step ? this.ctx.history.push('dataSource', step, newConfig.id) : null)?.uuid ?? null;
190
196
  }
191
197
 
192
198
  this.emit('update', newConfig, {
@@ -218,7 +224,7 @@ class DataSource extends BaseService {
218
224
  historyDescription,
219
225
  source: historySource,
220
226
  });
221
- this.lastPushedHistoryId = (step ? historyService.push('dataSource', step, id) : null)?.uuid ?? null;
227
+ this.lastPushedHistoryId = (step ? this.ctx.history.push('dataSource', step, id) : null)?.uuid ?? null;
222
228
  }
223
229
 
224
230
  this.emit('remove', id);
@@ -273,7 +279,7 @@ class DataSource extends BaseService {
273
279
  * @returns 撤销的 step;栈不存在或已无可撤销时返回 null
274
280
  */
275
281
  public undo(id: Id) {
276
- const step = historyService.undo('dataSource', id);
282
+ const step = this.ctx.history.undo('dataSource', id);
277
283
  if (!step) return null;
278
284
  this.applyHistoryStep(step, true);
279
285
  return step;
@@ -285,7 +291,7 @@ class DataSource extends BaseService {
285
291
  * @returns 重做的 step;栈不存在或已无可重做时返回 null
286
292
  */
287
293
  public redo(id: Id) {
288
- const step = historyService.redo('dataSource', id);
294
+ const step = this.ctx.history.redo('dataSource', id);
289
295
  if (!step) return null;
290
296
  this.applyHistoryStep(step, false);
291
297
  return step;
@@ -293,12 +299,12 @@ class DataSource extends BaseService {
293
299
 
294
300
  /** 是否可对指定数据源撤销。 */
295
301
  public canUndo(id: Id): boolean {
296
- return historyService.canUndo('dataSource', id);
302
+ return this.ctx.history.canUndo('dataSource', id);
297
303
  }
298
304
 
299
305
  /** 是否可对指定数据源重做。 */
300
306
  public canRedo(id: Id): boolean {
301
- return historyService.canRedo('dataSource', id);
307
+ return this.ctx.history.canRedo('dataSource', id);
302
308
  }
303
309
 
304
310
  /**
@@ -309,7 +315,7 @@ class DataSource extends BaseService {
309
315
  * @returns 实际移动到的最终游标位置
310
316
  */
311
317
  public goto(id: Id, targetCursor: number): number {
312
- let cursor = historyService.getCursor('dataSource', id);
318
+ let cursor = this.ctx.history.getCursor('dataSource', id);
313
319
  const target = Math.max(0, targetCursor);
314
320
  while (cursor > target) {
315
321
  if (!this.undo(id)) break;
@@ -333,7 +339,7 @@ class DataSource extends BaseService {
333
339
  * @returns 反向后产生的新 step;目标不存在 / 未应用时返回 null
334
340
  */
335
341
  public revert(id: Id, index: number): DataSourceStepValue | null {
336
- const list = historyService.getStepList('dataSource', id);
342
+ const list = this.ctx.history.getStepList('dataSource', id);
337
343
  const entry = list[index];
338
344
  if (!entry?.applied) return null;
339
345
  // 更新类步骤(前后 schema 都存在)必须带 changeRecords 才支持回滚,否则只能整 schema 替换,会冲掉后续无关变更。
@@ -352,7 +358,7 @@ class DataSource extends BaseService {
352
358
  */
353
359
  public revertById(uuids: string[]): (DataSourceStepValue | null)[] {
354
360
  return uuids.map((uuid) => {
355
- const location = historyService.findStepLocationByUuid('dataSource', uuid);
361
+ const location = this.ctx.history.findStepLocationByUuid('dataSource', uuid);
356
362
  if (!location) return null;
357
363
  return this.revert(location.id, location.index);
358
364
  });
@@ -401,7 +407,7 @@ class DataSource extends BaseService {
401
407
  coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
402
408
 
403
409
  Object.keys(customTarget.deps).forEach((nodeId: Id) => {
404
- const node = editorService.getNodeById(nodeId);
410
+ const node = this.ctx.editor.getNodeById(nodeId);
405
411
  if (!node) return;
406
412
  customTarget!.deps[nodeId].keys.forEach((key) => {
407
413
  const [relateDsId] = get(node, key);
@@ -415,7 +421,7 @@ class DataSource extends BaseService {
415
421
  });
416
422
  });
417
423
  }
418
- storageService.setItem(COPY_DS_STORAGE_KEY, copyData, {
424
+ this.ctx.storage.setItem(COPY_DS_STORAGE_KEY, copyData, {
419
425
  protocol: Protocol.OBJECT,
420
426
  });
421
427
  }
@@ -424,7 +430,7 @@ class DataSource extends BaseService {
424
430
  * @returns
425
431
  */
426
432
  public paste() {
427
- const dataSource: DataSourceSchema[] = storageService.getItem(COPY_DS_STORAGE_KEY);
433
+ const dataSource: DataSourceSchema[] = this.ctx.storage.getItem(COPY_DS_STORAGE_KEY);
428
434
  dataSource.forEach((item: DataSourceSchema) => {
429
435
  // 不覆盖同样id的数据源
430
436
  if (!this.getDataSourceById(item.id)) {
@@ -444,13 +450,13 @@ class DataSource extends BaseService {
444
450
  // 原本是新增 → revert 即删除
445
451
  if (!oldSchema && newSchema) {
446
452
  this.remove(`${id}`, { historyDescription, historySource: 'rollback' });
447
- return historyService.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
453
+ return this.ctx.history.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
448
454
  }
449
455
 
450
456
  // 原本是删除 → revert 即重新加回
451
457
  if (oldSchema && !newSchema) {
452
458
  this.add(cloneDeep(oldSchema), { historyDescription, historySource: 'rollback' });
453
- return historyService.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
459
+ return this.ctx.history.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
454
460
  }
455
461
 
456
462
  if (!oldSchema || !newSchema) return null;
@@ -474,11 +480,11 @@ class DataSource extends BaseService {
474
480
  historyDescription,
475
481
  historySource: 'rollback',
476
482
  });
477
- return historyService.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
483
+ return this.ctx.history.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
478
484
  }
479
485
 
480
486
  this.update(cloneDeep(oldSchema), { historyDescription, historySource: 'rollback' });
481
- return historyService.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
487
+ return this.ctx.history.getStepList('dataSource', id).slice(-1)[0]?.step ?? null;
482
488
  }
483
489
 
484
490
  /**
@@ -549,4 +555,4 @@ class DataSource extends BaseService {
549
555
 
550
556
  export type DataSourceService = DataSource;
551
557
 
552
- export default new DataSource();
558
+ export default DataSource;
@@ -15,19 +15,28 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- import { reactive, shallowReactive } from 'vue';
19
18
  import { throttle } from 'lodash-es';
20
19
 
21
20
  import type { DepData, DepExtendedData, Id, MApp, MNode, Target } from '@tmagic/core';
22
21
  import { DepTargetType, traverseTarget, Watcher } from '@tmagic/core';
23
22
  import { isPage } from '@tmagic/utils';
24
23
 
25
- import { CollectWorkerClient } from '@editor/utils/dep/collect-worker-client';
26
24
  import { IdleTask } from '@editor/utils/dep/idle-task';
27
25
  import type { DepsData } from '@editor/utils/dep/worker';
28
26
 
27
+ import type { CollectWorkerLike, SdkContext } from '../type';
28
+
29
29
  import BaseService from './BaseService';
30
30
 
31
+ /** Node 下没有 worker 时使用:始终不可用,依赖收集退回主线程空闲队列 */
32
+ const noopCollectWorker: CollectWorkerLike = {
33
+ isSupported: false,
34
+ collectDsl: () => Promise.resolve(null),
35
+ collect: () => Promise.resolve(null),
36
+ terminate: () => {},
37
+ abort: () => {},
38
+ };
39
+
31
40
  export interface DepEvents {
32
41
  'add-target': [target: Target];
33
42
  'remove-target': [id: string | number, type: string | DepTargetType];
@@ -59,16 +68,13 @@ interface CollectBatch {
59
68
  }
60
69
 
61
70
  class Dep extends BaseService {
62
- private state = shallowReactive<State>({
63
- collecting: false,
64
- taskLength: 0,
65
- });
71
+ private state: State;
66
72
 
67
73
  private idleTask = new IdleTask<void>();
68
74
 
69
- private collectWorker = new CollectWorkerClient();
75
+ private collectWorker: CollectWorkerLike;
70
76
 
71
- private watcher = new Watcher({ initialTargets: reactive({}) });
77
+ private watcher: Watcher;
72
78
 
73
79
  private waitingWorker?: Promise<void>;
74
80
 
@@ -78,9 +84,16 @@ class Dep extends BaseService {
78
84
 
79
85
  private activeBatches = new Set<CollectBatch>();
80
86
 
81
- constructor() {
87
+ constructor(private ctx: SdkContext) {
82
88
  super();
83
89
 
90
+ this.state = ctx.reactivity.shallowReactive<State>({
91
+ collecting: false,
92
+ taskLength: 0,
93
+ });
94
+ this.collectWorker = ctx.options.collectWorker ?? noopCollectWorker;
95
+ this.watcher = new Watcher({ initialTargets: ctx.reactivity.reactive({}) });
96
+
84
97
  this.idleTask.on(
85
98
  'update-task-length',
86
99
  throttle(({ length }) => {
@@ -229,7 +242,7 @@ class Dep extends BaseService {
229
242
 
230
243
  traverseTarget(this.watcher.getTargetsList(), (target) => {
231
244
  if (depsData[target.type]?.[target.id]) {
232
- target.deps = reactive(depsData[target.type][target.id]);
245
+ target.deps = this.ctx.reactivity.reactive(depsData[target.type][target.id]);
233
246
 
234
247
  if (target.type === DepTargetType.DATA_SOURCE && dsl.dataSourceDeps) {
235
248
  dsl.dataSourceDeps[target.id] = target.deps;
@@ -554,4 +567,4 @@ class Dep extends BaseService {
554
567
 
555
568
  export type DepService = Dep;
556
569
 
557
- export default new Dep();
570
+ export default Dep;