@leafer-in/editor 1.0.4 → 1.0.6

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
@@ -129,8 +129,9 @@ function scaleResizePath(leaf, scaleX, scaleY) {
129
129
  leaf.path = leaf.__.path;
130
130
  }
131
131
  function scaleResizePoints(leaf, scaleX, scaleY) {
132
- PathScaler.scalePoints(leaf.__.points, scaleX, scaleY);
133
- leaf.points = leaf.__.points;
132
+ const { points } = leaf;
133
+ typeof points[0] === 'object' ? points.forEach(p => { p.x *= scaleX, p.y *= scaleY; }) : PathScaler.scalePoints(points, scaleX, scaleY);
134
+ leaf.points = points;
134
135
  }
135
136
  function scaleResizeGroup(group, scaleX, scaleY) {
136
137
  const { children } = group;
@@ -318,31 +319,29 @@ class Stroker extends draw.UI {
318
319
  const { list } = this;
319
320
  if (list.length) {
320
321
  let leaf;
321
- const { stroke, strokeWidth, fill } = this.__;
322
- const { bounds } = options;
322
+ const data = this.__, { stroke, strokeWidth, fill } = data, { bounds } = options;
323
323
  for (let i = 0; i < list.length; i++) {
324
324
  leaf = list[i];
325
- if (bounds && bounds.hit(leaf.__world, options.matrix)) {
326
- const aScaleX = abs(leaf.__world.scaleX), aScaleY = abs(leaf.__world.scaleY);
325
+ const { worldTransform, worldRenderBounds } = leaf;
326
+ if (bounds && bounds.hit(worldRenderBounds, options.matrix)) {
327
+ const aScaleX = abs(worldTransform.scaleX), aScaleY = abs(worldTransform.scaleY);
327
328
  if (aScaleX !== aScaleY) {
328
- copy$1(matrix, leaf.__world);
329
+ copy$1(matrix, worldTransform);
329
330
  scale(matrix, 1 / aScaleX, 1 / aScaleY);
330
331
  canvas.setWorld(matrix, options.matrix);
331
332
  canvas.beginPath();
332
- this.__.strokeWidth = strokeWidth;
333
+ data.strokeWidth = strokeWidth;
333
334
  const { x, y, width, height } = leaf.__layout.boxBounds;
334
335
  canvas.rect(x * aScaleX, y * aScaleY, width * aScaleX, height * aScaleY);
335
336
  }
336
337
  else {
337
- canvas.setWorld(leaf.__world, options.matrix);
338
+ canvas.setWorld(worldTransform, options.matrix);
338
339
  canvas.beginPath();
339
- if (leaf.__.__useArrow) {
340
+ if (leaf.__.__useArrow)
340
341
  leaf.__drawPath(canvas);
341
- }
342
- else {
342
+ else
343
343
  leaf.__.__pathForRender ? leaf.__drawRenderPath(canvas) : leaf.__drawPathByBox(canvas);
344
- }
345
- this.__.strokeWidth = strokeWidth / abs(leaf.__world.scaleX);
344
+ data.strokeWidth = strokeWidth / abs(worldTransform.scaleX);
346
345
  }
347
346
  if (stroke)
348
347
  typeof stroke === 'string' ? draw.Paint.stroke(stroke, this, canvas) : draw.Paint.strokes(stroke, this, canvas);
@@ -350,7 +349,7 @@ class Stroker extends draw.UI {
350
349
  typeof fill === 'string' ? draw.Paint.fill(fill, this, canvas) : draw.Paint.fills(fill, this, canvas);
351
350
  }
352
351
  }
353
- this.__.strokeWidth = strokeWidth;
352
+ data.strokeWidth = strokeWidth;
354
353
  }
355
354
  }
356
355
  destroy() {
@@ -901,7 +900,6 @@ class EditBox extends draw.Group {
901
900
  this.editor = editor;
902
901
  this.visible = false;
903
902
  this.create();
904
- this.rect.syncEventer = editor;
905
903
  this.__listenEvents();
906
904
  }
907
905
  create() {
@@ -941,8 +939,11 @@ class EditBox extends draw.Group {
941
939
  circle.set(this.getPointStyle(mergeConfig.circle || mergeConfig.rotatePoint || pointsStyle[0]));
942
940
  rect.set(Object.assign({ stroke, strokeWidth }, (mergeConfig.rect || {})));
943
941
  rect.hittable = !single && !!moveable;
944
- element.syncEventer = (single && moveable) ? rect : null;
945
- this.app.interaction.bottomList = (single && moveable) ? [{ target: rect, proxy: element }] : null;
942
+ rect.syncEventer = single && this.editor;
943
+ if (single && moveable) {
944
+ element.syncEventer = rect;
945
+ this.app.interaction.bottomList = [{ target: rect, proxy: element }];
946
+ }
946
947
  }
947
948
  update(bounds) {
948
949
  this.visible = !this.editor.element.locked;
@@ -1114,6 +1115,11 @@ class EditBox extends draw.Group {
1114
1115
  if (editor.single) {
1115
1116
  const { element } = editor;
1116
1117
  if (element.isBranch) {
1118
+ if (element.textBox) {
1119
+ const find = element.children.find(item => item.editable && item instanceof draw.Text);
1120
+ if (find)
1121
+ return editor.openInnerEditor(find);
1122
+ }
1117
1123
  editor.openGroup(element);
1118
1124
  editor.target = editor.selector.findDeepOne(e);
1119
1125
  }
@@ -1259,12 +1265,12 @@ const config = {
1259
1265
  skewable: true
1260
1266
  };
1261
1267
 
1268
+ const bounds = new draw.Bounds();
1262
1269
  function simulate(editor) {
1263
- const { simulateTarget, leafList: targetList } = editor;
1264
- const { x, y, width, height } = new draw.Bounds().setListWithFn(targetList.list, (leaf) => leaf.worldBoxBounds);
1265
- const parent = simulateTarget.parent = targetList.list[0].leafer.zoomLayer;
1266
- const { scaleX, scaleY, e: worldX, f: worldY } = parent.__world;
1267
- simulateTarget.reset({ x: (x - worldX) / scaleX, y: (y - worldY) / scaleY, width: width / scaleX, height: height / scaleY });
1270
+ const { simulateTarget, list } = editor;
1271
+ const { zoomLayer } = list[0].leafer.zoomLayer;
1272
+ simulateTarget.safeChange(() => simulateTarget.reset(bounds.setListWithFn(list, (leaf) => leaf.getBounds('box', 'page')).get()));
1273
+ zoomLayer.add(simulateTarget);
1268
1274
  }
1269
1275
 
1270
1276
  function onTarget(editor, oldValue) {
@@ -1273,6 +1279,7 @@ function onTarget(editor, oldValue) {
1273
1279
  editor.leafList = target instanceof draw.LeafList ? target : new draw.LeafList(target instanceof Array ? target : target);
1274
1280
  }
1275
1281
  else {
1282
+ editor.simulateTarget.remove();
1276
1283
  editor.leafList.reset();
1277
1284
  editor.closeInnerEditor();
1278
1285
  }
@@ -1327,7 +1334,7 @@ const EditorHelper = {
1327
1334
  const ungroupList = [];
1328
1335
  app.lockLayout();
1329
1336
  list.forEach(leaf => {
1330
- if (leaf.isBranch) {
1337
+ if (leaf.isBranch && !leaf.isBranchLeaf) {
1331
1338
  const { parent, children } = leaf;
1332
1339
  while (children.length) {
1333
1340
  ungroupList.push(children[0]);
@@ -1398,17 +1405,77 @@ EditorGroupEvent.UNGROUP = 'editor.ungroup';
1398
1405
  EditorGroupEvent.OPEN = 'editor.open_group';
1399
1406
  EditorGroupEvent.CLOSE = 'editor.close_group';
1400
1407
 
1408
+ const { updateMatrix } = draw.LeafHelper;
1409
+ const checkMap = { x: 1, y: 1, scaleX: 1, scaleY: 1, rotation: 1, skewX: 1, skewY: 1 }, origin = 'top-left';
1410
+ class SimulateElement extends draw.Rect {
1411
+ get __tag() { return 'SimulateElement'; }
1412
+ constructor(editor) {
1413
+ super();
1414
+ this.checkChange = true;
1415
+ this.canChange = true;
1416
+ this.visible = this.hittable = false;
1417
+ this.on(draw.PropertyEvent.CHANGE, (event) => {
1418
+ if (this.checkChange && checkMap[event.attrName]) {
1419
+ const { attrName, newValue, oldValue } = event;
1420
+ const addValue = attrName[0] === 's' ? (newValue || 1) / (oldValue || 1) : (newValue || 0) - (oldValue || 0);
1421
+ this.canChange = false;
1422
+ const data = this.__;
1423
+ data[attrName] = oldValue;
1424
+ updateMatrix(this.parent);
1425
+ updateMatrix(this);
1426
+ const oldMatrix = new draw.Matrix(this.__world);
1427
+ data[attrName] = newValue;
1428
+ this.__layout.rotationChange();
1429
+ updateMatrix(this);
1430
+ this.changedTransform = new draw.Matrix(this.__world).divide(oldMatrix);
1431
+ switch (attrName) {
1432
+ case 'x':
1433
+ editor.move(addValue, 0);
1434
+ break;
1435
+ case 'y':
1436
+ editor.move(0, addValue);
1437
+ break;
1438
+ case 'rotation':
1439
+ editor.rotateOf(origin, addValue);
1440
+ break;
1441
+ case 'scaleX':
1442
+ editor.scaleOf(origin, addValue, 1);
1443
+ break;
1444
+ case 'scaleY':
1445
+ editor.scaleOf(origin, 1, addValue);
1446
+ break;
1447
+ case 'skewX':
1448
+ editor.skewOf(origin, addValue, 0);
1449
+ break;
1450
+ case 'skewY':
1451
+ editor.skewOf(origin, 0, addValue);
1452
+ }
1453
+ this.canChange = true;
1454
+ }
1455
+ });
1456
+ }
1457
+ safeChange(changeFn) {
1458
+ if (this.canChange) {
1459
+ this.checkChange = false;
1460
+ changeFn();
1461
+ this.checkChange = true;
1462
+ }
1463
+ }
1464
+ }
1465
+
1401
1466
  class Editor extends draw.Group {
1402
1467
  get mergeConfig() {
1403
1468
  const { element, config } = this;
1404
1469
  return this.single && element.editConfig ? Object.assign(Object.assign({}, config), element.editConfig) : config;
1405
1470
  }
1406
1471
  get list() { return this.leafList.list; }
1472
+ get dragHoverExclude() { return [this.editBox.rect]; }
1407
1473
  get editing() { return !!this.list.length; }
1408
1474
  get groupOpening() { return !!this.openedGroupList.length; }
1409
1475
  get multiple() { return this.list.length > 1; }
1410
1476
  get single() { return this.list.length === 1; }
1411
1477
  get dragging() { return this.editBox.dragging; }
1478
+ get moving() { return this.editBox.moving; }
1412
1479
  get element() { return this.multiple ? this.simulateTarget : this.list[0]; }
1413
1480
  get buttons() { return this.editBox.buttons; }
1414
1481
  constructor(userConfig, data) {
@@ -1416,7 +1483,7 @@ class Editor extends draw.Group {
1416
1483
  this.config = draw.DataHelper.clone(config);
1417
1484
  this.leafList = new draw.LeafList();
1418
1485
  this.openedGroupList = new draw.LeafList();
1419
- this.simulateTarget = new draw.Rect({ visible: false });
1486
+ this.simulateTarget = new SimulateElement(this);
1420
1487
  this.editBox = new EditBox(this);
1421
1488
  this.editToolList = {};
1422
1489
  this.selector = new EditSelect(this);
@@ -1554,18 +1621,18 @@ class Editor extends draw.Group {
1554
1621
  this.skewOf(origin, skewX, skewY);
1555
1622
  }
1556
1623
  move(x, y = 0) {
1557
- if (!this.mergeConfig.moveable || this.element.locked)
1624
+ if (!this.checkTransform('moveable'))
1558
1625
  return;
1559
1626
  const { element } = this;
1560
1627
  const world = element.getWorldPointByLocal(typeof x === 'object' ? Object.assign({}, x) : { x, y }, null, true);
1628
+ if (this.multiple)
1629
+ element.safeChange(() => element.move(x, y));
1561
1630
  const event = new EditorMoveEvent(EditorMoveEvent.MOVE, { target: element, editor: this, moveX: world.x, moveY: world.y });
1562
1631
  this.editTool.onMove(event);
1563
1632
  this.emitEvent(event);
1564
- if (this.multiple)
1565
- element.move(x, y);
1566
1633
  }
1567
1634
  scaleWithDrag(data) {
1568
- if (!this.mergeConfig.resizeable || this.element.locked)
1635
+ if (!this.checkTransform('resizeable'))
1569
1636
  return;
1570
1637
  const { element } = this;
1571
1638
  const event = new EditorScaleEvent(EditorScaleEvent.SCALE, Object.assign(Object.assign({}, data), { target: element, editor: this, worldOrigin: element.getWorldPoint(data.origin) }));
@@ -1573,50 +1640,53 @@ class Editor extends draw.Group {
1573
1640
  this.emitEvent(event);
1574
1641
  }
1575
1642
  scaleOf(origin, scaleX, scaleY = scaleX, _resize) {
1576
- if (!this.mergeConfig.resizeable || this.element.locked)
1643
+ if (!this.checkTransform('resizeable'))
1577
1644
  return;
1578
1645
  const { element } = this;
1579
1646
  const worldOrigin = this.getWorldOrigin(origin);
1580
- const transform = this.multiple && this.getChangedTransform(() => element.scaleOf(origin, scaleX, scaleY));
1647
+ const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.scaleOf(origin, scaleX, scaleY)));
1581
1648
  const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX, scaleY, transform });
1582
1649
  this.editTool.onScale(event);
1583
1650
  this.emitEvent(event);
1584
1651
  }
1585
1652
  flip(axis) {
1586
- if (this.element.locked)
1653
+ if (!this.checkTransform('resizeable'))
1587
1654
  return;
1588
1655
  const { element } = this;
1589
1656
  const worldOrigin = this.getWorldOrigin('center');
1590
- const transform = this.multiple ? this.getChangedTransform(() => element.flip(axis)) : new draw.Matrix(draw.LeafHelper.getFlipTransform(element, axis));
1657
+ const transform = this.multiple ? this.getChangedTransform(() => element.safeChange(() => element.flip(axis))) : new draw.Matrix(draw.LeafHelper.getFlipTransform(element, axis));
1591
1658
  const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX: axis === 'x' ? -1 : 1, scaleY: axis === 'y' ? -1 : 1, transform });
1592
1659
  this.editTool.onScale(event);
1593
1660
  this.emitEvent(event);
1594
1661
  }
1595
1662
  rotateOf(origin, rotation) {
1596
- if (!this.mergeConfig.rotateable || this.element.locked)
1663
+ if (!this.checkTransform('rotateable'))
1597
1664
  return;
1598
1665
  const { element } = this;
1599
1666
  const worldOrigin = this.getWorldOrigin(origin);
1600
- const transform = this.multiple && this.getChangedTransform(() => element.rotateOf(origin, rotation));
1667
+ const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.rotateOf(origin, rotation)));
1601
1668
  const event = new EditorRotateEvent(EditorRotateEvent.ROTATE, { target: element, editor: this, worldOrigin, rotation, transform });
1602
1669
  this.editTool.onRotate(event);
1603
1670
  this.emitEvent(event);
1604
1671
  }
1605
1672
  skewOf(origin, skewX, skewY = 0, _resize) {
1606
- if (!this.mergeConfig.skewable || this.element.locked)
1673
+ if (!this.checkTransform('skewable'))
1607
1674
  return;
1608
1675
  const { element } = this;
1609
1676
  const worldOrigin = this.getWorldOrigin(origin);
1610
- const transform = this.multiple && this.getChangedTransform(() => element.skewOf(origin, skewX, skewY));
1677
+ const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.skewOf(origin, skewX, skewY)));
1611
1678
  const event = new EditorSkewEvent(EditorSkewEvent.SKEW, { target: element, editor: this, worldOrigin, skewX, skewY, transform });
1612
1679
  this.editTool.onSkew(event);
1613
1680
  this.emitEvent(event);
1614
1681
  }
1682
+ checkTransform(type) { return this.element && !this.element.locked && this.mergeConfig[type]; }
1615
1683
  getWorldOrigin(origin) {
1616
1684
  return this.element.getWorldPoint(draw.LeafHelper.getInnerOrigin(this.element, origin));
1617
1685
  }
1618
1686
  getChangedTransform(func) {
1619
1687
  const { element } = this;
1688
+ if (this.multiple && !element.canChange)
1689
+ return element.changedTransform;
1620
1690
  const oldMatrix = new draw.Matrix(element.worldTransform);
1621
1691
  func();
1622
1692
  return new draw.Matrix(element.worldTransform).divide(oldMatrix);
@@ -1673,11 +1743,11 @@ class Editor extends draw.Group {
1673
1743
  this.emitEvent(event);
1674
1744
  group.emitEvent(event);
1675
1745
  }
1676
- openInnerEditor(target) {
1677
- if (target)
1746
+ openInnerEditor(target, select) {
1747
+ if (target && select)
1678
1748
  this.target = target;
1679
1749
  if (this.single) {
1680
- const editTarget = this.element;
1750
+ const editTarget = target || this.element;
1681
1751
  const tag = editTarget.editInner;
1682
1752
  if (tag && EditToolCreator.list[tag]) {
1683
1753
  this.editTool.unload();
@@ -1729,14 +1799,15 @@ class Editor extends draw.Group {
1729
1799
  }
1730
1800
  listenTargetEvents() {
1731
1801
  if (!this.targetEventIds.length) {
1732
- const { leafer } = this.list[0];
1802
+ const { app, leafer } = this;
1733
1803
  this.targetEventIds = [
1734
- this.app.on_(core.MoveEvent.BEFORE_MOVE, this.onMove, this, true),
1735
- this.app.on_(core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true),
1736
- this.app.on_(core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true),
1737
1804
  leafer.on_(draw.RenderEvent.START, this.update, this),
1738
- leafer.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); }),
1739
- leafer.on_(core.KeyEvent.DOWN, this.editBox.onArrow, this.editBox)
1805
+ app.on_(draw.RenderEvent.CHILD_START, this.forceRender, this),
1806
+ app.on_(core.MoveEvent.BEFORE_MOVE, this.onMove, this, true),
1807
+ app.on_(core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true),
1808
+ app.on_(core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true),
1809
+ app.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); }),
1810
+ app.on_(core.KeyEvent.DOWN, this.editBox.onArrow, this.editBox)
1740
1811
  ];
1741
1812
  }
1742
1813
  }
@@ -1752,7 +1823,8 @@ class Editor extends draw.Group {
1752
1823
  this.simulateTarget.destroy();
1753
1824
  Object.values(this.editToolList).forEach(item => item.destroy());
1754
1825
  this.editToolList = {};
1755
- this.target = this.hoverTarget = this.simulateTarget = this.editTool = this.innerEditor = null;
1826
+ this.target = this.hoverTarget = null;
1827
+ this.simulateTarget = this.editTool = this.innerEditor = null;
1756
1828
  super.destroy();
1757
1829
  }
1758
1830
  }
@@ -1869,10 +1941,7 @@ exports.EditTool = class EditTool extends InnerEditor {
1869
1941
  }
1870
1942
  update() {
1871
1943
  const { editor, editBox } = this;
1872
- const { simulateTarget, element } = editor;
1873
- if (editor.multiple)
1874
- simulateTarget.parent.updateLayout();
1875
- const { x, y, scaleX, scaleY, rotation, skewX, skewY, width, height } = element.getLayoutBounds('box', editor, true);
1944
+ const { x, y, scaleX, scaleY, rotation, skewX, skewY, width, height } = editor.element.getLayoutBounds('box', editor, true);
1876
1945
  editBox.set({ x, y, scaleX, scaleY, rotation, skewX, skewY });
1877
1946
  editBox.update({ x: 0, y: 0, width, height });
1878
1947
  this.onUpdate();
@@ -1887,7 +1956,7 @@ exports.EditTool = __decorate([
1887
1956
  ], exports.EditTool);
1888
1957
 
1889
1958
  const { left, right } = draw.Direction9;
1890
- const { move, copy } = draw.PointHelper;
1959
+ const { move, copy, toNumberPoints } = draw.PointHelper;
1891
1960
  exports.LineEditTool = class LineEditTool extends exports.EditTool {
1892
1961
  constructor() {
1893
1962
  super(...arguments);
@@ -1929,14 +1998,8 @@ exports.LineEditTool = class LineEditTool extends exports.EditTool {
1929
1998
  }
1930
1999
  getInnerMove(ui, event, lockRatio) {
1931
2000
  const movePoint = event.getInnerMove(ui);
1932
- if (lockRatio) {
1933
- if (Math.abs(movePoint.x) > Math.abs(movePoint.y)) {
1934
- movePoint.y = 0;
1935
- }
1936
- else {
1937
- movePoint.x = 0;
1938
- }
1939
- }
2001
+ if (lockRatio)
2002
+ Math.abs(movePoint.x) > Math.abs(movePoint.y) ? movePoint.y = 0 : movePoint.x = 0;
1940
2003
  return movePoint;
1941
2004
  }
1942
2005
  getFromToByPath(path) {
@@ -1945,7 +2008,8 @@ exports.LineEditTool = class LineEditTool extends exports.EditTool {
1945
2008
  to: { x: path[4], y: path[5] }
1946
2009
  };
1947
2010
  }
1948
- getFromToByPoints(points) {
2011
+ getFromToByPoints(originPoints) {
2012
+ const points = toNumberPoints(originPoints);
1949
2013
  return {
1950
2014
  from: { x: points[0], y: points[1] },
1951
2015
  to: { x: points[2], y: points[3] }
@@ -1998,6 +2062,15 @@ exports.LineEditTool = __decorate([
1998
2062
  ], exports.LineEditTool);
1999
2063
 
2000
2064
  draw.Creator.editor = function (options) { return new Editor(options); };
2065
+ draw.defineKey(draw.UI.prototype, 'editOuter', {
2066
+ get() { return this.__.__isLinePath ? 'LineEditTool' : 'EditTool'; }
2067
+ });
2068
+ draw.defineKey(draw.UI.prototype, 'editInner', {
2069
+ get() { return 'PathEditor'; }
2070
+ });
2071
+ draw.defineKey(draw.Text.prototype, 'editInner', {
2072
+ get() { return 'TextEditor'; }
2073
+ });
2001
2074
  draw.UI.setEditConfig = function (config) {
2002
2075
  draw.defineKey(this.prototype, 'editConfig', {
2003
2076
  get() { return typeof config === 'function' ? config(this) : config; }