@tmagic/stage 1.1.0-beta.6 → 1.1.0-beta.9

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.
@@ -1,8 +1,8 @@
1
1
  import EventEmitter$1, { EventEmitter } from 'events';
2
2
  import { removeClassName, removeClassNameByClassName, createDiv, injectStyle, isSameDomain, getHost, addClassName } from '@tmagic/utils';
3
+ import KeyController from 'keycon';
3
4
  import Moveable from 'moveable';
4
5
  import MoveableHelper from 'moveable-helper';
5
- import KeyController from 'keycon';
6
6
  import { throttle } from 'lodash-es';
7
7
  import Guides from '@scena/guides';
8
8
 
@@ -38,6 +38,18 @@ var Mode = /* @__PURE__ */ ((Mode2) => {
38
38
  })(Mode || {});
39
39
  const SELECTED_CLASS = "tmagic-stage-selected-area";
40
40
 
41
+ var ContainerHighlightType = /* @__PURE__ */ ((ContainerHighlightType2) => {
42
+ ContainerHighlightType2["DEFAULT"] = "default";
43
+ ContainerHighlightType2["ALT"] = "alt";
44
+ return ContainerHighlightType2;
45
+ })(ContainerHighlightType || {});
46
+ var StageDragStatus = /* @__PURE__ */ ((StageDragStatus2) => {
47
+ StageDragStatus2["START"] = "start";
48
+ StageDragStatus2["ING"] = "ing";
49
+ StageDragStatus2["END"] = "end";
50
+ return StageDragStatus2;
51
+ })(StageDragStatus || {});
52
+
41
53
  const getParents = (el, relative) => {
42
54
  let cur = el.parentElement;
43
55
  const parents = [];
@@ -215,14 +227,27 @@ class StageDragResize extends EventEmitter {
215
227
  elementGuidelines = [];
216
228
  mode = Mode.ABSOLUTE;
217
229
  moveableOptions = {};
218
- dragStatus = "end" /* END */;
230
+ dragStatus = StageDragStatus.END;
219
231
  ghostEl;
220
232
  moveableHelper;
233
+ isContainerHighlight = false;
221
234
  constructor(config) {
222
235
  super();
223
236
  this.core = config.core;
224
237
  this.container = config.container;
225
238
  this.mask = config.mask;
239
+ KeyController.global.keydown("alt", (e) => {
240
+ e.inputEvent.preventDefault();
241
+ this.isContainerHighlight = true;
242
+ });
243
+ KeyController.global.keyup("alt", (e) => {
244
+ e.inputEvent.preventDefault();
245
+ const doc = this.core.renderer.contentWindow?.document;
246
+ if (doc && this.canContainerHighlight()) {
247
+ removeClassNameByClassName(doc, this.core.containerHighlightClassName);
248
+ }
249
+ this.isContainerHighlight = false;
250
+ });
226
251
  }
227
252
  select(el, event) {
228
253
  const oldTarget = this.target;
@@ -287,7 +312,7 @@ class StageDragResize extends EventEmitter {
287
312
  this.moveable?.destroy();
288
313
  this.destroyGhostEl();
289
314
  this.destroyDragEl();
290
- this.dragStatus = "end" /* END */;
315
+ this.dragStatus = StageDragStatus.END;
291
316
  this.removeAllListeners();
292
317
  }
293
318
  init(el) {
@@ -351,7 +376,7 @@ class StageDragResize extends EventEmitter {
351
376
  this.moveable.on("resizeStart", (e) => {
352
377
  if (!this.target)
353
378
  return;
354
- this.dragStatus = "start" /* START */;
379
+ this.dragStatus = StageDragStatus.START;
355
380
  this.moveableHelper?.onResizeStart(e);
356
381
  frame.top = this.target.offsetTop;
357
382
  frame.left = this.target.offsetLeft;
@@ -360,7 +385,7 @@ class StageDragResize extends EventEmitter {
360
385
  if (!this.moveable || !this.target || !this.dragEl)
361
386
  return;
362
387
  const { beforeTranslate } = drag;
363
- this.dragStatus = "ing" /* ING */;
388
+ this.dragStatus = StageDragStatus.ING;
364
389
  this.moveableHelper?.onResize(e);
365
390
  if (this.mode === Mode.SORTABLE) {
366
391
  this.target.style.top = "0px";
@@ -371,7 +396,7 @@ class StageDragResize extends EventEmitter {
371
396
  this.target.style.width = `${width}px`;
372
397
  this.target.style.height = `${height}px`;
373
398
  }).on("resizeEnd", () => {
374
- this.dragStatus = "end" /* END */;
399
+ this.dragStatus = StageDragStatus.END;
375
400
  this.update(true);
376
401
  });
377
402
  }
@@ -388,7 +413,7 @@ class StageDragResize extends EventEmitter {
388
413
  this.moveable.on("dragStart", (e) => {
389
414
  if (!this.target)
390
415
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
391
- this.dragStatus = "start" /* START */;
416
+ this.dragStatus = StageDragStatus.START;
392
417
  this.moveableHelper?.onDragStart(e);
393
418
  if (this.mode === Mode.SORTABLE) {
394
419
  this.ghostEl = this.generateGhostEl(this.target);
@@ -402,8 +427,10 @@ class StageDragResize extends EventEmitter {
402
427
  globalThis.clearTimeout(timeout);
403
428
  timeout = void 0;
404
429
  }
405
- timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
406
- this.dragStatus = "ing" /* ING */;
430
+ if (this.canContainerHighlight()) {
431
+ timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
432
+ }
433
+ this.dragStatus = StageDragStatus.ING;
407
434
  if (this.ghostEl) {
408
435
  this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
409
436
  return;
@@ -417,10 +444,10 @@ class StageDragResize extends EventEmitter {
417
444
  timeout = void 0;
418
445
  }
419
446
  let parentEl = null;
420
- if (doc) {
447
+ if (doc && this.canContainerHighlight()) {
421
448
  parentEl = removeClassNameByClassName(doc, this.core.containerHighlightClassName);
422
449
  }
423
- if (this.dragStatus === "ing" /* ING */) {
450
+ if (this.dragStatus === StageDragStatus.ING) {
424
451
  if (parentEl) {
425
452
  this.update(false, parentEl);
426
453
  } else {
@@ -433,7 +460,7 @@ class StageDragResize extends EventEmitter {
433
460
  }
434
461
  }
435
462
  }
436
- this.dragStatus = "end" /* END */;
463
+ this.dragStatus = StageDragStatus.END;
437
464
  this.destroyGhostEl();
438
465
  });
439
466
  }
@@ -441,17 +468,17 @@ class StageDragResize extends EventEmitter {
441
468
  if (!this.moveable)
442
469
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
443
470
  this.moveable.on("rotateStart", (e) => {
444
- this.dragStatus = "start" /* START */;
471
+ this.dragStatus = StageDragStatus.START;
445
472
  this.moveableHelper?.onRotateStart(e);
446
473
  }).on("rotate", (e) => {
447
474
  if (!this.target || !this.dragEl)
448
475
  return;
449
- this.dragStatus = "ing" /* ING */;
476
+ this.dragStatus = StageDragStatus.ING;
450
477
  this.moveableHelper?.onRotate(e);
451
478
  const frame = this.moveableHelper?.getFrame(e.target);
452
479
  this.target.style.transform = frame?.toCSSObject().transform || "";
453
480
  }).on("rotateEnd", (e) => {
454
- this.dragStatus = "end" /* END */;
481
+ this.dragStatus = StageDragStatus.END;
455
482
  const frame = this.moveableHelper?.getFrame(e.target);
456
483
  this.emit("update", {
457
484
  el: this.target,
@@ -465,17 +492,17 @@ class StageDragResize extends EventEmitter {
465
492
  if (!this.moveable)
466
493
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
467
494
  this.moveable.on("scaleStart", (e) => {
468
- this.dragStatus = "start" /* START */;
495
+ this.dragStatus = StageDragStatus.START;
469
496
  this.moveableHelper?.onScaleStart(e);
470
497
  }).on("scale", (e) => {
471
498
  if (!this.target || !this.dragEl)
472
499
  return;
473
- this.dragStatus = "ing" /* ING */;
500
+ this.dragStatus = StageDragStatus.ING;
474
501
  this.moveableHelper?.onScale(e);
475
502
  const frame = this.moveableHelper?.getFrame(e.target);
476
503
  this.target.style.transform = frame?.toCSSObject().transform || "";
477
504
  }).on("scaleEnd", (e) => {
478
- this.dragStatus = "end" /* END */;
505
+ this.dragStatus = StageDragStatus.END;
479
506
  const frame = this.moveableHelper?.getFrame(e.target);
480
507
  this.emit("update", {
481
508
  el: this.target,
@@ -523,9 +550,13 @@ class StageDragResize extends EventEmitter {
523
550
  top = calcValueByFontsize(doc, this.dragEl.offsetTop) + parseFloat(translateY) - calcValueByFontsize(doc, parentTop);
524
551
  }
525
552
  this.emit("update", {
526
- el: this.target,
527
- parentEl,
528
- style: isResize ? { left, top, width, height } : { left, top }
553
+ data: [
554
+ {
555
+ el: this.target,
556
+ style: isResize ? { left, top, width, height } : { left, top }
557
+ }
558
+ ],
559
+ parentEl
529
560
  });
530
561
  }
531
562
  generateGhostEl(el) {
@@ -616,6 +647,9 @@ class StageDragResize extends EventEmitter {
616
647
  ...moveableOptions
617
648
  };
618
649
  }
650
+ canContainerHighlight() {
651
+ return this.core.containerHighlightType === ContainerHighlightType.DEFAULT || this.core.containerHighlightType === ContainerHighlightType.ALT && this.isContainerHighlight;
652
+ }
619
653
  }
620
654
 
621
655
  class TargetCalibrate extends EventEmitter {
@@ -907,11 +941,13 @@ class StageMask extends Rule {
907
941
  this.content.addEventListener("wheel", this.mouseWheelHandler);
908
942
  this.content.addEventListener("mousemove", this.highlightHandler);
909
943
  this.content.addEventListener("mouseleave", this.mouseLeaveHandler);
910
- KeyController.global.keydown("shift", (e) => {
944
+ const isMac = /mac os x/.test(navigator.userAgent.toLowerCase());
945
+ const ctrl = isMac ? "meta" : "ctrl";
946
+ KeyController.global.keydown(ctrl, (e) => {
911
947
  e.inputEvent.preventDefault();
912
948
  this.isMultiSelectStatus = true;
913
949
  });
914
- KeyController.global.keyup("shift", (e) => {
950
+ KeyController.global.keyup(ctrl, (e) => {
915
951
  e.inputEvent.preventDefault();
916
952
  this.isMultiSelectStatus = false;
917
953
  });
@@ -1100,6 +1136,7 @@ class StageMultiDragResize extends EventEmitter {
1100
1136
  targetList = [];
1101
1137
  dragElList = [];
1102
1138
  moveableForMulti;
1139
+ dragStatus = StageDragStatus.END;
1103
1140
  multiMoveableHelper;
1104
1141
  constructor(config) {
1105
1142
  super();
@@ -1145,6 +1182,7 @@ class StageMultiDragResize extends EventEmitter {
1145
1182
  id: matchEventTarget.id
1146
1183
  });
1147
1184
  });
1185
+ this.dragStatus = StageDragStatus.START;
1148
1186
  }).on("dragGroup", (params) => {
1149
1187
  const { events } = params;
1150
1188
  events.forEach((ev) => {
@@ -1161,18 +1199,29 @@ class StageMultiDragResize extends EventEmitter {
1161
1199
  }
1162
1200
  });
1163
1201
  this.multiMoveableHelper?.onDragGroup(params);
1202
+ this.dragStatus = StageDragStatus.ING;
1164
1203
  }).on("dragGroupEnd", () => {
1165
1204
  this.update();
1205
+ this.dragStatus = StageDragStatus.END;
1206
+ }).on("clickGroup", (params) => {
1207
+ const { inputTarget, targets } = params;
1208
+ if (!this.mask.isMultiSelectStatus && targets.length > 1 && targets.includes(inputTarget)) {
1209
+ this.emit("select", inputTarget.id.replace(DRAG_EL_ID_PREFIX, ""));
1210
+ }
1166
1211
  });
1167
1212
  }
1168
- canSelect(el) {
1213
+ canSelect(el, stop) {
1169
1214
  if (el.className.includes(PAGE_CLASS)) {
1170
1215
  this.core.highlightedDom = void 0;
1171
1216
  this.core.highlightLayer.clearHighlight();
1217
+ stop();
1172
1218
  return false;
1173
1219
  }
1174
1220
  const currentTargetMode = getMode(el);
1175
1221
  let selectedDomMode = "";
1222
+ if (this.core.selectedDom?.className.includes(PAGE_CLASS)) {
1223
+ return true;
1224
+ }
1176
1225
  if (this.targetList.length === 0 && this.core.selectedDom) {
1177
1226
  selectedDomMode = getMode(this.core.selectedDom);
1178
1227
  } else if (this.targetList.length > 0) {
@@ -1205,16 +1254,19 @@ class StageMultiDragResize extends EventEmitter {
1205
1254
  const doc = contentWindow?.document;
1206
1255
  if (!doc)
1207
1256
  return;
1208
- this.targetList.forEach((targetItem) => {
1209
- const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1210
- const left = calcValueByFontsize(doc, offset.left);
1211
- const top = calcValueByFontsize(doc, offset.top);
1212
- const width = calcValueByFontsize(doc, targetItem.clientWidth);
1213
- const height = calcValueByFontsize(doc, targetItem.clientHeight);
1214
- this.emit("update", {
1215
- el: targetItem,
1216
- style: isResize ? { left, top, width, height } : { left, top }
1217
- });
1257
+ this.emit("update", {
1258
+ data: this.targetList.map((targetItem) => {
1259
+ const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1260
+ const left = calcValueByFontsize(doc, offset.left);
1261
+ const top = calcValueByFontsize(doc, offset.top);
1262
+ const width = calcValueByFontsize(doc, targetItem.clientWidth);
1263
+ const height = calcValueByFontsize(doc, targetItem.clientHeight);
1264
+ return {
1265
+ el: targetItem,
1266
+ style: isResize ? { left, top, width, height } : { left, top }
1267
+ };
1268
+ }),
1269
+ parentEl: null
1218
1270
  });
1219
1271
  }
1220
1272
  getOptions(options = {}) {
@@ -1226,13 +1278,21 @@ class StageMultiDragResize extends EventEmitter {
1226
1278
  defaultGroupRotate: 0,
1227
1279
  defaultGroupOrigin: "50% 50%",
1228
1280
  draggable: true,
1229
- resizable: true,
1281
+ resizable: false,
1230
1282
  throttleDrag: 0,
1231
1283
  startDragRotate: 0,
1232
1284
  throttleDragRotate: 0,
1233
1285
  zoom: 1,
1234
1286
  origin: true,
1235
1287
  padding: { left: 0, top: 0, right: 0, bottom: 0 },
1288
+ snappable: true,
1289
+ bounds: {
1290
+ top: 0,
1291
+ left: -1,
1292
+ right: this.container.clientWidth - 1,
1293
+ bottom: this.container.clientHeight,
1294
+ ...multiMoveableOptions.bounds || {}
1295
+ },
1236
1296
  ...options,
1237
1297
  ...multiMoveableOptions
1238
1298
  };
@@ -1340,6 +1400,7 @@ class StageCore extends EventEmitter {
1340
1400
  zoom = DEFAULT_ZOOM;
1341
1401
  containerHighlightClassName;
1342
1402
  containerHighlightDuration;
1403
+ containerHighlightType;
1343
1404
  isContainer;
1344
1405
  canSelect;
1345
1406
  constructor(config) {
@@ -1348,8 +1409,9 @@ class StageCore extends EventEmitter {
1348
1409
  this.setZoom(config.zoom);
1349
1410
  this.canSelect = config.canSelect || ((el) => !!el.id);
1350
1411
  this.isContainer = config.isContainer;
1351
- this.containerHighlightClassName = config.containerHighlightClassName;
1352
- this.containerHighlightDuration = config.containerHighlightDuration;
1412
+ this.containerHighlightClassName = config.containerHighlightClassName || CONTAINER_HIGHLIGHT_CLASS;
1413
+ this.containerHighlightDuration = config.containerHighlightDuration || 800;
1414
+ this.containerHighlightType = config.containerHighlightType;
1353
1415
  this.renderer = new StageRender({ core: this });
1354
1416
  this.mask = new StageMask({ core: this });
1355
1417
  this.dr = new StageDragResize({ core: this, container: this.mask.content, mask: this.mask });
@@ -1376,6 +1438,8 @@ class StageCore extends EventEmitter {
1376
1438
  const el = await this.getElementFromPoint(event);
1377
1439
  if (!el)
1378
1440
  return;
1441
+ if (this.multiDr.dragStatus === StageDragStatus.ING)
1442
+ return;
1379
1443
  await this.highlight(el);
1380
1444
  if (this.highlightedDom === this.selectedDom) {
1381
1445
  this.highlightLayer.clearHighlight();
@@ -1407,6 +1471,10 @@ class StageCore extends EventEmitter {
1407
1471
  });
1408
1472
  this.multiDr.on("update", (data) => {
1409
1473
  setTimeout(() => this.emit("update", data));
1474
+ }).on("select", async (id) => {
1475
+ const el = await this.getTargetElement(id);
1476
+ this.select(el);
1477
+ setTimeout(() => this.emit("select", el));
1410
1478
  });
1411
1479
  }
1412
1480
  getElementsFromPoint(event) {
@@ -1440,7 +1508,7 @@ class StageCore extends EventEmitter {
1440
1508
  if (!canSelectByProp)
1441
1509
  return false;
1442
1510
  if (this.mask.isMultiSelectStatus) {
1443
- return this.multiDr.canSelect(el);
1511
+ return this.multiDr.canSelect(el, stop);
1444
1512
  }
1445
1513
  return true;
1446
1514
  }
@@ -1569,5 +1637,5 @@ class StageCore extends EventEmitter {
1569
1637
  }
1570
1638
  }
1571
1639
 
1572
- export { CONTAINER_HIGHLIGHT_CLASS, DEFAULT_ZOOM, DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, GuidesType, HIGHLIGHT_EL_ID_PREFIX, Mode, MouseButton, PAGE_CLASS, SELECTED_CLASS, StageDragResize, StageMask, StageRender, ZIndex, addSelectedClassName, calcValueByFontsize, StageCore as default, down, getAbsolutePosition, getMode, getOffset, getScrollParent, getTargetElStyle, isAbsolute, isFixed, isFixedParent, isRelative, isStatic, removeSelectedClassName, up };
1640
+ export { CONTAINER_HIGHLIGHT_CLASS, ContainerHighlightType, DEFAULT_ZOOM, DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, GuidesType, HIGHLIGHT_EL_ID_PREFIX, Mode, MouseButton, PAGE_CLASS, SELECTED_CLASS, StageDragResize, StageDragStatus, StageMask, StageRender, ZIndex, addSelectedClassName, calcValueByFontsize, StageCore as default, down, getAbsolutePosition, getMode, getOffset, getScrollParent, getTargetElStyle, isAbsolute, isFixed, isFixedParent, isRelative, isStatic, removeSelectedClassName, up };
1573
1641
  //# sourceMappingURL=tmagic-stage.mjs.map