@leafer-in/editor 1.0.4 → 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 -48
- package/dist/editor.esm.js +116 -49
- package/dist/editor.esm.min.js +1 -1
- package/dist/editor.js +115 -48
- 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/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() {
|
|
@@ -901,7 +899,6 @@ class EditBox extends draw.Group {
|
|
|
901
899
|
this.editor = editor;
|
|
902
900
|
this.visible = false;
|
|
903
901
|
this.create();
|
|
904
|
-
this.rect.syncEventer = editor;
|
|
905
902
|
this.__listenEvents();
|
|
906
903
|
}
|
|
907
904
|
create() {
|
|
@@ -941,8 +938,11 @@ class EditBox extends draw.Group {
|
|
|
941
938
|
circle.set(this.getPointStyle(mergeConfig.circle || mergeConfig.rotatePoint || pointsStyle[0]));
|
|
942
939
|
rect.set(Object.assign({ stroke, strokeWidth }, (mergeConfig.rect || {})));
|
|
943
940
|
rect.hittable = !single && !!moveable;
|
|
944
|
-
|
|
945
|
-
|
|
941
|
+
rect.syncEventer = single && this.editor;
|
|
942
|
+
if (single && moveable) {
|
|
943
|
+
element.syncEventer = rect;
|
|
944
|
+
this.app.interaction.bottomList = [{ target: rect, proxy: element }];
|
|
945
|
+
}
|
|
946
946
|
}
|
|
947
947
|
update(bounds) {
|
|
948
948
|
this.visible = !this.editor.element.locked;
|
|
@@ -1114,6 +1114,11 @@ class EditBox extends draw.Group {
|
|
|
1114
1114
|
if (editor.single) {
|
|
1115
1115
|
const { element } = editor;
|
|
1116
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
|
+
}
|
|
1117
1122
|
editor.openGroup(element);
|
|
1118
1123
|
editor.target = editor.selector.findDeepOne(e);
|
|
1119
1124
|
}
|
|
@@ -1259,12 +1264,12 @@ const config = {
|
|
|
1259
1264
|
skewable: true
|
|
1260
1265
|
};
|
|
1261
1266
|
|
|
1267
|
+
const bounds = new draw.Bounds();
|
|
1262
1268
|
function simulate(editor) {
|
|
1263
|
-
const { simulateTarget,
|
|
1264
|
-
const {
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
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);
|
|
1268
1273
|
}
|
|
1269
1274
|
|
|
1270
1275
|
function onTarget(editor, oldValue) {
|
|
@@ -1273,6 +1278,7 @@ function onTarget(editor, oldValue) {
|
|
|
1273
1278
|
editor.leafList = target instanceof draw.LeafList ? target : new draw.LeafList(target instanceof Array ? target : target);
|
|
1274
1279
|
}
|
|
1275
1280
|
else {
|
|
1281
|
+
editor.simulateTarget.remove();
|
|
1276
1282
|
editor.leafList.reset();
|
|
1277
1283
|
editor.closeInnerEditor();
|
|
1278
1284
|
}
|
|
@@ -1327,7 +1333,7 @@ const EditorHelper = {
|
|
|
1327
1333
|
const ungroupList = [];
|
|
1328
1334
|
app.lockLayout();
|
|
1329
1335
|
list.forEach(leaf => {
|
|
1330
|
-
if (leaf.isBranch) {
|
|
1336
|
+
if (leaf.isBranch && !leaf.isBranchLeaf) {
|
|
1331
1337
|
const { parent, children } = leaf;
|
|
1332
1338
|
while (children.length) {
|
|
1333
1339
|
ungroupList.push(children[0]);
|
|
@@ -1398,17 +1404,77 @@ EditorGroupEvent.UNGROUP = 'editor.ungroup';
|
|
|
1398
1404
|
EditorGroupEvent.OPEN = 'editor.open_group';
|
|
1399
1405
|
EditorGroupEvent.CLOSE = 'editor.close_group';
|
|
1400
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
|
+
|
|
1401
1465
|
class Editor extends draw.Group {
|
|
1402
1466
|
get mergeConfig() {
|
|
1403
1467
|
const { element, config } = this;
|
|
1404
1468
|
return this.single && element.editConfig ? Object.assign(Object.assign({}, config), element.editConfig) : config;
|
|
1405
1469
|
}
|
|
1406
1470
|
get list() { return this.leafList.list; }
|
|
1471
|
+
get dragHoverExclude() { return [this.editBox.rect]; }
|
|
1407
1472
|
get editing() { return !!this.list.length; }
|
|
1408
1473
|
get groupOpening() { return !!this.openedGroupList.length; }
|
|
1409
1474
|
get multiple() { return this.list.length > 1; }
|
|
1410
1475
|
get single() { return this.list.length === 1; }
|
|
1411
1476
|
get dragging() { return this.editBox.dragging; }
|
|
1477
|
+
get moving() { return this.editBox.moving; }
|
|
1412
1478
|
get element() { return this.multiple ? this.simulateTarget : this.list[0]; }
|
|
1413
1479
|
get buttons() { return this.editBox.buttons; }
|
|
1414
1480
|
constructor(userConfig, data) {
|
|
@@ -1416,7 +1482,7 @@ class Editor extends draw.Group {
|
|
|
1416
1482
|
this.config = draw.DataHelper.clone(config);
|
|
1417
1483
|
this.leafList = new draw.LeafList();
|
|
1418
1484
|
this.openedGroupList = new draw.LeafList();
|
|
1419
|
-
this.simulateTarget = new
|
|
1485
|
+
this.simulateTarget = new SimulateElement(this);
|
|
1420
1486
|
this.editBox = new EditBox(this);
|
|
1421
1487
|
this.editToolList = {};
|
|
1422
1488
|
this.selector = new EditSelect(this);
|
|
@@ -1554,18 +1620,18 @@ class Editor extends draw.Group {
|
|
|
1554
1620
|
this.skewOf(origin, skewX, skewY);
|
|
1555
1621
|
}
|
|
1556
1622
|
move(x, y = 0) {
|
|
1557
|
-
if (!this.
|
|
1623
|
+
if (!this.checkTransform('moveable'))
|
|
1558
1624
|
return;
|
|
1559
1625
|
const { element } = this;
|
|
1560
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));
|
|
1561
1629
|
const event = new EditorMoveEvent(EditorMoveEvent.MOVE, { target: element, editor: this, moveX: world.x, moveY: world.y });
|
|
1562
1630
|
this.editTool.onMove(event);
|
|
1563
1631
|
this.emitEvent(event);
|
|
1564
|
-
if (this.multiple)
|
|
1565
|
-
element.move(x, y);
|
|
1566
1632
|
}
|
|
1567
1633
|
scaleWithDrag(data) {
|
|
1568
|
-
if (!this.
|
|
1634
|
+
if (!this.checkTransform('resizeable'))
|
|
1569
1635
|
return;
|
|
1570
1636
|
const { element } = this;
|
|
1571
1637
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, Object.assign(Object.assign({}, data), { target: element, editor: this, worldOrigin: element.getWorldPoint(data.origin) }));
|
|
@@ -1573,50 +1639,53 @@ class Editor extends draw.Group {
|
|
|
1573
1639
|
this.emitEvent(event);
|
|
1574
1640
|
}
|
|
1575
1641
|
scaleOf(origin, scaleX, scaleY = scaleX, _resize) {
|
|
1576
|
-
if (!this.
|
|
1642
|
+
if (!this.checkTransform('resizeable'))
|
|
1577
1643
|
return;
|
|
1578
1644
|
const { element } = this;
|
|
1579
1645
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1580
|
-
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)));
|
|
1581
1647
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX, scaleY, transform });
|
|
1582
1648
|
this.editTool.onScale(event);
|
|
1583
1649
|
this.emitEvent(event);
|
|
1584
1650
|
}
|
|
1585
1651
|
flip(axis) {
|
|
1586
|
-
if (this.
|
|
1652
|
+
if (!this.checkTransform('resizeable'))
|
|
1587
1653
|
return;
|
|
1588
1654
|
const { element } = this;
|
|
1589
1655
|
const worldOrigin = this.getWorldOrigin('center');
|
|
1590
|
-
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));
|
|
1591
1657
|
const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX: axis === 'x' ? -1 : 1, scaleY: axis === 'y' ? -1 : 1, transform });
|
|
1592
1658
|
this.editTool.onScale(event);
|
|
1593
1659
|
this.emitEvent(event);
|
|
1594
1660
|
}
|
|
1595
1661
|
rotateOf(origin, rotation) {
|
|
1596
|
-
if (!this.
|
|
1662
|
+
if (!this.checkTransform('rotateable'))
|
|
1597
1663
|
return;
|
|
1598
1664
|
const { element } = this;
|
|
1599
1665
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1600
|
-
const transform = this.multiple && this.getChangedTransform(() => element.rotateOf(origin, rotation));
|
|
1666
|
+
const transform = this.multiple && this.getChangedTransform(() => element.safeChange(() => element.rotateOf(origin, rotation)));
|
|
1601
1667
|
const event = new EditorRotateEvent(EditorRotateEvent.ROTATE, { target: element, editor: this, worldOrigin, rotation, transform });
|
|
1602
1668
|
this.editTool.onRotate(event);
|
|
1603
1669
|
this.emitEvent(event);
|
|
1604
1670
|
}
|
|
1605
1671
|
skewOf(origin, skewX, skewY = 0, _resize) {
|
|
1606
|
-
if (!this.
|
|
1672
|
+
if (!this.checkTransform('skewable'))
|
|
1607
1673
|
return;
|
|
1608
1674
|
const { element } = this;
|
|
1609
1675
|
const worldOrigin = this.getWorldOrigin(origin);
|
|
1610
|
-
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)));
|
|
1611
1677
|
const event = new EditorSkewEvent(EditorSkewEvent.SKEW, { target: element, editor: this, worldOrigin, skewX, skewY, transform });
|
|
1612
1678
|
this.editTool.onSkew(event);
|
|
1613
1679
|
this.emitEvent(event);
|
|
1614
1680
|
}
|
|
1681
|
+
checkTransform(type) { return this.element && !this.element.locked && this.mergeConfig[type]; }
|
|
1615
1682
|
getWorldOrigin(origin) {
|
|
1616
1683
|
return this.element.getWorldPoint(draw.LeafHelper.getInnerOrigin(this.element, origin));
|
|
1617
1684
|
}
|
|
1618
1685
|
getChangedTransform(func) {
|
|
1619
1686
|
const { element } = this;
|
|
1687
|
+
if (this.multiple && !element.canChange)
|
|
1688
|
+
return element.changedTransform;
|
|
1620
1689
|
const oldMatrix = new draw.Matrix(element.worldTransform);
|
|
1621
1690
|
func();
|
|
1622
1691
|
return new draw.Matrix(element.worldTransform).divide(oldMatrix);
|
|
@@ -1673,11 +1742,11 @@ class Editor extends draw.Group {
|
|
|
1673
1742
|
this.emitEvent(event);
|
|
1674
1743
|
group.emitEvent(event);
|
|
1675
1744
|
}
|
|
1676
|
-
openInnerEditor(target) {
|
|
1677
|
-
if (target)
|
|
1745
|
+
openInnerEditor(target, select) {
|
|
1746
|
+
if (target && select)
|
|
1678
1747
|
this.target = target;
|
|
1679
1748
|
if (this.single) {
|
|
1680
|
-
const editTarget = this.element;
|
|
1749
|
+
const editTarget = target || this.element;
|
|
1681
1750
|
const tag = editTarget.editInner;
|
|
1682
1751
|
if (tag && EditToolCreator.list[tag]) {
|
|
1683
1752
|
this.editTool.unload();
|
|
@@ -1729,14 +1798,15 @@ class Editor extends draw.Group {
|
|
|
1729
1798
|
}
|
|
1730
1799
|
listenTargetEvents() {
|
|
1731
1800
|
if (!this.targetEventIds.length) {
|
|
1732
|
-
const { leafer } = this
|
|
1801
|
+
const { app, leafer } = this;
|
|
1733
1802
|
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
1803
|
leafer.on_(draw.RenderEvent.START, this.update, this),
|
|
1738
|
-
|
|
1739
|
-
|
|
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)
|
|
1740
1810
|
];
|
|
1741
1811
|
}
|
|
1742
1812
|
}
|
|
@@ -1869,10 +1939,7 @@ exports.EditTool = class EditTool extends InnerEditor {
|
|
|
1869
1939
|
}
|
|
1870
1940
|
update() {
|
|
1871
1941
|
const { editor, editBox } = this;
|
|
1872
|
-
const {
|
|
1873
|
-
if (editor.multiple)
|
|
1874
|
-
simulateTarget.parent.updateLayout();
|
|
1875
|
-
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);
|
|
1876
1943
|
editBox.set({ x, y, scaleX, scaleY, rotation, skewX, skewY });
|
|
1877
1944
|
editBox.update({ x: 0, y: 0, width, height });
|
|
1878
1945
|
this.onUpdate();
|