@tmagic/stage 1.4.0-beta.1 → 1.4.0-beta.2

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.
@@ -44,6 +44,27 @@ var AbleActionEventType = /* @__PURE__ */ ((AbleActionEventType2) => {
44
44
  AbleActionEventType2["REMOVE"] = "remove";
45
45
  return AbleActionEventType2;
46
46
  })(AbleActionEventType || {});
47
+ var ContainerHighlightType = /* @__PURE__ */ ((ContainerHighlightType2) => {
48
+ ContainerHighlightType2["DEFAULT"] = "default";
49
+ ContainerHighlightType2["ALT"] = "alt";
50
+ return ContainerHighlightType2;
51
+ })(ContainerHighlightType || {});
52
+ var RenderType = /* @__PURE__ */ ((RenderType2) => {
53
+ RenderType2["IFRAME"] = "iframe";
54
+ RenderType2["NATIVE"] = "native";
55
+ return RenderType2;
56
+ })(RenderType || {});
57
+ var SelectStatus = /* @__PURE__ */ ((SelectStatus2) => {
58
+ SelectStatus2["SELECT"] = "select";
59
+ SelectStatus2["MULTI_SELECT"] = "multiSelect";
60
+ return SelectStatus2;
61
+ })(SelectStatus || {});
62
+ var StageDragStatus = /* @__PURE__ */ ((StageDragStatus2) => {
63
+ StageDragStatus2["START"] = "start";
64
+ StageDragStatus2["ING"] = "ing";
65
+ StageDragStatus2["END"] = "end";
66
+ return StageDragStatus2;
67
+ })(StageDragStatus || {});
47
68
 
48
69
  const getParents = (el, relative) => {
49
70
  let cur = el.parentElement;
@@ -899,31 +920,9 @@ class MoveableOptionsManager extends EventEmitter {
899
920
  }
900
921
  }
901
922
 
