@leafer-in/editor 1.5.3 → 1.6.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.
package/dist/editor.cjs CHANGED
@@ -114,6 +114,28 @@ function targetAttr(fn) {
114
114
  });
115
115
  };
116
116
  }
117
+ function mergeConfigAttr() {
118
+ return (target, key) => {
119
+ draw.defineKey(target, key, {
120
+ get() {
121
+ const { config, element, dragPoint } = this, mergeConfig = Object.assign({}, config);
122
+ if (element && element.editConfig)
123
+ Object.assign(mergeConfig, element.editConfig);
124
+ if (dragPoint) {
125
+ if (dragPoint.editConfig)
126
+ Object.assign(mergeConfig, dragPoint.editConfig);
127
+ if (mergeConfig.editSize === 'font-size')
128
+ mergeConfig.lockRatio = true;
129
+ if (dragPoint.pointType === 'resize-rotate') {
130
+ mergeConfig.around || (mergeConfig.around = 'center');
131
+ draw.isNull(mergeConfig.lockRatio) && (mergeConfig.lockRatio = true);
132
+ }
133
+ }
134
+ return this.mergedConfig = mergeConfig;
135
+ }
136
+ });
137
+ };
138
+ }
117
139
 
118
140
  const { abs } = Math;
119
141
  const { copy: copy$1, scale } = draw.MatrixHelper;
