@tmagic/stage 1.1.0-beta.7 → 1.1.0

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,18 +391,20 @@
366
391
  if (!this.moveable || !this.target || !this.dragEl)
367
392
  return;
368
393
  const { beforeTranslate } = drag;
369
- this.dragStatus = "ing" /* ING */;
370
- this.moveableHelper?.onResize(e);
394
+ this.dragStatus = StageDragStatus.ING;
371
395
  if (this.mode === Mode.SORTABLE) {
372
396
  this.target.style.top = "0px";
397
+ this.dragEl.style.width = `${width}px`;
398
+ this.dragEl.style.height = `${height}px`;
373
399
  } else {
400
+ this.moveableHelper?.onResize(e);
374
401
  this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
375
402
  this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
376
403
  }
377
404
  this.target.style.width = `${width}px`;
378
405
  this.target.style.height = `${height}px`;
379
406
  }).on("resizeEnd", () => {
380
- this.dragStatus = "end" /* END */;
407
+ this.dragStatus = StageDragStatus.END;
381
408
  this.update(true);
382
409
  });
383
410
  }
@@ -394,7 +421,7 @@
394
421
  this.moveable.on("dragStart", (e) => {
395
422
  if (!this.target)
396
423
  throw new Error("\u672A\u9009\u4E2D\u7EC4\u4EF6");
397
- this.dragStatus = "start" /* START */;
424
+ this.dragStatus = StageDragStatus.START;
398
425
  this.moveableHelper?.onDragStart(e);
399
426
  if (this.mode === Mode.SORTABLE) {
400
427
  this.ghostEl = this.generateGhostEl(this.target);
@@ -408,8 +435,10 @@
408
435
  globalThis.clearTimeout(timeout);
409
436
  timeout = void 0;
410
437
  }
411
- timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
412
- this.dragStatus = "ing" /* ING */;
438
+ if (this.canContainerHighlight()) {
439
+ timeout = this.core.getAddContainerHighlightClassNameTimeout(e.inputEvent, [this.target]);
440
+ }
441
+ this.dragStatus = StageDragStatus.ING;
413
442
  if (this.ghostEl) {
414
443
  this.ghostEl.style.top = `${frame.top + e.beforeTranslate[1]}px`;
415
444
  return;
@@ -423,10 +452,10 @@
423
452
  timeout = void 0;
424
453
  }
425
454
  let parentEl = null;
426
- if (doc) {
455
+ if (doc && this.canContainerHighlight()) {
427
456
  parentEl = utils.removeClassNameByClassName(doc, this.core.containerHighlightClassName);
428
457
  }
429
- if (this.dragStatus === "ing" /* ING */) {
458
+ if (this.dragStatus === StageDragStatus.ING) {
430
459
  if (parentEl) {
431
460
  this.update(false, parentEl);
432
461
  } else {
@@ -439,7 +468,7 @@
439
468
  }
440
469
  }
441
470
  }
442
- this.dragStatus = "end" /* END */;
471
+ this.dragStatus = StageDragStatus.END;
443
472
  this.destroyGhostEl();
444
473
  });
445
474
  }
@@ -447,17 +476,17 @@
447
476
  if (!this.moveable)
