@tmagic/editor 1.8.0-beta.5 → 1.8.0-beta.6

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 (42) hide show
  1. package/dist/es/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +4 -1
  2. package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +8 -2
  3. package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +25 -8
  4. package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +6 -4
  5. package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +25 -12
  6. package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +27 -5
  7. package/dist/es/index.js +2 -2
  8. package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +2 -2
  9. package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +12 -4
  10. package/dist/es/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +16 -3
  11. package/dist/es/layouts/sidebar/data-source/DataSourceListPanel.vue_vue_type_script_setup_true_lang.js +23 -1
  12. package/dist/es/services/codeBlock.js +30 -19
  13. package/dist/es/services/dataSource.js +29 -21
  14. package/dist/es/services/editor.js +52 -33
  15. package/dist/es/services/history.js +10 -15
  16. package/dist/es/style.css +4 -1
  17. package/dist/es/utils/data-source/index.js +2 -0
  18. package/dist/es/utils/editor.js +68 -48
  19. package/dist/es/utils/history.js +42 -33
  20. package/dist/style.css +4 -1
  21. package/dist/tmagic-editor.umd.cjs +376 -206
  22. package/package.json +7 -7
  23. package/src/fields/CodeSelectCol.vue +7 -1
  24. package/src/fields/DataSourceFieldSelect/FieldSelect.vue +16 -2
  25. package/src/fields/DataSourceFields.vue +37 -8
  26. package/src/fields/DataSourceMethodSelect.vue +9 -4
  27. package/src/fields/DataSourceMethods.vue +42 -21
  28. package/src/fields/StyleSetter/components/Border.vue +15 -6
  29. package/src/layouts/history-list/HistoryListPanel.vue +2 -2
  30. package/src/layouts/history-list/InitialRow.vue +3 -0
  31. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +30 -2
  32. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +26 -1
  33. package/src/services/codeBlock.ts +28 -22
  34. package/src/services/dataSource.ts +29 -24
  35. package/src/services/editor.ts +41 -37
  36. package/src/services/history.ts +14 -19
  37. package/src/theme/style-setter/border.scss +4 -1
  38. package/src/type.ts +16 -1
  39. package/src/utils/data-source/index.ts +2 -0
  40. package/src/utils/editor.ts +125 -59
  41. package/src/utils/history.ts +46 -36
  42. package/types/index.d.ts +87 -68
@@ -132,6 +132,34 @@ const getMiddleTop = (node: MNode, parentNode: MNode, stage: StageCore | null) =
132
132
  return (Math.min(parentHeight, wrapperHeightDeal) - height) / 2;
133
133
  };
134
134
 
