@leafer-in/editor 2.2.0 → 2.2.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.
package/dist/editor.cjs CHANGED
@@ -44,6 +44,69 @@ EditorEvent.BEFORE_HOVER = "editor.before_hover";
44
44
 
45
45
  EditorEvent.HOVER = "editor.hover";
46
46
 
47
+ const order = (a, b) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
48
+
49
+ const reverseOrder = (a, b) => b.parent.children.indexOf(b) - a.parent.children.indexOf(a);
50
+
51
+ const EditorHelper = {
52
+ group(list, element, userGroup) {
53
+ list.sort(reverseOrder);
54
+ const {app: app, parent: parent} = list[0];
55
+ let group;
56
+ if (userGroup && userGroup.add) {
57
+ group = userGroup;
58
+ } else {
59
+ group = new draw.Group(userGroup);
60
+ }
61
+ parent.addAt(group, parent.children.indexOf(list[0]));
62
+ list.sort(order);
63
+ const matrx = new draw.Matrix(element.worldTransform);
64
+ matrx.divideParent(parent.scrollWorldTransform);
65
+ group.setTransform(matrx);
66
+ group.editable = true;
67
+ group.hitChildren = false;
68
+ app.lockLayout();
69
+ list.forEach(child => child.dropTo(group));
70
+ app.unlockLayout();
71
+ return group;
72
+ },
73
+ ungroup(list) {
74
+ const {app: app} = list[0];
75
+ const ungroupList = [];
76
+ app.lockLayout();
77
+ list.forEach(leaf => {
78
+ if (leaf.isBranch) {
79
+ const {parent: parent, children: children} = leaf;
80
+ while (children.length) {
81
+ ungroupList.push(children[0]);
82
+ children[0].dropTo(parent, parent.children.indexOf(leaf));
83
+ }
84
+ if (leaf.isBranchLeaf) ungroupList.push(leaf); else leaf.remove();
85
+ } else {
86
+ ungroupList.push(leaf);
87
+ }
88
+ });
89
+ app.unlockLayout();
90
+ return ungroupList;
91
+ },
92
+ toTop(list) {
93
+ list.sort(order);
94
+ list.forEach(leaf => {
95
+ if (leaf.parent) leaf.parent.add(leaf);
96
+ });
97
+ },
98
+ toBottom(list) {
99
+ list.sort(reverseOrder);
100
+ list.forEach(leaf => {
101
+ if (leaf.parent) leaf.parent.addAt(leaf, 0);
102
+ });
103
+ },
104
+ mergeMaskConfig(_mergeConfig, _editor) {},
105
+ getSiblingMaskElements(_editor) {
106
+ return undefined;
107
+ }
108
+ };
109
+
47
110
  function targetAttr(fn) {
48
111
  return (target, key) => {
49
112
  const privateKey = "_" + key;
@@ -92,6 +155,7 @@ function mergeConfigAttr() {
92
155
  get() {
93
156
  const {config: config, element: element, dragPoint: dragPoint, editBox: editBox, editTool: editTool, innerEditor: innerEditor, app: app} = this, mergeConfig = Object.assign({}, config);
94
157
  if (innerEditor) innerEditor.editConfig && Object.assign(mergeConfig, innerEditor.editConfig); else if (editTool) editTool.editConfig && Object.assign(mergeConfig, editTool.editConfig);
158
+ if (element && element.mask) EditorHelper.mergeMaskConfig(mergeConfig, this);
95
159
  if (element && element.editConfig) {
96
160
  let {editConfig: editConfig} = element;
97
161
  if (editConfig.hover || editConfig.hoverStyle || editConfig.hoverPathType) {
@@ -118,7 +182,28 @@ function mergeConfigAttr() {
118
182
  };
119
183
  }
120
184
 
121
- const {abs: abs$1} = Math;
185
+ function editToolMergeConfigAttr() {
186
+ return (target, key) => {
187
+ draw.defineKey(target, key, {
188
+ get() {
189
+ const t = this;
190
+ const {config: config, userConfig: userConfig, configKeepKeys: configKeepKeys} = t, mergedConfig = config ? draw.DataHelper.clone(config) : {};
191
+ if (configKeepKeys) {
192
+ for (let key in userConfig) {
193
+ if (!configKeepKeys.includes(key)) mergedConfig[key] = userConfig[key];
194
+ }
195
+ }
196
+ if (t.preMergedConfig) t.preMergedConfig(mergedConfig);
197
+ if (configKeepKeys) configKeepKeys.forEach(key => {
198
+ userConfig[key] && Object.assign(mergedConfig[key], userConfig[key]);
199
+ });
200
+ return t.mergedConfig = mergedConfig;
201
+ }
202
+ });
203
+ };
204
+ }
205
+
206
+ const {abs: abs$2} = Math;
122
207
 
123
208
  const {copy: copy$1} = draw.MatrixHelper;
124
209
 
@@ -161,7 +246,7 @@ class Stroker extends draw.UI {
161
246
  leaf = list[i];
162
247
  const {worldTransform: worldTransform, worldRenderBounds: worldRenderBounds} = leaf;
163
248
  if (worldRenderBounds.width && worldRenderBounds.height && (!bounds || bounds.hit(worldRenderBounds, options.matrix))) {
164
- const aScaleX = abs$1(worldTransform.scaleX), aScaleY = abs$1(worldTransform.scaleY);
249
+ const aScaleX = abs$2(worldTransform.scaleX), aScaleY = abs$2(worldTransform.scaleY);
165
250
  copy$1(matrix, worldTransform);
166
251
  matrix.half = strokeWidth % 2;
167
252
  canvas.setWorld(matrix, options.matrix);
@@ -316,7 +401,11 @@ class EditSelect extends draw.Group {
316
401
  }
317
402
  onSelect() {
318
403
  if (this.running) {
319
- this.targetStroker.setTarget(this.editor.list);
404
+ const {editor: editor} = this;
405
+ let {list: list} = editor;
406
+ const maskList = EditorHelper.getSiblingMaskElements(editor);
407
+ if (maskList) list = maskList.concat(list);
408
+ this.targetStroker.setTarget(list);
320
409
  this.hoverStroker.target = null;
321
410
  }
322
411
  }
@@ -488,13 +577,13 @@ class EditSelect extends draw.Group {
488
577
 
489
578
  const {topLeft: topLeft, top: top, topRight: topRight, right: right$1, bottomRight: bottomRight, bottom: bottom, bottomLeft: bottomLeft, left: left$1} = draw.Direction9;
490
579
 
491
- const {toPoint: toPoint} = draw.AroundHelper, {within: within, sign: sign} = draw.MathHelper, {abs: abs} = Math;
580
+ const {toPoint: toPoint} = draw.AroundHelper, {within: within, sign: sign} = draw.MathHelper, {abs: abs$1} = Math;
492
581
 
493
582
  const EditDataHelper = {
494
583
  getScaleData(target, startBounds, direction, totalMoveOrScale, lockRatio, around, flipable, scaleMode) {
495
584
  let align, origin = {}, scaleX = 1, scaleY = 1, lockScale;
496
585
  const {widthRange: widthRange, heightRange: heightRange, dragBounds: dragBounds, worldTransform: worldTransform, boxBounds: boxBounds} = target;
497
- const {width: width, height: height} = startBounds, worldScaleX = abs(worldTransform.scaleX), worldScaleY = abs(worldTransform.scaleY);
586
+ const {width: width, height: height} = startBounds, worldScaleX = abs$1(worldTransform.scaleX), worldScaleY = abs$1(worldTransform.scaleY);
498
587
  const originChangedScaleX = target.scaleX / startBounds.scaleX;
499
588
  const originChangedScaleY = target.scaleY / startBounds.scaleY;
500
589
  const signX = sign(originChangedScaleX);
@@ -574,7 +663,7 @@ const EditDataHelper = {
574
663
  break;
575
664
 
576
665
  default:
577
- lockScale = Math.sqrt(abs(scaleX * scaleY));
666
+ lockScale = Math.sqrt(abs$1(scaleX * scaleY));
578
667
  scaleX = sign(scaleX) * lockScale;
579
668
  scaleY = sign(scaleY) * lockScale;
580
669
  }
@@ -608,10 +697,10 @@ const EditDataHelper = {
608
697
  }
609
698
  const minWidth = 1, minHeight = 1;
610
699
  const worldWidth = worldScaleX * boxBounds.width, worldHeight = worldScaleY * boxBounds.height;
611
- if (useScaleX && abs(scaleX * worldWidth) < minWidth) scaleX = sign(scaleX) * minWidth / worldWidth;
612
- if (useScaleY && abs(scaleY * worldHeight) < minHeight) scaleY = sign(scaleY) * minHeight / worldHeight;
700
+ if (useScaleX && abs$1(scaleX * worldWidth) < minWidth) scaleX = sign(scaleX) * minWidth / worldWidth;
701
+ if (useScaleY && abs$1(scaleY * worldHeight) < minHeight) scaleY = sign(scaleY) * minHeight / worldHeight;
613
702
  if (lockRatio && scaleX !== scaleY) {
614
- lockScale = Math.min(abs(scaleX), abs(scaleY));
703
+ lockScale = Math.min(abs$1(scaleX), abs$1(scaleY));
615
704
  scaleX = sign(scaleX) * lockScale;
616
705
  scaleY = sign(scaleY) * lockScale;
617
706
  }
@@ -833,6 +922,8 @@ class EditPoint extends draw.Box {
833
922
 
834
923
  const fourDirection = [ "top", "right", "bottom", "left" ], editConfig = undefined;
835
924
 
925
+ const debug$1 = draw.Debug.get("EditBox");
926
+
836
927
  class EditBox extends draw.Group {
837
928
  get mergeConfig() {
838
929
  const {config: config} = this, {mergeConfig: mergeConfig, editBox: editBox} = this.editor;
@@ -903,10 +994,12 @@ class EditBox extends draw.Group {
903
994
  this.resizeLines = [];
904
995
  this.dragStartData = {};
905
996
  this.__eventIds = [];
997
+ this.widgets = [];
906
998
  this.editor = editor;
907
999
  this.visible = false;
908
1000
  this.create();
909
1001
  this.__listenEvents();
1002
+ this.createWidgets();
910
1003
  }
911
1004
  create() {
912
1005
  let rotatePoint, resizeLine, resizePoint;
@@ -942,7 +1035,7 @@ class EditBox extends draw.Group {
942
1035
  }
943
1036
  this.listenPointEvents(circle, "rotate", 2);
944
1037
  this.listenPointEvents(rect, "move", 8);
945
- view.addMany(...rotatePoints, rect, circle, buttons, ...resizeLines, ...resizePoints);
1038
+ view.add([ ...rotatePoints, rect, circle, buttons, ...resizeLines, ...resizePoints ]);
946
1039
  this.add(view);
947
1040
  }
948
1041
  load() {
@@ -955,6 +1048,7 @@ class EditBox extends draw.Group {
955
1048
  let resizeP;
956
1049
  for (let i = 0; i < 8; i++) {
957
1050
  resizeP = resizePoints[i];
1051
+ if (resizeP.children.length) resizeP.clear();
958
1052
  resizeP.set(this.getPointStyle(i % 2 ? middlePointsStyle[(i - 1) / 2 % middlePointsStyle.length] : pointsStyle[i / 2 % pointsStyle.length]));
959
1053
  resizeP.rotation = (i - (i % 2 ? 1 : 0)) / 2 * 90;
960
1054
  if (i % 2) resizeLines[(i - 1) / 2].set(Object.assign({
@@ -980,6 +1074,7 @@ class EditBox extends draw.Group {
980
1074
  }
981
1075
  if (single) draw.DataHelper.stintSet(target.__world, "ignorePixelSnap", ignorePixelSnap);
982
1076
  updateMoveCursor(this);
1077
+ this.loadWidgets();
983
1078
  }
984
1079
  update() {
985
1080
  const {editor: editor} = this;
@@ -1000,8 +1095,10 @@ class EditBox extends draw.Group {
1000
1095
  width: width,
1001
1096
  height: height
1002
1097
  });
1098
+ this.updateWidgets();
1003
1099
  }
1004
1100
  unload() {
1101
+ this.unloadWidgets();
1005
1102
  this.visible = false;
1006
1103
  if (this.app) this.rect.syncEventer = this.app.interaction.bottomList = null;
1007
1104
  }
@@ -1014,6 +1111,7 @@ class EditBox extends draw.Group {
1014
1111
  strokeBoxSpread && draw.BoundsHelper.spread(bounds, [ strokeBoxSpread * Math.abs(scaleY), strokeBoxSpread * Math.abs(scaleX) ]);
1015
1112
  }
1016
1113
  if (spread) draw.BoundsHelper.spread(bounds, spread);
1114
+ this.rectBounds = bounds;
1017
1115
  editMask.visible = mask ? true : 0;
1018
1116
  if (!draw.isUndefined(dimOthers) || !draw.isUndefined(bright)) {
1019
1117
  editor.setDimOthers(dimOthers);
@@ -1025,7 +1123,7 @@ class EditBox extends draw.Group {
1025
1123
  if (this.view.worldOpacity) {
1026
1124
  const {width: width, height: height} = bounds;
1027
1125
  const smallSize = draw.isNumber(hideOnSmall) ? hideOnSmall : 10;
1028
- const showPoints = editBox && !(hideOnSmall && width < smallSize && height < smallSize);
1126
+ const showPoints = this.showPoints = editBox && !(hideOnSmall && width < smallSize && height < smallSize);
1029
1127
  let point = {}, rotateP, resizeP, resizeL;
1030
1128
  for (let i = 0; i < 8; i++) {
1031
1129
  draw.AroundHelper.toPoint(draw.AroundHelper.directionData[i], bounds, point);
@@ -1319,13 +1417,38 @@ class EditBox extends draw.Group {
1319
1417
  __removeListenEvents() {
1320
1418
  this.off_(this.__eventIds);
1321
1419
  }
1420
+ static registerWidget(Widget) {
1421
+ const {tag: tag} = Widget.prototype;
1422
+ EditBox.WidgetList[tag] && debug$1.repeat(tag);
1423
+ EditBox.WidgetList[tag] = Widget;
1424
+ }
1425
+ createWidgets() {
1426
+ Object.values(EditBox.WidgetList).forEach(item => {
1427
+ this.widgets.push(new item(this));
1428
+ });
1429
+ }
1430
+ loadWidgets() {
1431
+ this.widgets.forEach(item => item.onLoad());
1432
+ }
1433
+ updateWidgets() {
1434
+ this.widgets.forEach(item => item.onUpdate());
1435
+ }
1436
+ unloadWidgets() {
1437
+ this.widgets.forEach(item => item.onUnload());
1438
+ }
1439
+ destroyWidgets() {
1440
+ this.widgets.forEach(item => item.onDestroy());
1441
+ }
1322
1442
  destroy() {
1323
1443
  this.editor = null;
1324
1444
  this.__removeListenEvents();
1445
+ this.destroyWidgets();
1325
1446
  super.destroy();
1326
1447
  }
1327
1448
  }
1328
1449
 
1450
+ EditBox.WidgetList = {};
1451
+
1329
1452
  const bigBounds = {
1330
1453
  x: 0,
1331
1454
  y: 0,
@@ -1472,65 +1595,6 @@ function onHover(editor, oldValue) {
1472
1595
  }));
1473
1596
  }
1474
1597
 
1475
- const order = (a, b) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
1476
-
1477
- const reverseOrder = (a, b) => b.parent.children.indexOf(b) - a.parent.children.indexOf(a);
1478
-
1479
- const EditorHelper = {
1480
- group(list, element, userGroup) {
1481
- list.sort(reverseOrder);
1482
- const {app: app, parent: parent} = list[0];
1483
- let group;
1484
- if (userGroup && userGroup.add) {
1485
- group = userGroup;
1486
- } else {
1487
- group = new draw.Group(userGroup);
1488
- }
1489
- parent.addAt(group, parent.children.indexOf(list[0]));
1490
- list.sort(order);
1491
- const matrx = new draw.Matrix(element.worldTransform);
1492
- matrx.divideParent(parent.scrollWorldTransform);
1493
- group.setTransform(matrx);
1494
- group.editable = true;
1495
- group.hitChildren = false;
1496
- app.lockLayout();
1497
- list.forEach(child => child.dropTo(group));
1498
- app.unlockLayout();
1499
- return group;
1500
- },
1501
- ungroup(list) {
1502
- const {app: app} = list[0];
1503
- const ungroupList = [];
1504
- app.lockLayout();
1505
- list.forEach(leaf => {
1506
- if (leaf.isBranch) {
1507
- const {parent: parent, children: children} = leaf;
1508
- while (children.length) {
1509
- ungroupList.push(children[0]);
1510
- children[0].dropTo(parent, parent.children.indexOf(leaf));
1511
- }
1512
- if (leaf.isBranchLeaf) ungroupList.push(leaf); else leaf.remove();
1513
- } else {
1514
- ungroupList.push(leaf);
1515
- }
1516
- });
1517
- app.unlockLayout();
1518
- return ungroupList;
1519
- },
1520
- toTop(list) {
1521
- list.sort(order);
1522
- list.forEach(leaf => {
1523
- if (leaf.parent) leaf.parent.add(leaf);
1524
- });
1525
- },
1526
- toBottom(list) {
1527
- list.sort(reverseOrder);
1528
- list.forEach(leaf => {
1529
- if (leaf.parent) leaf.parent.addAt(leaf, 0);
1530
- });
1531
- }
1532
- };
1533
-
1534
1598
  const debug = draw.Debug.get("EditToolCreator");
1535
1599
 
1536
1600
  function registerEditTool() {
@@ -2306,6 +2370,8 @@ __decorate([ targetAttr(onHover) ], exports.Editor.prototype, "hoverTarget", voi
2306
2370
 
2307
2371
  exports.Editor = __decorate([ core.useModule(TransformTool, [ "editBox", "editTool", "emitEvent" ]) ], exports.Editor);
2308
2372
 
2373
+ const {abs: abs} = Math, {scale: scale} = draw.PointHelper;
2374
+
2309
2375
  class InnerEditor {
2310
2376
  static registerInnerEditor() {
2311
2377
  EditToolCreator.register(this);
@@ -2316,6 +2382,9 @@ class InnerEditor {
2316
2382
  get mode() {
2317
2383
  return "focus";
2318
2384
  }
2385
+ get userConfig() {
2386
+ return this.editBox.mergeConfig[this.tag] || {};
2387
+ }
2319
2388
  get editBox() {
2320
2389
  return this._editBox || this.editor.editBox;
2321
2390
  }
@@ -2327,6 +2396,22 @@ class InnerEditor {
2327
2396
  this.editor = editor;
2328
2397
  this.create();
2329
2398
  }
2399
+ getEditBoxPoint(editTargetPoint, change) {
2400
+ const point = change ? editTargetPoint : {
2401
+ x: editTargetPoint.x,
2402
+ y: editTargetPoint.y
2403
+ }, {scaleX: scaleX, scaleY: scaleY} = this.editTarget.worldTransform;
2404
+ scale(point, abs(scaleX), abs(scaleY));
2405
+ return point;
2406
+ }
2407
+ getEditTargetPoint(editBoxPoint, change) {
2408
+ const point = change ? editBoxPoint : {
2409
+ x: editBoxPoint.x,
2410
+ y: editBoxPoint.y
2411
+ }, {scaleX: scaleX, scaleY: scaleY} = this.editTarget.worldTransform;
2412
+ scale(point, Math.abs(1 / scaleX), abs(1 / scaleY));
2413
+ return point;
2414
+ }
2330
2415
  onCreate() {}
2331
2416
  create() {
2332
2417
  this.view = new draw.Group;
@@ -2614,6 +2699,8 @@ exports.Stroker = Stroker;
2614
2699
 
2615
2700
  exports.TransformTool = TransformTool;
2616
2701
 
2702
+ exports.editToolMergeConfigAttr = editToolMergeConfigAttr;
2703
+
2617
2704
  exports.registerEditTool = registerEditTool;
2618
2705
 
2619
2706
  exports.registerInnerEditor = registerInnerEditor;