@leafer-in/editor 1.9.7 → 1.9.9

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.js CHANGED
@@ -45,10 +45,6 @@ this.LeaferIN.editor = function(exports, draw, core) {
45
45
  if (t.config) {
46
46
  const isSelect = key === "target";
47
47
  if (isSelect) {
48
- t.setDimOthers(false);
49
- t.setBright(false);
50
- if (draw.isArray(value) && value.length > 1 && value[0].locked) value.splice(0, 1);
51
- if (t.single) t.element.syncEventer = null;
52
48
  const {beforeSelect: beforeSelect} = t.config;
53
49
  if (beforeSelect) {
54
50
  const check = beforeSelect({
@@ -56,6 +52,10 @@ this.LeaferIN.editor = function(exports, draw, core) {
56
52
  });
57
53
  if (draw.isObject(check)) value = check; else if (check === false) return;
58
54
  }
55
+ t.setDimOthers(false);
56
+ t.setBright(false);
57
+ if (draw.isArray(value) && value.length > 1 && value[0].locked) value.splice(0, 1);
58
+ if (t.single) t.element.syncEventer = null;
59
59
  }
60
60
  const type = isSelect ? EditorEvent.BEFORE_SELECT : EditorEvent.BEFORE_HOVER;
61
61
  if (this.hasEvent(type)) this.emitEvent(new EditorEvent(type, {
@@ -226,11 +226,12 @@ this.LeaferIN.editor = function(exports, draw, core) {
226
226
  return !!this.originList;
227
227
  }
228
228
  get running() {
229
- const {editor: editor} = this;
230
- return this.hittable && editor.visible && editor.hittable && editor.mergeConfig.selector && this.app.mode === "normal";
229
+ const {editor: editor, app: app} = this;
230
+ return this.hittable && editor.visible && editor.hittable && editor.mergeConfig.selector && (app && app.mode === "normal");
231
231
  }
232
232
  get isMoveMode() {
233
- return this.app && this.app.interaction.moveMode;
233
+ const {app: app} = this;
234
+ return app && app.interaction.moveMode;
234
235
  }
235
236
  constructor(editor) {
236
237
  super();
@@ -293,12 +294,12 @@ this.LeaferIN.editor = function(exports, draw, core) {
293
294
  onTap(e) {
294
295
  if (e.multiTouch) return;
295
296
  const {editor: editor} = this;
296
- const {select: select} = editor.mergeConfig;
297
+ const {select: select, selectKeep: selectKeep} = editor.mergeConfig;
297
298
  if (select === "tap") this.checkAndSelect(e); else if (this.waitSelect) this.waitSelect();
298
299
  if (this.needRemoveItem) {
299
300
  editor.removeItem(this.needRemoveItem);
300
301
  } else if (this.isMoveMode) {
301
- editor.target = null;
302
+ if (!selectKeep) editor.target = null;
302
303
  }
303
304
  }
304
305
  checkAndSelect(e) {
@@ -313,7 +314,7 @@ this.LeaferIN.editor = function(exports, draw, core) {
313
314
  editor.target = find;
314
315
  }
315
316
  } else if (this.allow(e.target)) {
316
- if (!this.isHoldMultipleSelectKey(e)) editor.target = null;
317
+ if (!this.isHoldMultipleSelectKey(e) && !this.editor.mergedConfig.selectKeep) editor.target = null;
317
318
  }
318
319
  }
319
320
  }
@@ -428,91 +429,95 @@ this.LeaferIN.editor = function(exports, draw, core) {
428
429
  const {toPoint: toPoint} = draw.AroundHelper;
429
430
  const {within: within, sign: sign} = draw.MathHelper;
430
431
  const EditDataHelper = {
431
- getScaleData(target, startBounds, direction, totalMove, lockRatio, around, flipable, scaleMode) {
432
+ getScaleData(target, startBounds, direction, totalMoveOrScale, lockRatio, around, flipable, scaleMode) {
432
433
  let align, origin = {}, scaleX = 1, scaleY = 1;
433
434
  const {boxBounds: boxBounds, widthRange: widthRange, heightRange: heightRange, dragBounds: dragBounds, worldBoxBounds: worldBoxBounds} = target;
434
435
  const {width: width, height: height} = startBounds;
435
- if (around) {
436
- totalMove.x *= 2;
437
- totalMove.y *= 2;
438
- }
439
436
  const originChangedScaleX = target.scaleX / startBounds.scaleX;
440
437
  const originChangedScaleY = target.scaleY / startBounds.scaleY;
441
438
  const signX = sign(originChangedScaleX);
442
439
  const signY = sign(originChangedScaleY);
443
440
  const changedScaleX = scaleMode ? originChangedScaleX : signX * boxBounds.width / width;
444
441
  const changedScaleY = scaleMode ? originChangedScaleY : signY * boxBounds.height / height;
445
- totalMove.x *= scaleMode ? originChangedScaleX : signX;
446
- totalMove.y *= scaleMode ? originChangedScaleY : signY;
447
- const topScale = (-totalMove.y + height) / height;
448
- const rightScale = (totalMove.x + width) / width;
449
- const bottomScale = (totalMove.y + height) / height;
450
- const leftScale = (-totalMove.x + width) / width;
451
- switch (direction) {
452
- case top:
453
- scaleY = topScale;
454
- align = "bottom";
455
- break;
442
+ if (draw.isNumber(totalMoveOrScale)) {
443
+ scaleX = scaleY = Math.sqrt(totalMoveOrScale);
444
+ } else {
445
+ if (around) {
446
+ totalMoveOrScale.x *= 2;
447
+ totalMoveOrScale.y *= 2;
448
+ }
449
+ totalMoveOrScale.x *= scaleMode ? originChangedScaleX : signX;
450
+ totalMoveOrScale.y *= scaleMode ? originChangedScaleY : signY;
451
+ const topScale = (-totalMoveOrScale.y + height) / height;
452
+ const rightScale = (totalMoveOrScale.x + width) / width;
453
+ const bottomScale = (totalMoveOrScale.y + height) / height;
454
+ const leftScale = (-totalMoveOrScale.x + width) / width;
455
+ switch (direction) {
456
+ case top:
457
+ scaleY = topScale;
458
+ align = "bottom";
459
+ break;
456
460
 
457
- case right$1:
458
- scaleX = rightScale;
459
- align = "left";
460
- break;
461
+ case right$1:
462
+ scaleX = rightScale;
463
+ align = "left";
464
+ break;
461
465
 
462
- case bottom:
463
- scaleY = bottomScale;
464
- align = "top";
465
- break;
466
+ case bottom:
467
+ scaleY = bottomScale;
468
+ align = "top";
469
+ break;
466
470
 
467
- case left$1:
468
- scaleX = leftScale;
469
- align = "right";
470
- break;
471
+ case left$1:
472
+ scaleX = leftScale;
473
+ align = "right";
474
+ break;
471
475
 
472
- case topLeft:
473
- scaleY = topScale;
474
- scaleX = leftScale;
475
- align = "bottom-right";
476
- break;
476
+ case topLeft:
477
+ scaleY = topScale;
478
+ scaleX = leftScale;
479
+ align = "bottom-right";
480
+ break;
477
481
 
478
- case topRight:
479
- scaleY = topScale;
480
- scaleX = rightScale;
481
- align = "bottom-left";
482
- break;
482
+ case topRight:
483
+ scaleY = topScale;
484
+ scaleX = rightScale;
485
+ align = "bottom-left";
486
+ break;
483
487
 
484
- case bottomRight:
485
- scaleY = bottomScale;
486
- scaleX = rightScale;
487
- align = "top-left";
488
- break;
488
+ case bottomRight:
489
+ scaleY = bottomScale;
490
+ scaleX = rightScale;
491
+ align = "top-left";
492
+ break;
489
493
 
490
- case bottomLeft:
491
- scaleY = bottomScale;
492
- scaleX = leftScale;
493
- align = "top-right";
494
- }
495
- if (lockRatio) {
496
- if (lockRatio === "corner" && direction % 2) {
497
- lockRatio = false;
498
- } else {
499
- let scale;
500
- switch (direction) {
501
- case top:
502
- case bottom:
503
- scale = scaleY;
504
- break;
494
+ case bottomLeft:
495
+ scaleY = bottomScale;
496
+ scaleX = leftScale;
497
+ align = "top-right";
498
+ }
499
+ if (lockRatio) {
500
+ if (lockRatio === "corner" && direction % 2) {
501
+ lockRatio = false;
502
+ } else {
503
+ let scale;
504
+ switch (direction) {
505
+ case top:
506
+ case bottom:
507
+ scale = scaleY;
508
+ break;
505
509
 
506
- case left$1:
507
- case right$1:
508
- scale = scaleX;
509
- break;
510
+ case left$1:
511
+ case right$1:
512
+ scale = scaleX;
513
+ break;
510
514
 
511
- default:
512
- scale = Math.sqrt(Math.abs(scaleX * scaleY));
515
+ default:
516
+ scale = Math.sqrt(Math.abs(scaleX * scaleY));
517
+ }
518
+ scaleX = scaleX < 0 ? -scale : scale;
519
+ scaleY = scaleY < 0 ? -scale : scale;
513
520
  }
514
- scaleX = scaleX < 0 ? -scale : scale;
515
- scaleY = scaleY < 0 ? -scale : scale;
516
521
  }
517
522
  }
518
523
  const useScaleX = scaleX !== 1, useScaleY = scaleY !== 1;
@@ -793,6 +798,9 @@ this.LeaferIN.editor = function(exports, draw, core) {
793
798
  const {moveable: moveable, resizeable: resizeable, rotateable: rotateable} = this.mergeConfig;
794
799
  return draw.isString(moveable) || draw.isString(resizeable) || draw.isString(rotateable);
795
800
  }
801
+ get canDragLimitAnimate() {
802
+ return this.moving && this.mergeConfig.dragLimitAnimate && this.target.dragBounds;
803
+ }
796
804
  constructor(editor) {
797
805
  super();
798
806
  this.view = new draw.Group;
@@ -1026,10 +1034,9 @@ this.LeaferIN.editor = function(exports, draw, core) {
1026
1034
  onDragStart(e) {
1027
1035
  this.dragging = true;
1028
1036
  const point = this.dragPoint = e.current, {pointType: pointType} = point;
1029
- const {editor: editor, dragStartData: dragStartData} = this, {target: target} = this, {moveable: moveable, resizeable: resizeable, rotateable: rotateable, skewable: skewable, hideOnMove: hideOnMove} = this.mergeConfig;
1037
+ const {moveable: moveable, resizeable: resizeable, rotateable: rotateable, skewable: skewable} = this.mergeConfig;
1030
1038
  if (pointType === "move") {
1031
1039
  moveable && (this.moving = true);
1032
- editor.opacity = hideOnMove ? 0 : 1;
1033
1040
  } else {
1034
1041
  if (pointType.includes("rotate") || this.isHoldRotateKey(e) || !resizeable) {
1035
1042
  rotateable && (this.rotating = true);
@@ -1038,23 +1045,7 @@ this.LeaferIN.editor = function(exports, draw, core) {
1038
1045
  } else if (pointType === "resize") resizeable && (this.resizing = true);
1039
1046
  if (pointType === "skew") skewable && (this.skewing = true);
1040
1047
  }
1041
- dragStartData.x = e.x;
1042
- dragStartData.y = e.y;
1043
- dragStartData.point = {
1044
- x: target.x,
1045
- y: target.y
1046
- };
1047
- dragStartData.bounds = Object.assign({}, target.getLayoutBounds("box", "local"));
1048
- dragStartData.rotation = target.rotation;
1049
- if (pointType && pointType.includes("resize")) draw.ResizeEvent.resizingKeys = editor.leafList.keys;
1050
- }
1051
- onDragEnd(e) {
1052
- if (this.moving && this.mergeConfig.dragLimitAnimate && this.target.dragBounds) this.transformTool.onMove(e);
1053
- this.dragPoint = null;
1054
- this.resetDoing();
1055
- const {pointType: pointType} = e.current;
1056
- if (pointType === "move") this.editor.opacity = 1;
1057
- if (pointType && pointType.includes("resize")) draw.ResizeEvent.resizingKeys = null;
1048
+ this.onTransformStart(e);
1058
1049
  }
1059
1050
  onDrag(e) {
1060
1051
  const {transformTool: transformTool, moving: moving, resizing: resizing, rotating: rotating, skewing: skewing} = this;
@@ -1069,37 +1060,59 @@ this.LeaferIN.editor = function(exports, draw, core) {
1069
1060
  }
1070
1061
  updatePointCursor(this, e);
1071
1062
  }
1072
- resetDoing() {
1073
- if (this.canUse) this.dragging = this.gesturing = this.moving = this.resizing = this.rotating = this.skewing = false;
1063
+ onDragEnd(e) {
1064
+ this.onTransformEnd(e);
1065
+ this.dragPoint = null;
1066
+ }
1067
+ onTransformStart(e) {
1068
+ if (this.canUse) {
1069
+ if (this.moving) this.editor.opacity = this.mergedConfig.hideOnMove ? 0 : 1;
1070
+ if (this.resizing) draw.ResizeEvent.resizingKeys = this.editor.leafList.keys;
1071
+ const {dragStartData: dragStartData, target: target} = this;
1072
+ dragStartData.x = e.x;
1073
+ dragStartData.y = e.y;
1074
+ dragStartData.totalOffset = draw.getPointData();
1075
+ dragStartData.point = {
1076
+ x: target.x,
1077
+ y: target.y
1078
+ };
1079
+ dragStartData.bounds = Object.assign({}, target.getLayoutBounds("box", "local"));
1080
+ dragStartData.rotation = target.rotation;
1081
+ }
1082
+ }
1083
+ onTransformEnd(e) {
1084
+ if (this.canUse) {
1085
+ if (this.canDragLimitAnimate && (e instanceof core.DragEvent || e instanceof core.MoveEvent)) this.transformTool.onMove(e);
1086
+ if (this.resizing) draw.ResizeEvent.resizingKeys = null;
1087
+ this.dragging = this.gesturing = this.moving = this.resizing = this.rotating = this.skewing = false;
1088
+ this.editor.opacity = 1;
1089
+ this.update();
1090
+ }
1074
1091
  }
1075
1092
  onMove(e) {
1076
1093
  if (this.canGesture && e.moveType !== "drag") {
1077
1094
  e.stop();
1078
- if (draw.isString(this.mergeConfig.moveable)) {
1095
+ if (draw.isString(this.mergedConfig.moveable)) {
1079
1096
  this.gesturing = this.moving = true;
1080
- this.transformTool.onMove(e);
1097
+ e.type === core.MoveEvent.START ? this.onTransformStart(e) : this.transformTool.onMove(e);
1081
1098
  }
1082
1099
  }
1083
1100
  }
1084
- onMoveEnd(e) {
1085
- if (this.moving) this.transformTool.onMove(e);
1086
- this.resetDoing();
1087
- }
1088
1101
  onScale(e) {
1089
1102
  if (this.canGesture) {
1090
1103
  e.stop();
1091
- if (draw.isString(this.mergeConfig.resizeable)) {
1104
+ if (draw.isString(this.mergedConfig.resizeable)) {
1092
1105
  this.gesturing = this.resizing = true;
1093
- this.transformTool.onScale(e);
1106
+ e.type === core.ZoomEvent.START ? this.onTransformStart(e) : this.transformTool.onScale(e);
1094
1107
  }
1095
1108
  }
1096
1109
  }
1097
1110
  onRotate(e) {
1098
1111
  if (this.canGesture) {
1099
1112
  e.stop();
1100
- if (draw.isString(this.mergeConfig.rotateable)) {
1113
+ if (draw.isString(this.mergedConfig.rotateable)) {
1101
1114
  this.gesturing = this.rotating = true;
1102
- this.transformTool.onRotate(e);
1115
+ e.type === core.RotateEvent.START ? this.onTransformStart(e) : this.transformTool.onRotate(e);
1103
1116
  }
1104
1117
  }
1105
1118
  }
@@ -1112,8 +1125,7 @@ this.LeaferIN.editor = function(exports, draw, core) {
1112
1125
  updatePointCursor(this, e);
1113
1126
  }
1114
1127
  onArrow(e) {
1115
- const {editor: editor, transformTool: transformTool} = this;
1116
- if (this.canUse && editor.editing && this.mergeConfig.keyEvent) {
1128
+ if (this.canUse && this.mergeConfig.keyEvent) {
1117
1129
  let x = 0, y = 0;
1118
1130
  const distance = e.shiftKey ? 10 : 1;
1119
1131
  switch (e.code) {
@@ -1132,7 +1144,7 @@ this.LeaferIN.editor = function(exports, draw, core) {
1132
1144
  case "ArrowRight":
1133
1145
  x = distance;
1134
1146
  }
1135
- if (x || y) transformTool.move(x, y);
1147
+ if (x || y) this.transformTool.move(x, y);
1136
1148
  }
1137
1149
  }
1138
1150
  onDoubleTap(e) {
@@ -1173,7 +1185,7 @@ this.LeaferIN.editor = function(exports, draw, core) {
1173
1185
  const {rect: rect, editor: editor, __eventIds: events} = this;
1174
1186
  events.push(rect.on_([ [ core.PointerEvent.DOUBLE_TAP, this.onDoubleTap, this ], [ core.PointerEvent.LONG_PRESS, this.onLongPress, this ] ]));
1175
1187
  this.waitLeafer(() => {
1176
- events.push(editor.app.on_([ [ [ core.KeyEvent.HOLD, core.KeyEvent.UP ], this.onKey, this ], [ core.KeyEvent.DOWN, this.onArrow, this ], [ core.MoveEvent.BEFORE_MOVE, this.onMove, this, true ], [ core.ZoomEvent.BEFORE_ZOOM, this.onScale, this, true ], [ core.RotateEvent.BEFORE_ROTATE, this.onRotate, this, true ], [ core.MoveEvent.END, this.onMoveEnd, this ], [ core.ZoomEvent.END, this.resetDoing, this ], [ core.RotateEvent.END, this.resetDoing, this ] ]));
1188
+ events.push(editor.app.on_([ [ [ core.KeyEvent.HOLD, core.KeyEvent.UP ], this.onKey, this ], [ core.KeyEvent.DOWN, this.onArrow, this ], [ [ core.MoveEvent.START, core.MoveEvent.BEFORE_MOVE ], this.onMove, this, true ], [ [ core.ZoomEvent.START, core.ZoomEvent.BEFORE_ZOOM ], this.onScale, this, true ], [ [ core.RotateEvent.START, core.RotateEvent.BEFORE_ROTATE ], this.onRotate, this, true ], [ [ core.MoveEvent.END, core.ZoomEvent.END, core.RotateEvent.END ], this.onTransformEnd, this ] ]));
1177
1189
  });
1178
1190
  }
1179
1191
  __removeListenEvents() {
@@ -1522,56 +1534,61 @@ this.LeaferIN.editor = function(exports, draw, core) {
1522
1534
  const isMoveEnd = e.type === core.MoveEvent.END || e.type === core.DragEvent.END;
1523
1535
  const axisDrag = draw.isString(target.draggable);
1524
1536
  const checkLimitMove = !dragLimitAnimate || isMoveEnd || axisDrag;
1537
+ const total = {
1538
+ x: e.totalX,
1539
+ y: e.totalY
1540
+ };
1525
1541
  if (e instanceof core.MoveEvent) {
1526
- move = e.getLocalMove(target);
1527
- if (checkLimitMove) core.DragEvent.limitMove(target, move);
1528
- } else {
1529
- const total = {
1530
- x: e.totalX,
1531
- y: e.totalY
1532
- };
1533
- if (e.shiftKey) {
1534
- if (Math.abs(total.x) > Math.abs(total.y)) total.y = 0; else total.x = 0;
1535
- }
1536
- move = core.DragEvent.getValidMove(target, dragStartData.point, total, checkLimitMove);
1542
+ draw.PointHelper.move(total, target.getWorldPointByLocal(dragStartData.totalOffset, null, true));
1543
+ }
1544
+ if (e.shiftKey) {
1545
+ if (Math.abs(total.x) > Math.abs(total.y)) total.y = 0; else total.x = 0;
1537
1546
  }
1547
+ move = core.DragEvent.getValidMove(target, dragStartData.point, total, checkLimitMove);
1538
1548
  if (move.x || move.y) {
1539
1549
  if (dragLimitAnimate && !axisDrag && isMoveEnd) draw.LeafHelper.animateMove(this, move, draw.isNumber(dragLimitAnimate) ? dragLimitAnimate : .3); else this.move(move);
1540
1550
  }
1541
1551
  }
1542
1552
  onScale(e) {
1543
1553
  const {target: target, mergeConfig: mergeConfig, single: single, dragStartData: dragStartData} = this.editBox;
1544
- let {around: around, lockRatio: lockRatio, flipable: flipable, editSize: editSize} = mergeConfig;
1554
+ let {around: around, lockRatio: lockRatio, flipable: flipable, editSize: editSize} = mergeConfig, totalMove;
1545
1555
  if (e instanceof core.ZoomEvent) {
1546
- this.scaleOf(target.getBoxPoint(e), e.scale, e.scale);
1556
+ around = target.getBoxPoint(e);
1557
+ totalMove = e.totalScale;
1547
1558
  } else {
1548
- const {direction: direction} = e.current;
1549
- if (e.shiftKey || target.lockRatio) lockRatio = true;
1550
- const data = EditDataHelper.getScaleData(target, dragStartData.bounds, direction, e.getInnerTotal(target), lockRatio, EditDataHelper.getAround(around, e.altKey), flipable, !single || editSize === "scale");
1551
- if (this.editTool && this.editTool.onScaleWithDrag) {
1552
- data.drag = e;
1553
- this.scaleWithDrag(data);
1554
- } else {
1555
- this.scaleOf(data.origin, data.scaleX, data.scaleY);
1556
- }
1559
+ totalMove = e.getInnerTotal(target);
1560
+ }
1561
+ const {direction: direction} = e.current;
1562
+ if (e.shiftKey || target.lockRatio) lockRatio = true;
1563
+ const data = EditDataHelper.getScaleData(target, dragStartData.bounds, direction, totalMove, lockRatio, EditDataHelper.getAround(around, e.altKey), flipable, !single || editSize === "scale");
1564
+ const targetX = target.x, targetY = target.y;
1565
+ if (e instanceof core.DragEvent && this.editTool && this.editTool.onScaleWithDrag) {
1566
+ data.drag = e;
1567
+ this.scaleWithDrag(data);
1568
+ } else {
1569
+ this.scaleOf(data.origin, data.scaleX, data.scaleY);
1557
1570
  }
1571
+ draw.PointHelper.move(dragStartData.totalOffset, target.x - targetX, target.y - targetY);
1558
1572
  }
1559
1573
  onRotate(e) {
1560
1574
  const {target: target, mergeConfig: mergeConfig, dragStartData: dragStartData} = this.editBox;
1561
- const {around: around, rotateAround: rotateAround, rotateGap: rotateGap} = mergeConfig;
1575
+ const {around: around, rotateAround: rotateAround, rotateGap: rotateGap, diagonalRotateKey: diagonalRotateKey} = mergeConfig;
1562
1576
  const {direction: direction} = e.current;
1563
1577
  let origin, rotation;
1564
1578
  if (e instanceof core.RotateEvent) {
1565
1579
  rotation = e.rotation;
1566
1580
  origin = rotateAround ? draw.AroundHelper.getPoint(rotateAround, target.boxBounds) : target.getBoxPoint(e);
1567
1581
  } else {
1568
- const data = EditDataHelper.getRotateData(target, direction, e, dragStartData, e.shiftKey ? null : rotateAround || target.around || target.origin || around || "center");
1582
+ const isDiagonalRotate = diagonalRotateKey ? e.isHoldKeys(diagonalRotateKey) : e.shiftKey;
1583
+ const data = EditDataHelper.getRotateData(target, direction, e, dragStartData, isDiagonalRotate ? null : rotateAround || target.around || target.origin || around || "center");
1569
1584
  rotation = dragStartData.rotation + data.rotation - target.rotation;
1570
1585
  origin = data.origin;
1571
1586
  }
1572
1587
  rotation = draw.MathHelper.float(draw.MathHelper.getGapRotation(rotation, rotateGap, target.rotation), 2);
1573
1588
  if (!rotation) return;
1589
+ const targetX = target.x, targetY = target.y;
1574
1590
  this.rotateOf(origin, rotation);
1591
+ draw.PointHelper.move(dragStartData.totalOffset, target.x - targetX, target.y - targetY);
1575
1592
  }
1576
1593
  onSkew(e) {
1577
1594
  const {target: target, mergeConfig: mergeConfig} = this.editBox;