@leafer-in/editor 1.0.0-rc.25 → 1.0.0-rc.27

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
@@ -1,185 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ var resize = require('@leafer-in/resize');
3
4
  var draw = require('@leafer-ui/draw');
4
5
  var core = require('@leafer-ui/core');
5
6
 
6
- const { M, L, C, Q, Z, N, D, X, G, F, O, P, U } = draw.PathCommandMap;
7
- const PathScaler = {
8
- scale(data, scaleX, scaleY) {
9
- if (!data)
10
- return;
11
- let command;
12
- let i = 0, len = data.length;
13
- while (i < len) {
14
- command = data[i];
15
- switch (command) {
16
- case M:
17
- scalePoints(data, scaleX, scaleY, i, 1);
18
- i += 3;
19
- break;
20
- case L:
21
- scalePoints(data, scaleX, scaleY, i, 1);
22
- i += 3;
23
- break;
24
- case C:
25
- scalePoints(data, scaleX, scaleY, i, 3);
26
- i += 7;
27
- break;
28
- case Q:
29
- scalePoints(data, scaleX, scaleY, i, 2);
30
- i += 5;
31
- break;
32
- case Z:
33
- i += 1;
34
- break;
35
- case N:
36
- scalePoints(data, scaleX, scaleY, i, 2);
37
- i += 5;
38
- break;
39
- case D:
40
- scalePoints(data, scaleX, scaleY, i, 2);
41
- i += 9;
42
- break;
43
- case X:
44
- scalePoints(data, scaleX, scaleY, i, 2);
45
- i += 6;
46
- break;
47
- case G:
48
- scalePoints(data, scaleX, scaleY, i, 2);
49
- i += 9;
50
- break;
51
- case F:
52
- scalePoints(data, scaleX, scaleY, i, 2);
53
- i += 5;
54
- break;
55
- case O:
56
- data[i] = G;
57
- data.splice(i + 4, 0, data[i + 3], 0);
58
- scalePoints(data, scaleX, scaleY, i, 2);
59
- i += 7 + 2;
60
- len += 2;
61
- break;
62
- case P:
63
- data[i] = F;
64
- data.splice(i + 4, 0, data[i + 3]);
65
- scalePoints(data, scaleX, scaleY, i, 2);
66
- i += 4 + 1;
67
- len += 1;
68
- break;
69
- case U:
70
- scalePoints(data, scaleX, scaleY, i, 2);
71
- i += 6;
72
- break;
73
- }
74
- }
75
- },
76
- scalePoints(data, scaleX, scaleY, start, pointCount) {
77
- for (let i = pointCount ? start + 1 : 0, end = pointCount ? i + pointCount * 2 : data.length; i < end; i += 2) {
78
- data[i] *= scaleX;
79
- data[i + 1] *= scaleY;
80
- }
81
- }
82
- };
83
- const { scalePoints } = PathScaler;
84
-
85
- const matrix$1 = draw.MatrixHelper.get();
86
- function scaleResize(leaf, scaleX, scaleY) {
87
- if (leaf.pathInputed) {
88
- scaleResizePath(leaf, scaleX, scaleY);
89
- }
90
- else {
91
- leaf.width *= scaleX;
92
- leaf.height *= scaleY;
93
- }
94
- }
95
- function scaleResizeFont(leaf, scaleX, scaleY) {
96
- if (scaleX !== 1)
97
- leaf.fontSize *= scaleX;
98
- else if (scaleY !== 1)
99
- leaf.fontSize *= scaleY;
100
- }
101
- function scaleResizePath(leaf, scaleX, scaleY) {
102
- PathScaler.scale(leaf.__.path, scaleX, scaleY);
103
- leaf.path = leaf.__.path;
104
- }
105
- function scaleResizePoints(leaf, scaleX, scaleY) {
106
- PathScaler.scalePoints(leaf.__.points, scaleX, scaleY);
107
- leaf.points = leaf.__.points;
108
- }
109
- function scaleResizeGroup(group, scaleX, scaleY) {
110
- const { children } = group;
111
- for (let i = 0; i < children.length; i++) {
112
- matrix$1.a = scaleX;
113
- matrix$1.d = scaleY;
114
- children[i].transform(matrix$1, true);
115
- }
116
- }
117
-
118
- draw.Leaf.prototype.scaleResize = function (scaleX, scaleY = scaleX, noResize) {
119
- const data = this;
120
- if (noResize) {
121
- data.scaleX *= scaleX;
122
- data.scaleY *= scaleY;
123
- }
124
- else {
125
- if (scaleX < 0)
126
- data.scaleX *= -1, scaleX = -scaleX;
127
- if (scaleY < 0)
128
- data.scaleY *= -1, scaleY = -scaleY;
129
- this.__scaleResize(scaleX, scaleY);
130
- }
131
- };
132
- draw.Leaf.prototype.__scaleResize = function (scaleX, scaleY) {
133
- scaleResize(this, scaleX, scaleY);
134
- };
135
- draw.Text.prototype.__scaleResize = function (scaleX, scaleY) {
136
- if (this.editConfig && this.editConfig.editSize === 'font-size') {
137
- scaleResizeFont(this, scaleX, scaleY);
138
- }
139
- else {
140
- scaleResize(this, scaleX, scaleY);
141
- }
142
- };
143
- draw.Path.prototype.__scaleResize = function (scaleX, scaleY) {
144
- scaleResizePath(this, scaleX, scaleY);
145
- };
146
- draw.Line.prototype.__scaleResize = function (scaleX, scaleY) {
147
- if (this.pathInputed) {
148
- scaleResizePath(this, scaleX, scaleY);
149
- }
150
- else if (this.points) {
151
- scaleResizePoints(this, scaleX, scaleY);
152
- }
153
- else {
154
- const point = this.toPoint;
155
- point.x *= scaleX;
156
- point.y *= scaleY;
157
- this.toPoint = point;
158
- }
159
- };
160
- draw.Polygon.prototype.__scaleResize = function (scaleX, scaleY) {
161
- if (this.pathInputed) {
162
- scaleResizePath(this, scaleX, scaleY);
163
- }
164
- else if (this.points) {
165
- scaleResizePoints(this, scaleX, scaleY);
166
- }
167
- else {
168
- scaleResize(this, scaleX, scaleY);
169
- }
170
- };
171
- draw.Group.prototype.__scaleResize = function (scaleX, scaleY) {
172
- scaleResizeGroup(this, scaleX, scaleY);
173
- };
174
- draw.Box.prototype.__scaleResize = function (scaleX, scaleY) {
175
- if (this.__.__autoSize && this.children.length) {
176
- scaleResizeGroup(this, scaleX, scaleY);
177
- }
178
- else {
179
- scaleResize(this, scaleX, scaleY);
180
- }
181
- };
182
-
183
7
  /******************************************************************************
184
8
  Copyright (c) Microsoft Corporation.
185
9
 
@@ -805,6 +629,7 @@ class EditBox extends draw.Group {
805
629
  this.editor = editor;
806
630
  this.visible = false;
807
631
  this.create();
632
+ this.rect.syncEventer = editor;
808
633
  this.__listenEvents();
809
634
  }
810
635
  create() {
@@ -847,9 +672,9 @@ class EditBox extends draw.Group {
847
672
  rect.hittable = !single && moveable;
848
673
  element.syncEventer = (single && moveable) ? rect : null;
849
674
  this.app.interaction.bottomList = (single && moveable) ? [{ target: rect, proxy: element }] : null;
850
- this.visible = !element.locked;
851
675
  }
852
676
  update(bounds) {
677
+ this.visible = !this.editor.element.locked;
853
678
  if (this.view.worldOpacity) {
854
679
  const { mergeConfig } = this.editor;
855
680
  const { width, height } = bounds;
@@ -866,7 +691,7 @@ class EditBox extends draw.Group {
866
691
  resizeP.set(point);
867
692
  rotateP.set(point);
868
693
  resizeL.set(point);
869
- resizeP.visible = resizeL.visible = showPoints && (resizeable || rotateable);
694
+ resizeP.visible = resizeL.visible = showPoints && !!(resizeable || rotateable);
870
695
  rotateP.visible = showPoints && rotateable && resizeable && !mergeConfig.rotatePoint;
871
696
  if (i % 2) {
872
697
  resizeP.visible = rotateP.visible = showPoints && !!middlePoint;
@@ -1028,6 +853,8 @@ class EditBox extends draw.Group {
1028
853
  rect.on_(core.DragEvent.START, this.onDragStart, this),
1029
854
  rect.on_(core.DragEvent.DRAG, editor.onMove, editor),
1030
855
  rect.on_(core.DragEvent.END, this.onDragEnd, this),
856
+ rect.on_(core.ZoomEvent.BEFORE_ZOOM, editor.onScale, editor, true),
857
+ rect.on_(core.RotateEvent.BEFORE_ROTATE, editor.onRotate, editor, true),
1031
858
  rect.on_(core.PointerEvent.ENTER, () => updateMoveCursor(editor)),
1032
859
  rect.on_(core.PointerEvent.DOUBLE_TAP, this.onDoubleTap, this),
1033
860
  rect.on_(core.PointerEvent.LONG_PRESS, this.onLongPress, this)
@@ -1239,6 +1066,27 @@ const EditToolCreator = {
1239
1066
  };
1240
1067
  const { list } = EditToolCreator;
1241
1068
 
1069
+ class InnerEditorEvent extends EditorEvent {
1070
+ constructor(type, data) {
1071
+ super(type, data);
1072
+ }
1073
+ }
1074
+ InnerEditorEvent.BEFORE_OPEN = 'innerEditor.before_open';
1075
+ InnerEditorEvent.OPEN = 'innerEditor.open';
1076
+ InnerEditorEvent.BEFORE_CLOSE = 'innerEditor.before_close';
1077
+ InnerEditorEvent.CLOSE = 'innerEditor.close';
1078
+
1079
+ class EditorGroupEvent extends EditorEvent {
1080
+ constructor(type, data) {
1081
+ super(type, data);
1082
+ }
1083
+ }
1084
+ EditorGroupEvent.GROUP = 'editor.group';
1085
+ EditorGroupEvent.BEFORE_UNGROUP = 'editor.before_ungroup';
1086
+ EditorGroupEvent.UNGROUP = 'editor.ungroup';
1087
+ EditorGroupEvent.OPEN = 'editor.open_group';
1088
+ EditorGroupEvent.CLOSE = 'editor.close_group';
1089
+
1242
1090
  class Editor extends draw.Group {
1243
1091
  get list() { return this.leafList.list; }
1244
1092
  get editing() { return !!this.list.length; }
@@ -1322,17 +1170,25 @@ class Editor extends draw.Group {
1322
1170
  }
1323
1171
  onScale(e) {
1324
1172
  const { element } = this;
1325
- const { direction } = e.current;
1326
- let { around, lockRatio } = this.mergeConfig;
1327
- if (e.shiftKey)
1328
- lockRatio = true;
1329
- const data = EditDataHelper.getScaleData(element.boxBounds, direction, e.getInnerMove(element), lockRatio, EditDataHelper.getAround(around, e.altKey));
1330
- if (this.editTool.onScaleWithDrag) {
1331
- data.drag = e;
1332
- this.scaleWithDrag(data);
1173
+ if (e instanceof core.ZoomEvent) {
1174
+ if (this.mergeConfig.resizeable === 'zoom') {
1175
+ e.stop();
1176
+ this.scaleOf(element.getInnerPoint(e), e.scale, e.scale);
1177
+ }
1333
1178
  }
1334
1179
  else {
1335
- this.scaleOf(data.origin, data.scaleX, data.scaleY);
1180
+ const { direction } = e.current;
1181
+ let { around, lockRatio } = this.mergeConfig;
1182
+ if (e.shiftKey || element.lockRatio)
1183
+ lockRatio = true;
1184
+ const data = EditDataHelper.getScaleData(element.boxBounds, direction, e.getInnerMove(element), lockRatio, EditDataHelper.getAround(around, e.altKey));
1185
+ if (this.editTool.onScaleWithDrag) {
1186
+ data.drag = e;
1187
+ this.scaleWithDrag(data);
1188
+ }
1189
+ else {
1190
+ this.scaleOf(data.origin, data.scaleX, data.scaleY);
1191
+ }
1336
1192
  }
1337
1193
  }
1338
1194
  onRotate(e) {
@@ -1343,7 +1199,12 @@ class Editor extends draw.Group {
1343
1199
  const { element } = this;
1344
1200
  let origin, rotation;
1345
1201
  if (e instanceof core.RotateEvent) {
1346
- rotation = e.rotation, origin = element.getInnerPoint(e);
1202
+ if (this.mergeConfig.rotateable === 'rotate') {
1203
+ e.stop();
1204
+ rotation = e.rotation, origin = element.getInnerPoint(e);
1205
+ }
1206
+ else
1207
+ return;
1347
1208
  }
1348
1209
  else {
1349
1210
  const last = { x: e.x - e.moveX, y: e.y - e.moveY };
@@ -1378,6 +1239,8 @@ class Editor extends draw.Group {
1378
1239
  element.move(x, y);
1379
1240
  }
1380
1241
  scaleWithDrag(data) {
1242
+ if (!this.mergeConfig.resizeable || this.element.locked)
1243
+ return;
1381
1244
  const { element } = this;
1382
1245
  const worldOrigin = element.getWorldPoint(data.origin);
1383
1246
  const event = new EditorScaleEvent(EditorScaleEvent.SCALE, Object.assign(Object.assign({}, data), { target: element, editor: this, worldOrigin }));
@@ -1385,8 +1248,10 @@ class Editor extends draw.Group {
1385
1248
  this.emitEvent(event);
1386
1249
  }
1387
1250
  scaleOf(origin, scaleX, scaleY = scaleX, _resize) {
1251
+ if (!this.mergeConfig.resizeable || this.element.locked)
1252
+ return;
1388
1253
  const { element } = this;
1389
- const worldOrigin = element.getWorldPoint(origin);
1254
+ const worldOrigin = element.getWorldPoint(draw.LeafHelper.getInnerOrigin(element, origin));
1390
1255
  let transform;
1391
1256
  if (this.multiple) {
1392
1257
  const oldMatrix = new draw.Matrix(element.worldTransform);
@@ -1398,8 +1263,10 @@ class Editor extends draw.Group {
1398
1263
  this.emitEvent(event);
1399
1264
  }
1400
1265
  rotateOf(origin, rotation) {
1266
+ if (!this.mergeConfig.rotateable || this.element.locked)
1267
+ return;
1401
1268
  const { element } = this;
1402
- const worldOrigin = element.getWorldPoint(origin);
1269
+ const worldOrigin = element.getWorldPoint(draw.LeafHelper.getInnerOrigin(element, origin));
1403
1270
  let transform;
1404
1271
  if (this.multiple) {
1405
1272
  const oldMatrix = new draw.Matrix(element.worldTransform);
@@ -1411,8 +1278,10 @@ class Editor extends draw.Group {
1411
1278
  this.emitEvent(event);
1412
1279
  }
1413
1280
  skewOf(origin, skewX, skewY = 0, _resize) {
1281
+ if (!this.mergeConfig.skewable || this.element.locked)
1282
+ return;
1414
1283
  const { element } = this;
1415
- const worldOrigin = element.getWorldPoint(origin);
1284
+ const worldOrigin = element.getWorldPoint(draw.LeafHelper.getInnerOrigin(element, origin));
1416
1285
  let transform;
1417
1286
  if (this.multiple) {
1418
1287
  const oldMatrix = new draw.Matrix(element.worldTransform);
@@ -1426,22 +1295,30 @@ class Editor extends draw.Group {
1426
1295
  this.emitEvent(event);
1427
1296
  }
1428
1297
  group(userGroup) {
1429
- if (this.multiple)
1298
+ if (this.multiple) {
1430
1299
  this.target = EditorHelper.group(this.list, this.element, userGroup);
1300
+ this.emitGroupEvent(EditorGroupEvent.GROUP, this.target);
1301
+ }
1431
1302
  return this.target;
1432
1303
  }
1433
1304
  ungroup() {
1434
- if (this.list.length)
1435
- this.target = EditorHelper.ungroup(this.list);
1305
+ const { list } = this;
1306
+ if (list.length) {
1307
+ list.forEach(item => item.isBranch && this.emitGroupEvent(EditorGroupEvent.BEFORE_UNGROUP, item));
1308
+ this.target = EditorHelper.ungroup(list);
1309
+ list.forEach(item => item.isBranch && this.emitGroupEvent(EditorGroupEvent.UNGROUP, item));
1310
+ }
1436
1311
  return this.list;
1437
1312
  }
1438
1313
  openGroup(group) {
1439
1314
  this.openedGroupList.add(group);
1440
1315
  group.hitChildren = true;
1316
+ this.emitGroupEvent(EditorGroupEvent.OPEN, group);
1441
1317
  }
1442
1318
  closeGroup(group) {
1443
1319
  this.openedGroupList.remove(group);
1444
1320
  group.hitChildren = false;
1321
+ this.emitGroupEvent(EditorGroupEvent.CLOSE, group);
1445
1322
  }
1446
1323
  checkOpenedGroups() {
1447
1324
  const opened = this.openedGroupList;
@@ -1451,28 +1328,58 @@ class Editor extends draw.Group {
1451
1328
  list = [], opened.forEach(item => this.list.every(leaf => !draw.LeafHelper.hasParent(leaf, item)) && list.push(item));
1452
1329
  list.forEach(item => this.closeGroup(item));
1453
1330
  }
1331
+ if (this.editing && !this.selector.dragging)
1332
+ this.checkDeepSelect();
1333
+ }
1334
+ checkDeepSelect() {
1335
+ let parent, { list } = this;
1336
+ for (let i = 0; i < list.length; i++) {
1337
+ parent = list[i].parent;
1338
+ while (parent && !parent.hitChildren) {
1339
+ this.openGroup(parent);
1340
+ parent = parent.parent;
1341
+ }
1342
+ }
1454
1343
  }
1455
- openInnerEditor() {
1344
+ emitGroupEvent(type, group) {
1345
+ const event = new EditorGroupEvent(type, { editTarget: group });
1346
+ this.emitEvent(event);
1347
+ group.emitEvent(event);
1348
+ }
1349
+ openInnerEditor(target) {
1350
+ if (target)
1351
+ this.target = target;
1456
1352
  if (this.single) {
1457
- const tag = this.element.editInner;
1458
- if (tag) {
1459
- if (EditToolCreator.list[tag]) {
1460
- this.editTool.unload();
1461
- this.innerEditing = true;
1462
- this.innerEditor = this.editToolList[tag] || EditToolCreator.get(tag, this);
1463
- this.innerEditor.load();
1464
- }
1353
+ const editTarget = this.element;
1354
+ const tag = editTarget.editInner;
1355
+ if (tag && EditToolCreator.list[tag]) {
1356
+ this.editTool.unload();
1357
+ this.innerEditing = true;
1358
+ this.innerEditor = this.editToolList[tag] || EditToolCreator.get(tag, this);
1359
+ this.innerEditor.editTarget = editTarget;
1360
+ this.emitInnerEvent(InnerEditorEvent.BEFORE_OPEN);
1361
+ this.innerEditor.load();
1362
+ this.emitInnerEvent(InnerEditorEvent.OPEN);
1465
1363
  }
1466
1364
  }
1467
1365
  }
1468
1366
  closeInnerEditor() {
1469
1367
  if (this.innerEditing) {
1470
1368
  this.innerEditing = false;
1369
+ this.emitInnerEvent(InnerEditorEvent.BEFORE_CLOSE);
1471
1370
  this.innerEditor.unload();
1371
+ this.emitInnerEvent(InnerEditorEvent.CLOSE);
1472
1372
  this.editTool.load();
1473
1373
  this.innerEditor = null;
1474
1374
  }
1475
1375
  }
1376
+ emitInnerEvent(type) {
1377
+ const { innerEditor } = this;
1378
+ const { editTarget } = innerEditor;
1379
+ const event = new InnerEditorEvent(type, { editTarget, innerEditor });
1380
+ this.emitEvent(event);
1381
+ editTarget.emitEvent(event);
1382
+ }
1476
1383
  lock() {
1477
1384
  this.list.forEach(leaf => leaf.locked = true);
1478
1385
  this.update();
@@ -1785,19 +1692,21 @@ exports.EditSelectHelper = EditSelectHelper;
1785
1692
  exports.EditToolCreator = EditToolCreator;
1786
1693
  exports.Editor = Editor;
1787
1694
  exports.EditorEvent = EditorEvent;
1695
+ exports.EditorGroupEvent = EditorGroupEvent;
1788
1696
  exports.EditorHelper = EditorHelper;
1789
1697
  exports.EditorMoveEvent = EditorMoveEvent;
1790
1698
  exports.EditorRotateEvent = EditorRotateEvent;
1791
1699
  exports.EditorScaleEvent = EditorScaleEvent;
1792
1700
  exports.EditorSkewEvent = EditorSkewEvent;
1793
1701
  exports.InnerEditor = InnerEditor;
1794
- exports.PathScaler = PathScaler;
1702
+ exports.InnerEditorEvent = InnerEditorEvent;
1795
1703
  exports.SelectArea = SelectArea;
1796
1704
  exports.Stroker = Stroker;
1797
1705
  exports.registerEditTool = registerEditTool;
1798
1706
  exports.registerInnerEditor = registerInnerEditor;
1799
- exports.scaleResize = scaleResize;
1800
- exports.scaleResizeFont = scaleResizeFont;
1801
- exports.scaleResizeGroup = scaleResizeGroup;
1802
- exports.scaleResizePath = scaleResizePath;
1803
- exports.scaleResizePoints = scaleResizePoints;
1707
+ Object.keys(resize).forEach(function (k) {
1708
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1709
+ enumerable: true,
1710
+ get: function () { return resize[k]; }
1711
+ });
1712
+ });