@leafer-in/editor 1.0.0-beta.18 → 1.0.0-rc.11

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
@@ -1,7 +1,524 @@
1
1
  this.LeaferIN = this.LeaferIN || {};
2
- this.LeaferIN.editor = (function (exports, core) {
2
+ this.LeaferIN.editor = (function (exports, draw, core) {
3
3
  'use strict';
4
4
 
5
+ const { M, L, C, Q, Z, N, D, X, G, F, O, P, U } = draw.PathCommandMap;
6
+ const PathScaler = {
7
+ scale(data, scaleX, scaleY) {
8
+ if (!data)
9
+ return;
10
+ let command;
11
+ let i = 0, len = data.length;
12
+ while (i < len) {
13
+ command = data[i];
14
+ switch (command) {
15
+ case M:
16
+ scalePoints(data, scaleX, scaleY, i, 1);
17
+ i += 3;
18
+ break;
19
+ case L:
20
+ scalePoints(data, scaleX, scaleY, i, 1);
21
+ i += 3;
22
+ break;
23
+ case C:
24
+ scalePoints(data, scaleX, scaleY, i, 3);
25
+ i += 7;
26
+ break;
27
+ case Q:
28
+ scalePoints(data, scaleX, scaleY, i, 2);
29
+ i += 5;
30
+ break;
31
+ case Z:
32
+ i += 1;
33
+ break;
34
+ case N:
35
+ scalePoints(data, scaleX, scaleY, i, 2);
36
+ i += 5;
37
+ break;
38
+ case D:
39
+ scalePoints(data, scaleX, scaleY, i, 2);
40
+ i += 9;
41
+ break;
42
+ case X:
43
+ scalePoints(data, scaleX, scaleY, i, 2);
44
+ i += 6;
45
+ break;
46
+ case G:
47
+ scalePoints(data, scaleX, scaleY, i, 2);
48
+ i += 9;
49
+ break;
50
+ case F:
51
+ scalePoints(data, scaleX, scaleY, i, 2);
52
+ i += 5;
53
+ break;
54
+ case O:
55
+ data[i] = G;
56
+ data.splice(i + 4, 0, data[i + 3], 0);
57
+ scalePoints(data, scaleX, scaleY, i, 2);
58
+ i += 7 + 2;
59
+ len += 2;
60
+ break;
61
+ case P:
62
+ data[i] = F;
63
+ data.splice(i + 4, 0, data[i + 3]);
64
+ scalePoints(data, scaleX, scaleY, i, 2);
65
+ i += 4 + 1;
66
+ len += 1;
67
+ break;
68
+ case U:
69
+ scalePoints(data, scaleX, scaleY, i, 2);
70
+ i += 6;
71
+ break;
72
+ }
73
+ }
74
+ },
75
+ scalePoints(data, scaleX, scaleY, start, pointCount) {
76
+ for (let i = pointCount ? start + 1 : 0, end = pointCount ? i + pointCount * 2 : data.length; i < end; i += 2) {
77
+ data[i] *= scaleX;
78
+ data[i + 1] *= scaleY;
79
+ }
80
+ }
81
+ };
82
+ const { scalePoints } = PathScaler;
83
+
84
+ draw.Leaf.prototype.scaleResize = function (scaleX, scaleY = scaleX, noResize) {
85
+ const data = this;
86
+ if (noResize) {
87
+ data.scaleX *= scaleX;
88
+ data.scaleY *= scaleY;
89
+ }
90
+ else {
91
+ if (scaleX < 0)
92
+ data.scaleX *= -1, scaleX = -scaleX;
93
+ if (scaleY < 0)
94
+ data.scaleY *= -1, scaleY = -scaleY;
95
+ this.__scaleResize(scaleX, scaleY);
96
+ }
97
+ };
98
+ function scaleResize(leaf, scaleX, scaleY) {
99
+ if (scaleX !== 1)
100
+ leaf.width *= scaleX;
101
+ if (scaleY !== 1)
102
+ leaf.height *= scaleY;
103
+ }
104
+ draw.Leaf.prototype.__scaleResize = function (scaleX, scaleY) {
105
+ scaleResize(this, scaleX, scaleY);
106
+ };
107
+ draw.Path.prototype.__scaleResize = function (scaleX, scaleY) {
108
+ PathScaler.scale(this.__.path, scaleX, scaleY);
109
+ this.path = this.__.path;
110
+ };
111
+ draw.Line.prototype.__scaleResize = function (scaleX, scaleY) {
112
+ if (this.points) {
113
+ PathScaler.scalePoints(this.__.points, scaleX, scaleY);
114
+ this.points = this.__.points;
115
+ }
116
+ else {
117
+ const point = this.toPoint;
118
+ point.x *= scaleX;
119
+ point.y *= scaleY;
120
+ this.toPoint = point;
121
+ }
122
+ };
123
+ draw.Polygon.prototype.__scaleResize = function (scaleX, scaleY) {
124
+ if (this.points) {
125
+ PathScaler.scalePoints(this.__.points, scaleX, scaleY);
126
+ this.points = this.__.points;
127
+ }
128
+ else {
129
+ scaleResize(this, scaleX, scaleY);
130
+ }
131
+ };
132
+ const matrix$1 = draw.MatrixHelper.get();
133
+ function groupScaleResize(group, scaleX, scaleY) {
134
+ const { children } = group;
135
+ for (let i = 0; i < children.length; i++) {
136
+ matrix$1.a = scaleX;
137
+ matrix$1.d = scaleY;
138
+ children[i].transform(matrix$1, true);
139
+ }
140
+ }
141
+ draw.Group.prototype.__scaleResize = function (scaleX, scaleY) {
142
+ groupScaleResize(this, scaleX, scaleY);
143
+ };
144
+ draw.Box.prototype.__scaleResize = function (scaleX, scaleY) {
145
+ if (this.__.__autoBounds && this.children.length) {
146
+ groupScaleResize(this, scaleX, scaleY);
147
+ }
148
+ else {
149
+ scaleResize(this, scaleX, scaleY);
150
+ }
151
+ };
152
+
153
+ /******************************************************************************
154
+ Copyright (c) Microsoft Corporation.
155
+
156
+ Permission to use, copy, modify, and/or distribute this software for any
157
+ purpose with or without fee is hereby granted.
158
+
159
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
160
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
161
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
162
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
163
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
164
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
165
+ PERFORMANCE OF THIS SOFTWARE.
166
+ ***************************************************************************** */
167
+ /* global Reflect, Promise, SuppressedError, Symbol */
168
+
169
+
170
+ function __decorate(decorators, target, key, desc) {
171
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
172
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
173
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
174
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
175
+ }
176
+
177
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
178
+ var e = new Error(message);
179
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
180
+ };
181
+
182
+ class EditorEvent extends core.Event {
183
+ constructor(type, data) {
184
+ super(type);
185
+ if (data)
186
+ Object.assign(this, data);
187
+ }
188
+ }
189
+ EditorEvent.SELECT = 'editor.select';
190
+ EditorEvent.HOVER = 'editor.hover';
191
+
192
+ class EditorMoveEvent extends EditorEvent {
193
+ constructor(type, data) {
194
+ super(type, data);
195
+ }
196
+ }
197
+ EditorMoveEvent.MOVE = 'editor.move';
198
+
199
+ class EditorScaleEvent extends EditorEvent {
200
+ constructor(type, data) {
201
+ super(type, data);
202
+ }
203
+ }
204
+ EditorScaleEvent.SCALE = 'editor.scale';
205
+
206
+ class EditorRotateEvent extends EditorEvent {
207
+ constructor(type, data) {
208
+ super(type, data);
209
+ }
210
+ }
211
+ EditorRotateEvent.ROTATE = 'editor.rotate';
212
+
213
+ class EditorSkewEvent extends EditorEvent {
214
+ constructor(type, data) {
215
+ super(type, data);
216
+ }
217
+ }
218
+ EditorSkewEvent.SKEW = 'editor.skew';
219
+
220
+ function targetAttr(fn) {
221
+ return (target, key) => {
222
+ const privateKey = '_' + key;
223
+ core.defineKey(target, key, {
224
+ get() { return this[privateKey]; },
225
+ set(value) {
226
+ const old = this[privateKey];
227
+ if (old !== value)
228
+ this[privateKey] = value, fn(this, old);
229
+ }
230
+ });
231
+ };
232
+ }
233
+
234
+ const matrix = core.MatrixHelper.get();
235
+ const { abs } = Math;
236
+ const { copy, scale } = core.MatrixHelper;
237
+ class Stroker extends core.UI {
238
+ constructor() {
239
+ super();
240
+ this.list = [];
241
+ this.hittable = false;
242
+ this.strokeAlign = 'center';
243
+ }
244
+ setTarget(target, style) {
245
+ const { stroke, strokeWidth } = style;
246
+ this.set({ stroke, strokeWidth });
247
+ this.target = target;
248
+ }
249
+ __draw(canvas, options) {
250
+ const { list } = this;
251
+ if (list.length) {
252
+ let leaf;
253
+ const { stroke, strokeWidth } = this.__;
254
+ const { bounds } = options;
255
+ for (let i = 0; i < list.length; i++) {
256
+ leaf = list[i];
257
+ if (bounds && bounds.hit(leaf.__world, options.matrix)) {
258
+ let drewPath;
259
+ if (leaf.__.editSize === 'scale') {
260
+ const aScaleX = abs(leaf.__world.scaleX), aScaleY = abs(leaf.__world.scaleY);
261
+ if (aScaleX !== aScaleY) {
262
+ copy(matrix, leaf.__world);
263
+ scale(matrix, 1 / aScaleX, 1 / aScaleY);
264
+ canvas.setWorld(matrix, options.matrix);
265
+ canvas.beginPath();
266
+ this.__.strokeWidth = strokeWidth;
267
+ const { x, y, width, height } = leaf.__layout.boxBounds;
268
+ canvas.rect(x * aScaleX, y * aScaleY, width * aScaleX, height * aScaleY);
269
+ drewPath = true;
270
+ }
271
+ }
272
+ if (!drewPath) {
273
+ canvas.setWorld(leaf.__world, options.matrix);
274
+ canvas.beginPath();
275
+ leaf.__.__pathForRender ? leaf.__drawRenderPath(canvas) : leaf.__drawPathByBox(canvas);
276
+ this.__.strokeWidth = strokeWidth / abs(leaf.__world.scaleX);
277
+ }
278
+ typeof stroke === 'string' ? core.Paint.stroke(stroke, this, canvas) : core.Paint.strokes(stroke, this, canvas);
279
+ }
280
+ }
281
+ this.__.strokeWidth = strokeWidth;
282
+ }
283
+ }
284
+ destroy() {
285
+ this.target = null;
286
+ super.destroy();
287
+ }
288
+ }
289
+ __decorate([
290
+ targetAttr(onTarget$1)
291
+ ], Stroker.prototype, "target", void 0);
292
+ function onTarget$1(stroker) {
293
+ const value = stroker.target;
294
+ stroker.list = value ? (value instanceof Array ? value : [value]) : [];
295
+ stroker.forceUpdate();
296
+ }
297
+
298
+ class SelectArea extends core.Group {
299
+ constructor(data) {
300
+ super(data);
301
+ this.strokeArea = new core.Rect({ strokeAlign: 'center' });
302
+ this.fillArea = new core.Rect();
303
+ this.visible = this.hittable = false;
304
+ this.addMany(this.fillArea, this.strokeArea);
305
+ }
306
+ setStyle(style, userStyle) {
307
+ const { visible, stroke, strokeWidth } = style;
308
+ this.visible = visible;
309
+ this.strokeArea.reset(Object.assign({ stroke, strokeWidth }, (userStyle || {})));
310
+ this.fillArea.reset({ visible: userStyle ? false : true, fill: stroke, opacity: 0.2 });
311
+ }
312
+ setBounds(bounds) {
313
+ this.strokeArea.set(bounds);
314
+ this.fillArea.set(bounds);
315
+ }
316
+ }
317
+
318
+ const { No, Yes, NoAndSkip, YesAndSkip } = core.Answer;
319
+ const EditSelectHelper = {
320
+ findOne(path) {
321
+ return path.list.find((leaf) => leaf.editable);
322
+ },
323
+ findBounds(leaf, bounds) {
324
+ if (leaf.__.hittable && leaf.__.visible && !leaf.__.locked && bounds.hit(leaf.__world)) {
325
+ if (leaf.__.editable) {
326
+ if (leaf.isBranch && !leaf.__.hitChildren) {
327
+ return leaf.__.hitSelf ? YesAndSkip : NoAndSkip;
328
+ }
329
+ else if (leaf.isFrame) {
330
+ return bounds.includes(leaf.__layout.boxBounds, leaf.__world) ? YesAndSkip : No;
331
+ }
332
+ else {
333
+ if (bounds.hit(leaf.__layout.boxBounds, leaf.__world) && leaf.__.hitSelf)
334
+ return Yes;
335
+ }
336
+ }
337
+ return No;
338
+ }
339
+ else {
340
+ return leaf.isBranch ? NoAndSkip : No;
341
+ }
342
+ }
343
+ };
344
+
345
+ const { findOne } = EditSelectHelper;
346
+ class EditSelect extends core.Group {
347
+ get dragging() { return !!this.originList; }
348
+ get running() { return this.editor.hittable && this.editor.config.selector; }
349
+ get isMoveMode() { return this.app && this.app.interaction.moveMode; }
350
+ constructor(editor) {
351
+ super();
352
+ this.hoverStroker = new Stroker();
353
+ this.targetStroker = new Stroker();
354
+ this.bounds = new core.Bounds();
355
+ this.selectArea = new SelectArea();
356
+ this.__eventIds = [];
357
+ this.editor = editor;
358
+ this.addMany(this.targetStroker, this.hoverStroker, this.selectArea);
359
+ this.__listenEvents();
360
+ }
361
+ onHover() {
362
+ const { editor } = this;
363
+ if (this.running && !this.dragging && !editor.dragging) {
364
+ const { stroke, strokeWidth, hover } = editor.config;
365
+ this.hoverStroker.setTarget(hover ? this.editor.hoverTarget : null, { stroke, strokeWidth });
366
+ }
367
+ else {
368
+ this.hoverStroker.target = null;
369
+ }
370
+ }
371
+ onSelect() {
372
+ if (this.running) {
373
+ const { config, list } = this.editor;
374
+ const { stroke, strokeWidth } = config;
375
+ this.targetStroker.setTarget(list, { stroke, strokeWidth: Math.max(1, strokeWidth / 2) });
376
+ this.hoverStroker.target = null;
377
+ }
378
+ }
379
+ update() {
380
+ if (this.running)
381
+ this.targetStroker.forceUpdate();
382
+ }
383
+ onPointerMove(e) {
384
+ if (this.running && !this.isMoveMode) {
385
+ const find = e.shiftKey ? this.findDeepOne(e) : findOne(e.path);
386
+ this.editor.hoverTarget = this.editor.hasItem(find) ? null : find;
387
+ }
388
+ if (this.isMoveMode) {
389
+ this.editor.hoverTarget = null;
390
+ }
391
+ }
392
+ onBeforeDown(e) {
393
+ if (this.running && !this.isMoveMode && !e.middle) {
394
+ const find = this.lastDownLeaf = findOne(e.path);
395
+ if (find) {
396
+ if (e.shiftKey) {
397
+ this.editor.shiftItem(find);
398
+ }
399
+ else {
400
+ this.editor.target = find;
401
+ }
402
+ this.editor.updateLayout();
403
+ if (!find.locked)
404
+ this.app.interaction.updateDownData(null, { findList: [this.editor.editBox.rect] });
405
+ }
406
+ else if (this.allow(e.target)) {
407
+ if (!e.shiftKey)
408
+ this.editor.target = null;
409
+ }
410
+ }
411
+ }
412
+ onTap(e) {
413
+ if (this.running && e.shiftKey && !e.middle && !this.lastDownLeaf) {
414
+ const find = this.findDeepOne(e);
415
+ if (find)
416
+ this.editor.shiftItem(find);
417
+ }
418
+ else if (this.isMoveMode) {
419
+ this.editor.target = null;
420
+ }
421
+ this.lastDownLeaf = null;
422
+ }
423
+ onDragStart(e) {
424
+ if (this.running && this.allowDrag(e)) {
425
+ const { editor } = this;
426
+ const { stroke, strokeWidth, area } = editor.config;
427
+ const { x, y } = e.getInner(this);
428
+ this.bounds.set(x, y);
429
+ this.selectArea.setStyle({ visible: true, stroke, strokeWidth, x, y }, area);
430
+ this.selectArea.setBounds(this.bounds.get());
431
+ this.originList = editor.leafList.clone();
432
+ }
433
+ }
434
+ onDrag(e) {
435
+ if (this.editor.dragging) {
436
+ this.onDragEnd();
437
+ return;
438
+ }
439
+ if (this.dragging) {
440
+ const { editor } = this;
441
+ const total = e.getInnerTotal(this);
442
+ const dragBounds = this.bounds.clone().unsign();
443
+ const list = new core.LeafList(editor.app.find(EditSelectHelper.findBounds, dragBounds));
444
+ this.bounds.width = total.x;
445
+ this.bounds.height = total.y;
446
+ this.selectArea.setBounds(dragBounds.get());
447
+ if (list.length) {
448
+ const selectList = [];
449
+ this.originList.forEach(item => { if (!list.has(item))
450
+ selectList.push(item); });
451
+ list.forEach(item => { if (!this.originList.has(item))
452
+ selectList.push(item); });
453
+ if (selectList.length !== editor.list.length || editor.list.some((child, index) => child !== selectList[index])) {
454
+ editor.target = selectList;
455
+ }
456
+ }
457
+ else {
458
+ editor.target = this.originList.list;
459
+ if (editor.leafList.length)
460
+ editor.update();
461
+ }
462
+ }
463
+ }
464
+ onDragEnd() {
465
+ if (this.dragging)
466
+ this.originList = null, this.selectArea.visible = false;
467
+ }
468
+ onAutoMove(e) {
469
+ if (this.dragging) {
470
+ const { x, y } = e.getLocalMove(this);
471
+ this.bounds.x += x;
472
+ this.bounds.y += y;
473
+ }
474
+ }
475
+ allow(target) {
476
+ return target.leafer !== this.editor.leafer;
477
+ }
478
+ allowDrag(e) {
479
+ if (this.editor.config.boxSelect && !e.target.draggable) {
480
+ return (!this.editor.hasTarget && this.allow(e.target)) || (e.shiftKey && !findOne(e.path));
481
+ }
482
+ else {
483
+ return false;
484
+ }
485
+ }
486
+ findDeepOne(e) {
487
+ const options = { exclude: new core.LeafList(this.editor.editBox.rect) };
488
+ return findOne(e.target.leafer.interaction.findPath(e, options));
489
+ }
490
+ __listenEvents() {
491
+ const { editor } = this;
492
+ editor.waitLeafer(() => {
493
+ const { app } = editor;
494
+ app.selector.proxy = editor;
495
+ this.__eventIds = [
496
+ editor.on_(EditorEvent.HOVER, this.onHover, this),
497
+ editor.on_(EditorEvent.SELECT, this.onSelect, this),
498
+ app.on_(core.PointerEvent.MOVE, this.onPointerMove, this),
499
+ app.on_(core.PointerEvent.BEFORE_DOWN, this.onBeforeDown, this),
500
+ app.on_(core.PointerEvent.TAP, this.onTap, this),
501
+ app.on_(core.DragEvent.START, this.onDragStart, this),
502
+ app.on_(core.DragEvent.DRAG, this.onDrag, this),
503
+ app.on_(core.DragEvent.END, this.onDragEnd, this),
504
+ app.on_(core.MoveEvent.MOVE, this.onAutoMove, this),
505
+ app.on_([core.ZoomEvent.ZOOM, core.MoveEvent.MOVE], () => { this.editor.hoverTarget = null; }),
506
+ ];
507
+ });
508
+ }
509
+ __removeListenEvents() {
510
+ if (this.__eventIds) {
511
+ this.off_(this.__eventIds);
512
+ this.__eventIds.length = 0;
513
+ }
514
+ }
515
+ destroy() {
516
+ this.editor = this.originList = this.lastDownLeaf = null;
517
+ this.__removeListenEvents();
518
+ super.destroy();
519
+ }
520
+ }
521
+
5
522
  var IDirection8;
6
523
  (function (IDirection8) {
7
524
  IDirection8[IDirection8["topLeft"] = 0] = "topLeft";
@@ -14,257 +531,573 @@ this.LeaferIN.editor = (function (exports, core) {
14
531
  IDirection8[IDirection8["left"] = 7] = "left";
15
532
  })(IDirection8 || (IDirection8 = {}));
16
533
 
17
- const { scaleOfOuter, reset } = core.MatrixHelper;
18
- const { topLeft: topLeft$1, top: top$1, topRight: topRight$1, right: right$2, bottomRight: bottomRight$1, bottom: bottom$1, bottomLeft: bottomLeft$1, left: left$2 } = IDirection8;
19
- const matrix = {};
20
- function getResizeData(old, direction, move, lockRatio, around) {
21
- if (around) {
22
- move.x *= 2;
23
- move.y *= 2;
24
- }
25
- let origin, scaleX = 1, scaleY = 1;
26
- const { x, y, width, height } = old;
27
- const topScale = (-move.y + height) / height;
28
- const rightScale = (move.x + width) / width;
29
- const bottomScale = (move.y + height) / height;
30
- const leftScale = (-move.x + width) / width;
31
- switch (direction) {
32
- case top$1:
33
- scaleY = topScale;
34
- if (lockRatio)
35
- scaleX = scaleY;
36
- origin = { x: x + width / 2, y: y + height };
37
- break;
38
- case right$2:
39
- scaleX = rightScale;
40
- if (lockRatio)
41
- scaleY = scaleX;
42
- origin = { x, y: y + height / 2 };
43
- break;
44
- case bottom$1:
45
- scaleY = bottomScale;
46
- if (lockRatio)
47
- scaleX = scaleY;
48
- origin = { x: x + width / 2, y };
49
- break;
50
- case left$2:
51
- scaleX = leftScale;
52
- if (lockRatio)
534
+ const { topLeft, top, topRight, right: right$1, bottomRight, bottom, bottomLeft, left: left$1 } = IDirection8;
535
+ const { toPoint } = core.AroundHelper;
536
+ const EditDataHelper = {
537
+ getScaleData(bounds, direction, pointMove, lockRatio, around) {
538
+ let origin, scaleX = 1, scaleY = 1;
539
+ const { width, height } = bounds;
540
+ if (around) {
541
+ pointMove.x *= 2;
542
+ pointMove.y *= 2;
543
+ }
544
+ if (Math.abs(pointMove.x) === width)
545
+ pointMove.x += 0.1;
546
+ if (Math.abs(pointMove.y) === height)
547
+ pointMove.y += 0.1;
548
+ const topScale = (-pointMove.y + height) / height;
549
+ const rightScale = (pointMove.x + width) / width;
550
+ const bottomScale = (pointMove.y + height) / height;
551
+ const leftScale = (-pointMove.x + width) / width;
552
+ switch (direction) {
553
+ case top:
554
+ scaleY = topScale;
555
+ origin = { x: 0.5, y: 1 };
556
+ break;
557
+ case right$1:
558
+ scaleX = rightScale;
559
+ origin = { x: 0, y: 0.5 };
560
+ break;
561
+ case bottom:
562
+ scaleY = bottomScale;
563
+ origin = { x: 0.5, y: 0 };
564
+ break;
565
+ case left$1:
566
+ scaleX = leftScale;
567
+ origin = { x: 1, y: 0.5 };
568
+ break;
569
+ case topLeft:
570
+ scaleY = topScale;
571
+ scaleX = leftScale;
572
+ origin = { x: 1, y: 1 };
573
+ break;
574
+ case topRight:
575
+ scaleY = topScale;
576
+ scaleX = rightScale;
577
+ origin = { x: 0, y: 1 };
578
+ break;
579
+ case bottomRight:
580
+ scaleY = bottomScale;
581
+ scaleX = rightScale;
582
+ origin = { x: 0, y: 0 };
583
+ break;
584
+ case bottomLeft:
585
+ scaleY = bottomScale;
586
+ scaleX = leftScale;
587
+ origin = { x: 1, y: 0 };
588
+ }
589
+ if (lockRatio) {
590
+ if (scaleX !== 1)
53
591
  scaleY = scaleX;
54
- origin = { x: x + width, y: y + height / 2 };
55
- break;
56
- case topLeft$1:
57
- scaleY = topScale;
58
- scaleX = leftScale;
59
- if (lockRatio)
60
- scaleX = scaleY;
61
- origin = { x: x + width, y: y + height };
62
- break;
63
- case topRight$1:
64
- scaleY = topScale;
65
- scaleX = rightScale;
66
- if (lockRatio)
592
+ else
67
593
  scaleX = scaleY;
68
- origin = { x, y: y + height };
69
- break;
70
- case bottomRight$1:
71
- scaleY = bottomScale;
72
- scaleX = rightScale;
73
- if (lockRatio)
74
- scaleX = scaleY;
75
- origin = { x, y };
76
- break;
77
- case bottomLeft$1:
78
- scaleY = bottomScale;
79
- scaleX = leftScale;
80
- if (lockRatio)
81
- scaleX = scaleY;
82
- origin = { x: x + width, y };
83
- break;
84
- }
85
- if (around) {
86
- if (typeof around === 'object') {
87
- origin = { x: x + width / around.x, y: y + height / around.y };
88
594
  }
89
- else {
90
- origin = { x: x + width / 2, y: y + height / 2 };
595
+ toPoint(around || origin, bounds, origin);
596
+ return { origin, scaleX, scaleY, direction, lockRatio, around };
597
+ },
598
+ getRotateData(bounds, direction, current, last, around) {
599
+ let origin;
600
+ switch (direction) {
601
+ case topLeft:
602
+ origin = { x: 1, y: 1 };
603
+ break;
604
+ case topRight:
605
+ origin = { x: 0, y: 1 };
606
+ break;
607
+ case bottomRight:
608
+ origin = { x: 0, y: 0 };
609
+ break;
610
+ case bottomLeft:
611
+ origin = { x: 1, y: 0 };
612
+ break;
613
+ default:
614
+ origin = { x: 0.5, y: 0.5 };
615
+ }
616
+ toPoint(around || origin, bounds, origin);
617
+ return { origin, rotation: core.PointHelper.getRotation(last, origin, current) };
618
+ },
619
+ getSkewData(bounds, direction, move, around) {
620
+ let origin, skewX = 0, skewY = 0;
621
+ let last;
622
+ switch (direction) {
623
+ case top:
624
+ last = { x: 0.5, y: 0 };
625
+ origin = { x: 0.5, y: 1 };
626
+ skewX = 1;
627
+ break;
628
+ case bottom:
629
+ last = { x: 0.5, y: 1 };
630
+ origin = { x: 0.5, y: 0 };
631
+ skewX = 1;
632
+ break;
633
+ case left$1:
634
+ last = { x: 0, y: 0.5 };
635
+ origin = { x: 1, y: 0.5 };
636
+ skewY = 1;
637
+ break;
638
+ case right$1:
639
+ last = { x: 1, y: 0.5 };
640
+ origin = { x: 0, y: 0.5 };
641
+ skewY = 1;
642
+ }
643
+ const { x, y, width, height } = bounds;
644
+ last.x = x + last.x * width;
645
+ last.y = y + last.y * height;
646
+ toPoint(around || origin, bounds, origin);
647
+ const rotation = core.PointHelper.getRotation(last, origin, { x: last.x + (skewX ? move.x : 0), y: last.y + (skewY ? move.y : 0) });
648
+ skewX ? skewX = -rotation : skewY = rotation;
649
+ return { origin, skewX, skewY };
650
+ },
651
+ getAround(around, altKey) {
652
+ return (altKey && !around) ? 'center' : around;
653
+ },
654
+ getRotateDirection(direction, rotation, totalDirection = 8) {
655
+ direction = (direction + Math.round(rotation / (360 / totalDirection))) % totalDirection;
656
+ if (direction < 0)
657
+ direction += totalDirection;
658
+ return direction;
659
+ },
660
+ getFlipDirection(direction, flipedX, flipedY) {
661
+ if (flipedX) {
662
+ switch (direction) {
663
+ case left$1:
664
+ direction = right$1;
665
+ break;
666
+ case topLeft:
667
+ direction = topRight;
668
+ break;
669
+ case bottomLeft:
670
+ direction = bottomRight;
671
+ break;
672
+ case right$1:
673
+ direction = left$1;
674
+ break;
675
+ case topRight:
676
+ direction = topLeft;
677
+ break;
678
+ case bottomRight:
679
+ direction = bottomLeft;
680
+ break;
681
+ }
682
+ }
683
+ if (flipedY) {
684
+ switch (direction) {
685
+ case top:
686
+ direction = bottom;
687
+ break;
688
+ case topLeft:
689
+ direction = bottomLeft;
690
+ break;
691
+ case topRight:
692
+ direction = bottomRight;
693
+ break;
694
+ case bottom:
695
+ direction = top;
696
+ break;
697
+ case bottomLeft:
698
+ direction = topLeft;
699
+ break;
700
+ case bottomRight:
701
+ direction = topRight;
702
+ break;
703
+ }
91
704
  }
705
+ return direction;
92
706
  }
93
- reset(matrix);
94
- scaleOfOuter(matrix, origin, scaleX, scaleY);
95
- const bounds = { x: old.x + matrix.e, y: old.y + matrix.f, width: width * scaleX, height: height * scaleY };
96
- return { bounds, old, origin, scaleX, scaleY, direction, lockRatio, around, };
97
- }
707
+ };
98
708
 
99
- const { topLeft, top, topRight, right: right$1, bottomRight, bottom, bottomLeft, left: left$1 } = IDirection8;
100
709
  function updateCursor(editor, e) {
101
- const point = editor.enterPoint;
102
- if (!point || !editor.target || !editor.visible)
710
+ const { editBox } = editor, point = editBox.enterPoint;
711
+ if (!point || !editor.hasTarget || !editBox.visible)
103
712
  return;
104
- let { rotation } = editor;
105
- let { resizeCursor, rotateCursor, resizeable } = editor.config;
106
- const mirror = editor.tool.getMirrorData(editor);
107
- const { __direction, __isResizePoint } = point.__;
108
- editor.enterPoint = point;
109
- if (__isResizePoint && (e.metaKey || e.ctrlKey || !resizeable))
110
- resizeCursor = rotateCursor;
111
- if (mirror.x || mirror.y) {
112
- mirrorCursors(resizeCursor = [...resizeCursor], mirror.x, mirror.y);
113
- mirrorCursors(rotateCursor = [...rotateCursor], mirror.y, mirror.x);
114
- if (mirror.x + mirror.y === 1)
115
- rotation = -rotation;
116
- }
117
- let index = (__direction + Math.round(rotation / 45)) % 8;
118
- if (index < 0)
119
- index += 8;
120
- point.cursor = __isResizePoint ? resizeCursor[index] : rotateCursor[index];
713
+ let { rotation } = editBox;
714
+ const { resizeCursor, rotateCursor, skewCursor, resizeable, rotateable, skewable } = editor.config;
715
+ const { pointType } = point, { flippedX, flippedY } = editBox;
716
+ let showResize = pointType === 'resize';
717
+ if (showResize && rotateable && (e.metaKey || e.ctrlKey || !resizeable))
718
+ showResize = false;
719
+ const showSkew = skewable && !showResize && point.name === 'resize-line';
720
+ const cursor = showSkew ? skewCursor : (showResize ? resizeCursor : rotateCursor);
721
+ rotation += (EditDataHelper.getFlipDirection(point.direction, flippedX, flippedY) + 1) * 45;
722
+ const { url, x, y } = cursor;
723
+ point.cursor = { url: toDataURL(url, rotation), x, y };
121
724
  }
122
- function mirrorCursors(mirror, mirrorX, mirrorY) {
123
- if (mirrorX) {
124
- const topCursor = mirror[top], topLeftCursor = mirror[topLeft], topRightCursor = mirror[topRight];
125
- mirror[top] = mirror[bottom];
126
- mirror[topLeft] = mirror[bottomLeft];
127
- mirror[topRight] = mirror[bottomRight];
128
- mirror[bottom] = topCursor;
129
- mirror[bottomLeft] = topLeftCursor;
130
- mirror[bottomRight] = topRightCursor;
131
- }
132
- if (mirrorY) {
133
- const leftCursor = mirror[left$1], topLeftCursor = mirror[topLeft], bottomLeftCursor = mirror[bottomLeft];
134
- mirror[left$1] = mirror[right$1];
135
- mirror[topLeft] = mirror[topRight];
136
- mirror[bottomLeft] = mirror[bottomRight];
137
- mirror[right$1] = leftCursor;
138
- mirror[topRight] = topLeftCursor;
139
- mirror[bottomRight] = bottomLeftCursor;
140
- }
725
+ function updateMoveCursor(editor) {
726
+ editor.editBox.rect.cursor = editor.config.moveCursor;
727
+ }
728
+ function toDataURL(svg, rotation) {
729
+ return '"data:image/svg+xml,' + encodeURIComponent(svg.replace('{{rotation}}', rotation.toString())) + '"';
141
730
  }
142
731
 
143
- const RectTool = {
144
- name: 'RectTool',
145
- getMirrorData(editor) {
146
- const { scaleX, scaleY } = editor.target;
147
- return {
148
- x: scaleX < 0 ? 1 : 0,
149
- y: scaleY < 0 ? 1 : 0
150
- };
151
- },
152
- resize(e) {
153
- const { target, bounds, resizeType, old } = e;
154
- const { x, y, width, height } = bounds;
155
- const point = { x: x - old.x, y: y - old.y };
156
- target.innerToWorld(point, null, true, target.parent);
157
- target.x += point.x;
158
- target.y += point.y;
159
- if (resizeType === 'scale') {
160
- target.scaleX *= width / old.width;
161
- target.scaleY *= height / old.height;
162
- }
163
- else {
164
- if (width < 0) {
165
- target.width = -width;
166
- target.scaleX *= -1;
167
- }
168
- else {
169
- if (target.width !== width)
170
- target.width = width;
171
- }
172
- if (height < 0) {
173
- target.height = -height;
174
- target.scaleY *= -1;
175
- }
176
- else {
177
- if (target.height !== height)
178
- target.height = height;
732
+ class EditPoint extends core.Box {
733
+ }
734
+
735
+ const fourDirection = ['top', 'right', 'bottom', 'left'];
736
+ class EditBox extends core.Group {
737
+ get flipped() { return this.flippedX || this.flippedY; }
738
+ get flippedX() { return this.scaleX < 0; }
739
+ get flippedY() { return this.scaleY < 0; }
740
+ get flippedOne() { return this.scaleX * this.scaleY < 0; }
741
+ constructor(editor) {
742
+ super();
743
+ this.rect = new core.Box({ name: 'rect', hitFill: 'all', hitStroke: 'none', strokeAlign: 'center', hitRadius: 5 });
744
+ this.circle = new EditPoint({ name: 'circle', strokeAlign: 'center', around: 'center', cursor: 'crosshair', hitRadius: 5 });
745
+ this.buttons = new core.Group({ around: 'center', hitSelf: false });
746
+ this.resizePoints = [];
747
+ this.rotatePoints = [];
748
+ this.resizeLines = [];
749
+ this.__eventIds = [];
750
+ this.editor = editor;
751
+ this.visible = false;
752
+ this.create();
753
+ this.__listenEvents();
754
+ }
755
+ create() {
756
+ let rotatePoint, resizeLine, resizePoint;
757
+ const { resizePoints, rotatePoints, resizeLines, rect, circle, buttons } = this;
758
+ const arounds = [{ x: 1, y: 1 }, { x: 0.5, y: 1 }, { x: 0, y: 1 }, { x: 0, y: 0.5 }, { x: 0, y: 0 }, { x: 0.5, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 0.5 }];
759
+ for (let i = 0; i < 8; i++) {
760
+ rotatePoint = new EditPoint({ name: 'rotate-point', around: arounds[i], width: 15, height: 15, hitFill: "all" });
761
+ rotatePoints.push(rotatePoint);
762
+ this.listenPointEvents(rotatePoint, 'rotate', i);
763
+ if (i % 2) {
764
+ resizeLine = new EditPoint({ name: 'resize-line', around: 'center', width: 10, height: 10, hitFill: "all" });
765
+ resizeLines.push(resizeLine);
766
+ this.listenPointEvents(resizeLine, 'resize', i);
179
767
  }
768
+ resizePoint = new EditPoint({ name: 'resize-point', around: 'center', strokeAlign: 'center', hitRadius: 5 });
769
+ resizePoints.push(resizePoint);
770
+ this.listenPointEvents(resizePoint, 'resize', i);
180
771
  }
181
- },
182
- rotate(e) {
183
- const { target, rotation, origin } = e;
184
- target.rotateOf(origin, rotation);
185
- },
186
- update(editor) {
187
- const { target, config, rotatePoints, targetRect, rect, circle, resizeLines, resizePoints } = editor;
188
- const { type, resizeable, rotateable, stroke, pointFill, pointSize, pointRadius } = config;
189
- const defaultStyle = { fill: pointFill, stroke, width: pointSize, height: pointSize, cornerRadius: pointRadius };
190
- const pointStyles = config.point instanceof Array ? config.point : [config.point || defaultStyle];
191
- const box = new core.Bounds(target.boxBounds);
192
- const w = target.worldTransform, pw = editor.parent.worldTransform;
193
- const matrix = new core.Matrix(w);
194
- matrix.divide(pw);
195
- const worldX = matrix.e, worldY = matrix.f;
196
- let { scaleX, scaleY, rotation, skewX, skewY } = w;
197
- scaleX /= pw.scaleX, scaleY /= pw.scaleY, rotation -= pw.rotation, skewX -= pw.skewX, skewY -= pw.skewY;
198
- const { x, y, width, height } = box.scale(scaleX, scaleY);
199
- editor.set({ x: worldX, y: worldY, rotation, skewX, skewY });
200
- targetRect.set({ x, y, width: box.width / scaleX, height: box.height / scaleY, scaleX, scaleY, visible: true });
201
- const points = [
202
- { x, y },
203
- { x: x + width / 2, y },
204
- { x: x + width, y },
205
- { x: x + width, y: y + height / 2 },
206
- { x: x + width, y: y + height },
207
- { x: x + width / 2, y: y + height },
208
- { x, y: y + height },
209
- { x, y: y + height / 2 }
210
- ];
211
- const rectPoints = [];
212
- let point, style, rotateP, resizeP, resizeL;
772
+ buttons.add(circle);
773
+ this.listenPointEvents(circle, 'rotate', 2);
774
+ this.addMany(...rotatePoints, rect, buttons, ...resizeLines, ...resizePoints);
775
+ }
776
+ update(bounds) {
777
+ const { config, list } = this.editor;
778
+ const { width, height } = bounds;
779
+ const { rect, circle, resizePoints, rotatePoints, resizeLines } = this;
780
+ const { middlePoint, resizeable, rotateable, stroke, strokeWidth } = config;
781
+ const pointsStyle = this.getPointsStyle();
782
+ const middlePointsStyle = this.getMiddlePointsStyle();
783
+ const showPoints = width > 20 || height > 20;
784
+ this.visible = list[0] && !list[0].locked;
785
+ let point = {}, style, rotateP, resizeP, resizeL;
213
786
  for (let i = 0; i < 8; i++) {
214
- point = points[i];
215
- style = pointStyles[i % pointStyles.length];
216
- resizeP = resizePoints[i];
217
- resizeL = resizeLines[Math.floor(i / 2)];
218
- rotateP = rotatePoints[i];
787
+ core.AroundHelper.toPoint(core.AroundHelper.directionData[i], bounds, point);
788
+ style = this.getPointStyle((i % 2) ? middlePointsStyle[((i - 1) / 2) % middlePointsStyle.length] : pointsStyle[(i / 2) % pointsStyle.length]);
789
+ resizeP = resizePoints[i], rotateP = rotatePoints[i], resizeL = resizeLines[Math.floor(i / 2)];
219
790
  resizeP.set(style);
220
- resizeP.x = rotateP.x = resizeL.x = point.x;
221
- resizeP.y = rotateP.y = resizeL.y = point.y;
222
- resizeP.visible = resizeL.visible = resizeable || rotateable;
223
- rotateP.visible = rotateable && resizeable;
791
+ resizeP.set(point), rotateP.set(point), resizeL.set(point);
792
+ resizeP.visible = resizeL.visible = showPoints && (resizeable || rotateable);
793
+ rotateP.visible = showPoints && rotateable && resizeable;
224
794
  if (i % 2) {
795
+ resizeP.visible = rotateP.visible = showPoints && !!middlePoint;
225
796
  if (((i + 1) / 2) % 2) {
226
- resizeL.width = Math.abs(width);
227
- rotateP.width = Math.max(10, Math.abs(width) - 30);
797
+ resizeL.width = width;
798
+ if (resizeP.width > width - 30)
799
+ resizeP.visible = false;
228
800
  }
229
801
  else {
230
- resizeL.height = Math.abs(height);
231
- rotateP.height = Math.max(10, Math.abs(height) - 30);
802
+ resizeL.height = height;
803
+ resizeP.rotation = 90;
804
+ if (resizeP.width > height - 30)
805
+ resizeP.visible = false;
232
806
  }
233
- resizeP.rotation = 90;
234
- resizeP.visible = type === 'mobile';
235
807
  }
236
808
  else {
237
- rectPoints.push(point.x, point.y);
809
+ resizeP.rotation = (i / 2) * 90;
238
810
  }
239
811
  }
240
- style = config.rotatePoint || style;
241
- circle.set(style);
242
- circle.x = x + width / 2;
243
- if (!style.y)
244
- circle.y = y - (10 + (resizeP.height + circle.height) / 2) * (this.getMirrorData(editor).y ? -1 : 1);
245
- circle.visible = rotateable && type === 'mobile';
246
- rect.set(config.rect || { stroke });
247
- rect.points = rectPoints;
248
- rect.visible = true;
812
+ circle.visible = showPoints && rotateable && !!config.rotatePoint;
813
+ circle.set(this.getPointStyle(config.rotatePoint || pointsStyle[0]));
814
+ rect.set(Object.assign({ stroke, strokeWidth }, (config.rect || {})));
815
+ rect.set(Object.assign(Object.assign({}, bounds), { visible: true }));
816
+ this.buttons.visible = showPoints;
817
+ this.layoutButtons();
818
+ }
819
+ layoutButtons() {
820
+ const { buttons, resizePoints } = this;
821
+ const { buttonsDirection, buttonsFixed, buttonsMargin, middlePoint } = this.editor.config;
822
+ const { flippedX, flippedY } = this;
823
+ let index = fourDirection.indexOf(buttonsDirection);
824
+ if ((index % 2 && flippedX) || ((index + 1) % 2 && flippedY)) {
825
+ if (buttonsFixed)
826
+ index = (index + 2) % 4;
827
+ }
828
+ const direction = buttonsFixed ? EditDataHelper.getRotateDirection(index, this.flippedOne ? this.rotation : -this.rotation, 4) : index;
829
+ const point = resizePoints[direction * 2 + 1];
830
+ const useX = direction % 2;
831
+ const sign = (!direction || direction === 3) ? -1 : 1;
832
+ const useWidth = index % 2;
833
+ const margin = (buttonsMargin + (useWidth ? ((middlePoint ? point.width : 0) + buttons.boxBounds.width) : ((middlePoint ? point.height : 0) + buttons.boxBounds.height)) / 2) * sign;
834
+ if (useX) {
835
+ buttons.x = point.x + margin;
836
+ buttons.y = point.y;
837
+ }
838
+ else {
839
+ buttons.x = point.x;
840
+ buttons.y = point.y + margin;
841
+ }
842
+ if (buttonsFixed) {
843
+ buttons.rotation = (direction - index) * 90;
844
+ buttons.scaleX = flippedX ? -1 : 1;
845
+ buttons.scaleY = flippedY ? -1 : 1;
846
+ }
847
+ }
848
+ getPointStyle(userStyle) {
849
+ const { stroke, strokeWidth, pointFill, pointSize, pointRadius } = this.editor.config;
850
+ const defaultStyle = { fill: pointFill, stroke, strokeWidth, width: pointSize, height: pointSize, cornerRadius: pointRadius };
851
+ return userStyle ? Object.assign(defaultStyle, userStyle) : defaultStyle;
852
+ }
853
+ getPointsStyle() {
854
+ const { point } = this.editor.config;
855
+ return point instanceof Array ? point : [point];
249
856
  }
857
+ getMiddlePointsStyle() {
858
+ const { middlePoint } = this.editor.config;
859
+ return middlePoint instanceof Array ? middlePoint : (middlePoint ? [middlePoint] : this.getPointsStyle());
860
+ }
861
+ onDragStart(e) {
862
+ this.dragging = true;
863
+ if (e.target.name === 'rect') {
864
+ this.moving = true;
865
+ this.editor.opacity = this.editor.config.hideOnMove ? 0 : 1;
866
+ }
867
+ }
868
+ onDragEnd(e) {
869
+ this.dragging = false;
870
+ this.moving = false;
871
+ if (e.target.name === 'rect')
872
+ this.editor.opacity = 1;
873
+ }
874
+ onDrag(e) {
875
+ const { editor } = this;
876
+ const point = this.enterPoint = e.current;
877
+ if (point.pointType === 'rotate' || e.metaKey || e.ctrlKey || !editor.config.resizeable) {
878
+ if (editor.config.rotateable)
879
+ editor.onRotate(e);
880
+ updateCursor(editor, e);
881
+ }
882
+ else {
883
+ editor.onScale(e);
884
+ }
885
+ }
886
+ onArrow(e) {
887
+ if (this.editor.hasTarget) {
888
+ const move = { x: 0, y: 0 };
889
+ const distance = e.shiftKey ? 10 : 1;
890
+ switch (e.code) {
891
+ case 'ArrowDown':
892
+ move.y = distance;
893
+ break;
894
+ case 'ArrowUp':
895
+ move.y = -distance;
896
+ break;
897
+ case 'ArrowLeft':
898
+ move.x = -distance;
899
+ break;
900
+ case 'ArrowRight':
901
+ move.x = distance;
902
+ }
903
+ if (move.x || move.y)
904
+ this.editor.move(move.x, move.y);
905
+ }
906
+ }
907
+ onDoubleClick() {
908
+ const { editor } = this;
909
+ if (editor.single && editor.element.isBranch) ;
910
+ }
911
+ listenPointEvents(point, type, direction) {
912
+ const { editor } = this;
913
+ point.direction = direction;
914
+ point.pointType = type;
915
+ point.on_(core.DragEvent.START, this.onDragStart, this);
916
+ point.on_(core.DragEvent.DRAG, this.onDrag, this);
917
+ point.on_(core.DragEvent.END, this.onDragEnd, this);
918
+ point.on_(core.PointerEvent.LEAVE, () => this.enterPoint = null);
919
+ if (point.name !== 'circle')
920
+ point.on_(core.PointerEvent.ENTER, (e) => { this.enterPoint = point, updateCursor(editor, e); });
921
+ }
922
+ __listenEvents() {
923
+ const { rect, editor } = this;
924
+ this.__eventIds = [
925
+ editor.on_(EditorEvent.SELECT, () => { this.visible = editor.hasTarget; }),
926
+ rect.on_(core.DragEvent.START, this.onDragStart, this),
927
+ rect.on_(core.DragEvent.DRAG, editor.onMove, editor),
928
+ rect.on_(core.DragEvent.END, this.onDragEnd, this),
929
+ rect.on_(core.PointerEvent.ENTER, () => updateMoveCursor(editor)),
930
+ rect.on_(core.PointerEvent.DOUBLE_CLICK, this.onDoubleClick, this)
931
+ ];
932
+ }
933
+ __removeListenEvents() {
934
+ this.off_(this.__eventIds);
935
+ this.__eventIds.length = 0;
936
+ }
937
+ destroy() {
938
+ this.editor = null;
939
+ this.__removeListenEvents();
940
+ super.destroy();
941
+ }
942
+ }
943
+
944
+ const filterStyle = `
945
+ <feOffset dy="1"/>
946
+ <feGaussianBlur stdDeviation="1.5"/>
947
+ <feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2 0"/>
948
+ <feBlend mode="normal" in="SourceGraphic" result="shape"/>`;
949
+ const resizeSVG = `
950
+ <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">
951
+ <g filter="url(#f)">
952
+ <g transform="rotate({{rotation}},12,12)">
953
+ <path d="M7.5 8.0H8.5V5.9L6.8 7.2L7.5 8.0ZM3 11.4L2.3 10.6L1.3 11.4L2.3 12.2L3 11.4ZM7.5 10.4H6.5V11.4H7.5V10.4ZM16.5 10.4V11.4H17.5V10.4H16.5ZM16.5 8.0L17.1 7.2L15.5 5.9V8.0H16.5ZM21 11.4L21.6 12.2L22.6 11.4L21.6 10.6L21 11.4ZM16.5 14.9H15.5V16.9L17.1 15.7L16.5 14.9ZM16.5 12.4H17.5V11.4H16.5V12.4ZM7.5 12.4V11.4H6.5V12.4H7.5ZM7.5 14.9L6.8 15.7L8.5 16.9V14.9H7.5ZM6.8 7.2L2.3 10.6L3.6 12.2L8.1 8.7L6.8 7.2ZM8.5 10.4V8.0H6.5V10.4H8.5ZM16.5 9.4H7.5V11.4H16.5V9.4ZM17.5 10.4V8.0H15.5V10.4H17.5ZM15.8 8.7L20.3 12.2L21.6 10.6L17.1 7.2L15.8 8.7ZM20.3 10.6L15.8 14.1L17.1 15.7L21.6 12.2L20.3 10.6ZM17.5 14.9V12.4H15.5V14.9H17.5ZM7.5 13.4H16.5V11.4H7.5V13.4ZM8.5 14.9V12.4H6.5V14.9H8.5ZM2.3 12.2L6.8 15.7L8.1 14.1L3.6 10.6L2.3 12.2Z" fill="white"/>
954
+ <path fill-rule="evenodd" d="M3 11.4L7.5 8.0V10.4H16.5V8.0L21 11.4L16.5 14.9V12.4H7.5V14.9L3 11.4Z" fill="black"/>
955
+ </g>
956
+ </g>
957
+ <defs>
958
+ <filter id="f" x="-1.6" y="3.9" width="27.2" height="16.9" filterUnits="userSpaceOnUse">
959
+ ${filterStyle}
960
+ </filter>
961
+ </defs>
962
+ </svg>
963
+ `;
964
+ const rotateSVG = `
965
+ <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">
966
+ <g filter="url(#f)">
967
+ <g transform="rotate(135,12,12),rotate({{rotation}},12,12)">
968
+ <path d="M20.4 8H21.4L20.8 7.1L17.3 2.6L17 2.1L16.6 2.6L13.1 7.1L12.5 8H13.5H15.4C14.9 11.8 11.8 14.9 8 15.4V13.5V12.5L7.1 13.1L2.6 16.6L2.1 17L2.6 17.3L7.1 20.8L8 21.4V20.4V18.4C13.5 17.9 17.9 13.5 18.4 8H20.4Z" stroke="white"/>
969
+ <path fill-rule="evenodd" d="M17 3L20.4 7.5H17.9C17.7 13.1 13.1 17.7 7.5 17.9V20.4L3 17L7.5 13.5V15.9C12.0 15.7 15.7 12.0 15.9 7.5H13.5L17 3Z" fill="black"/>
970
+ </g>
971
+ </g>
972
+ <defs>
973
+ <filter id="f" x="-1.6" y="-0.6" width="27.1" height="27.1" filterUnits="userSpaceOnUse">
974
+ ${filterStyle}
975
+ </filter>
976
+ </defs>
977
+ </svg>
978
+ `;
979
+ const skewSVG = `
980
+ <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">
981
+ <g filter="url(#f)">
982
+ <g transform="rotate(90,12,12),rotate({{rotation}},12,12)">
983
+ <path d="M21 10.4L21 11.4L23.8 11.4L21.6 9.6L21 10.4ZM17 10.4V11.4L17 11.4L17 10.4ZM15.5 6L16.1 5.2L14.5 3.9V6H15.5ZM15.5 8.4V9.4H16.5V8.4H15.5ZM6 8.4V7.4H5V8.4H6ZM6 10.4H5V11.4H6V10.4ZM7 14.4V13.4L7 13.4L7 14.4ZM3 14.4L3 13.4L0.1 13.4L2.3 15.2L3 14.4ZM8.5 18.9L7.8 19.7L9.5 21.0V18.9H8.5ZM8.5 16.4V15.4H7.5V16.4H8.5ZM19 16.4V17.4H20V16.4H19ZM19 14.4H20V13.4H19V14.4ZM21 9.4L17 9.4L17 11.4L21 11.4L21 9.4ZM14.8 6.7L20.3 11.2L21.6 9.6L16.1 5.2L14.8 6.7ZM16.5 8.4V6H14.5V8.4H16.5ZM6 9.4H15.5V7.4H6V9.4ZM7 10.4V8.4H5V10.4H7ZM15.5 9.4H6V11.4H15.5V9.4ZM17 9.4H15.5V11.4H17V9.4ZM7 15.4H8.5V13.4H7V15.4ZM3 15.4L7 15.4L7 13.4L3 13.4L3 15.4ZM9.1 18.1L3.6 13.6L2.3 15.2L7.8 19.7L9.1 18.1ZM7.5 16.4V18.9H9.5V16.4H7.5ZM19 15.4H8.5V17.4H19V15.4ZM18 14.4V16.4H20V14.4H18ZM8.5 15.4H19V13.4H8.5V15.4Z" fill="white"/>
984
+ <path fill-rule="evenodd" d="M17 10.4L21 10.4L15.5 6V8.4H6V10.4H15.5H17ZM8.5 14.4H7L3 14.4L8.5 18.9V16.4H19V14.4H8.5Z" fill="black"/>
985
+ </g>
986
+ </g>
987
+ <defs>
988
+ <filter x="-2.8" y="1.9" width="29.6" height="23.1" filterUnits="userSpaceOnUse" >
989
+ ${filterStyle}
990
+ </filter>
991
+ </defs>
992
+ </svg>
993
+ `;
994
+
995
+ const config = {
996
+ editSize: 'auto',
997
+ stroke: '#836DFF',
998
+ strokeWidth: 2,
999
+ pointFill: '#FFFFFF',
1000
+ pointSize: 10,
1001
+ pointRadius: 16,
1002
+ rotateGap: 45,
1003
+ buttonsDirection: 'bottom',
1004
+ buttonsMargin: 12,
1005
+ moveCursor: 'move',
1006
+ resizeCursor: { url: resizeSVG, x: 12, y: 12 },
1007
+ rotateCursor: { url: rotateSVG, x: 12, y: 12 },
1008
+ skewCursor: { url: skewSVG, x: 12, y: 12 },
1009
+ selector: true,
1010
+ hover: true,
1011
+ boxSelect: true,
1012
+ resizeable: true,
1013
+ rotateable: true,
1014
+ skewable: true
250
1015
  };
251
1016
 
1017
+ const { transformWorld, zoomOfWorld, skewOfWorld, rotateOfWorld, moveWorld } = core.LeafHelper;
1018
+ class EditTool {
1019
+ constructor() {
1020
+ this.tag = 'EditTool';
1021
+ }
1022
+ onMove(e) {
1023
+ const { moveX, moveY, editor } = e;
1024
+ const { app, list } = editor;
1025
+ app.lockLayout();
1026
+ list.forEach(target => {
1027
+ moveWorld(target, moveX, moveY);
1028
+ });
1029
+ app.unlockLayout();
1030
+ }
1031
+ onScale(e) {
1032
+ const { scaleX, scaleY, transform, worldOrigin, editor } = e;
1033
+ const { app, list } = editor;
1034
+ app.lockLayout();
1035
+ list.forEach(target => {
1036
+ const resize = editor.getEditSize(target) === 'size';
1037
+ if (transform) {
1038
+ transformWorld(target, transform, resize);
1039
+ }
1040
+ else {
1041
+ zoomOfWorld(target, worldOrigin, scaleX, scaleY, resize);
1042
+ }
1043
+ });
1044
+ app.unlockLayout();
1045
+ }
1046
+ onRotate(e) {
1047
+ const { rotation, transform, worldOrigin, editor } = e;
1048
+ const { app, list } = editor;
1049
+ app.lockLayout();
1050
+ list.forEach(target => {
1051
+ const resize = editor.getEditSize(target) === 'size';
1052
+ if (transform) {
1053
+ transformWorld(target, transform, resize);
1054
+ }
1055
+ else {
1056
+ rotateOfWorld(target, worldOrigin, rotation);
1057
+ }
1058
+ });
1059
+ app.unlockLayout();
1060
+ }
1061
+ onSkew(e) {
1062
+ const { skewX, skewY, transform, worldOrigin, editor } = e;
1063
+ const { app, list } = editor;
1064
+ app.lockLayout();
1065
+ list.forEach(target => {
1066
+ const resize = editor.getEditSize(target) === 'size';
1067
+ if (transform) {
1068
+ transformWorld(target, transform, resize);
1069
+ }
1070
+ else {
1071
+ skewOfWorld(target, worldOrigin, skewX, skewY, resize);
1072
+ }
1073
+ });
1074
+ app.unlockLayout();
1075
+ }
1076
+ update(editor) {
1077
+ const { simulateTarget, element } = editor;
1078
+ if (editor.multiple)
1079
+ simulateTarget.parent.updateLayout();
1080
+ const { x, y, scaleX, scaleY, rotation, skewX, skewY, width, height } = element.getLayoutBounds('box', editor, true);
1081
+ editor.editBox.set({ x, y, scaleX, scaleY, rotation, skewX, skewY });
1082
+ editor.editBox.update({ x: 0, y: 0, width, height });
1083
+ }
1084
+ }
1085
+ EditTool.list = [];
1086
+
252
1087
  const { left, right } = IDirection8;
253
- const LineTool = {
254
- name: 'LineTool',
255
- getMirrorData(_editor) {
256
- return {
257
- x: 0,
258
- y: 0
259
- };
260
- },
261
- resize(e) {
262
- const { direction, dragEvent, lockRatio, around } = e;
1088
+ class LineEditTool extends EditTool {
1089
+ constructor() {
1090
+ super(...arguments);
1091
+ this.tag = 'LineEditTool';
1092
+ this.scaleOfEvent = true;
1093
+ }
1094
+ onScaleWithDrag(e) {
1095
+ const { drag, direction, lockRatio, around } = e;
263
1096
  const target = e.target;
264
- const fromPoint = { x: 0, y: 0 };
1097
+ const fromPoint = core.getPointData();
265
1098
  const { toPoint } = target;
266
1099
  target.rotation = 0;
267
- let { x, y } = dragEvent.getInnerMove(target);
1100
+ let { x, y } = drag.getInnerMove(target);
268
1101
  if (lockRatio) {
269
1102
  if (Math.abs(x) > Math.abs(y)) {
270
1103
  y = 0;
@@ -295,226 +1128,375 @@ this.LeaferIN.editor = (function (exports, core) {
295
1128
  target.y = fromPoint.y;
296
1129
  target.getInnerPointByLocal(toPoint, null, null, true);
297
1130
  target.toPoint = toPoint;
298
- },
299
- rotate(e) {
300
- RectTool.rotate(e);
301
- },
1131
+ }
1132
+ onSkew(_e) {
1133
+ }
302
1134
  update(editor) {
303
- const { rotatePoints, circle, resizeLines, resizePoints } = editor;
304
- RectTool.update(editor);
1135
+ const { rotatePoints, resizeLines, resizePoints } = editor.editBox;
1136
+ super.update(editor);
305
1137
  for (let i = 0; i < 8; i++) {
306
1138
  if (i < 4)
307
1139
  resizeLines[i].visible = false;
308
- resizePoints[i].visible = rotatePoints[i].visible = i === left || i === right;
1140
+ resizePoints[i].visible = rotatePoints[i].visible = (i === left || i === right);
309
1141
  }
310
- circle.visible = false;
311
1142
  }
312
- };
1143
+ }
313
1144
 
314
- class EditorResizeEvent extends core.Event {
315
- constructor(type, data) {
316
- super(type);
317
- if (data)
318
- Object.assign(this, data);
1145
+ function getEditTool(list) {
1146
+ if (list.length === 1) {
1147
+ const leaf = list[0];
1148
+ if (leaf instanceof core.Line && !leaf.points) {
1149
+ return new LineEditTool();
1150
+ }
1151
+ else {
1152
+ return new EditTool();
1153
+ }
1154
+ }
1155
+ else {
1156
+ return new EditTool();
319
1157
  }
320
1158
  }
321
- EditorResizeEvent.RESIZE = 'editor.resize';
322
1159
 
323
- class EditorRotateEvent extends core.Event {
324
- constructor(type, data) {
325
- super(type);
326
- if (data)
327
- Object.assign(this, data);
1160
+ function simulate(editor) {
1161
+ const { simulateTarget, leafList: targetList } = editor;
1162
+ const { x, y, width, height } = new core.Bounds().setListWithFn(targetList.list, (leaf) => leaf.worldBoxBounds);
1163
+ const parent = simulateTarget.parent = targetList.list[0].leafer.zoomLayer;
1164
+ const { scaleX, scaleY, e: worldX, f: worldY } = parent.__world;
1165
+ simulateTarget.reset({ x: (x - worldX) / scaleX, y: (y - worldY) / scaleY, width: width / scaleX, height: height / scaleY });
1166
+ }
1167
+
1168
+ function onTarget(editor, oldValue) {
1169
+ const { target } = editor;
1170
+ if (target) {
1171
+ editor.leafList = target instanceof core.LeafList ? target : new core.LeafList(target instanceof Array ? target : target);
1172
+ }
1173
+ else {
1174
+ editor.leafList.reset();
1175
+ }
1176
+ editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor, value: target, oldValue }));
1177
+ if (editor.hasTarget) {
1178
+ editor.waitLeafer(() => {
1179
+ if (editor.multiple)
1180
+ simulate(editor);
1181
+ updateMoveCursor(editor);
1182
+ editor.updateEditTool();
1183
+ editor.update();
1184
+ editor.listenTargetEvents();
1185
+ });
1186
+ }
1187
+ else {
1188
+ editor.removeTargetEvents();
328
1189
  }
329
1190
  }
330
- EditorRotateEvent.ROTATE = 'editor.rotate';
1191
+ function onHover(editor, oldValue) {
1192
+ editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor, value: editor.hoverTarget, oldValue }));
1193
+ }
331
1194
 
332
- class Editor extends core.Group {
333
- get target() { return this._target; }
334
- set target(value) {
335
- this.__removeTargetEvents();
336
- this.visible = !!value;
337
- this._target = value;
338
- if (value)
339
- this.onTarget();
1195
+ const order = (a, b) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
1196
+ const reverseOrder = (a, b) => b.parent.children.indexOf(b) - a.parent.children.indexOf(a);
1197
+ const EditorHelper = {
1198
+ group(list, element, userGroup) {
1199
+ list.sort(reverseOrder);
1200
+ const { app, parent } = list[0];
1201
+ let group;
1202
+ if (userGroup && userGroup.add) {
1203
+ group = userGroup;
1204
+ }
1205
+ else {
1206
+ group = new core.Group(userGroup);
1207
+ }
1208
+ parent.addAt(group, parent.children.indexOf(list[0]));
1209
+ list.sort(order);
1210
+ const matrx = new core.Matrix(element.worldTransform);
1211
+ matrx.divideParent(parent.worldTransform);
1212
+ group.setTransform(matrx);
1213
+ group.editable = true;
1214
+ group.hitChildren = false;
1215
+ app.lockLayout();
1216
+ list.forEach(child => child.dropTo(group));
1217
+ app.unlockLayout();
1218
+ return group;
1219
+ },
1220
+ ungroup(list) {
1221
+ const { app } = list[0];
1222
+ const ungroupList = [];
1223
+ app.lockLayout();
1224
+ list.forEach(leaf => {
1225
+ if (leaf.isBranch) {
1226
+ const { parent, children } = leaf;
1227
+ while (children.length) {
1228
+ ungroupList.push(children[0]);
1229
+ children[0].dropTo(parent, parent.children.indexOf(leaf));
1230
+ }
1231
+ leaf.remove();
1232
+ }
1233
+ else {
1234
+ ungroupList.push(leaf);
1235
+ }
1236
+ });
1237
+ app.unlockLayout();
1238
+ return ungroupList;
1239
+ },
1240
+ toTop(list) {
1241
+ list.sort(order);
1242
+ list.forEach(leaf => {
1243
+ if (leaf.parent)
1244
+ leaf.parent.add(leaf);
1245
+ });
1246
+ },
1247
+ toBottom(list) {
1248
+ list.sort(reverseOrder);
1249
+ list.forEach(leaf => {
1250
+ if (leaf.parent)
1251
+ leaf.parent.addAt(leaf, 0);
1252
+ });
340
1253
  }
1254
+ };
1255
+
1256
+ class Editor extends core.Group {
1257
+ get list() { return this.leafList.list; }
1258
+ get hasTarget() { return !!this.list.length; }
1259
+ get multiple() { return this.list.length > 1; }
1260
+ get single() { return this.list.length === 1; }
1261
+ get element() { return this.multiple ? this.simulateTarget : this.list[0]; }
1262
+ get buttons() { return this.editBox.buttons; }
1263
+ get dragging() { return this.editBox.dragging; }
341
1264
  constructor(userConfig, data) {
342
1265
  super(data);
343
- this.config = {
344
- type: 'pc',
345
- stroke: '#836DFF',
346
- pointFill: '#FFFFFF',
347
- pointSize: 10,
348
- pointRadius: 10,
349
- rotateGap: 90,
350
- hideOnMove: false,
351
- moveCursor: 'move',
352
- resizeType: 'auto',
353
- resizeCursor: ['nwse-resize', 'ns-resize', 'nesw-resize', 'ew-resize', 'nwse-resize', 'ns-resize', 'nesw-resize', 'ew-resize'],
354
- rotateCursor: ['ne-resize', 'e-resize', 'se-resize', 's-resize', 'sw-resize', 'w-resize', 'nw-resize', 'n-resize'],
355
- resizeable: true,
356
- rotateable: true
357
- };
358
- this.resizePoints = [];
359
- this.rotatePoints = [];
360
- this.resizeLines = [];
361
- this.targetRect = new core.Rect({ hitFill: 'all', hitRadius: 5 });
362
- this.rect = new core.Polygon({ hittable: false, strokeAlign: 'center' });
363
- this.circle = new core.Rect({ around: 'center', hitRadius: 10 });
364
- this.__eventIds = [];
365
- this.__targetEventIds = [];
1266
+ this.config = config;
1267
+ this.leafList = new core.LeafList();
1268
+ this.simulateTarget = new core.Rect({ visible: false });
1269
+ this.editBox = new EditBox(this);
1270
+ this.selector = new EditSelect(this);
1271
+ this.targetEventIds = [];
366
1272
  if (userConfig)
367
1273
  this.config = core.DataHelper.default(userConfig, this.config);
368
- this.init();
1274
+ this.addMany(this.selector, this.editBox);
369
1275
  }
370
- init() {
371
- let rotatePoint, resizeLine, resizePoint;
372
- const { resizePoints, rotatePoints, resizeLines } = this;
373
- for (let i = 0; i < 8; i++) {
374
- rotatePoint = new core.Rect({ around: 'center', width: 30, height: 30, hitRadius: 10, hitFill: "all" });
375
- rotatePoints.push(rotatePoint);
376
- this.__listenPointEvents(rotatePoint, 'rotate', i);
377
- if (i % 2) {
378
- resizeLine = new core.Rect({ around: 'center', width: 10, height: 10, hitFill: "all" });
379
- resizeLines.push(resizeLine);
380
- this.__listenPointEvents(resizeLine, 'resize', i);
381
- }
382
- resizePoint = new core.Rect({ around: 'center', hitRadius: 5 });
383
- resizePoints.push(resizePoint);
384
- this.__listenPointEvents(resizePoint, 'resize', i);
385
- }
386
- this.__listenPointEvents(this.circle, 'rotate', 1);
387
- this.addMany(...rotatePoints, this.targetRect, this.rect, this.circle, ...resizeLines, ...resizePoints);
388
- this.__listenEvents();
1276
+ hasItem(item) {
1277
+ return this.leafList.has(item);
389
1278
  }
390
- onTarget() {
391
- this.tool = this.getTool(this.target);
392
- this.waitLeafer(() => {
393
- this.update();
394
- this.updateMoveCursor();
395
- this.__listenTargetEvents();
396
- });
1279
+ addItem(item) {
1280
+ if (!this.hasItem(item) && !item.locked)
1281
+ this.leafList.add(item), this.target = this.leafList.list;
397
1282
  }
398
- getTool(value) {
399
- return (value.tag === 'Line' && value.resizeable) ? LineTool : RectTool;
1283
+ removeItem(item) {
1284
+ if (this.hasItem(item))
1285
+ this.leafList.remove(item), this.target = this.leafList.list;
400
1286
  }
401
- update() {
402
- if (!this.target)
403
- return;
404
- this.tool.update(this);
1287
+ shiftItem(item) {
1288
+ this.hasItem(item) ? this.removeItem(item) : this.addItem(item);
405
1289
  }
406
- onDrag(e) {
407
- const { resizeable, rotateable } = this.config;
408
- if (e.metaKey || e.ctrlKey || !resizeable) {
409
- if (rotateable)
410
- this.onRotate(e);
411
- }
412
- else {
413
- this.onResize(e);
1290
+ update() {
1291
+ if (this.target) {
1292
+ if (this.editTool)
1293
+ this.editTool.update(this);
1294
+ this.selector.update();
414
1295
  }
415
1296
  }
1297
+ updateEditTool() {
1298
+ this.editTool = getEditTool(this.list);
1299
+ }
1300
+ getEditSize(ui) {
1301
+ let { editSize } = this.config;
1302
+ return editSize === 'auto' ? ui.editSize : editSize;
1303
+ }
416
1304
  onMove(e) {
417
- const { target } = this;
418
- const { x, y } = e.getLocalMove(target);
1305
+ const move = e.getLocalMove(this.element);
419
1306
  if (e.shiftKey) {
420
- if (Math.abs(x) > Math.abs(y)) {
421
- target.x += x;
422
- }
423
- else {
424
- target.y += y;
425
- }
1307
+ if (Math.abs(move.x) > Math.abs(move.y))
1308
+ move.y = 0;
1309
+ else
1310
+ move.x = 0;
1311
+ }
1312
+ this.move(move.x, move.y);
1313
+ }
1314
+ onScale(e) {
1315
+ const { element } = this;
1316
+ const { direction } = e.current;
1317
+ let { around, lockRatio } = this.config;
1318
+ if (e.shiftKey)
1319
+ lockRatio = true;
1320
+ const data = EditDataHelper.getScaleData(element.boxBounds, direction, e.getInnerMove(element), lockRatio, EditDataHelper.getAround(around, e.altKey));
1321
+ if (this.editTool.onScaleWithDrag) {
1322
+ data.drag = e;
1323
+ this.scaleWithDrag(data);
426
1324
  }
427
1325
  else {
428
- target.x += x;
429
- target.y += y;
1326
+ this.scaleOf(data.origin, data.scaleX, data.scaleY);
430
1327
  }
431
1328
  }
432
1329
  onRotate(e) {
433
- const { target } = this;
434
- const { rotateGap } = this.config;
435
- const { x, y, width, height } = target.boxBounds;
436
- const origin = { x: x + width / 2, y: y + height / 2 };
437
- let rotation;
1330
+ const { skewable, around, rotateGap } = this.config;
1331
+ const { direction, name } = e.current;
1332
+ if (skewable && name === 'resize-line')
1333
+ return this.onSkew(e);
1334
+ const { element } = this;
1335
+ let origin, rotation;
438
1336
  if (e instanceof core.RotateEvent) {
439
- rotation = e.rotation;
1337
+ rotation = e.rotation, origin = element.getInnerPoint(e);
440
1338
  }
441
1339
  else {
442
- const point = e;
443
- const last = { x: point.x - e.moveX, y: point.y - e.moveY };
444
- rotation = core.PointHelper.getChangeAngle(last, target.getWorldPoint(origin), point);
445
- }
446
- rotation = core.MathHelper.getGapRotation(target.rotation + rotation, rotateGap) - target.rotation;
447
- const event = new EditorRotateEvent(EditorRotateEvent.ROTATE, { editor: this, target, origin, rotation });
448
- this.tool.rotate(event);
449
- target.emitEvent(event);
450
- }
451
- onResize(e) {
452
- const { target } = this;
453
- const { __direction } = e.current.__;
454
- let { resizeType, around, lockRatio } = this.config;
455
- if (e.shiftKey)
456
- lockRatio = true;
457
- if (e.altKey && !around)
458
- around = 'center';
459
- if (resizeType === 'auto')
460
- resizeType = target.resizeable ? 'size' : 'scale';
461
- const data = getResizeData(target.boxBounds, __direction, e.getInnerMove(this.targetRect), lockRatio, around);
462
- const event = new EditorResizeEvent(EditorResizeEvent.RESIZE, Object.assign(Object.assign({}, data), { target, editor: this, dragEvent: e, resizeType }));
463
- this.tool.resize(event);
464
- target.emitEvent(event);
465
- }
466
- updateMoveCursor() {
467
- this.targetRect.cursor = this.config.moveCursor;
1340
+ const last = { x: e.x - e.moveX, y: e.y - e.moveY };
1341
+ const data = EditDataHelper.getRotateData(element.boxBounds, direction, e.getInner(element), element.getInnerPoint(last), e.shiftKey ? null : (around || 'center'));
1342
+ rotation = data.rotation;
1343
+ origin = data.origin;
1344
+ }
1345
+ rotation = core.MathHelper.getGapRotation(rotation, rotateGap, element.rotation);
1346
+ if (!rotation)
1347
+ return;
1348
+ if (element.scaleX * element.scaleY < 0)
1349
+ rotation = -rotation;
1350
+ this.rotateOf(origin, core.MathHelper.float(rotation, 2));
468
1351
  }
469
- __listenEvents() {
470
- this.__eventIds = [
471
- this.targetRect.on_(core.DragEvent.START, () => { this.opacity = this.config.hideOnMove ? 0 : 1; }),
472
- this.targetRect.on_(core.DragEvent.DRAG, this.onMove, this),
473
- this.targetRect.on_(core.DragEvent.END, () => { this.opacity = 1; }),
474
- this.targetRect.on_(core.PointerEvent.ENTER, this.updateMoveCursor, this)
475
- ];
1352
+ onSkew(e) {
1353
+ const { element } = this;
1354
+ const { around } = this.config;
1355
+ const { origin, skewX, skewY } = EditDataHelper.getSkewData(element.boxBounds, e.current.direction, e.getInnerMove(element), EditDataHelper.getAround(around, e.altKey));
1356
+ if (!skewX && !skewY)
1357
+ return;
1358
+ this.skewOf(origin, skewX, skewY);
476
1359
  }
477
- __removeListenEvents() {
478
- this.targetRect.off_(this.__eventIds);
479
- this.__eventIds.length = 0;
1360
+ move(x, y) {
1361
+ const { element } = this;
1362
+ const world = element.getWorldPointByLocal({ x, y }, null, true);
1363
+ const event = new EditorMoveEvent(EditorMoveEvent.MOVE, { target: element, editor: this, moveX: world.x, moveY: world.y });
1364
+ this.editTool.onMove(event);
1365
+ this.emitEvent(event);
1366
+ if (this.multiple)
1367
+ element.move(x, y);
480
1368
  }
481
- __listenPointEvents(point, type, direction) {
482
- point.__.__direction = direction;
483
- const resize = point.__.__isResizePoint = type === 'resize';
484
- point.on_(core.DragEvent.DRAG, resize ? this.onDrag : this.onRotate, this);
485
- point.on_(core.PointerEvent.LEAVE, () => this.enterPoint = null);
486
- point.on_(core.PointerEvent.ENTER, (e) => { this.enterPoint = point; updateCursor(this, e); });
1369
+ scaleWithDrag(data) {
1370
+ const { element } = this;
1371
+ const worldOrigin = element.getWorldPoint(data.origin);
1372
+ const event = new EditorScaleEvent(EditorScaleEvent.SCALE, Object.assign(Object.assign({}, data), { target: element, editor: this, worldOrigin }));
1373
+ this.editTool.onScaleWithDrag(event);
1374
+ this.emitEvent(event);
487
1375
  }
488
- __listenTargetEvents() {
489
- if (this.target) {
490
- const { leafer } = this.target;
491
- this.__targetEventIds = [
1376
+ scaleOf(origin, scaleX, scaleY = scaleX, _resize) {
1377
+ const { element } = this;
1378
+ const worldOrigin = element.getWorldPoint(origin);
1379
+ let transform;
1380
+ if (this.multiple) {
1381
+ const oldMatrix = new core.Matrix(element.worldTransform);
1382
+ element.scaleOf(origin, scaleX, scaleY);
1383
+ transform = new core.Matrix(element.worldTransform).divide(oldMatrix);
1384
+ }
1385
+ const event = new EditorScaleEvent(EditorScaleEvent.SCALE, { target: element, editor: this, worldOrigin, scaleX, scaleY, transform });
1386
+ this.editTool.onScale(event);
1387
+ this.emitEvent(event);
1388
+ }
1389
+ rotateOf(origin, rotation) {
1390
+ const { element } = this;
1391
+ const worldOrigin = element.getWorldPoint(origin);
1392
+ let transform;
1393
+ if (this.multiple) {
1394
+ const oldMatrix = new core.Matrix(element.worldTransform);
1395
+ element.rotateOf(origin, rotation);
1396
+ transform = new core.Matrix(element.worldTransform).divide(oldMatrix);
1397
+ }
1398
+ const event = new EditorRotateEvent(EditorRotateEvent.ROTATE, { target: element, editor: this, worldOrigin, rotation, transform });
1399
+ this.editTool.onRotate(event);
1400
+ this.emitEvent(event);
1401
+ }
1402
+ skewOf(origin, skewX, skewY = 0, _resize) {
1403
+ const { element } = this;
1404
+ const worldOrigin = element.getWorldPoint(origin);
1405
+ let transform;
1406
+ if (this.multiple) {
1407
+ const oldMatrix = new core.Matrix(element.worldTransform);
1408
+ element.skewOf(origin, skewX, skewY);
1409
+ transform = new core.Matrix(element.worldTransform).divide(oldMatrix);
1410
+ }
1411
+ const event = new EditorSkewEvent(EditorSkewEvent.SKEW, {
1412
+ target: element, editor: this, skewX, skewY, transform, worldOrigin
1413
+ });
1414
+ this.editTool.onSkew(event);
1415
+ this.emitEvent(event);
1416
+ }
1417
+ group(userGroup) {
1418
+ if (this.multiple)
1419
+ this.target = EditorHelper.group(this.list, this.element, userGroup);
1420
+ return this.target;
1421
+ }
1422
+ ungroup() {
1423
+ if (this.list.length)
1424
+ this.target = EditorHelper.ungroup(this.list);
1425
+ return this.list;
1426
+ }
1427
+ lock() {
1428
+ this.list.forEach(leaf => leaf.locked = true);
1429
+ this.update();
1430
+ }
1431
+ unlock() {
1432
+ this.list.forEach(leaf => leaf.locked = false);
1433
+ this.update();
1434
+ }
1435
+ toTop() {
1436
+ if (this.list.length) {
1437
+ EditorHelper.toTop(this.list);
1438
+ this.leafList.update();
1439
+ }
1440
+ }
1441
+ toBottom() {
1442
+ if (this.list.length) {
1443
+ EditorHelper.toBottom(this.list);
1444
+ this.leafList.update();
1445
+ }
1446
+ }
1447
+ listenTargetEvents() {
1448
+ if (!this.targetEventIds.length) {
1449
+ const { leafer } = this.list[0];
1450
+ this.targetEventIds = [
492
1451
  leafer.on_(core.RenderEvent.START, this.update, this),
493
- leafer.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); })
1452
+ leafer.on_([core.KeyEvent.HOLD, core.KeyEvent.UP], (e) => { updateCursor(this, e); }),
1453
+ leafer.on_(core.KeyEvent.DOWN, this.editBox.onArrow, this.editBox)
494
1454
  ];
495
1455
  }
496
1456
  }
497
- __removeTargetEvents() {
498
- if (this.__targetEventIds.length) {
499
- const { leafer } = this.target;
500
- if (leafer)
501
- leafer.off_(this.__targetEventIds);
502
- this.__targetEventIds.length = 0;
1457
+ removeTargetEvents() {
1458
+ const { targetEventIds } = this;
1459
+ if (targetEventIds.length) {
1460
+ this.off_(targetEventIds);
1461
+ targetEventIds.length = 0;
503
1462
  }
504
1463
  }
505
1464
  destroy() {
506
- this.__removeListenEvents();
507
- this._target = null;
508
- super.destroy();
1465
+ if (!this.destroyed) {
1466
+ this.simulateTarget.destroy();
1467
+ this.target = this.hoverTarget = this.simulateTarget = null;
1468
+ super.destroy();
1469
+ }
509
1470
  }
510
1471
  }
1472
+ __decorate([
1473
+ targetAttr(onHover)
1474
+ ], Editor.prototype, "hoverTarget", void 0);
1475
+ __decorate([
1476
+ targetAttr(onTarget)
1477
+ ], Editor.prototype, "target", void 0);
1478
+
1479
+ core.Creator.editor = function (options) {
1480
+ return new Editor(options);
1481
+ };
511
1482
 
1483
+ exports.EditBox = EditBox;
1484
+ exports.EditDataHelper = EditDataHelper;
1485
+ exports.EditPoint = EditPoint;
1486
+ exports.EditSelect = EditSelect;
1487
+ exports.EditSelectHelper = EditSelectHelper;
1488
+ exports.EditTool = EditTool;
512
1489
  exports.Editor = Editor;
513
- exports.EditorResizeEvent = EditorResizeEvent;
1490
+ exports.EditorEvent = EditorEvent;
1491
+ exports.EditorHelper = EditorHelper;
1492
+ exports.EditorMoveEvent = EditorMoveEvent;
514
1493
  exports.EditorRotateEvent = EditorRotateEvent;
515
- exports.LineTool = LineTool;
516
- exports.RectTool = RectTool;
1494
+ exports.EditorScaleEvent = EditorScaleEvent;
1495
+ exports.EditorSkewEvent = EditorSkewEvent;
1496
+ exports.LineEditTool = LineEditTool;
1497
+ exports.SelectArea = SelectArea;
1498
+ exports.Stroker = Stroker;
517
1499
 
518
1500
  return exports;
519
1501
 
520
- })({}, LeaferUI);
1502
+ })({}, LeaferUI, LeaferUI);