@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
@@ -16,7 +16,6 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { reactive } from 'vue';
20
19
  import { cloneDeep } from 'lodash-es';
21
20
  import type { Writable } from 'type-fest';
22
21
 
@@ -25,6 +24,8 @@ import { toLine } from '@tmagic/utils';
25
24
 
26
25
  import type { AsyncHookPlugin, SyncHookPlugin } from '@editor/type';
27
26
 
27
+ import type { SdkContext } from '../type';
28
+
28
29
  import BaseService from './BaseService';
29
30
 
30
31
  const canUsePluginMethods = {
@@ -35,15 +36,17 @@ const canUsePluginMethods = {
35
36
  type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
36
37
  type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
37
38
 
38
- const eventMap: Record<string, EventOption[]> = reactive({});
39
- const methodMap: Record<string, EventOption[]> = reactive({});
40
-
41
39
  class Events extends BaseService {
42
- constructor() {
40
+ private eventMap: Record<string, EventOption[]>;
41
+ private methodMap: Record<string, EventOption[]>;
42
+
43
+ constructor(ctx: SdkContext) {
43
44
  super([
44
45
  ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
45
46
  ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
46
47
  ]);
48
+ this.eventMap = ctx.reactivity.reactive({});
49
+ this.methodMap = ctx.reactivity.reactive({});
47
50
  }
48
51
 
49
52
  public setEvents(events: Record<string, EventOption[]>) {
@@ -53,11 +56,11 @@ class Events extends BaseService {
53
56
  }
54
57
 
55
58
  public setEvent(type: string, events: EventOption[]) {
56
- eventMap[toLine(type)] = [...events];
59
+ this.eventMap[toLine(type)] = [...events];
57
60
  }
58
61
 
59
62
  public getEvent(type: string, _data: { node?: MNode | null } = {}): EventOption[] {
60
- return cloneDeep(eventMap[toLine(type)]) || [];
63
+ return cloneDeep(this.eventMap[toLine(type)]) || [];
61
64
  }
62
65
 
63
66
  public setMethods(methods: Record<string, EventOption[]>) {
@@ -67,17 +70,17 @@ class Events extends BaseService {
67
70
  }
68
71
 
69
72
  public setMethod(type: string, method: EventOption[]) {
70
- methodMap[toLine(type)] = [...method];
73
+ this.methodMap[toLine(type)] = [...method];
71
74
  }
72
75
 
73
76
  public getMethod(type: string, _data: { node?: MNode | null; targetId?: Id } = {}) {
74
- return cloneDeep(methodMap[toLine(type)]) || [];
77
+ return cloneDeep(this.methodMap[toLine(type)]) || [];
75
78
  }
76
79
 
77
80
  public resetState() {
78
81
  // 原地清空而不是重新赋值,保留 reactive 代理身份,已建立的 computed / 渲染副作用才能收到更新
79
- Object.keys(eventMap).forEach((type) => delete eventMap[type]);
80
- Object.keys(methodMap).forEach((type) => delete methodMap[type]);
82
+ Object.keys(this.eventMap).forEach((type) => delete this.eventMap[type]);
83
+ Object.keys(this.methodMap).forEach((type) => delete this.methodMap[type]);
81
84
  }
82
85
 
83
86
  public destroy() {
@@ -93,4 +96,4 @@ class Events extends BaseService {
93
96
 
94
97
  export type EventsService = Events;
95
98
 
96
- export default new Events();
99
+ export default Events;
@@ -16,7 +16,6 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { reactive } from 'vue';
20
19
  import type { Writable } from 'type-fest';
21
20
 
22
21
  import type { Id, MPage, MPageFragment } from '@tmagic/core';
@@ -37,7 +36,6 @@ import type {
37
36
  StepValue,
38
37
  SyncHookPlugin,
39
38
  } from '@editor/type';
40
- import { getEditorConfig } from '@editor/utils/config';
41
39
  import {
42
40
  deserializeStacks,
43
41
  getOrCreateStack,
@@ -46,11 +44,11 @@ import {
46
44
  serializeStacks,
47
45
  undoFloor,
48
46
  } from '@editor/utils/history';
49
- import { idbGet, idbSet } from '@editor/utils/indexed-db';
50
47
  import { UndoRedo } from '@editor/utils/undo-redo';
51
48
 
49
+ import type { SdkContext } from '../type';
50
+
52
51
  import BaseService from './BaseService';
53
- import editorService from './editor';
54
52
 
55
53
  const canUsePluginMethods = {
56
54
  sync: ['push', 'undo', 'redo'] as const,
@@ -80,18 +78,57 @@ const DEFAULT_KEY: IDBValidKey = 'default';
80
78
  // 全部历史栈统一收敛到 steps 字段(见 HistorySteps)。
81
79
  const PERSIST_VERSION = 3;
82
80
 
83
- class History extends BaseService {
84
- public state = reactive<HistoryState>({
85
- steps: {
86
- page: {},
87
- codeBlock: {},
88
- dataSource: {},
89
- },
90
- stepNames: { ...DEFAULT_STEP_NAMES },
81
+ const EXTRA_MAP_KEYS = ['modifiedNodeIds', 'invalidNodeIdsBefore', 'invalidNodeIdsAfter'] as const;
82
+
83
+ /**
84
+ * 转换各栈 step.extra 中的 Map 字段,返回新对象(不修改入参,避免污染运行中的历史状态)。
85
+ * - 导出:Map -> entries 数组,使快照可以直接 `JSON.stringify`
86
+ * - 还原:entries 数组 -> Map
87
+ */
88
+ const convertExtraMaps = (
89
+ stacks: Record<string, any> | undefined,
90
+ restoring: boolean,
91
+ ): Record<string, any> | undefined => {
92
+ if (!stacks) return stacks;
93
+
94
+ const convertValue = (value: any) => {
95
+ if (restoring) return Array.isArray(value) ? new Map(value) : value;
96
+ return value instanceof Map ? [...value.entries()] : value;
97
+ };
98
+
99
+ const result: Record<string, any> = {};
100
+ Object.entries(stacks).forEach(([id, stack]: [string, any]) => {
101
+ result[id] = !stack?.elementList
102
+ ? stack
103
+ : {
104
+ ...stack,
105
+ elementList: stack.elementList.map((step: any) => {
106
+ if (!step?.extra) return step;
107
+ const extra = { ...step.extra };
108
+ // 导出与还原使用同一份 key 清单,保证往返对称
109
+ EXTRA_MAP_KEYS.forEach((key) => {
110
+ if (extra[key]) extra[key] = convertValue(extra[key]);
111
+ });
112
+ return { ...step, extra };
113
+ }),
114
+ };
91
115
  });
116
+ return result;
117
+ };
92
118
 
93
- constructor() {
119
+ class History extends BaseService {
120
+ public state: HistoryState;
121
+
122
+ constructor(private ctx: SdkContext) {
94
123
  super([...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false }))]);
124
+ this.state = ctx.reactivity.reactive<HistoryState>({
125
+ steps: {
126
+ page: {},
127
+ codeBlock: {},
128
+ dataSource: {},
129
+ },
130
+ stepNames: { ...DEFAULT_STEP_NAMES },
131
+ });
95
132
  }
96
133
 
97
134
  /**
@@ -148,7 +185,12 @@ class History extends BaseService {
148
185
  public setMarker(
149
186
  stepType: HistoryStepType,
150
187
  id: Id,
151
- options: { name?: string; description?: string; source?: HistoryOpSource; extra?: Record<string, any> } = {},
188
+ options: {
189
+ name?: string;
190
+ description?: string;
191
+ source?: HistoryOpSource;
192
+ extra?: Record<string, any>;
193
+ } = {},
152
194
  ): StepValue | null {
153
195
  if (!this.isValidStackId(id)) return null;
154
196
 
@@ -385,21 +427,46 @@ class History extends BaseService {
385
427
  public async saveToIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState> {
386
428
  const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
387
429
 
430
+ const persistence = this.getPersistence();
431
+ const snapshot = this.serialize();
432
+
433
+ await persistence.set(this.resolveDbName(dbName, appId), storeName, key, snapshot);
434
+ this.emit('save-to-indexed-db', snapshot);
435
+ return snapshot;
436
+ }
437
+
438
+ /**
439
+ * 把当前内存中的全部历史栈导出成可持久化的快照(纯同步,不依赖 IndexedDB),
440
+ * 可用于无状态调用:下次通过 {@link restore} 或 `createDslSdk({ history })` 还原。
441
+ */
442
+ public serialize({ jsonSafe = false }: { jsonSafe?: boolean } = {}): PersistedHistoryState {
388
443
  // serializeStacks 会在序列化各栈时只把每条 step 的 diff(可能含函数)序列化成字符串,其余字段原样保留,
389
444
  // 因此整份快照可直接按对象写入 IndexedDB(结构化克隆),避免序列化整份快照的开销;读取时再用 parseDSL 还原 diff。
390
445
  const steps: PersistedHistoryState['steps'] = { page: {}, codeBlock: {}, dataSource: {} };
391
446
  Object.entries(this.state.steps).forEach(([stepType, bucket]) => {
392
447
  steps[stepType] = serializeStacks(bucket);
448
+ // `extra` 里的校验错误记录是 Map,JSON 无法表示;jsonSafe 时转成 entries 数组,restore 时会还原
449
+ if (jsonSafe) steps[stepType] = convertExtraMaps(steps[stepType], false) as any;
393
450
  });
394
451
 
395
- const snapshot: PersistedHistoryState = {
452
+ return {
396
453
  version: PERSIST_VERSION,
397
454
  steps,
398
455
  savedAt: Date.now(),
399
456
  };
457
+ }
400
458
 
401
- await idbSet(this.resolveDbName(dbName, appId), storeName, key, snapshot);
402
- this.emit('save-to-indexed-db', snapshot);
459
+ /**
460
+ * 用 {@link serialize} 导出的快照重建全部撤销/重做栈,会整体覆盖当前内存中的历史状态。
461
+ */
462
+ public restore(snapshot: PersistedHistoryState): PersistedHistoryState {
463
+ // 各 step 的 diff 以序列化字符串存储(含函数),由 deserializeStacks 逐条用 parseDSL 还原。
464
+ const steps: HistoryState['steps'] = { page: {}, codeBlock: {}, dataSource: {} };
465
+ Object.entries(snapshot.steps).forEach(([stepType, bucket]) => {
466
+ // 兼容 jsonSafe 导出的快照(Map 被转成了 entries 数组)
467
+ steps[stepType] = deserializeStacks(convertExtraMaps(bucket, true) as any, this.ctx.parseDSL);
468
+ });
469
+ this.state.steps = steps;
403
470
  return snapshot;
404
471
  }
405
472
 
@@ -414,17 +481,14 @@ class History extends BaseService {
414
481
  public async restoreFromIndexedDB(options: HistoryPersistOptions = {}): Promise<PersistedHistoryState | null> {
415
482
  const { dbName = DEFAULT_DB_NAME, storeName = DEFAULT_STORE_NAME, key = DEFAULT_KEY, appId } = options;
416
483
 
417
- const snapshot = await idbGet<PersistedHistoryState>(this.resolveDbName(dbName, appId), storeName, key);
484
+ const snapshot = await this.getPersistence().get<PersistedHistoryState>(
485
+ this.resolveDbName(dbName, appId),
486
+ storeName,
487
+ key,
488
+ );
418
489
  if (!snapshot) return null;
419
490
 
420
- // 各 step 的 diff 以序列化字符串存储(含函数),由 deserializeStacks 逐条用 parseDSL 还原。
421
- const parseDSL = getEditorConfig('parseDSL');
422
-
423
- const steps: HistoryState['steps'] = { page: {}, codeBlock: {}, dataSource: {} };
424
- Object.entries(snapshot.steps).forEach(([stepType, bucket]) => {
425
- steps[stepType] = deserializeStacks(bucket, parseDSL);
426
- });
427
- this.state.steps = steps;
491
+ this.restore(snapshot);
428
492
  // initial 基线作为各栈 index 0 的 step 随 steps 一并还原,无需单独恢复;活动页由 editorService 维护。
429
493
  this.emit('restore-from-indexed-db', snapshot);
430
494
  return snapshot;
@@ -583,17 +647,25 @@ class History extends BaseService {
583
647
  });
584
648
  }
585
649
 
650
+ private getPersistence() {
651
+ const persistence = this.ctx.options.historyPersistence;
652
+ if (!persistence) {
653
+ throw new Error('未提供 historyPersistence,无法读写历史快照;请改用 serialize() / restore()');
654
+ }
655
+ return persistence;
656
+ }
657
+
586
658
  /**
587
659
  * 把基础 dbName 与当前 DSL(root app)的 id 拼成最终库名,实现不同应用历史隔离。
588
660
  * 取不到 app id(如尚未加载 DSL)时退回基础 dbName。
589
661
  */
590
662
  private resolveDbName(dbName: string, appId?: Id): string {
591
663
  // 优先用显式传入的 appId(「先恢复再 set root」时 root 尚未就绪);否则回退到当前 root.id。
592
- const resolvedAppId = appId ?? editorService.get('root')?.id;
664
+ const resolvedAppId = appId ?? this.ctx.editor.get('root')?.id;
593
665
  return resolvedAppId ? `${dbName}-${resolvedAppId}` : dbName;
594
666
  }
595
667
  }
596
668
 
597
669
  export type HistoryService = History;
598
670
 
599
- export default new History();
671
+ export default History;
@@ -16,7 +16,6 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { reactive } from 'vue';
20
19
  import { cloneDeep, mergeWith } from 'lodash-es';
21
20
  import type { Writable } from 'type-fest';
22
21
 
@@ -25,7 +24,6 @@ import { Target, Watcher } from '@tmagic/core';
25
24
  import type { FormConfig } from '@tmagic/form';
26
25
  import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils';
27
26
 
28
- import editorService from '@editor/services/editor';
29
27
  import type {
30
28
  AsyncHookPlugin,
31
29
  PropsFormConfigFunction,
@@ -33,7 +31,9 @@ import type {
33
31
  PropsState,
34
32
  SyncHookPlugin,
35
33
  } from '@editor/type';
36
- import { fillConfig } from '@editor/utils/props';
34
+
35
+ import type { SdkContext } from '../type';
36
+ import { fillConfig } from '../utils/props';
37
37
 
38
38
  import BaseService from './BaseService';
39
39
 
@@ -53,21 +53,22 @@ type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
53
53
  type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
54
54
 
55
55
  class Props extends BaseService {
56
- private state = reactive<PropsState>({
57
- propsConfigMap: {},
58
- propsValueMap: {},
59
- relateIdMap: {},
60
- /** 禁用数据源 */
61
- disabledDataSource: false,
62
- /** 禁用代码块 */
63
- disabledCodeBlock: false,
64
- });
65
-
66
- constructor() {
56
+ private state: PropsState;
57
+
58
+ constructor(private ctx: SdkContext) {
67
59
  super([
68
60
  ...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
69
61
  ...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
70
62
  ]);
63
+ this.state = ctx.reactivity.reactive<PropsState>({
64
+ propsConfigMap: {},
65
+ propsValueMap: {},
66
+ relateIdMap: {},
67
+ /** 禁用数据源 */
68
+ disabledDataSource: false,
69
+ /** 禁用代码块 */
70
+ disabledCodeBlock: false,
71
+ });
71
72
  }
72
73
 
73
74
  public setDisabledDataSource(disabled: boolean) {
@@ -86,10 +87,9 @@ class Props extends BaseService {
86
87
  return this.state.disabledCodeBlock;
87
88
  }
88
89
 
89
- public setPropsConfigs(configs: Record<string, FormConfig | PropsFormConfigFunction>) {
90
- Object.keys(configs).forEach((type: string) => {
91
- this.setPropsConfig(toLine(type), configs[type]);
92
- });
90
+ /** 批量设置组件属性表单配置,返回的 Promise 在全部配置处理完(含异步的)后 resolve */
91
+ public async setPropsConfigs(configs: Record<string, FormConfig | PropsFormConfigFunction>) {
92
+ await Promise.all(Object.keys(configs).map((type: string) => this.setPropsConfig(toLine(type), configs[type])));
93
93
  this.emit('props-configs-change');
94
94
  }
95
95
 
@@ -102,13 +102,15 @@ class Props extends BaseService {
102
102
  labelWidth,
103
103
  disabledDataSource: this.getDisabledDataSource(),
104
104
  disabledCodeBlock: this.getDisabledCodeBlock(),
105
+ copyText: this.ctx.options.copyText,
106
+ notify: this.ctx.options.notify,
105
107
  });
106
108
  }
107
109
 
108
110
  public async setPropsConfig(type: string, config: FormConfig | PropsFormConfigFunction) {
109
111
  let c: FormConfig;
110
112
  if (typeof config === 'function') {
111
- c = config({ editorService });
113
+ c = config({ editorService: this.ctx.editor });
112
114
  } else {
113
115
  c = config;
114
116
  }
@@ -134,9 +136,7 @@ class Props extends BaseService {
134
136
  }
135
137
 
136
138
  public setPropsValues(values: Record<string, Partial<MNode> | PropsFormValueFunction>) {
137
- Object.keys(values).forEach((type: string) => {
138
- this.setPropsValue(toLine(type), values[type]);
139
- });
139
+ return Promise.all(Object.keys(values).map((type: string) => this.setPropsValue(toLine(type), values[type])));
140
140
  }
141
141
 
142
142
  public getPropsValues(): Record<string, Partial<MNode>> {
@@ -151,7 +151,7 @@ class Props extends BaseService {
151
151
  public async setPropsValue(type: string, value: Partial<MNode> | PropsFormValueFunction) {
152
152
  let v = value;
153
153
  if (typeof value === 'function') {
154
- v = value({ editorService });
154
+ v = value({ editorService: this.ctx.editor });
155
155
  }
156
156
  this.state.propsValueMap[toLine(type)] = v;
157
157
  }
@@ -206,7 +206,7 @@ class Props extends BaseService {
206
206
  */
207
207
  /* eslint no-param-reassign: ["error", { "props": false }] */
208
208
  public setNewItemId(config: MNode, force = true) {
209
- if (force || editorService.getNodeById(config.id)) {
209
+ if (force || this.ctx.editor.getNodeById(config.id)) {
210
210
  const newId = this.createId(config.type || 'component');
211
211
  this.setRelateId(config.id, newId);
212
212
  config.id = newId;
@@ -325,4 +325,4 @@ class Props extends BaseService {
325
325
 
326
326
  export type PropsService = Props;
327
327
 
328
- export default new Props();
328
+ export default Props;
@@ -2,7 +2,8 @@ import serialize from 'serialize-javascript';
2
2
  import type { Writable } from 'type-fest';
3
3
 
4
4
  import type { SyncHookPlugin } from '@editor/type';
5
- import { getEditorConfig } from '@editor/utils/config';
5
+
6
+ import type { SdkContext, StorageLike } from '../type';
6
7
 
7
8
  import BaseService from './BaseService';
8
9
 
@@ -26,15 +27,48 @@ const canUsePluginMethods = {
26
27
 
27
28
  type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
28
29
 
30
+ /** 默认的内存存储,Node 下没有 localStorage 时使用 */
31
+ export class MemoryStorage implements StorageLike {
32
+ private map = new Map<string, string>();
33
+
34
+ get length() {
35
+ return this.map.size;
36
+ }
37
+
38
+ clear() {
39
+ this.map.clear();
40
+ }
41
+
42
+ getItem(key: string) {
43
+ return this.map.has(key) ? (this.map.get(key) as string) : null;
44
+ }
45
+
46
+ key(index: number) {
47
+ return Array.from(this.map.keys())[index] ?? null;
48
+ }
49
+
50
+ removeItem(key: string) {
51
+ this.map.delete(key);
52
+ }
53
+
54
+ setItem(key: string, value: string) {
55
+ this.map.set(key, `${value}`);
56
+ }
57
+ }
58
+
29
59
  /**
30
60
  * 数据存储服务
31
61
  */
32
62
  export class WebStorage extends BaseService {
33
- private storage: Storage = globalThis.localStorage;
63
+ private storage: StorageLike;
34
64
  private namespace = 'tmagic';
35
65
 
36
- constructor() {
66
+ constructor(
67
+ private ctx: SdkContext,
68
+ storage?: StorageLike,
69
+ ) {
37
70
  super(canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })));
71
+ this.storage = storage ?? new MemoryStorage();
38
72
  }
39
73
 
40
74
  /**
@@ -42,12 +76,12 @@ export class WebStorage extends BaseService {
42
76
  * const storageService = new StorageService();
43
77
  * storageService.usePlugin({
44
78
  * // 替换存储对象为 sessionStorage
45
- * async afterGetStorage(): Promise<Storage> {
79
+ * async afterGetStorage(): Promise<StorageLike> {
46
80
  * return window.sessionStorage;
47
81
  * },
48
82
  * });
49
83
  */
50
- public getStorage(): Storage {
84
+ public getStorage(): StorageLike {
51
85
  return this.storage;
52
86
  }
53
87
 
@@ -76,7 +110,7 @@ export class WebStorage extends BaseService {
76
110
 
77
111
  switch (protocol) {
78
112
  case Protocol.OBJECT:
79
- return getEditorConfig('parseDSL')(`(${item})`);
113
+ return this.ctx.parseDSL(`(${item})`);
80
114
  case Protocol.JSON:
81
115
  return JSON.parse(item);
82
116
  case Protocol.NUMBER:
@@ -153,4 +187,4 @@ export class WebStorage extends BaseService {
153
187
  }
154
188
 
155
189
  export type StorageService = WebStorage;
156
- export default new WebStorage();
190
+ export default WebStorage;