902
- var ContainerHighlightType = /* @__PURE__ */ ((ContainerHighlightType2) => {
903
- ContainerHighlightType2["DEFAULT"] = "default";
904
- ContainerHighlightType2["ALT"] = "alt";
905
- return ContainerHighlightType2;
906
- })(ContainerHighlightType || {});
907
- var RenderType = /* @__PURE__ */ ((RenderType2) => {
908
- RenderType2["IFRAME"] = "iframe";
909
- RenderType2["NATIVE"] = "native";
910
- return RenderType2;
911
- })(RenderType || {});
912
- var SelectStatus = /* @__PURE__ */ ((SelectStatus2) => {
913
- SelectStatus2["SELECT"] = "select";
914
- SelectStatus2["MULTI_SELECT"] = "multiSelect";
915
- return SelectStatus2;
916
- })(SelectStatus || {});
917
- var StageDragStatus = /* @__PURE__ */ ((StageDragStatus2) => {
918
- StageDragStatus2["START"] = "start";
919
- StageDragStatus2["ING"] = "ing";
920
- StageDragStatus2["END"] = "end";
921
- return StageDragStatus2;
922
- })(StageDragStatus || {});
923
-
924
923
  class StageDragResize extends MoveableOptionsManager {
925
924
  /** 目标节点 */
926
- target;
925
+ target = null;
927
926
  /** Moveable拖拽类实例 */
928
927
  moveable;
929
928
  /** 拖动状态 */
@@ -956,6 +955,11 @@ class StageDragResize extends MoveableOptionsManager {
956
955
  * @param event 鼠标事件
957
956
  */
958
957
  select(el, event) {
958
+ if (!el) {
959
+ this.moveable?.destroy();
960
+ this.moveable = void 0;
961
+ return;
962
+ }
959
963
  if (!this.moveable || el !== this.target) {
960
964
  this.initMoveable(el);
961
965
  } else {
@@ -998,6 +1002,12 @@ class StageDragResize extends MoveableOptionsManager {
998
1002
  this.dragStatus = StageDragStatus.END;
999
1003
  this.removeAllListeners();
1000
1004
  }
1005
+ on(eventName, listener) {
1006
+ return super.on(eventName, listener);
1007
+ }
1008
+ emit(eventName, ...args) {
1009
+ return super.emit(eventName, ...args);
1010
+ }
1001
1011
  init(el) {
1002
1012
  if (/(auto|scroll)/.test(el.style.overflow)) {
1003
1013
  el.style.overflow = "hidden";
@@ -1098,16 +1108,19 @@ class StageDragResize extends MoveableOptionsManager {
1098
1108
  }).on("rotateEnd", (e) => {
1099
1109
  this.dragStatus = StageDragStatus.END;
1100
1110
  const frame = this.dragResizeHelper?.getFrame(e.target);
1101
- this.emit("update", {
1102
- data: [
1103
- {
1104
- el: this.target,
1105
- style: {
1106
- transform: frame?.get("transform")
1111
+ if (this.target && frame) {
1112
+ this.emit("update", {
1113
+ data: [
1114
+ {
1115
+ el: this.target,
1116
+ style: {
1117
+ transform: frame.get("transform")
1118
+ }
1107
1119
  }
1108
- }
1109
- ]
1110
- });
1120
+ ],
1121
+ parentEl: null
1122
+ });
1123
+ }
1111
1124
  });
1112
1125
  }
1113
1126
  bindScaleEvent() {
@@ -1124,16 +1137,19 @@ class StageDragResize extends MoveableOptionsManager {
1124
1137
  }).on("scaleEnd", (e) => {
1125
1138
  this.dragStatus = StageDragStatus.END;
1126
1139
  const frame = this.dragResizeHelper.getFrame(e.target);
1127
- this.emit("update", {
1128
- data: [
1129
- {
1130
- el: this.target,
1131
- style: {
1132
- transform: frame?.get("transform")
1140
+ if (this.target && frame) {
1141
+ this.emit("update", {
1142
+ data: [
1143
+ {
1144
+ el: this.target,
1145
+ style: {
1146
+ transform: frame.get("transform")
1147
+ }
1133
1148
  }
1134
- }
1135
- ]
1136
- });
1149
+ ],
1150
+ parentEl: null
1151
+ });
1152
+ }
1137
1153
  });
1138
1154
  }
1139
1155
  sort() {
@@ -1368,6 +1384,12 @@ class StageMultiDragResize extends MoveableOptionsManager {
1368
1384
  this.moveableForMulti?.destroy();
1369
1385
  this.dragResizeHelper.destroy();
1370
1386
  }
1387
+ on(eventName, listener) {
1388
+ return super.on(eventName, listener);
1389
+ }
1390
+ emit(eventName, ...args) {
1391
+ return super.emit(eventName, ...args);
1392
+ }
1371
1393
  /**
1372
1394
  * 拖拽完成后将更新的位置信息暴露给上层业务方,业务方可以接收事件进行保存
1373
1395
  * @param isResize 是否进行大小缩放
@@ -1398,7 +1420,7 @@ class ActionManager extends EventEmitter {
1398
1420
  /** 单选、多选、高亮的容器(蒙层的content) */
1399
1421
  container;
1400
1422
  /** 当前选中的节点 */
1401
- selectedEl;
1423
+ selectedEl = null;
1402
1424
  /** 多选选中的节点组 */
1403
1425
  selectedElList = [];
1404
1426
  /** 当前高亮的节点 */
@@ -1429,7 +1451,7 @@ class ActionManager extends EventEmitter {
1429
1451
  return;
1430
1452
  }
1431
1453
  this.emit("mousemove", event);
1432
- this.highlight(el);
1454
+ this.highlight(el.id);
1433
1455
  }, throttleTime);
1434
1456
  constructor(config) {
1435
1457
  super();
@@ -1533,6 +1555,7 @@ class ActionManager extends EventEmitter {
1533
1555
  return el;
1534
1556
  }
1535
1557
  }
1558
+ return null;
1536
1559
  }
1537
1560
  /**
1538
1561
  * 判断一个元素能否在当前场景被选中
@@ -1569,8 +1592,14 @@ class ActionManager extends EventEmitter {
1569
1592
  this.clearSelectStatus(SelectStatus.MULTI_SELECT);
1570
1593
  this.dr.select(el, event);
1571
1594
  }
1572
- multiSelect(idOrElList) {
1573
- this.selectedElList = idOrElList.map((idOrEl) => this.getTargetElement(idOrEl));
1595
+ multiSelect(ids) {
1596
+ this.selectedElList = [];
1597
+ ids.forEach((id) => {
1598
+ const el = this.getTargetElement(id);
1599
+ if (el) {
1600
+ this.selectedElList.push(el);
1601
+ }
1602
+ });
1574
1603
  this.clearSelectStatus(SelectStatus.SELECT);
1575
1604
  this.multiDr?.multiSelect(this.selectedElList);
1576
1605
  }
@@ -1580,10 +1609,10 @@ class ActionManager extends EventEmitter {
1580
1609
  setHighlightEl(el) {
1581
1610
  this.highlightedEl = el;
1582
1611
  }
1583
- highlight(idOrEl) {
1612
+ highlight(id) {
1584
1613
  let el;
1585
1614
  try {
1586
- el = this.getTargetElement(idOrEl);
1615
+ el = this.getTargetElement(id);
1587
1616
  } catch (error) {
1588
1617
  this.clearHighlight();
1589
1618
  return;
@@ -1659,6 +1688,12 @@ class ActionManager extends EventEmitter {
1659
1688
  this.multiDr?.destroy();
1660
1689
  this.highlightLayer.destroy();
1661
1690
  }
1691
+ on(eventName, listener) {
1692
+ return super.on(eventName, listener);
1693
+ }
1694
+ emit(eventName, ...args) {
1695
+ return super.emit(eventName, ...args);
1696
+ }
1662
1697
  createDr(config) {
1663
1698
  const createDrHelper = () => new DragResizeHelper({
1664
1699
  container: config.container,
@@ -1678,9 +1713,9 @@ class ActionManager extends EventEmitter {
1678
1713
  setTimeout(() => this.emit("update", data));
1679
1714
  }).on("sort", (data) => {
1680
1715
  setTimeout(() => this.emit("sort", data));
1681
- }).on("select-parent", () => {
1716
+ }).on(AbleActionEventType.SELECT_PARENT, () => {
1682
1717
  this.emit("select-parent");
1683
- }).on("remove", () => {
1718
+ }).on(AbleActionEventType.REMOVE, () => {
1684
1719
  const drTarget = this.dr.getTarget();
1685
1720
  if (!drTarget)
1686
1721
  return;
@@ -1709,11 +1744,10 @@ class ActionManager extends EventEmitter {
1709
1744
  });
1710
1745
  multiDr?.on("update", (data) => {
1711
1746
  this.emit("multi-update", data);
1712
- }).on("change-to-select", async (id, e) => {
1747
+ }).on("change-to-select", (id, e) => {
1713
1748
  if (this.isMultiSelectStatus)
1714
- return false;
1715
- const el = this.getTargetElement(id);
1716
- this.emit("change-to-select", el, e);
1749
+ return;
1750
+ this.emit("change-to-select", id, e);
1717
1751
  });
1718
1752
  return multiDr;
1719
1753
  }
@@ -1747,11 +1781,13 @@ class ActionManager extends EventEmitter {
1747
1781
  return;
1748
1782
  if (this.selectedEl && !this.selectedEl.className.includes(PAGE_CLASS)) {
1749
1783
  this.selectedElList.push(this.selectedEl);
1750
- this.setSelectedEl(void 0);
1784
+ this.setSelectedEl(null);
1751
1785
  }
1752
1786
  const existIndex = this.selectedElList.findIndex((selectedDom) => selectedDom.id === el.id);
1753
1787
  if (existIndex !== -1) {
1754
- this.selectedElList.splice(existIndex, 1);
1788
+ if (this.selectedElList.length > 1) {
1789
+ this.selectedElList.splice(existIndex, 1);
1790
+ }
1755
1791
  } else {
1756
1792
  this.selectedElList.push(el);
1757
1793
  }
@@ -2138,6 +2174,12 @@ class StageMask extends Rule {
2138
2174
  this.wrapperResizeObserver?.disconnect();
2139
2175
  super.destroy();
2140
2176
  }
2177
+ on(eventName, listener) {
2178
+ return super.on(eventName, listener);
2179
+ }
2180
+ emit(eventName, ...args) {
2181
+ return super.emit(eventName, ...args);
2182
+ }
2141
2183
  /**
2142
2184
  * 监听选中元素是否在画布可视区域内,如果目标元素不在可视区域内,通过滚动使该元素出现在可视区域
2143
2185
  */
@@ -2313,14 +2355,11 @@ class StageRender extends EventEmitter$1 {
2313
2355
  const runtime = await this.getRuntime();
2314
2356
  runtime?.update?.(data);
2315
2357
  }
2316
- async select(els) {
2358
+ async select(ids) {
2317
2359
  const runtime = await this.getRuntime();
2318
- for (const el of els) {
2319
- await runtime?.select?.(el.id);
2320
- if (runtime?.beforeSelect) {
2321
- await runtime.beforeSelect(el);
2322
- }
2323
- this.flagSelectedEl(el);
2360
+ for (const id of ids) {
2361
+ await runtime?.select?.(id);
2362
+ this.flagSelectedEl(this.getTargetElement(id));
2324
2363
  }
2325
2364
  }
2326
2365
  setZoom(zoom = DEFAULT_ZOOM) {
@@ -2376,14 +2415,8 @@ class StageRender extends EventEmitter$1 {
2376
2415
  }
2377
2416
  return this.getDocument()?.elementsFromPoint(x / this.zoom, y / this.zoom);
2378
2417
  }
2379
- getTargetElement(idOrEl) {
2380
- if (typeof idOrEl === "string" || typeof idOrEl === "number") {
2381
- const el = this.getDocument()?.getElementById(`${idOrEl}`);
2382
- if (!el)
2383
- throw new Error(`不存在ID为${idOrEl}的元素`);
2384
- return el;
2385
- }
2386
- return idOrEl;
2418
+ getTargetElement(id) {
2419
+ return this.getDocument()?.getElementById(`${id}`) || null;
2387
2420
  }
2388
2421
  /**
2389
2422
  * 销毁实例
@@ -2395,6 +2428,12 @@ class StageRender extends EventEmitter$1 {
2395
2428
  this.iframe = void 0;
2396
2429
  this.removeAllListeners();
2397
2430
  }
2431
+ on(eventName, listener) {
2432
+ return super.on(eventName, listener);
2433
+ }
2434
+ emit(eventName, ...args) {
2435
+ return super.emit(eventName, ...args);
2436
+ }
2398
2437
  createIframe() {
2399
2438
  this.iframe = globalThis.document.createElement("iframe");
2400
2439
  this.iframe.src = this.runtimeUrl && isSameDomain(this.runtimeUrl) ? this.runtimeUrl : "";
@@ -2425,7 +2464,7 @@ class StageRender extends EventEmitter$1 {
2425
2464
  const doc = this.getDocument();
2426
2465
  if (doc) {
2427
2466
  removeSelectedClassName(doc);
2428
- addSelectedClassName(el, doc);
2467
+ el && addSelectedClassName(el, doc);
2429
2468
  }
2430
2469
  }
2431
2470
  iframeLoadHandler = async () => {
@@ -2488,33 +2527,33 @@ class StageCore extends EventEmitter$1 {
2488
2527
  }
2489
2528
  /**
2490
2529
  * 单选选中元素
2491
- * @param idOrEl 选中的id或者元素
2530
+ * @param id 选中的id
2492
2531
  */
2493
- async select(idOrEl, event) {
2494
- const el = this.renderer.getTargetElement(idOrEl);
2532
+ async select(id, event) {
2533
+ const el = this.renderer.getTargetElement(id);
2495
2534
  if (el === this.actionManager.getSelectedEl())
2496
2535
  return;
2497
- await this.renderer.select([el]);
2498
- this.mask.setLayout(el);
2536
+ await this.renderer.select([id]);
2537
+ el && this.mask.setLayout(el);
2499
2538
  this.actionManager.select(el, event);
2500
- if (this.autoScrollIntoView || el.dataset.autoScrollIntoView) {
2539
+ if (el && (this.autoScrollIntoView || el.dataset.autoScrollIntoView)) {
2501
2540
  this.mask.observerIntersection(el);
2502
2541
  }
2503
2542
  }
2504
2543
  /**
2505
2544
  * 多选选中多个元素
2506
- * @param idOrElList 选中元素的id或元素列表
2545
+ * @param ids 选中元素的id列表
2507
2546
  */
2508
- async multiSelect(idOrElList) {
2509
- const els = idOrElList.map((idOrEl) => this.renderer.getTargetElement(idOrEl));
2547
+ async multiSelect(ids) {
2548
+ const els = ids.map((id) => this.renderer.getTargetElement(id)).filter((el) => Boolean(el));
2510
2549
  if (els.length === 0)
2511
2550
  return;
2512
2551
  const lastEl = els[els.length - 1];
2513
2552
  const isReduceSelect = els.length < this.actionManager.getSelectedElList().length;
2514
- await this.renderer.select(els);
2515
- this.mask.setLayout(lastEl);
2516
- this.actionManager.multiSelect(idOrElList);
2517
- if ((this.autoScrollIntoView || lastEl.dataset.autoScrollIntoView) && !isReduceSelect) {
2553
+ await this.renderer.select(ids);
2554
+ lastEl && this.mask.setLayout(lastEl);
2555
+ this.actionManager.multiSelect(ids);
2556
+ if (lastEl && (this.autoScrollIntoView || lastEl.dataset.autoScrollIntoView) && !isReduceSelect) {
2518
2557
  this.mask.observerIntersection(lastEl);
2519
2558
  }
2520
2559
  }
@@ -2522,8 +2561,8 @@ class StageCore extends EventEmitter$1 {
2522
2561
  * 高亮选中元素
2523
2562
  * @param el 要高亮的元素
2524
2563
  */
2525
- highlight(idOrEl) {
2526
- this.actionManager.highlight(idOrEl);
2564
+ highlight(id) {
2565
+ this.actionManager.highlight(id);
2527
2566
  }
2528
2567
  clearHighlight() {
2529
2568
  this.actionManager.clearHighlight();
@@ -2619,6 +2658,12 @@ class StageCore extends EventEmitter$1 {
2619
2658
  this.removeAllListeners();
2620
2659
  this.container = void 0;
2621
2660
  }
2661
+ on(eventName, listener) {
2662
+ return super.on(eventName, listener);
2663
+ }
2664
+ emit(eventName, ...args) {
2665
+ return super.emit(eventName, ...args);
2666
+ }
2622
2667
  /**
2623
2668
  * 监听页面大小变化
2624
2669
  */
@@ -2648,7 +2693,7 @@ class StageCore extends EventEmitter$1 {
2648
2693
  updateDragEl: config.updateDragEl,
2649
2694
  getRootContainer: () => this.container,
2650
2695
  getRenderDocument: () => this.renderer.getDocument(),
2651
- getTargetElement: (idOrEl) => this.renderer.getTargetElement(idOrEl),
2696
+ getTargetElement: (id) => this.renderer.getTargetElement(id),
2652
2697
  getElementsFromPoint: (point) => this.renderer.getElementsFromPoint(point)
2653
2698
  };
2654
2699
  return actionManagerConfig;
@@ -2683,12 +2728,12 @@ class StageCore extends EventEmitter$1 {
2683
2728
  * 初始化ActionManager类本身抛出来的事件监听
2684
2729
  */
2685
2730
  initActionManagerEvent() {
2686
- this.actionManager.on("before-select", (idOrEl, event) => {
2687
- this.select(idOrEl, event);
2731
+ this.actionManager.on("before-select", (el, event) => {
2732
+ this.select(el.id, event);
2688
2733
  }).on("select", (selectedEl, event) => {
2689
2734
  this.emit("select", selectedEl, event);
2690
- }).on("before-multi-select", (idOrElList) => {
2691
- this.multiSelect(idOrElList);
2735
+ }).on("before-multi-select", (els) => {
2736
+ this.multiSelect(els.map((el) => el.id));
2692
2737
  }).on("multi-select", (selectedElList, event) => {
2693
2738
  this.emit("multi-select", selectedElList, event);
2694
2739
  }).on("dblclick", (event) => {
@@ -2713,9 +2758,12 @@ class StageCore extends EventEmitter$1 {
2713
2758
  * 初始化MultiDragResize类通过ActionManager抛出来的事件监听
2714
2759
  */
2715
2760
  initMulDrEvent() {
2716
- this.actionManager.on("change-to-select", (el, e) => {
2717
- this.select(el);
2718
- setTimeout(() => this.emit("select", el, e));
2761
+ this.actionManager.on("change-to-select", (id, e) => {
2762
+ this.select(id);
2763
+ setTimeout(() => {
2764
+ const el = this.renderer.getTargetElement(id);
2765
+ el && this.emit("select", el, e);
2766
+ });
2719
2767
  }).on("multi-update", (data) => {
2720
2768
  this.emit("update", data);
2721
2769
  });
@@ -2724,7 +2772,7 @@ class StageCore extends EventEmitter$1 {
2724
2772
  * 初始化Highlight类通过ActionManager抛出来的事件监听
2725
2773
  */
2726
2774
  initHighlightEvent() {
2727
- this.actionManager.on("highlight", async (highlightEl) => {
2775
+ this.actionManager.on("highlight", (highlightEl) => {
2728
2776
  this.emit("highlight", highlightEl);
2729
2777
  });
2730
2778
  }