@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,15 +1,15 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('events'), require('@tmagic/utils'), require('moveable'), require('moveable-helper'), require('keycon'), require('lodash-es'), require('@scena/guides')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'events', '@tmagic/utils', 'moveable', 'moveable-helper', 'keycon', 'lodash-es', '@scena/guides'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TMagicStage = {}, global.EventEmitter, global.utils, global.Moveable, global.MoveableHelper, global.KeyController, global.lodashEs, global.Guides));
5
- })(this, (function (exports, EventEmitter, utils, Moveable, MoveableHelper, KeyController, lodashEs, Guides) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('events'), require('@tmagic/utils'), require('keycon'), require('moveable'), require('moveable-helper'), require('lodash-es'), require('@scena/guides')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'events', '@tmagic/utils', 'keycon', 'moveable', 'moveable-helper', 'lodash-es', '@scena/guides'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TMagicStage = {}, global.EventEmitter, global.utils, global.KeyController, global.Moveable, global.MoveableHelper, global.lodashEs, global.Guides));
5
+ })(this, (function (exports, EventEmitter, utils, KeyController, Moveable, MoveableHelper, lodashEs, Guides) { 'use strict';
6
6
 
7
7
  const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
8
8
 
9
9
  const EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
10
+ const KeyController__default = /*#__PURE__*/_interopDefaultLegacy(KeyController);
10
11
  const Moveable__default = /*#__PURE__*/_interopDefaultLegacy(Moveable);
11
12
  const MoveableHelper__default = /*#__PURE__*/_interopDefaultLegacy(MoveableHelper);
12
- const KeyController__default = /*#__PURE__*/_interopDefaultLegacy(KeyController);
13
13
  const Guides__default = /*#__PURE__*/_interopDefaultLegacy(Guides);
14
14
 
15
15
  const GHOST_EL_ID_PREFIX = "ghost_el_";
@@ -44,6 +44,18 @@
44
44
  })(Mode || {});
45
45
  const SELECTED_CLASS = "tmagic-stage-selected-area";
46
46
 
47
+ var ContainerHighlightType = /* @__PURE__ */ ((ContainerHighlightType2) => {
48
+ ContainerHighlightType2["DEFAULT"] = "default";
49
+ ContainerHighlightType2["ALT"] = "alt";
50
+ return ContainerHighlightType2;
51
+ })(ContainerHighlightType || {});
52
+ var StageDragStatus = /* @__PURE__ */ ((StageDragStatus2) => {
53
+ StageDragStatus2["START"] = "start";
54
+ StageDragStatus2["ING"] = "ing";
55
+ StageDragStatus2["END"] = "end";
56
+ return StageDragStatus2;
57
+ })(StageDragStatus || {});
58
+
47
59
  const getParents = (el, relative) => {
48
60
  let cur = el.parentElement;
49
61
  const parents = [];
@@ -221,14 +233,27 @@
221
233
  elementGuidelines = [];
222
234
  mode = Mode.ABSOLUTE;
223
235
  moveableOptions = {};
224
- dragStatus = "end" /* END */;
236
+ dragStatus = StageDragStatus.END;
225
237
  ghostEl;
226
238
  moveableHelper;
239
+ isContainerHighlight = false;
227
240
  constructor(config) {
228
241
  super();
229
242
  this.core = config.core;
230
243
  this.container = config.container;
231
244
  this.mask = config.mask;
245
+ KeyController__default.default.global.keydown("alt", (e) => {
246
+ e.inputEvent.preventDefault();
247
+ this.isContainerHighlight = true;
248
+ });
249
+ KeyController__default.default.global.keyup("alt", (e) => {
250
+ e.inputEvent.preventDefault();
251
+ const doc = this.core.renderer.contentWindow?.document;
252
+ if (doc && this.canContainerHighlight()) {
253
+ utils.removeClassNameByClassName(doc, this.core.containerHighlightClassName);
254
+ }
255
+ this.isContainerHighlight = false;
256
+ });
232
257
  }
233
258
  select(el, event) {
234
259
  const oldTarget = this.target;
@@ -293,7 +318,7 @@
293
318
  this.moveable?.destroy();
294
319
  this.destroyGhostEl();
295
320
  this.destroyDragEl();
296
- this.dragStatus = "end" /* END */;
321
+ this.dragStatus = StageDragStatus.END;
297
322
  this.removeAllListeners();
298
323
  }
299
324
  init(el) {
@@ -357,7 +382,7 @@
357
382
  this.moveable.on("resizeStart", (e) => {
358
383
  if (!this.target)
359
384
  return;
360
- this.dragStatus = "start" /* START */;
385
+ this.dragStatus = StageDragStatus.START;
361
386
  this.moveableHelper?.onResizeStart(e);
362
387
  frame.top = this.target.offsetTop;
363
388
  frame.left = this.target.offsetLeft;
@@ -366,7 +391,7 @@
366
391
  if (!this.moveable || !this.target || !this.dragEl)
367
392
  return;
368
393
  const { beforeTranslate } = drag;
369
- this.dragStatus = "ing" /* ING */;
394
+ this.dragStatus = StageDragStatus.ING;
370
395
  this.moveableHelper?.onResize(e);
371
396
  if (this.mode === Mode.SORTABLE) {
372
397
  this.target.style.top = "0px";
@@ -377,7 +402,7 @@
377
402
  this.target.style.width = `${width}px`;
378
403
  this.target.style.height = `${height}px`;
379
404
  }).on("resizeEnd", () => {
380
- this.dragStatus = "end" /* END */;
405
+ this.dragStatus = StageDragStatus.END;
381
406
  this.update(true);
382
407
  });
383
408
  }
@@ -394,7 +419,7 @@
394
419
  this.moveable.on("dragStart", (e) => {
395
420
  if (!this.target)
396
421
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
397
- this.dragStatus = "start" /* START */;
422
+ this.dragStatus = StageDragStatus.START;
398
423
  this.moveableHelper?.onDragStart(e);
399
424
  if (this.mode === Mode.SORTABLE) {
400
425
  this.ghostEl = this.generateGhostEl(this.target);
@@ -408,8 +433,10 @@
408
433
  globalThis.clearTimeout(timeout);
409
434
  timeout = void 0;
410
435
  }
411
- timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
412
- this.dragStatus = "ing" /* ING */;
436
+ if (this.canContainerHighlight()) {
437
+ timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
438
+ }
439
+ this.dragStatus = StageDragStatus.ING;
413
440
  if (this.ghostEl) {
414
441
  this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
415
442
  return;
@@ -423,10 +450,10 @@
423
450
  timeout = void 0;
424
451
  }
425
452
  let parentEl = null;
426
- if (doc) {
453
+ if (doc && this.canContainerHighlight()) {
427
454
  parentEl = utils.removeClassNameByClassName(doc, this.core.containerHighlightClassName);
428
455
  }
429
- if (this.dragStatus === "ing" /* ING */) {
456
+ if (this.dragStatus === StageDragStatus.ING) {
430
457
  if (parentEl) {
431
458
  this.update(false, parentEl);
432
459
  } else {
@@ -439,7 +466,7 @@
439
466
  }
440
467
  }
441
468
  }
442
- this.dragStatus = "end" /* END */;
469
+ this.dragStatus = StageDragStatus.END;
443
470
  this.destroyGhostEl();
444
471
  });
445
472
  }
@@ -447,17 +474,17 @@
447
474
  if (!this.moveable)
448
475
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
449
476
  this.moveable.on("rotateStart", (e) => {
450
- this.dragStatus = "start" /* START */;
477
+ this.dragStatus = StageDragStatus.START;
451
478
  this.moveableHelper?.onRotateStart(e);
452
479
  }).on("rotate", (e) => {
453
480
  if (!this.target || !this.dragEl)
454
481
  return;
455
- this.dragStatus = "ing" /* ING */;
482
+ this.dragStatus = StageDragStatus.ING;
456
483
  this.moveableHelper?.onRotate(e);
457
484
  const frame = this.moveableHelper?.getFrame(e.target);
458
485
  this.target.style.transform = frame?.toCSSObject().transform || "";
459
486
  }).on("rotateEnd", (e) => {
460
- this.dragStatus = "end" /* END */;
487
+ this.dragStatus = StageDragStatus.END;
461
488
  const frame = this.moveableHelper?.getFrame(e.target);
462
489
  this.emit("update", {
463
490
  el: this.target,
@@ -471,17 +498,17 @@
471
498
  if (!this.moveable)
472
499
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
473
500
  this.moveable.on("scaleStart", (e) => {
474
- this.dragStatus = "start" /* START */;
501
+ this.dragStatus = StageDragStatus.START;
475
502
  this.moveableHelper?.onScaleStart(e);
476
503
  }).on("scale", (e) => {
477
504
  if (!this.target || !this.dragEl)
478
505
  return;
479
- this.dragStatus = "ing" /* ING */;
506
+ this.dragStatus = StageDragStatus.ING;
480
507
  this.moveableHelper?.onScale(e);
481
508
  const frame = this.moveableHelper?.getFrame(e.target);
482
509
  this.target.style.transform = frame?.toCSSObject().transform || "";
483
510
  }).on("scaleEnd", (e) => {
484
- this.dragStatus = "end" /* END */;
511
+ this.dragStatus = StageDragStatus.END;
485
512
  const frame = this.moveableHelper?.getFrame(e.target);
486
513
  this.emit("update", {
487
514
  el: this.target,
@@ -529,9 +556,13 @@
529
556
  top = calcValueByFontsize(doc, this.dragEl.offsetTop) + parseFloat(translateY) - calcValueByFontsize(doc, parentTop);
530
557
  }
531
558
  this.emit("update", {
532
- el: this.target,
533
- parentEl,
534
- style: isResize ? { left, top, width, height } : { left, top }
559
+ data: [
560
+ {
561
+ el: this.target,
562
+ style: isResize ? { left, top, width, height } : { left, top }
563
+ }
564
+ ],
565
+ parentEl
535
566
  });
536
567
  }
537
568
  generateGhostEl(el) {
@@ -622,6 +653,9 @@
622
653
  ...moveableOptions
623
654
  };
624
655
  }
656
+ canContainerHighlight() {
657
+ return this.core.containerHighlightType === ContainerHighlightType.DEFAULT || this.core.containerHighlightType === ContainerHighlightType.ALT && this.isContainerHighlight;
658
+ }
625
659
  }
626
660
 
627
661
  class TargetCalibrate extends EventEmitter.EventEmitter {
@@ -913,11 +947,13 @@
913
947
  this.content.addEventListener("wheel", this.mouseWheelHandler);
914
948
  this.content.addEventListener("mousemove", this.highlightHandler);
915
949
  this.content.addEventListener("mouseleave", this.mouseLeaveHandler);
916
- KeyController__default.default.global.keydown("shift", (e) => {
950
+ const isMac = /mac os x/.test(navigator.userAgent.toLowerCase());
951
+ const ctrl = isMac ? "meta" : "ctrl";
952
+ KeyController__default.default.global.keydown(ctrl, (e) => {
917
953
  e.inputEvent.preventDefault();
918
954
  this.isMultiSelectStatus = true;
919
955
  });
920
- KeyController__default.default.global.keyup("shift", (e) => {
956
+ KeyController__default.default.global.keyup(ctrl, (e) => {
921
957
  e.inputEvent.preventDefault();
922
958
  this.isMultiSelectStatus = false;
923
959
  });
@@ -1106,6 +1142,7 @@
1106
1142
  targetList = [];
1107
1143
  dragElList = [];
1108
1144
  moveableForMulti;
1145
+ dragStatus = StageDragStatus.END;
1109
1146
  multiMoveableHelper;
1110
1147
  constructor(config) {
1111
1148
  super();
@@ -1151,6 +1188,7 @@
1151
1188
  id: matchEventTarget.id
1152
1189
  });
1153
1190
  });
1191
+ this.dragStatus = StageDragStatus.START;
1154
1192
  }).on("dragGroup", (params) => {
1155
1193
  const { events } = params;
1156
1194
  events.forEach((ev) => {
@@ -1167,18 +1205,29 @@
1167
1205
  }
1168
1206
  });
1169
1207
  this.multiMoveableHelper?.onDragGroup(params);
1208
+ this.dragStatus = StageDragStatus.ING;
1170
1209
  }).on("dragGroupEnd", () => {
1171
1210
  this.update();
1211
+ this.dragStatus = StageDragStatus.END;
1212
+ }).on("clickGroup", (params) => {
1213
+ const { inputTarget, targets } = params;
1214
+ if (!this.mask.isMultiSelectStatus && targets.length > 1 && targets.includes(inputTarget)) {
1215
+ this.emit("select", inputTarget.id.replace(DRAG_EL_ID_PREFIX, ""));
1216
+ }
1172
1217
  });
1173
1218
  }
1174
- canSelect(el) {
1219
+ canSelect(el, stop) {
1175
1220
  if (el.className.includes(PAGE_CLASS)) {
1176
1221
  this.core.highlightedDom = void 0;
1177
1222
  this.core.highlightLayer.clearHighlight();
1223
+ stop();
1178
1224
  return false;
1179
1225
  }
1180
1226
  const currentTargetMode = getMode(el);
1181
1227
  let selectedDomMode = "";
1228
+ if (this.core.selectedDom?.className.includes(PAGE_CLASS)) {
1229
+ return true;
1230
+ }
1182
1231
  if (this.targetList.length === 0 && this.core.selectedDom) {
1183
1232
  selectedDomMode = getMode(this.core.selectedDom);
1184
1233
  } else if (this.targetList.length > 0) {
@@ -1211,16 +1260,19 @@
1211
1260
  const doc = contentWindow?.document;
1212
1261
  if (!doc)
1213
1262
  return;
1214
- this.targetList.forEach((targetItem) => {
1215
- const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1216
- const left = calcValueByFontsize(doc, offset.left);
1217
- const top = calcValueByFontsize(doc, offset.top);
1218
- const width = calcValueByFontsize(doc, targetItem.clientWidth);
1219
- const height = calcValueByFontsize(doc, targetItem.clientHeight);
1220
- this.emit("update", {
1221
- el: targetItem,
1222
- style: isResize ? { left, top, width, height } : { left, top }
1223
- });
1263
+ this.emit("update", {
1264
+ data: this.targetList.map((targetItem) => {
1265
+ const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1266
+ const left = calcValueByFontsize(doc, offset.left);
1267
+ const top = calcValueByFontsize(doc, offset.top);
1268
+ const width = calcValueByFontsize(doc, targetItem.clientWidth);
1269
+ const height = calcValueByFontsize(doc, targetItem.clientHeight);
1270
+ return {
1271
+ el: targetItem,
1272
+ style: isResize ? { left, top, width, height } : { left, top }
1273
+ };
1274
+ }),
1275
+ parentEl: null
1224
1276
  });
1225
1277
  }
1226
1278
  getOptions(options = {}) {
@@ -1232,13 +1284,21 @@
1232
1284
  defaultGroupRotate: 0,
1233
1285
  defaultGroupOrigin: "50% 50%",
1234
1286
  draggable: true,
1235
- resizable: true,
1287
+ resizable: false,
1236
1288
  throttleDrag: 0,
1237
1289
  startDragRotate: 0,
1238
1290
  throttleDragRotate: 0,
1239
1291
  zoom: 1,
1240
1292
  origin: true,
1241
1293
  padding: { left: 0, top: 0, right: 0, bottom: 0 },
1294
+ snappable: true,
1295
+ bounds: {
1296
+ top: 0,
1297
+ left: -1,
1298
+ right: this.container.clientWidth - 1,
1299
+ bottom: this.container.clientHeight,
1300
+ ...multiMoveableOptions.bounds || {}
1301
+ },
1242
1302
  ...options,
1243
1303
  ...multiMoveableOptions
1244
1304
  };
@@ -1346,6 +1406,7 @@
1346
1406
  zoom = DEFAULT_ZOOM;
1347
1407
  containerHighlightClassName;
1348
1408
  containerHighlightDuration;
1409
+ containerHighlightType;
1349
1410
  isContainer;
1350
1411
  canSelect;
1351
1412
  constructor(config) {
@@ -1354,8 +1415,9 @@
1354
1415
  this.setZoom(config.zoom);
1355
1416
  this.canSelect = config.canSelect || ((el) => !!el.id);
1356
1417
  this.isContainer = config.isContainer;
1357
- this.containerHighlightClassName = config.containerHighlightClassName;
1358
- this.containerHighlightDuration = config.containerHighlightDuration;
1418
+ this.containerHighlightClassName = config.containerHighlightClassName || CONTAINER_HIGHLIGHT_CLASS;
1419
+ this.containerHighlightDuration = config.containerHighlightDuration || 800;
1420
+ this.containerHighlightType = config.containerHighlightType;
1359
1421
  this.renderer = new StageRender({ core: this });
1360
1422
  this.mask = new StageMask({ core: this });
1361
1423
  this.dr = new StageDragResize({ core: this, container: this.mask.content, mask: this.mask });
@@ -1382,6 +1444,8 @@
1382
1444
  const el = await this.getElementFromPoint(event);
1383
1445
  if (!el)
1384
1446
  return;
1447
+ if (this.multiDr.dragStatus === StageDragStatus.ING)
1448
+ return;
1385
1449
  await this.highlight(el);
1386
1450
  if (this.highlightedDom === this.selectedDom) {
1387
1451
  this.highlightLayer.clearHighlight();
@@ -1413,6 +1477,10 @@
1413
1477
  });
1414
1478
  this.multiDr.on("update", (data) => {
1415
1479
  setTimeout(() => this.emit("update", data));
1480
+ }).on("select", async (id) => {
1481
+ const el = await this.getTargetElement(id);
1482
+ this.select(el);
1483
+ setTimeout(() => this.emit("select", el));
1416
1484
  });
1417
1485
  }
1418
1486
  getElementsFromPoint(event) {
@@ -1446,7 +1514,7 @@
1446
1514
  if (!canSelectByProp)
1447
1515
  return false;
1448
1516
  if (this.mask.isMultiSelectStatus) {
1449
- return this.multiDr.canSelect(el);
1517
+ return this.multiDr.canSelect(el, stop);
1450
1518
  }
1451
1519
  return true;
1452
1520
  }
@@ -1576,6 +1644,7 @@
1576
1644
  }
1577
1645
 
1578
1646
  exports.CONTAINER_HIGHLIGHT_CLASS = CONTAINER_HIGHLIGHT_CLASS;
1647
+ exports.ContainerHighlightType = ContainerHighlightType;
1579
1648
  exports.DEFAULT_ZOOM = DEFAULT_ZOOM;
1580
1649
  exports.DRAG_EL_ID_PREFIX = DRAG_EL_ID_PREFIX;
1581
1650
  exports.GHOST_EL_ID_PREFIX = GHOST_EL_ID_PREFIX;
@@ -1586,6 +1655,7 @@
1586
1655
  exports.PAGE_CLASS = PAGE_CLASS;
1587
1656
  exports.SELECTED_CLASS = SELECTED_CLASS;
1588
1657
  exports.StageDragResize = StageDragResize;
1658
+ exports.StageDragStatus = StageDragStatus;
1589
1659
  exports.StageMask = StageMask;
1590
1660
  exports.StageRender = StageRender;
1591
1661
  exports.ZIndex = ZIndex;