@@ -125,6 +147,7 @@ class Stroker extends draw.UI {
125
147
  constructor() {
126
148
  super();
127
149
  this.list = [];
150
+ this.visible = 0;
128
151
  this.hittable = false;
129
152
  this.strokeAlign = 'center';
130
153
  }
@@ -138,10 +161,10 @@ class Stroker extends draw.UI {
138
161
  if (list.length) {
139
162
  setListWithFn(bounds$1, list, worldBounds);
140
163
  this.set(bounds$1);
164
+ this.visible = true;
141
165
  }
142
- else {
143
- this.width = this.height = 1;
144
- }
166
+ else
167
+ this.visible = 0;
145
168
  }
146
169
  __draw(canvas, options) {
147
170
  const { list } = this;
@@ -153,8 +176,9 @@ class Stroker extends draw.UI {
153
176
  const { worldTransform, worldRenderBounds } = leaf;
154
177
  if (worldRenderBounds.width && worldRenderBounds.height && (!bounds || bounds.hit(worldRenderBounds, options.matrix))) {
155
178
  const aScaleX = abs(worldTransform.scaleX), aScaleY = abs(worldTransform.scaleY);
179
+ copy$1(matrix, worldTransform);
180
+ matrix.half = strokeWidth % 2;
156
181
  if (aScaleX !== aScaleY) {
157
- copy$1(matrix, worldTransform);
158
182
  scale(matrix, 1 / aScaleX, 1 / aScaleY);
159
183
  canvas.setWorld(matrix, options.matrix);
160
184
  canvas.beginPath();
@@ -163,7 +187,7 @@ class Stroker extends draw.UI {
163
187
  canvas.rect(x * aScaleX, y * aScaleY, width * aScaleX, height * aScaleY);
164
188
  }
165
189
  else {
166
- canvas.setWorld(worldTransform, options.matrix);
190
+ canvas.setWorld(matrix, options.matrix);
167
191
  canvas.beginPath();
168
192
  if (leaf.__.__useArrow)
169
193
  leaf.__drawPath(canvas);
@@ -198,7 +222,8 @@ class SelectArea extends draw.Group {
198
222
  super(data);
199
223
  this.strokeArea = new draw.Rect({ strokeAlign: 'center' });
200
224
  this.fillArea = new draw.Rect();
201
- this.visible = this.hittable = false;
225
+ this.visible = 0;
226
+ this.hittable = false;
202
227
  this.addMany(this.fillArea, this.strokeArea);
203
228
  }
204
229
  setStyle(style, userStyle) {
@@ -395,7 +420,7 @@ class EditSelect extends draw.Group {
395
420
  if (e.multiTouch)
396
421
  return;
397
422
  if (this.dragging)
398
- this.originList = null, this.selectArea.visible = false;
423
+ this.originList = null, this.selectArea.visible = 0;
399
424
  }
400
425
  onAutoMove(e) {
401
426
  if (this.dragging) {
@@ -543,8 +568,11 @@ const EditDataHelper = {
543
568
  scaleY = scaleY < 0 ? -scale : scale;
544
569
  }
545
570
  }
546
- scaleX /= changedScaleX;
547
- scaleY /= changedScaleY;
571
+ const useScaleX = scaleX !== 1, useScaleY = scaleY !== 1;
572
+ if (useScaleX)
573
+ scaleX /= changedScaleX;
574
+ if (useScaleY)
575
+ scaleY /= changedScaleY;
548
576
  if (!flipable) {
549
577
  const { worldTransform } = element;
550
578
  if (scaleX < 0)
@@ -559,22 +587,27 @@ const EditDataHelper = {
559
587
  localBounds.scaleOf(element.getLocalPointByInner(origin), scaleX, scaleY);
560
588
  if (!draw.BoundsHelper.includes(allowBounds, localBounds)) {
561
589
  const realBounds = localBounds.getIntersect(allowBounds);
562
- scaleX *= realBounds.width / localBounds.width;
563
- scaleY *= realBounds.height / localBounds.height;
590
+ const fitScaleX = realBounds.width / localBounds.width, fitScaleY = realBounds.height / localBounds.height;
591
+ if (useScaleX)
592
+ scaleX *= fitScaleX;
593
+ if (useScaleY)
594
+ scaleY *= fitScaleY;
564
595
  }
565
596
  }
566
- if (widthRange) {
597
+ if (useScaleX && widthRange) {
567
598
  const nowWidth = boxBounds.width * element.scaleX;
568
599
  scaleX = within(nowWidth * scaleX, widthRange) / nowWidth;
569
600
  }
570
- if (heightRange) {
601
+ if (useScaleY && heightRange) {
571
602
  const nowHeight = boxBounds.height * element.scaleY;
572
603
  scaleY = within(nowHeight * scaleY, heightRange) / nowHeight;
573
604
  }
574
- if (Math.abs(scaleX * worldBoxBounds.width) < 1)
605
+ if (useScaleX && Math.abs(scaleX * worldBoxBounds.width) < 1)
575
606
  scaleX = (scaleX < 0 ? -1 : 1) / worldBoxBounds.width;
576
- if (Math.abs(scaleY * worldBoxBounds.height) < 1)
607
+ if (useScaleY && Math.abs(scaleY * worldBoxBounds.height) < 1)
577
608
  scaleY = (scaleY < 0 ? -1 : 1) / worldBoxBounds.height;
609
+ if (lockRatio && scaleX !== scaleY)
610
+ scaleY = scaleX = Math.min(scaleX, scaleY);
578
611
  return { origin, scaleX, scaleY, direction, lockRatio, around };
579
612
  },
580
613
  getRotateData(bounds, direction, current, last, around) {
@@ -745,7 +778,7 @@ class EditBox extends draw.Group {
745
778
  this.view = new draw.Group();
746
779
  this.rect = new draw.Box({ name: 'rect', hitFill: 'all', hitStroke: 'none', strokeAlign: 'center', hitRadius: 5 });
747
780
  this.circle = new EditPoint({ name: 'circle', strokeAlign: 'center', around: 'center', cursor: 'crosshair', hitRadius: 5 });
748
- this.buttons = new draw.Group({ around: 'center', hitSelf: false });
781
+ this.buttons = new draw.Group({ around: 'center', hitSelf: false, visible: 0 });
749
782
  this.resizePoints = [];
750
783
  this.rotatePoints = [];
751
784
  this.resizeLines = [];
@@ -800,12 +833,13 @@ class EditBox extends draw.Group {
800
833
  }
801
834
  }
802
835
  update(bounds) {
803
- const { mergeConfig, element, multiple } = this.editor;
804
- const { middlePoint, resizeable, rotateable, hideOnSmall, editBox } = mergeConfig;
836
+ const { rect, circle, buttons, resizePoints, rotatePoints, resizeLines, editor } = this;
837
+ const { mergeConfig, element, multiple, editMask } = editor;
838
+ const { middlePoint, resizeable, rotateable, hideOnSmall, editBox, mask } = mergeConfig;
805
839
  this.visible = !element.locked;
840
+ editMask.visible = mask ? true : 0;
806
841
  if (this.view.worldOpacity) {
807
842
  const { width, height } = bounds;
808
- const { rect, circle, buttons, resizePoints, rotatePoints, resizeLines } = this;
809
843
  const smallSize = typeof hideOnSmall === 'number' ? hideOnSmall : 10;
810
844
  const showPoints = editBox && !(hideOnSmall && width < smallSize && height < smallSize);
811
845
  let point = {}, rotateP, resizeP, resizeL;
@@ -840,10 +874,12 @@ class EditBox extends draw.Group {
840
874
  if (rect.path)
841
875
  rect.path = null;
842
876
  rect.set(Object.assign(Object.assign({}, bounds), { visible: multiple ? true : editBox }));
843
- buttons.visible = showPoints && buttons.children.length > 0;
877
+ buttons.visible = showPoints && buttons.children.length > 0 || 0;
844
878
  if (buttons.visible)
845
879
  this.layoutButtons(mergeConfig);
846
880
  }
881
+ else
882
+ rect.set(bounds);
847
883
  }
848
884
  layoutCircle(config) {
849
885
  const { circleDirection, circleMargin, buttonsMargin, buttonsDirection, middlePoint } = config;
@@ -906,7 +942,7 @@ class EditBox extends draw.Group {
906
942
  }
907
943
  onDragStart(e) {
908
944
  this.dragging = true;
909
- const point = this.dragPoint = e.current;
945
+ const point = this.dragPoint = e.current, { pointType } = point;
910
946
  const { editor, dragStartData } = this, { element } = editor;
911
947
  if (point.name === 'rect') {
912
948
  this.moving = true;
@@ -917,43 +953,53 @@ class EditBox extends draw.Group {
917
953
  dragStartData.point = { x: element.x, y: element.y };
918
954
  dragStartData.bounds = Object.assign({}, element.getLayoutBounds('box', 'local'));
919
955
  dragStartData.rotation = element.rotation;
956
+ if (pointType && pointType.includes('resize'))
957
+ draw.ResizeEvent.resizingKeys = editor.leafList.keys;
920
958
  }
921
959
  onDragEnd(e) {
922
960
  this.dragging = false;
923
961
  this.dragPoint = null;
924
962
  this.moving = false;
925
- if (e.current.name === 'rect')
963
+ const { name, pointType } = e.current;
964
+ if (name === 'rect')
926
965
  this.editor.opacity = 1;
966
+ if (pointType && pointType.includes('resize'))
967
+ draw.ResizeEvent.resizingKeys = null;
927
968
  }
928
969
  onDrag(e) {
929
970
  const { editor } = this;
930
971
  const { pointType } = this.enterPoint = e.current;
931
- if (pointType.includes('rotate') || e.metaKey || e.ctrlKey || !editor.mergeConfig.resizeable)
972
+ if (pointType.includes('rotate') || e.metaKey || e.ctrlKey || !editor.mergeConfig.resizeable) {
932
973
  editor.onRotate(e);
933
- if (pointType.includes('resize'))
974
+ if (pointType === 'resize-rotate')
975
+ editor.onScale(e);
976
+ }
977
+ else if (pointType === 'resize')
934
978
  editor.onScale(e);
935
979
  if (pointType === 'skew')
936
980
  editor.onSkew(e);
937
981
  updateCursor(editor, e);
938
982
  }
939
983
  onArrow(e) {
940
- if (this.editor.editing && this.editor.mergeConfig.keyEvent) {
941
- const move = { x: 0, y: 0 };
984
+ const { editor } = this;
985
+ if (editor.editing && editor.mergeConfig.keyEvent) {
986
+ let x = 0, y = 0;
942
987
  const distance = e.shiftKey ? 10 : 1;
943
988
  switch (e.code) {
944
989
  case 'ArrowDown':
945
- move.y = distance;
990
+ y = distance;
946
991
  break;
947
992
  case 'ArrowUp':
948
- move.y = -distance;
993
+ y = -distance;
949
994
  break;
950
995
  case 'ArrowLeft':
951
- move.x = -distance;
996
+ x = -distance;
952
997
  break;
953
998
  case 'ArrowRight':
954
- move.x = distance;
999
+ x = distance;
955
1000
  }
956
- this.editor.move(move);
1001
+ if (x || y)
1002
+ editor.move(x, y);
957
1003
  }
958
1004
  }
959
1005
  onDoubleTap(e) {
@@ -1019,22 +1065,32 @@ class EditBox extends draw.Group {
1019
1065
  }
1020
1066
  }
1021
1067
 
1068
+ const bigBounds = { x: 0, y: 0, width: 100000, height: 100000 };
1022
1069
  class EditMask extends draw.UI {
1023
1070
  constructor(editor) {
1024
1071
  super();
1025
1072
  this.editor = editor;
1026
1073
  this.hittable = false;
1074
+ this.visible = 0;
1075
+ }
1076
+ __updateWorldBounds() {
1077
+ Object.assign(this.__local, bigBounds);
1078
+ Object.assign(this.__world, bigBounds);
1027
1079
  }
1028
1080
  __draw(canvas, options) {
1029
- const { editor } = this;
1030
- const { mask } = editor.mergeConfig;
1031
- if (mask && editor.list.length) {
1032
- const { rect } = editor.editBox;
1033
- const { width, height } = rect.__;
1034
- canvas.resetTransform();
1081
+ const { editor } = this, { mask } = editor.mergedConfig;
1082
+ if (mask && editor.editing) {
1035
1083
  canvas.fillWorld(canvas.bounds, mask === true ? 'rgba(0,0,0,0.8)' : mask);
1036
- canvas.setWorld(rect.__world, options.matrix);
1037
- canvas.clearRect(0, 0, width, height);
1084
+ if (options.bounds && !options.bounds.hit(editor.editBox.rect.__world, options.matrix))
1085
+ return;
1086
+ canvas.saveBlendMode('destination-out');
1087
+ editor.list.forEach(item => {
1088
+ item.__renderShape(canvas, options);
1089
+ const { __box, parent } = item;
1090
+ if ((item = __box) || ((item = parent) && parent.textBox))
1091
+ item.__renderShape(canvas, options);
1092
+ });
1093
+ canvas.restoreBlendMode();
1038
1094
  }
1039
1095
  }
1040
1096
  destroy() {
@@ -1337,22 +1393,6 @@ class SimulateElement extends draw.Rect {
1337
1393
  }
1338
1394
 
1339
1395
  class Editor extends draw.Group {
1340
- get mergeConfig() {
1341
- const { config, element, dragPoint } = this, mergeConfig = Object.assign({}, config);
1342
- if (element && element.editConfig)
1343
- Object.assign(mergeConfig, element.editConfig);
1344
- if (dragPoint) {
1345
- if (dragPoint.editConfig)
1346
- Object.assign(mergeConfig, dragPoint.editConfig);
1347
- if (mergeConfig.editSize === 'font-size')
1348
- mergeConfig.lockRatio = true;
1349
- if (dragPoint.pointType === 'resize-rotate') {
1350
- mergeConfig.around || (mergeConfig.around = 'center');
1351
- draw.isNull(mergeConfig.lockRatio) && (mergeConfig.lockRatio = true);
1352
- }
1353
- }
1354
- return mergeConfig;
1355
- }
1356
1396
  get list() { return this.leafList.list; }
1357
1397
  get dragHoverExclude() { return [this.editBox.rect]; }
1358
1398
  get editing() { return !!this.list.length; }
@@ -1366,7 +1406,6 @@ class Editor extends draw.Group {
1366
1406
  get buttons() { return this.editBox.buttons; }
1367
1407
  constructor(userConfig, data) {
1368
1408
  super(data);
1369
- this.config = draw.DataHelper.clone(config);
1370
1409
  this.leafList = new draw.LeafList();
1371
1410
  this.openedGroupList = new draw.LeafList();
1372
1411
  this.simulateTarget = new SimulateElement(this);
@@ -1375,8 +1414,10 @@ class Editor extends draw.Group {
1375
1414
  this.selector = new EditSelect(this);
1376
1415
  this.editMask = new EditMask(this);
1377
1416
  this.targetEventIds = [];
1417
+ let mergedConfig = draw.DataHelper.clone(config);
1378
1418
  if (userConfig)
1379
- this.config = draw.DataHelper.default(userConfig, this.config);
1419
+ mergedConfig = draw.DataHelper.default(userConfig, mergedConfig);
1420
+ this.mergedConfig = this.config = mergedConfig;
1380
1421
  this.addMany(this.editMask, this.selector, this.editBox);
1381
1422
  if (!core.Plugin.has('resize'))
1382
1423
  this.config.editSize = 'scale';
@@ -1716,8 +1757,7 @@ class Editor extends draw.Group {
1716
1757
  }
1717
1758
  }
1718
1759
  emitInnerEvent(type) {
1719
- const { innerEditor } = this;
1720
- const { editTarget } = innerEditor;
1760
+ const { innerEditor } = this, { editTarget } = innerEditor;
1721
1761
  const event = new InnerEditorEvent(type, { editTarget, innerEditor });
1722
1762
  this.emitEvent(event);
1723
1763
  editTarget.emitEvent(event);
@@ -1742,25 +1782,40 @@ class Editor extends draw.Group {
1742
1782
  this.leafList.update();
1743
1783
  }
1744
1784
  }
1785
+ onAppRenderStart(app) {
1786
+ if (this.targetChanged = app.children.some(leafer => leafer !== this.leafer && leafer.renderer.changed))
1787
+ this.editBox.forceRender();
1788
+ }
1789
+ onRenderStart() {
1790
+ if (this.targetChanged)
1791
+ this.update();
1792
+ }
1793
+ onKey(e) {
1794
+ updateCursor(this, e);
1795
+ }
1745
1796
  listenTargetEvents() {
1746
1797
  if (!this.targetEventIds.length) {
1747
- const { app, leafer } = this;
1798
+ const { app, leafer, editBox, editMask } = this;
1748
1799
  this.targetEventIds = [
1749
- leafer.on_(draw.RenderEvent.START, this.update, this),
1750
- app.on_(draw.RenderEvent.CHILD_START, this.forceRender, this),
1800
+ leafer.on_(draw.RenderEvent.START, this.onRenderStart, this),
1801
+ app.on_(draw.RenderEvent.CHILD_START, this.onAppRenderStart, this),
1751
1802
  app.on_(core.MoveEvent.BEFORE_MOVE, this.onMove, this, true),
1752
1803
  app.on_(core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true),
1753
1804
  app.on_(core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true),
1754
- app.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); }),
1755
- app.on_(core.KeyEvent.DOWN, this.editBox.onArrow, this.editBox)
1805
+ app.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], this.onKey, this),
1806
+ app.on_(core.KeyEvent.DOWN, editBox.onArrow, editBox)
1756
1807
  ];
1808
+ if (editMask.visible)
1809
+ editMask.forceRender();
1757
1810
  }
1758
1811
  }
1759
1812
  removeTargetEvents() {
1760
- const { targetEventIds } = this;
1813
+ const { targetEventIds, editMask } = this;
1761
1814
  if (targetEventIds.length) {
1762
1815
  this.off_(targetEventIds);
1763
1816
  targetEventIds.length = 0;
1817
+ if (editMask.visible)
1818
+ editMask.forceRender();
1764
1819
  }
1765
1820
  }
1766
1821
  destroy() {
@@ -1774,6 +1829,9 @@ class Editor extends draw.Group {
1774
1829
  }
1775
1830
  }
1776
1831
  }
1832
+ __decorate([
1833
+ mergeConfigAttr()
1834
+ ], Editor.prototype, "mergeConfig", void 0);
1777
1835
  __decorate([
1778
1836
  targetAttr(onHover)
1779
1837
  ], Editor.prototype, "hoverTarget", void 0);
@@ -1838,9 +1896,7 @@ exports.EditTool = class EditTool extends InnerEditor {
1838
1896
  const { moveX, moveY, editor } = e;
1839
1897
  const { app, list } = editor;
1840
1898
  app.lockLayout();
1841
- list.forEach(target => {
1842
- target.moveWorld(moveX, moveY);
1843
- });
1899
+ list.forEach(target => { target.moveWorld(moveX, moveY); });
1844
1900
  app.unlockLayout();
1845
1901
  }
1846
1902
  onScale(e) {
@@ -1849,12 +1905,10 @@ exports.EditTool = class EditTool extends InnerEditor {
1849
1905
  app.lockLayout();
1850
1906
  list.forEach(target => {
1851
1907
  const resize = editor.getEditSize(target) !== 'scale';
1852
- if (transform) {
1908
+ if (transform)
1853
1909
  target.transformWorld(transform, resize);
1854
- }
1855
- else {
1910
+ else
1856
1911
  target.scaleOfWorld(worldOrigin, scaleX, scaleY, resize);
1857
- }
1858
1912
  });
1859
1913
  app.unlockLayout();
1860
1914
  }
@@ -1864,12 +1918,10 @@ exports.EditTool = class EditTool extends InnerEditor {
1864
1918
  app.lockLayout();
1865
1919
  list.forEach(target => {
1866
1920
  const resize = editor.getEditSize(target) !== 'scale';
1867
- if (transform) {
1921
+ if (transform)
1868
1922
  target.transformWorld(transform, resize);
1869
- }
1870
- else {
1923
+ else
1871
1924
  target.rotateOfWorld(worldOrigin, rotation);
1872
- }
1873
1925
  });
1874
1926
  app.unlockLayout();
1875
1927
  }
@@ -1879,12 +1931,10 @@ exports.EditTool = class EditTool extends InnerEditor {
1879
1931
  app.lockLayout();
1880
1932
  list.forEach(target => {
1881
1933
  const resize = editor.getEditSize(target) !== 'scale';
1882
- if (transform) {
1934
+ if (transform)
1883
1935
  target.transformWorld(transform, resize);
1884
- }
1885
- else {
1936
+ else
1886
1937
  target.skewOfWorld(worldOrigin, skewX, skewY, resize);
1887
- }
1888
1938
  });
1889
1939
  app.unlockLayout();
1890
1940
  }