448
477
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
449
478
  this.moveable.on("rotateStart", (e) => {
450
- this.dragStatus = "start" /* START */;
479
+ this.dragStatus = StageDragStatus.START;
451
480
  this.moveableHelper?.onRotateStart(e);
452
481
  }).on("rotate", (e) => {
453
482
  if (!this.target || !this.dragEl)
454
483
  return;
455
- this.dragStatus = "ing" /* ING */;
484
+ this.dragStatus = StageDragStatus.ING;
456
485
  this.moveableHelper?.onRotate(e);
457
486
  const frame = this.moveableHelper?.getFrame(e.target);
458
487
  this.target.style.transform = frame?.toCSSObject().transform || "";
459
488
  }).on("rotateEnd", (e) => {
460
- this.dragStatus = "end" /* END */;
489
+ this.dragStatus = StageDragStatus.END;
461
490
  const frame = this.moveableHelper?.getFrame(e.target);
462
491
  this.emit("update", {
463
492
  el: this.target,
@@ -471,17 +500,17 @@
471
500
  if (!this.moveable)
472
501
  throw new Error("moveable \u672A\u521D\u59CB\u5316");
473
502
  this.moveable.on("scaleStart", (e) => {
474
- this.dragStatus = "start" /* START */;
503
+ this.dragStatus = StageDragStatus.START;
475
504
  this.moveableHelper?.onScaleStart(e);
476
505
  }).on("scale", (e) => {
477
506
  if (!this.target || !this.dragEl)
478
507
  return;
479
- this.dragStatus = "ing" /* ING */;
508
+ this.dragStatus = StageDragStatus.ING;
480
509
  this.moveableHelper?.onScale(e);
481
510
  const frame = this.moveableHelper?.getFrame(e.target);
482
511
  this.target.style.transform = frame?.toCSSObject().transform || "";
483
512
  }).on("scaleEnd", (e) => {
484
- this.dragStatus = "end" /* END */;
513
+ this.dragStatus = StageDragStatus.END;
485
514
  const frame = this.moveableHelper?.getFrame(e.target);
486
515
  this.emit("update", {
487
516
  el: this.target,
@@ -529,9 +558,13 @@
529
558
  top = calcValueByFontsize(doc, this.dragEl.offsetTop) + parseFloat(translateY) - calcValueByFontsize(doc, parentTop);
530
559
  }
531
560
  this.emit("update", {
532
- el: this.target,
533
- parentEl,
534
- style: isResize ? { left, top, width, height } : { left, top }
561
+ data: [
562
+ {
563
+ el: this.target,
564
+ style: isResize ? { left, top, width, height } : { left, top }
565
+ }
566
+ ],
567
+ parentEl
535
568
  });
536
569
  }
537
570
  generateGhostEl(el) {
@@ -569,6 +602,7 @@
569
602
  return {};
570
603
  const isAbsolute = this.mode === Mode.ABSOLUTE;
571
604
  const isFixed = this.mode === Mode.FIXED;
605
+ const isSortable = this.mode === Mode.SORTABLE;
572
606
  let { moveableOptions = {} } = this.core.config;
573
607
  if (typeof moveableOptions === "function") {
574
608
  moveableOptions = moveableOptions(this.core);
@@ -615,13 +649,16 @@
615
649
  top: 0,
616
650
  left: -1,
617
651
  right: this.container.clientWidth - 1,
618
- bottom: this.container.clientHeight,
652
+ bottom: isSortable ? void 0 : this.container.clientHeight,
619
653
  ...moveableOptions.bounds || {}
620
654
  },
621
655
  ...options,
622
656
  ...moveableOptions
623
657
  };
624
658
  }
659
+ canContainerHighlight() {
660
+ return this.core.containerHighlightType === ContainerHighlightType.DEFAULT || this.core.containerHighlightType === ContainerHighlightType.ALT && this.isContainerHighlight;
661
+ }
625
662
  }
626
663
 
627
664
  class TargetCalibrate extends EventEmitter.EventEmitter {
@@ -1108,6 +1145,7 @@
1108
1145
  targetList = [];
1109
1146
  dragElList = [];
1110
1147
  moveableForMulti;
1148
+ dragStatus = StageDragStatus.END;
1111
1149
  multiMoveableHelper;
1112
1150
  constructor(config) {
1113
1151
  super();
@@ -1140,9 +1178,7 @@
1140
1178
  createAuto: true
1141
1179
  });
1142
1180
  const frames = [];
1143
- this.moveableForMulti.on("dragGroupStart", (params) => {
1144
- const { events } = params;
1145
- this.multiMoveableHelper?.onDragGroupStart(params);
1181
+ const setFrames = (events) => {
1146
1182
  events.forEach((ev) => {
1147
1183
  const matchEventTarget = this.targetList.find((targetItem) => targetItem.id === ev.target.id.replace(DRAG_EL_ID_PREFIX, ""));
1148
1184
  if (!matchEventTarget)
@@ -1153,6 +1189,40 @@
1153
1189
  id: matchEventTarget.id
1154
1190
  });
1155
1191
  });
1192
+ };
1193
+ this.moveableForMulti.on("resizeGroupStart", (params) => {
1194
+ const { events } = params;
1195
+ this.multiMoveableHelper?.onResizeGroupStart(params);
1196
+ setFrames(events);
1197
+ this.dragStatus = StageDragStatus.START;
1198
+ }).on("resizeGroup", (params) => {
1199
+ const { events } = params;
1200
+ events.forEach((ev) => {
1201
+ const { width, height, beforeTranslate } = ev.drag;
1202
+ const frameSnapShot = frames.find((frameItem) => frameItem.id === ev.target.id.replace(DRAG_EL_ID_PREFIX, ""));
1203
+ if (!frameSnapShot)
1204
+ return;
1205
+ const targeEl = this.targetList.find((targetItem) => targetItem.id === ev.target.id.replace(DRAG_EL_ID_PREFIX, ""));
1206
+ if (!targeEl)
1207
+ return;
1208
+ const isParentIncluded = this.targetList.find((targetItem) => targetItem.id === targeEl.parentElement?.id);
1209
+ if (!isParentIncluded) {
1210
+ targeEl.style.left = `${frameSnapShot.left + beforeTranslate[0]}px`;
1211
+ targeEl.style.top = `${frameSnapShot.top + beforeTranslate[1]}px`;
1212
+ }
1213
+ targeEl.style.width = `${width}px`;
1214
+ targeEl.style.height = `${height}px`;
1215
+ });
1216
+ this.multiMoveableHelper?.onResizeGroup(params);
1217
+ this.dragStatus = StageDragStatus.ING;
1218
+ }).on("resizeGroupEnd", () => {
1219
+ this.update(true);
1220
+ this.dragStatus = StageDragStatus.END;
1221
+ }).on("dragGroupStart", (params) => {
1222
+ const { events } = params;
1223
+ this.multiMoveableHelper?.onDragGroupStart(params);
1224
+ setFrames(events);
1225
+ this.dragStatus = StageDragStatus.START;
1156
1226
  }).on("dragGroup", (params) => {
1157
1227
  const { events } = params;
1158
1228
  events.forEach((ev) => {
@@ -1169,8 +1239,15 @@
1169
1239
  }
1170
1240
  });
1171
1241
  this.multiMoveableHelper?.onDragGroup(params);
1242
+ this.dragStatus = StageDragStatus.ING;
1172
1243
  }).on("dragGroupEnd", () => {
1173
1244
  this.update();
1245
+ this.dragStatus = StageDragStatus.END;
1246
+ }).on("clickGroup", (params) => {
1247
+ const { inputTarget, targets } = params;
1248
+ if (!this.mask.isMultiSelectStatus && targets.length > 1 && targets.includes(inputTarget)) {
1249
+ this.emit("select", inputTarget.id.replace(DRAG_EL_ID_PREFIX, ""));
1250
+ }
1174
1251
  });
1175
1252
  }
1176
1253
  canSelect(el, stop) {
@@ -1217,16 +1294,19 @@
1217
1294
  const doc = contentWindow?.document;
1218
1295
  if (!doc)
1219
1296
  return;
1220
- this.targetList.forEach((targetItem) => {
1221
- const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1222
- const left = calcValueByFontsize(doc, offset.left);
1223
- const top = calcValueByFontsize(doc, offset.top);
1224
- const width = calcValueByFontsize(doc, targetItem.clientWidth);
1225
- const height = calcValueByFontsize(doc, targetItem.clientHeight);
1226
- this.emit("update", {
1227
- el: targetItem,
1228
- style: isResize ? { left, top, width, height } : { left, top }
1229
- });
1297
+ this.emit("update", {
1298
+ data: this.targetList.map((targetItem) => {
1299
+ const offset = { left: targetItem.offsetLeft, top: targetItem.offsetTop };
1300
+ const left = calcValueByFontsize(doc, offset.left);
1301
+ const top = calcValueByFontsize(doc, offset.top);
1302
+ const width = calcValueByFontsize(doc, targetItem.clientWidth);
1303
+ const height = calcValueByFontsize(doc, targetItem.clientHeight);
1304
+ return {
1305
+ el: targetItem,
1306
+ style: isResize ? { left, top, width, height } : { left, top }
1307
+ };
1308
+ }),
1309
+ parentEl: null
1230
1310
  });
1231
1311
  }
1232
1312
  getOptions(options = {}) {
@@ -1245,13 +1325,21 @@
1245
1325
  zoom: 1,
1246
1326
  origin: true,
1247
1327
  padding: { left: 0, top: 0, right: 0, bottom: 0 },
1328
+ snappable: true,
1329
+ bounds: {
1330
+ top: 0,
1331
+ left: -1,
1332
+ right: this.container.clientWidth - 1,
1333
+ bottom: this.container.clientHeight,
1334
+ ...multiMoveableOptions.bounds || {}
1335
+ },
1248
1336
  ...options,
1249
1337
  ...multiMoveableOptions
1250
1338
  };
1251
1339
  }
1252
1340
  }
1253
1341
 
1254
- const style = ".tmagic-stage-container-highlight::after {\n content: '';\n position: absolute;\n width: 100%;\n height: 100%;\n top: 0;\n left: 0;\n background-color: #000;\n opacity: .1;\n pointer-events: none;\n}\n";
1342
+ const style = ".tmagic-stage-container-highlight::after {\n content: '';\n position: absolute;\n width: 100%;\n height: 100%;\n top: 0;\n left: 0;\n background-color: #000;\n opacity: .1;\n pointer-events: none;\n}\n\n.magic-ui-container.magic-layout-relative {\n min-height: 50px;\n}\n";
1255
1343
 
1256
1344
  class StageRender extends EventEmitter.EventEmitter {
1257
1345
  contentWindow = null;
@@ -1352,6 +1440,7 @@
1352
1440
  zoom = DEFAULT_ZOOM;
1353
1441
  containerHighlightClassName;
1354
1442
  containerHighlightDuration;
1443
+ containerHighlightType;
1355
1444
  isContainer;
1356
1445
  canSelect;
1357
1446
  constructor(config) {
@@ -1360,8 +1449,9 @@
1360
1449
  this.setZoom(config.zoom);
1361
1450
  this.canSelect = config.canSelect || ((el) => !!el.id);
1362
1451
  this.isContainer = config.isContainer;
1363
- this.containerHighlightClassName = config.containerHighlightClassName;
1364
- this.containerHighlightDuration = config.containerHighlightDuration;
1452
+ this.containerHighlightClassName = config.containerHighlightClassName || CONTAINER_HIGHLIGHT_CLASS;
1453
+ this.containerHighlightDuration = config.containerHighlightDuration || 800;
1454
+ this.containerHighlightType = config.containerHighlightType;
1365
1455
  this.renderer = new StageRender({ core: this });
1366
1456
  this.mask = new StageMask({ core: this });
1367
1457
  this.dr = new StageDragResize({ core: this, container: this.mask.content, mask: this.mask });
@@ -1388,6 +1478,8 @@
1388
1478
  const el = await this.getElementFromPoint(event);
1389
1479
  if (!el)
1390
1480
  return;
1481
+ if (this.multiDr.dragStatus === StageDragStatus.ING)
1482
+ return;
1391
1483
  await this.highlight(el);
1392
1484
  if (this.highlightedDom === this.selectedDom) {
1393
1485
  this.highlightLayer.clearHighlight();
@@ -1419,6 +1511,10 @@
1419
1511
  });
1420
1512
  this.multiDr.on("update", (data) => {
1421
1513
  setTimeout(() => this.emit("update", data));
1514
+ }).on("select", async (id) => {
1515
+ const el = await this.getTargetElement(id);
1516
+ this.select(el);
1517
+ setTimeout(() => this.emit("select", el));
1422
1518
  });
1423
1519
  }
1424
1520
  getElementsFromPoint(event) {
@@ -1582,6 +1678,7 @@
1582
1678
  }
1583
1679
 
1584
1680
  exports.CONTAINER_HIGHLIGHT_CLASS = CONTAINER_HIGHLIGHT_CLASS;
1681
+ exports.ContainerHighlightType = ContainerHighlightType;
1585
1682
  exports.DEFAULT_ZOOM = DEFAULT_ZOOM;
1586
1683
  exports.DRAG_EL_ID_PREFIX = DRAG_EL_ID_PREFIX;
1587
1684
  exports.GHOST_EL_ID_PREFIX = GHOST_EL_ID_PREFIX;
@@ -1592,6 +1689,7 @@
1592
1689
  exports.PAGE_CLASS = PAGE_CLASS;
1593
1690
  exports.SELECTED_CLASS = SELECTED_CLASS;
1594
1691
  exports.StageDragResize = StageDragResize;
1692
+ exports.StageDragStatus = StageDragStatus;
1595
1693
  exports.StageMask = StageMask;
1596
1694
  exports.StageRender = StageRender;
1597
1695
  exports.ZIndex = ZIndex;