@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,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 }) => {
@@ -137,8 +150,10 @@ class Dep extends BaseService {
137
150
  const targets = this.watcher.getCollectableTargets(type);
138
151
 
139
152
  if (targets.length) {
153
+ // 先统一删除所有节点的旧依赖,再统一收集。
154
+ // 如果逐节点“删除 + 收集”,共用 id 的多棵树(如复制出的页面片),
155
+ // 后一棵树的删除会清掉前一棵树刚写入的依赖。
140
156
  for (const node of nodes) {
141
- // 先删除原有依赖,再批量收集
142
157
  if (isPage(node)) {
143
158
  for (const target of targets) {
144
159
  this.removePageDep(target, depExtendedData);
@@ -146,6 +161,8 @@ class Dep extends BaseService {
146
161
  } else {
147
162
  this.watcher.removeTargetsDep(targets, node);
148
163
  }
164
+ }
165
+ for (const node of nodes) {
149
166
  this.watcher.collectItems(node, targets, depExtendedData, deep);
150
167
  }
151
168
  }
@@ -229,7 +246,7 @@ class Dep extends BaseService {
229
246
 
230
247
  traverseTarget(this.watcher.getTargetsList(), (target) => {
231
248
  if (depsData[target.type]?.[target.id]) {
232
- target.deps = reactive(depsData[target.type][target.id]);
249
+ target.deps = this.ctx.reactivity.reactive(depsData[target.type][target.id]);
233
250
 
234
251
  if (target.type === DepTargetType.DATA_SOURCE && dsl.dataSourceDeps) {
235
252
  dsl.dataSourceDeps[target.id] = target.deps;
@@ -554,4 +571,4 @@ class Dep extends BaseService {
554
571
 
555
572
  export type DepService = Dep;
556
573
 
557
- export default new Dep();
574
+ export default Dep;