135
+ // 同时存在一对相反的定位属性(如 left/right)时只保留一个,避免元素被拉伸
136
+ const removeConflictPosition = (style: Record<string, any>, primary: string, secondary: string) => {
137
+ // '' / 0 / undefined / null 视为无效值
138
+ const isInvalid = (value: any) => value === '' || value === 0 || value === undefined || value === null;
139
+
140
+ if (!(primary in style) || !(secondary in style)) {
141
+ return;
142
+ }
143
+
144
+ const primaryValue = style[primary];
145
+ const secondaryValue = style[secondary];
146
+ const primaryInvalid = isInvalid(primaryValue);
147
+ const secondaryInvalid = isInvalid(secondaryValue);
148
+
149
+ if (primaryInvalid && !secondaryInvalid) {
150
+ delete style[primary];
151
+ } else if (secondaryInvalid && !primaryInvalid) {
152
+ delete style[secondary];
153
+ } else if (primaryInvalid && secondaryInvalid) {
154
+ // 都是无效值时优先保留 0,否则保留 primary(left/top)
155
+ if (secondaryValue === 0 && primaryValue !== 0) {
156
+ delete style[primary];
157
+ } else {
158
+ delete style[secondary];
159
+ }
160
+ }
161
+ };
162
+
135
163
  export const getInitPositionStyle = (style: Record<string, any> = {}, layout: Layout) => {
136
164
  if (layout === Layout.ABSOLUTE) {
137
165
  const newStyle: Record<string, any> = {
@@ -139,6 +167,9 @@ export const getInitPositionStyle = (style: Record<string, any> = {}, layout: La
139
167
  position: 'absolute',
140
168
  };
141
169
 
170
+ removeConflictPosition(newStyle, 'left', 'right');
171
+ removeConflictPosition(newStyle, 'top', 'bottom');
172
+
142
173
  if (typeof newStyle.left === 'undefined' && typeof newStyle.right === 'undefined') {
143
174
  newStyle.left = 0;
144
175
  }
@@ -178,43 +209,68 @@ export const setLayout = (node: MNode, layout: Layout) => {
178
209
  return node;
179
210
  };
180
211
 
181
- export const change2Fixed = (node: MNode, root: MApp) => {
182
- const style = {
183
- ...(node.style || {}),
184
- };
212
+ type PositionKey = 'left' | 'top' | 'right' | 'bottom';
185
213
 
186
- const path = getNodePath(node.id, root.items);
187
- const offset = {
188
- left: 0,
189
- top: 0,
190
- };
214
+ // 判断 style 是否显式设置了某个定位属性,使用 typeof 判断以避免将 0(如 right: 0)误判为未设置
215
+ const hasPositionValue = (style: Record<string, any> | undefined, key: PositionKey): boolean =>
216
+ typeof style?.[key] !== 'undefined' && style?.[key] !== '';
191
217
 
192
- if (!node.style?.right && isNumber(node.style?.left || 0)) {
193
- for (const value of path) {
194
- if (value.style?.right || !isNumber(value.style?.left || 0)) {
195
- offset.left = 0;
196
- break;
197
- }
198
- offset.left = offset.left + Number(value.style?.left || 0);
218
+ /**
219
+ * 沿节点路径累加(或抵消)某一方向上的定位偏移量
220
+ * 当路径上的祖先使用了反方向定位(例如计算 left 时祖先用了 right)或非数值定位时,
221
+ * 无法简单地通过求和换算坐标,此时返回 0 表示放弃补偿、保持原值
222
+ * @param path 从根到目标节点的路径(含节点自身)
223
+ * @param dir 需要累加的定位方向
224
+ * @param opposite 反方向定位属性
225
+ * @param sign change2Fixed 取 1(累加祖先偏移),Fixed2Other 取 -1(抵消祖先偏移)
226
+ * @param init 偏移量初始值
227
+ */
228
+ const accumulatePositionOffset = (
229
+ path: MNode[],
230
+ dir: PositionKey,
231
+ opposite: PositionKey,
232
+ sign: 1 | -1,
233
+ init = 0,
234
+ ): number => {
235
+ let offset = init;
236
+ for (const value of path) {
237
+ if (hasPositionValue(value.style, opposite) || !isNumber(value.style?.[dir] || 0)) {
238
+ return 0;
199
239
  }
240
+ offset = offset + sign * Number(value.style?.[dir] || 0);
200
241
  }
242
+ return offset;
243
+ };
201
244
 
202
- if (!node.style?.bottom && isNumber(node.style?.top || 0)) {
203
- for (const value of path) {
204
- if (value.style?.bottom || !isNumber(value.style?.top || 0)) {
205
- offset.top = 0;
206
- break;
207
- }
208
- offset.top = offset.top + Number(value.style?.top || 0);
209
- }
210
- }
245
+ export const change2Fixed = (node: MNode, path: MNode[]) => {
246
+ const style = {
247
+ ...(node.style || {}),
248
+ };
211
249
 
212
- if (offset.left) {
213
- style.left = offset.left;
250
+ // 水平方向:以 left 锚定则累加祖先 left,以 right 锚定则累加祖先 right
251
+ if (!hasPositionValue(node.style, 'right') && isNumber(node.style?.left || 0)) {
252
+ const left = accumulatePositionOffset(path, 'left', 'right', 1);
253
+ if (left) {
254
+ style.left = left;
255
+ }
256
+ } else if (hasPositionValue(node.style, 'right') && isNumber(node.style?.right || 0)) {
257
+ const right = accumulatePositionOffset(path, 'right', 'left', 1);
258
+ if (right) {
259
+ style.right = right;
260
+ }
214
261
  }
215
262
 
216
- if (offset.top) {
217
- style.top = offset.top;
263
+ // 垂直方向:以 top 锚定则累加祖先 top,以 bottom 锚定则累加祖先 bottom
264
+ if (!hasPositionValue(node.style, 'bottom') && isNumber(node.style?.top || 0)) {
265
+ const top = accumulatePositionOffset(path, 'top', 'bottom', 1);
266
+ if (top) {
267
+ style.top = top;
268
+ }
269
+ } else if (hasPositionValue(node.style, 'bottom') && isNumber(node.style?.bottom || 0)) {
270
+ const bottom = accumulatePositionOffset(path, 'bottom', 'top', 1);
271
+ if (bottom) {
272
+ style.bottom = bottom;
273
+ }
218
274
  }
219
275
 
220
276
  return style;
@@ -222,34 +278,29 @@ export const change2Fixed = (node: MNode, root: MApp) => {
222
278
 
223
279
  export const Fixed2Other = async (
224
280
  node: MNode,
225
- root: MApp,
281
+ path: MNode[],
226
282
  getLayout: (parent: MNode, node?: MNode) => Promise<Layout>,
227
283
  ) => {
228
- const path = getNodePath(node.id, root.items);
229
284
  const cur = path.pop();
230
285
  const offset = {
231
- left: cur?.style?.left || 0,
232
- top: cur?.style?.top || 0,
286
+ left: 0,
287
+ top: 0,
288
+ right: 0,
289
+ bottom: 0,
233
290
  };
234
291
 
235
- if (!node.style?.right && isNumber(node.style?.left || 0)) {
236
- for (const value of path) {
237
- if (value.style?.right || !isNumber(value.style?.left || 0)) {
238
- offset.left = 0;
239
- break;
240
- }
241
- offset.left = offset.left - Number(value.style?.left || 0);
242
- }
292
+ // 水平方向:抵消祖先在对应锚定方向上的偏移量,初始值为节点自身的偏移
293
+ if (!hasPositionValue(node.style, 'right') && isNumber(node.style?.left || 0)) {
294
+ offset.left = accumulatePositionOffset(path, 'left', 'right', -1, Number(cur?.style?.left || 0));
295
+ } else if (hasPositionValue(node.style, 'right') && isNumber(node.style?.right || 0)) {
296
+ offset.right = accumulatePositionOffset(path, 'right', 'left', -1, Number(cur?.style?.right || 0));
243
297
  }
244
298
 
245
- if (!node.style?.bottom && isNumber(node.style?.top || 0)) {
246
- for (const value of path) {
247
- if (value.style?.bottom || !isNumber(value.style?.top || 0)) {
248
- offset.top = 0;
249
- break;
250
- }
251
- offset.top = offset.top - Number(value.style?.top || 0);
252
- }
299
+ // 垂直方向:同上
300
+ if (!hasPositionValue(node.style, 'bottom') && isNumber(node.style?.top || 0)) {
301
+ offset.top = accumulatePositionOffset(path, 'top', 'bottom', -1, Number(cur?.style?.top || 0));
302
+ } else if (hasPositionValue(node.style, 'bottom') && isNumber(node.style?.bottom || 0)) {
303
+ offset.bottom = accumulatePositionOffset(path, 'bottom', 'top', -1, Number(cur?.style?.bottom || 0));
253
304
  }
254
305
 
255
306
  const style = node.style || {};
@@ -269,6 +320,14 @@ export const Fixed2Other = async (
269
320
  style.top = offset.top;
270
321
  }
271
322
 
323
+ if (offset.right) {
324
+ style.right = offset.right;
325
+ }
326
+
327
+ if (offset.bottom) {
328
+ style.bottom = offset.bottom;
329
+ }
330
+
272
331
  return {
273
332
  ...style,
274
333
  position: 'absolute',
@@ -316,11 +375,18 @@ export const fixNodePosition = (config: MNode, parent: MContainer, stage: StageC
316
375
  return config.style;
317
376
  }
318
377
 
319
- return {
320
- ...(config.style || {}),
321
- top: getMiddleTop(config, parent, stage),
322
- left: fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document),
323
- };
378
+ const style = { ...(config.style || {}) };
379
+ const baseStyle = config.style || {};
380
+
381
+ if ('left' in baseStyle && !('right' in baseStyle)) {
382
+ style.left = fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document);
383
+ }
384
+
385
+ if ('top' in baseStyle && !('bottom' in baseStyle)) {
386
+ style.top = getMiddleTop(config, parent, stage);
387
+ }
388
+
389
+ return style;
324
390
  };
325
391
 
326
392
  // 序列化配置
@@ -460,11 +526,11 @@ export const resolveSelectedNode = (
460
526
  throw new Error('没有ID,无法选中');
461
527
  }
462
528
 
463
- const { node, parent, page } = getNodeInfoFn(id);
529
+ const { node, parent, page, path } = getNodeInfoFn(id);
464
530
  if (!node) throw new Error('获取不到组件信息');
465
531
  if (node.id === rootId) throw new Error('不能选根节点');
466
532
 
467
- return { node, parent, page };
533
+ return { node, parent, page, path };
468
534
  };
469
535
 
470
536
  /**
@@ -479,16 +545,16 @@ export const resolveSelectedNode = (
479
545
  export const toggleFixedPosition = async (
480
546
  dist: MNode,
481
547
  src: MNode,
482
- root: MApp,
548
+ path: MNode[],
483
549
  getLayoutFn: (parent: MNode, node?: MNode | null) => Promise<Layout>,
484
550
  ): Promise<MNode> => {
485
551
  const newConfig = cloneDeep(dist);
486
552
 
487
553
  if (!isPop(src) && newConfig.style?.position) {
488
554
  if (isFixed(newConfig.style) && !isFixed(src.style || {})) {
489
- newConfig.style = change2Fixed(newConfig, root);
555
+ newConfig.style = change2Fixed(newConfig, path);
490
556
  } else if (!isFixed(newConfig.style) && isFixed(src.style || {})) {
491
- newConfig.style = await Fixed2Other(newConfig, root, getLayoutFn);
557
+ newConfig.style = await Fixed2Other(newConfig, path, getLayoutFn);
492
558
  }
493
559
  }
494
560
 
@@ -17,6 +17,7 @@
17
17
  */
18
18
 
19
19
  import { cloneDeep } from 'lodash-es';
20
+ import serialize from 'serialize-javascript';
20
21
 
21
22
  import type { Id } from '@tmagic/core';
22
23
  import type { ChangeRecord } from '@tmagic/form';
@@ -121,8 +122,7 @@ export const markStackSaved = <S extends { saved?: boolean }>(undoRedo?: UndoRed
121
122
 
122
123
  /**
123
124
  * 把单个「按 id 分栈」的历史栈(代码块 / 数据源)拆成若干 group:
124
- * - 把"新增/删除"独立成组(语义上属于一次性事件,不应与 update 合并);
125
- * - 连续 'update' 合并到同一组,组内 steps 顺序就是发生顺序。
125
+ * 每条操作记录独立成组,不做相邻 update 合并(与页面历史的合并策略不同)。
126
126
  *
127
127
  * 代码块与数据源除 `kind` 外结构完全一致,统一由本方法处理;`kind` 决定返回的具体分组类型。
128
128
  */
@@ -139,38 +139,19 @@ export const mergeStackSteps = <S extends BaseStepValue, K extends 'code-block'
139
139
  applied: boolean;
140
140
  isCurrent?: boolean;
141
141
  }[] => {
142
- type Group = {
143
- kind: K;
144
- id: Id;
145
- opType: HistoryOpType;
146
- steps: { step: S; index: number; applied: boolean; isCurrent?: boolean }[];
147
- applied: boolean;
148
- isCurrent?: boolean;
149
- };
150
- const groups: Group[] = [];
151
- let current: Group | null = null;
152
142
  const currentIndex = cursor - 1;
153
- list.forEach((step, index) => {
154
- const { opType } = step;
143
+ return list.map((step, index) => {
155
144
  const applied = index < cursor;
156
145
  const isCurrent = index === currentIndex;
157
- if (opType === 'update' && current?.opType === 'update') {
158
- current.steps.push({ step, index, applied, isCurrent });
159
- current.applied = applied;
160
- if (isCurrent) current.isCurrent = true;
161
- } else {
162
- current = {
163
- kind,
164
- id,
165
- opType,
166
- steps: [{ step, index, applied, isCurrent }],
167
- applied,
168
- isCurrent,
169
- };
170
- groups.push(current);
171
- }
146
+ return {
147
+ kind,
148
+ id,
149
+ opType: step.opType,
150
+ steps: [{ step, index, applied, isCurrent }],
151
+ applied,
152
+ isCurrent,
153
+ };
172
154
  });
173
- return groups;
174
155
  };
175
156
 
176
157
  /**
@@ -254,11 +235,25 @@ export const detectPageTargetName = (step: StepValue): string | undefined => {
254
235
  return undefined;
255
236
  };
256
237
 
257
- /** 把 `Record<Id, UndoRedo>` 整体序列化为 `Record<Id, SerializedUndoRedo>`。 */
258
- export const serializeStacks = <T>(stacks: Record<Id, UndoRedo<T>>) => {
238
+ /**
239
+ * `Record<Id, UndoRedo>` 整体序列化为 `Record<Id, SerializedUndoRedo>`。
240
+ *
241
+ * 序列化(深克隆)的同一趟里,只把每条 step 中可能含函数的 `diff` 用 serialize-javascript 序列化成字符串,
242
+ * 其余字段(uuid / opType / timestamp / `modifiedNodeIds` Map 等)原样保留,交给 IndexedDB 结构化克隆。
243
+ * 这样既能写入函数,又避免序列化整份快照的开销;读取时再由 {@link parseStacksStepDiff} 还原 diff。
244
+ * 不含 `diff` 的元素(如通用栈)原样透传。
245
+ */
246
+ export const serializeStacks = <T extends { diff?: unknown }>(stacks: Record<Id, UndoRedo<T>>) => {
259
247
  const result: Record<Id, ReturnType<UndoRedo<T>['serialize']>> = {};
260
248
  Object.entries(stacks).forEach(([id, undoRedo]) => {
261
- if (undoRedo) result[id] = undoRedo.serialize();
249
+ if (!undoRedo) return;
250
+ const serialized = undoRedo.serialize();
251
+ result[id] = {
252
+ ...serialized,
253
+ elementList: serialized.elementList.map((step) =>
254
+ step.diff === undefined ? step : Object.assign({}, step, { diff: serialize(step.diff) }),
255
+ ),
256
+ };
262
257
  });
263
258
  return result;
264
259
  };
@@ -266,15 +261,27 @@ export const serializeStacks = <T>(stacks: Record<Id, UndoRedo<T>>) => {
266
261
  /**
267
262
  * 把 `Record<Id, SerializedUndoRedo>` 整体还原为 `Record<Id, UndoRedo>`。
268
263
  * 还原时把每个栈的游标定位到最近一条已保存(`saved === true`)记录之后。
264
+ *
265
+ * 与 {@link serializeStacks} 相反:当传入 `parse`(parseDSL)时,把每条 step 中以字符串形式存储的 `diff`
266
+ * 解析回真实对象(含函数);不含 `diff` 的元素(如通用栈)原样透传。
269
267
  */
270
268
  export const deserializeStacks = <T extends { saved?: boolean }>(
271
269
  stacks: Record<Id, ReturnType<UndoRedo<T>['serialize']>> = {},
270
+ parse?: (serialized: string) => unknown,
272
271
  ): Record<Id, UndoRedo<T>> => {
273
272
  const result: Record<Id, UndoRedo<T>> = {};
274
273
  Object.entries(stacks).forEach(([id, serialized]) => {
275
- if (serialized) {
276
- result[id] = UndoRedo.fromSerialized<T>(serialized, { isSavedStep: (element) => element.saved === true });
277
- }
274
+ if (!serialized) return;
275
+ const elementList = parse
276
+ ? serialized.elementList.map((step) => {
277
+ const { diff } = step as { diff?: unknown };
278
+ return typeof diff === 'string' ? Object.assign({}, step, { diff: parse(`(${diff})`) }) : step;
279
+ })
280
+ : serialized.elementList;
281
+ result[id] = UndoRedo.fromSerialized<T>(
282
+ { ...serialized, elementList },
283
+ { isSavedStep: (element) => element.saved === true },
284
+ );
278
285
  });
279
286
  return result;
280
287
  };
@@ -296,3 +303,6 @@ export const getOrCreateStack = <T>(stacks: Record<Id, UndoRedo<T>>, id: Id): Un
296
303
  export const undoFloor = (undoRedo: UndoRedo<StepValue>): number => {
297
304
  return undoRedo.getElementList()[0]?.opType === 'initial' ? 1 : 0;
298
305
  };
306
+
307
+ /** 将单次 push 产生的 history uuid(或 null)转为 *AndGetHistoryId 返回用的 uuid 列表。 */
308
+ export const getLastPushedHistoryIds = (historyId: string | null): string[] => (historyId ? [historyId] : []);