@leafer-in/editor 1.0.3 → 1.0.5
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 +115 -49
- package/dist/editor.esm.js +116 -50
- package/dist/editor.esm.min.js +1 -1
- package/dist/editor.js +115 -49
- package/dist/editor.min.cjs +1 -1
- package/dist/editor.min.js +1 -1
- package/package.json +8 -8
- package/src/Editor.ts +34 -28
- package/src/display/EditBox.ts +11 -5
- package/src/display/SimulateElement.ts +87 -0
- package/src/display/Stroker.ts +12 -14
- package/src/editor/simulate.ts +6 -6
- package/src/editor/target.ts +1 -0
- package/src/helper/EditDataHelper.ts +0 -2
- package/src/helper/EditorHelper.ts +1 -1
- package/src/tool/EditTool.ts +1 -5
- package/types/index.d.ts +11 -8
package/dist/editor.cjs
CHANGED
|
@@ -318,31 +318,29 @@ class Stroker extends draw.UI {
|
|
|
318
318
|
const { list } = this;
|
|
319
319
|
if (list.length) {
|
|
320
320
|
let leaf;
|
|
321
|
-
const { stroke, strokeWidth, fill } =
|
|
322
|
-
const { bounds } = options;
|
|
321
|
+
const data = this.__, { stroke, strokeWidth, fill } = data, { bounds } = options;
|
|
323
322
|
for (let i = 0; i < list.length; i++) {
|
|
324
323
|
leaf = list[i];
|
|
325
|
-
|
|
326
|
-
|
|
324
|
+
const { worldTransform, worldRenderBounds } = leaf;
|
|
325
|
+
if (bounds && bounds.hit(worldRenderBounds, options.matrix)) {
|
|
326
|
+
const aScaleX = abs(worldTransform.scaleX), aScaleY = abs(worldTransform.scaleY);
|
|
327
327
|
if (aScaleX !== aScaleY) {
|
|
328
|
-
copy$1(matrix,
|
|
328
|
+
copy$1(matrix, worldTransform);
|
|
329
329
|
scale(matrix, 1 / aScaleX, 1 / aScaleY);
|
|
330
330
|
canvas.setWorld(matrix, options.matrix);
|
|
331
331
|
canvas.beginPath();
|
|
332
|
-
|
|
332
|
+
data.strokeWidth = strokeWidth;
|
|
333
333
|
const { x, y, width, height } = leaf.__layout.boxBounds;
|
|
334
334
|
canvas.rect(x * aScaleX, y * aScaleY, width * aScaleX, height * aScaleY);
|
|
335
335
|
}
|
|
336
336
|
else {
|
|
337
|
-
canvas.setWorld(
|
|
337
|
+
canvas.setWorld(worldTransform, options.matrix);
|
|
338
338
|
canvas.beginPath();
|
|
339
|
-
if (leaf.__.__useArrow)
|
|
339
|
+
if (leaf.__.__useArrow)
|
|
340
340
|
leaf.__drawPath(canvas);
|
|
341
|
-
|
|
342
|
-
else {
|
|
341
|
+
else
|
|
343
342
|
leaf.__.__pathForRender ? leaf.__drawRenderPath(canvas) : leaf.__drawPathByBox(canvas);
|
|
344
|
-
|
|
345
|
-
this.__.strokeWidth = strokeWidth / abs(leaf.__world.scaleX);
|
|
343
|
+
data.strokeWidth = strokeWidth / abs(worldTransform.scaleX);
|
|
346
344
|
}
|
|
347
345
|
if (stroke)
|
|
348
346
|
typeof stroke === 'string' ? draw.Paint.stroke(stroke, this, canvas) : draw.Paint.strokes(stroke, this, canvas);
|
|
@@ -350,7 +348,7 @@ class Stroker extends draw.UI {
|
|
|
350
348
|
typeof fill === 'string' ? draw.Paint.fill(fill, this, canvas) : draw.Paint.fills(fill, this, canvas);
|
|
351
349
|
}
|
|
352
350
|
}
|
|
353
|
-
|
|
351
|
+
data.strokeWidth = strokeWidth;
|
|
354
352
|
}
|
|
355
353
|
}
|
|
356
354
|
destroy() {
|
|
@@ -728,7 +726,6 @@ const EditDataHelper = {
|
|
|
728
726
|
scaleY = within(nowHeight * scaleY, heightRange) / nowHeight;
|
|
729
727
|
}
|
|
730
728
|
toPoint(around || align, boxBounds, origin, true);
|
|
731
|
-
console.log({ origin, scaleX, scaleY, direction, lockRatio, around });
|
|
732
729
|
return { origin, scaleX, scaleY, direction, lockRatio, around };
|
|
733
730
|
},
|
|
734
731
|
getRotateData(bounds, direction, current, last, around) {
|
|
@@ -902,7 +899,6 @@ class EditBox extends draw.Group {
|
|
|
902
899
|
this.editor = editor;
|
|
903
900
|
this.visible = false;
|
|
904
901
|
this.create();
|
|
905
|
-
this.rect.syncEventer = editor;
|
|
906
902
|
this.__listenEvents();
|
|
907
903
|
}
|
|
908
904
|
create() {
|
|
@@ -942,8 +938,11 @@ class EditBox extends draw.Group {
|
|
|
942
938
|
circle.set(this.getPointStyle(mergeConfig.circle || mergeConfig.rotatePoint || pointsStyle[0]));
|
|
943
939
|
rect.set(Object.assign({ stroke, strokeWidth }, (mergeConfig.rect || {})));
|
|
944
940
|
rect.hittable = !single && !!moveable;
|
|
945
|
-
|
|
946
|
-
|
|
941
|
+
rect.syncEventer = single && this.editor;
|
|
942
|
+
if (single && moveable) {
|
|
943
|
+
element.syncEventer = rect;
|
|
944
|
+
this.app.interaction.bottomList = [{ target: rect, proxy: element }];
|
|
945
|
+
}
|
|
947
946
|
}
|
|
948
947
|
update(bounds) {
|
|
949
948
|
this.visible = !this.editor.element.locked;
|
|
@@ -1115,6 +1114,11 @@ class EditBox extends draw.Group {
|
|
|
1115
1114
|
if (editor.single) {
|
|
1116
1115
|
const { element } = editor;
|
|
1117
1116
|
if (element.isBranch) {
|
|
1117
|
+
if (element.textBox) {
|
|
1118
|
+
const find = element.children.find(item => item.editable && item instanceof draw.Text);
|
|
1119
|
+
if (find)
|
|
1120
|
+
return editor.openInnerEditor(find);
|
|
1121
|
+
}
|
|
1118
1122
|
editor.openGroup(element);
|
|
1119
1123
|
editor.target = editor.selector.findDeepOne(e);
|
|
1120
1124
|
}
|
|
@@ -1260,12 +1264,12 @@ const config = {
|
|
|
1260
1264
|
skewable: true
|
|
1261
1265
|
};
|
|
1262
1266
|
|
|
1267
|
+
const bounds = new draw.Bounds();
|
|
1263
1268
|
function simulate(editor) {
|
|
1264
|
-
const { simulateTarget,
|
|
1265
|
-
const {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
simulateTarget.reset({ x: (x - worldX) / scaleX, y: (y - worldY) / scaleY, width: width / scaleX, height: height / scaleY });
|
|
1269
|
+
const { simulateTarget, list } = editor;
|
|
1270
|
+
const { zoomLayer } = list[0].leafer.zoomLayer;
|
|
1271
|
+
simulateTarget.safeChange(() => simulateTarget.reset(bounds.setListWithFn(list, (leaf) => leaf.getBounds('box', 'page')).get()));
|
|
1272
|
+
zoomLayer.add(simulateTarget);
|
|
1269
1273
|
}
|
|
1270
1274
|
|
|
1271
1275
|
function onTarget(editor, oldValue) {
|
|
@@ -1274,6 +1278,7 @@ function onTarget(editor, oldValue) {
|
|
|
1274
1278
|
editor.leafList = target instanceof draw.LeafList ? target : new draw.LeafList(target instanceof Array ? target : target);
|
|
1275
1279
|
}
|
|
1276
1280
|
else {
|
|
1281
|
+
editor.simulateTarget.remove();
|
|
1277
1282
|
editor.leafList.reset();
|
|
1278
1283
|
editor.closeInnerEditor();
|
|
1279
1284
|
}
|
|
@@ -1328,7 +1333,7 @@ const EditorHelper = {
|
|
|
1328
1333
|
const ungroupList = [];
|
|
1329
1334
|
app.lockLayout();
|
|
1330
1335
|
list.forEach(leaf => {
|
|
1331
|
-
if (leaf.isBranch) {
|
|
1336
|
+
if (leaf.isBranch && !leaf.isBranchLeaf) {
|
|
1332
1337
|
const { parent, children } = leaf;
|
|
1333
1338
|
while (children.length) {
|
|
1334
1339
|
ungroupList.push(children[0]);
|
|
@@ -1399,17 +1404,77 @@ EditorGroupEvent.UNGROUP = 'editor.ungroup';
|
|
|
1399
1404
|
EditorGroupEvent.OPEN = 'editor.open_group';
|
|
1400
1405
|
EditorGroupEvent.CLOSE = 'editor.close_group';
|
|
1401
1406
|
|
|
1407
|
+
const { updateMatrix } = draw.LeafHelper;
|
|
1408
|
+
const checkMap = { x: 1, y: 1, scaleX: 1, scaleY: 1, rotation: 1, skewX: 1, skewY: 1 }, origin = 'top-left';
|
|
1409
|
+
class SimulateElement extends draw.Rect {
|
|
1410
|
+
get __tag() { return 'SimulateElement'; }
|
|
1411
|
+
constructor(editor) {
|
|
1412
|
+
super();
|
|
1413
|
+
this.checkChange = true;
|
|
1414
|
+
this.canChange = true;
|
|
1415
|
+
this.visible = this.hittable = false;
|
|
1416
|
+
this.on(draw.PropertyEvent.CHANGE, (event) => {
|
|
1417
|
+
if (this.checkChange && checkMap[event.attrName]) {
|
|
1418
|
+
const { attrName, newValue, oldValue } = event;
|
|
1419
|
+
const addValue = attrName[0] === 's' ? (newValue || 1) / (oldValue || 1) : (newValue || 0) - (oldValue || 0);
|
|
1420
|
+
this.canChange = false;
|
|
1421
|
+
const data = this.__;
|
|
1422
|
+
data[attrName] = oldValue;
|
|
1423
|
+
updateMatrix(this.parent);
|
|
1424
|
+
updateMatrix(this);
|
|
1425
|
+
const oldMatrix = new draw.Matrix(this.__world);
|
|
1426
|
+
data[attrName] = newValue;
|
|
1427
|
+
this.__layout.rotationChange();
|
|
1428
|
+
updateMatrix(this);
|
|
1429
|
+
this.changedTransform = new draw.Matrix(this.__world).divide(oldMatrix);
|
|
1430
|
+
switch (attrName) {
|
|
1431
|
+
case 'x':
|
|
1432
|
+
editor.move(addValue, 0);
|
|
1433
|
+
break;
|
|
1434
|
+
case 'y':
|
|
1435
|
+
editor.move(0, addValue);
|
|
1436
|
+
break;
|
|
1437
|
+
case 'rotation':
|
|
1438
|
+
editor.rotateOf(origin, addValue);
|
|
1439
|
+
break;
|
|
1440
|
+
case 'scaleX':
|
|
1441
|
+
editor.scaleOf(origin, addValue, 1);
|
|
1442
|
+
break;
|
|
1443
|
+
case 'scaleY':
|
|
1444
|
+
editor.scaleOf(origin, 1, addValue);
|
|
1445
|
+
break;
|
|
1446
|
+
case 'skewX':
|
|
1447
|
+
editor.skewOf(origin, addValue, 0);
|
|
1448
|
+
break;
|
|
1449
|
+
case 'skewY':
|
|
1450
|
+
editor.skewOf(origin, 0, addValue);
|
|
1451
|
+
}
|
|
1452
|
+
this.canChange = true;
|
|
1453
|
+
}
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
safeChange(changeFn) {
|
|
1457
|
+
if (this.canChange) {
|
|
1458
|
+
this.checkChange = false;
|
|
1459
|
+
changeFn();
|
|
1460
|
+
this.checkChange = true;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1402
1465
|
class Editor extends draw.Group {
|
|
1403
1466
|
get mergeConfig() {
|
|
1404
1467
|
const { element, config } = this;
|
|
1405
1468
|
return this.single && element.editConfig ? Object.assign(Object.assign({}, config), element.editConfig) : config;
|
|
1406
1469
|
}
|
|
1407
1470
|
get list() { return this.leafList.list; }
|
|
1471
|
+
get dragHoverExclude() { return [this.editBox.rect]; }
|
|
1408
1472
|
get editing() { return !!this.list.length; }
|
|
1409
1473
|
get groupOpening() { return !!this.openedGroupList.length; }
|
|
1410
1474
|
get multiple() { return this.list.length > 1; }
|
|
1411
1475
|
get single() { return this.list.length === 1; }
|
|
1412
1476
|
get dragging() { return this.editBox.dragging; }
|
|
1477
|
+
get moving() { return this.editBox.moving; }
|
|
1413
1478
|
get element() { return this.multiple ? this.simulateTarget : this.list[0]; }
|
|
1414
1479
|
get buttons() { return this.editBox.buttons; }
|
|
1415
1480
|
constructor(userConfig, data) {
|
|
@@ -1417,7 +1482,7 @@ class Editor extends draw.Group {
|
|
|
1417
1482
|
this.config = draw.DataHelper.clone(config);
|
|
1418
1483
|
this.leafList = new draw.LeafList();
|
|
1419
1484
|
this.openedGroupList = new draw.LeafList();
|
|
1420
|
-
this.simulateTarget = new
|
|
1485
|
+
this.simulateTarget = new SimulateElement(this);
|
|
1421
1486
|
this.editBox = new EditBox(this);
|
|
1422
1487
|
this.editToolList = {};
|
|
1423
1488
|
this.selector = new EditSelect(this);
|
|
@@ -1555,18 +1620,18 @@ class Editor extends draw.Group {
|
|
|
1555
1620
|
this.skewOf(origin, skewX, skewY);
|
|
1556
1621
|
}
|
|
1557
1622
|
move(x, y = 0) {
|
|
1558
|
-
if (!this.
|
|
1623
|
+
if (!this.checkTransform('moveable'))
|
|
1559
1624
|
return;
|
|
1560
1625
|
const { element } = this;
|
|
1561
1626
|
const world = element.getWorldPointByLocal(typeof x === 'object' ? Object.assign({}, x) : { x, y }, null, true);
|
|
1627
|
+
if (this.multiple)
|
|
1628
|
+
element.safeChange(() => element.move(x, y));
|
|
1562
1629
|
const event = new EditorMoveEvent(EditorMoveEvent.MOVE, { target: element, editor: this, moveX: world.x, moveY: world.y });
|
|
1563
1630
|
this.editTool.onMove(event);
|
|
1564
1631
|
this.emitEvent(event);
|
|
1565
|
-
if (this.multiple)
|
|
1566
|
-
element.move(x, y);
|
|
1567
1632
|
}
|
|
1568
1633
|
scaleWithDrag(data) {
|
|
1569
|
-
if (!this.
|
|
1634
|
+
if (!this.checkTransform('resizeable'))
|
|
1570
1635
|
return;
|
|
1571
1636
|
const { element } = this;
|
|
1572
1637
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, Object.assign(Object.assign({}, data), { target: element, editor: this, worldOrigin: element.getWorldPoint(data.origin) }));
|
|
@@ -1574,50 +1639,53 @@ class Editor extends draw.Group {
|
|
|
1574
1639
|
this.emitEvent(event);
|
|
1575
1640
|
}
|
|
1576
1641
|
scaleOf(origin, scaleX, scaleY = scaleX, _resize) {
|
|
1577
|
-
if (!this.
|
|
1642
|
+
if (!this.checkTransform('resizeable'))
|
|
1578
1643
|
return;
|
|
1579
1644
|
const { element } = this;
|
|
1580
1645
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1581
|
-
const transform = this.multiple && this.getChangedTransform(() => element.scaleOf(origin, scaleX, scaleY));
|
|
1646
|
+
const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.scaleOf(origin, scaleX, scaleY)));
|
|
1582
1647
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX, scaleY, transform });
|
|
1583
1648
|
this.editTool.onScale(event);
|
|
1584
1649
|
this.emitEvent(event);
|
|
1585
1650
|
}
|
|
1586
1651
|
flip(axis) {
|
|
1587
|
-
if (this.
|
|
1652
|
+
if (!this.checkTransform('resizeable'))
|
|
1588
1653
|
return;
|
|
1589
1654
|
const { element } = this;
|
|
1590
1655
|
const worldOrigin = this.getWorldOrigin('center');
|
|
1591
|
-
const transform = this.multiple ? this.getChangedTransform(() => element.flip(axis)) : new draw.Matrix(draw.LeafHelper.getFlipTransform(element, axis));
|
|
1656
|
+
const transform = this.multiple ? this.getChangedTransform(() => element.safeChange(() => element.flip(axis))) : new draw.Matrix(draw.LeafHelper.getFlipTransform(element, axis));
|
|
1592
1657
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX: axis === 'x' ? -1 : 1, scaleY: axis === 'y' ? -1 : 1, transform });
|
|
1593
1658
|
this.editTool.onScale(event);
|
|
1594
1659
|
this.emitEvent(event);
|
|
1595
1660
|
}
|
|
1596
1661
|
rotateOf(origin, rotation) {
|
|
1597
|
-
if (!this.
|
|
1662
|
+
if (!this.checkTransform('rotateable'))
|
|
1598
1663
|
return;
|
|
1599
1664
|
const { element } = this;
|
|
1600
1665
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1601
|
-
const transform = this.multiple && this.getChangedTransform(() => element.rotateOf(origin, rotation));
|
|
1666
|
+
const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.rotateOf(origin, rotation)));
|
|
1602
1667
|
const event = new EditorRotateEvent(EditorRotateEvent.ROTATE, { target: element, editor: this, worldOrigin, rotation, transform });
|
|
1603
1668
|
this.editTool.onRotate(event);
|
|
1604
1669
|
this.emitEvent(event);
|
|
1605
1670
|
}
|
|
1606
1671
|
skewOf(origin, skewX, skewY = 0, _resize) {
|
|
1607
|
-
if (!this.
|
|
1672
|
+
if (!this.checkTransform('skewable'))
|
|
1608
1673
|
return;
|
|
1609
1674
|
const { element } = this;
|
|
1610
1675
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1611
|
-
const transform = this.multiple && this.getChangedTransform(() => element.skewOf(origin, skewX, skewY));
|
|
1676
|
+
const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.skewOf(origin, skewX, skewY)));
|
|
1612
1677
|
const event = new EditorSkewEvent(EditorSkewEvent.SKEW, { target: element, editor: this, worldOrigin, skewX, skewY, transform });
|
|
1613
1678
|
this.editTool.onSkew(event);
|
|
1614
1679
|
this.emitEvent(event);
|
|
1615
1680
|
}
|
|
1681
|
+
checkTransform(type) { return this.element && !this.element.locked && this.mergeConfig[type]; }
|
|
1616
1682
|
getWorldOrigin(origin) {
|
|
1617
1683
|
return this.element.getWorldPoint(draw.LeafHelper.getInnerOrigin(this.element, origin));
|
|
1618
1684
|
}
|
|
1619
1685
|
getChangedTransform(func) {
|
|
1620
1686
|
const { element } = this;
|
|
1687
|
+
if (this.multiple && !element.canChange)
|
|
1688
|
+
return element.changedTransform;
|
|
1621
1689
|
const oldMatrix = new draw.Matrix(element.worldTransform);
|
|
1622
1690
|
func();
|
|
1623
1691
|
return new draw.Matrix(element.worldTransform).divide(oldMatrix);
|
|
@@ -1674,11 +1742,11 @@ class Editor extends draw.Group {
|
|
|
1674
1742
|
this.emitEvent(event);
|
|
1675
1743
|
group.emitEvent(event);
|
|
1676
1744
|
}
|
|
1677
|
-
openInnerEditor(target) {
|
|
1678
|
-
if (target)
|
|
1745
|
+
openInnerEditor(target, select) {
|
|
1746
|
+
if (target && select)
|
|
1679
1747
|
this.target = target;
|
|
1680
1748
|
if (this.single) {
|
|
1681
|
-
const editTarget = this.element;
|
|
1749
|
+
const editTarget = target || this.element;
|
|
1682
1750
|
const tag = editTarget.editInner;
|
|
1683
1751
|
if (tag && EditToolCreator.list[tag]) {
|
|
1684
1752
|
this.editTool.unload();
|
|
@@ -1730,14 +1798,15 @@ class Editor extends draw.Group {
|
|
|
1730
1798
|
}
|
|
1731
1799
|
listenTargetEvents() {
|
|
1732
1800
|
if (!this.targetEventIds.length) {
|
|
1733
|
-
const { leafer } = this
|
|
1801
|
+
const { app, leafer } = this;
|
|
1734
1802
|
this.targetEventIds = [
|
|
1735
|
-
this.app.on_(core.MoveEvent.BEFORE_MOVE, this.onMove, this, true),
|
|
1736
|
-
this.app.on_(core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true),
|
|
1737
|
-
this.app.on_(core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true),
|
|
1738
1803
|
leafer.on_(draw.RenderEvent.START, this.update, this),
|
|
1739
|
-
|
|
1740
|
-
|
|
1804
|
+
app.on_(draw.RenderEvent.CHILD_START, this.forceRender, this),
|
|
1805
|
+
app.on_(core.MoveEvent.BEFORE_MOVE, this.onMove, this, true),
|
|
1806
|
+
app.on_(core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true),
|
|
1807
|
+
app.on_(core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true),
|
|
1808
|
+
app.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); }),
|
|
1809
|
+
app.on_(core.KeyEvent.DOWN, this.editBox.onArrow, this.editBox)
|
|
1741
1810
|
];
|
|
1742
1811
|
}
|
|
1743
1812
|
}
|
|
@@ -1870,10 +1939,7 @@ exports.EditTool = class EditTool extends InnerEditor {
|
|
|
1870
1939
|
}
|
|
1871
1940
|
update() {
|
|
1872
1941
|
const { editor, editBox } = this;
|
|
1873
|
-
const {
|
|
1874
|
-
if (editor.multiple)
|
|
1875
|
-
simulateTarget.parent.updateLayout();
|
|
1876
|
-
const { x, y, scaleX, scaleY, rotation, skewX, skewY, width, height } = element.getLayoutBounds('box', editor, true);
|
|
1942
|
+
const { x, y, scaleX, scaleY, rotation, skewX, skewY, width, height } = editor.element.getLayoutBounds('box', editor, true);
|
|
1877
1943
|
editBox.set({ x, y, scaleX, scaleY, rotation, skewX, skewY });
|
|
1878
1944
|
editBox.update({ x: 0, y: 0, width, height });
|
|
1879
1945
|
this.onUpdate();
|