@leafer-ui/core 1.0.0-beta.9 → 1.0.0-rc.10

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.
@@ -0,0 +1,1328 @@
1
+ import { Leafer, Leaf, BoundsHelper as BoundsHelper$1, PointHelper as PointHelper$1, UI, Platform, Rect, Group } from '@leafer-ui/draw';
2
+ export * from '@leafer-ui/draw';
3
+ import { registerUI, Creator, PropertyEvent, Debug, DataHelper, canvasSizeAttrs, LayoutEvent, RenderEvent, Event, EventCreator, registerUIEvent, LeafList, PointHelper, LeafHelper, BoundsHelper, Bounds, ResizeEvent, LeaferEvent, CanvasManager } from '@leafer/core';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol */
20
+
21
+
22
+ function __decorate(decorators, target, key, desc) {
23
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
+ 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;
26
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
27
+ }
28
+
29
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
30
+ var e = new Error(message);
31
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
32
+ };
33
+
34
+ let App = class App extends Leafer {
35
+ get __tag() { return 'App'; }
36
+ get isApp() { return true; }
37
+ constructor(userConfig, data) {
38
+ super(userConfig, data);
39
+ }
40
+ init(userConfig, parentApp) {
41
+ super.init(userConfig, parentApp);
42
+ if (userConfig) {
43
+ const { ground, tree, sky, editor } = userConfig;
44
+ if (ground)
45
+ this.ground = this.addLeafer(ground);
46
+ if (tree || editor)
47
+ this.tree = this.addLeafer(tree);
48
+ if (sky || editor)
49
+ this.sky = this.addLeafer(sky || { type: 'draw', usePartRender: false });
50
+ if (editor) {
51
+ this.editor = Creator.editor(editor);
52
+ this.sky.add(this.editor);
53
+ }
54
+ }
55
+ }
56
+ __setApp() {
57
+ const { canvas } = this;
58
+ const { realCanvas, view } = this.config;
59
+ if (realCanvas || view === this.canvas.view || !canvas.parentView) {
60
+ this.realCanvas = true;
61
+ }
62
+ else {
63
+ canvas.unrealCanvas();
64
+ }
65
+ this.leafer = this;
66
+ this.watcher.disable();
67
+ this.layouter.disable();
68
+ this.__eventIds.push(this.on_(PropertyEvent.CHANGE, this.__onPropertyChange, this));
69
+ }
70
+ start() {
71
+ super.start();
72
+ this.children.forEach(leafer => leafer.start());
73
+ }
74
+ stop() {
75
+ this.children.forEach(leafer => leafer.stop());
76
+ super.stop();
77
+ }
78
+ unlockLayout() {
79
+ super.unlockLayout();
80
+ this.children.forEach(leafer => leafer.unlockLayout());
81
+ }
82
+ lockLayout() {
83
+ super.lockLayout();
84
+ this.children.forEach(leafer => leafer.lockLayout());
85
+ }
86
+ forceRender(bounds) {
87
+ this.children.forEach(leafer => leafer.forceRender(bounds));
88
+ }
89
+ addLeafer(merge) {
90
+ const leafer = new Leafer(merge);
91
+ this.add(leafer);
92
+ return leafer;
93
+ }
94
+ add(leafer) {
95
+ if (!leafer.view) {
96
+ if (this.realCanvas && !this.canvas.bounds) {
97
+ setTimeout(() => this.add(leafer), 10);
98
+ return;
99
+ }
100
+ leafer.init(this.__getChildConfig(leafer.userConfig), this);
101
+ }
102
+ super.add(leafer);
103
+ this.__listenChildEvents(leafer);
104
+ }
105
+ __onPropertyChange() {
106
+ if (Debug.showHitView)
107
+ this.children.forEach(leafer => leafer.forceUpdate('surface'));
108
+ }
109
+ __onCreated() {
110
+ this.created = this.children.every(child => child.created);
111
+ }
112
+ __onReady() {
113
+ if (this.children.every(child => child.ready))
114
+ super.__onReady();
115
+ }
116
+ __onViewReady() {
117
+ if (this.children.every(child => child.viewReady))
118
+ super.__onViewReady();
119
+ }
120
+ __onChildRenderEnd(e) {
121
+ this.renderer.addBlock(e.renderBounds);
122
+ if (this.viewReady)
123
+ this.renderer.update();
124
+ }
125
+ __render(canvas, _options) {
126
+ this.children.forEach(leafer => canvas.copyWorld(leafer.canvas));
127
+ }
128
+ __onResize(event) {
129
+ this.children.forEach(leafer => leafer.resize(event));
130
+ super.__onResize(event);
131
+ }
132
+ __checkUpdateLayout() {
133
+ this.children.forEach(leafer => leafer.__layout.update());
134
+ }
135
+ __getChildConfig(userConfig) {
136
+ let config = Object.assign({}, this.config);
137
+ config.hittable = config.realCanvas = undefined;
138
+ if (userConfig)
139
+ DataHelper.assign(config, userConfig);
140
+ if (this.autoLayout)
141
+ DataHelper.copyAttrs(config, this, canvasSizeAttrs);
142
+ config.view = this.realCanvas ? undefined : this.view;
143
+ config.fill = undefined;
144
+ return config;
145
+ }
146
+ __listenChildEvents(leafer) {
147
+ leafer.once(LayoutEvent.END, () => this.__onReady());
148
+ leafer.once(RenderEvent.START, () => this.__onCreated());
149
+ leafer.once(RenderEvent.END, () => this.__onViewReady());
150
+ if (this.realCanvas)
151
+ this.__eventIds.push(leafer.on_(RenderEvent.END, this.__onChildRenderEnd, this));
152
+ }
153
+ };
154
+ App = __decorate([
155
+ registerUI()
156
+ ], App);
157
+
158
+ function draw(leafer) {
159
+ const { config } = leafer;
160
+ config.move.dragOut = false;
161
+ }
162
+
163
+ const downKeyMap = {};
164
+ const Keyboard = {
165
+ isHoldSpaceKey() {
166
+ return Keyboard.isHold('Space');
167
+ },
168
+ isHold(code) {
169
+ return downKeyMap[code];
170
+ },
171
+ setDownCode(code) {
172
+ if (!downKeyMap[code])
173
+ downKeyMap[code] = true;
174
+ },
175
+ setUpCode(code) {
176
+ downKeyMap[code] = false;
177
+ }
178
+ };
179
+
180
+ const PointerButton = {
181
+ LEFT: 1,
182
+ RIGHT: 2,
183
+ MIDDLE: 4,
184
+ defaultLeft(event) { if (!event.buttons)
185
+ event.buttons = 1; },
186
+ left(event) { return event.buttons === 1; },
187
+ right(event) { return event.buttons === 2; },
188
+ middle(event) { return event.buttons === 4; }
189
+ };
190
+
191
+ class UIEvent extends Event {
192
+ get spaceKey() { return Keyboard.isHoldSpaceKey(); }
193
+ get left() { return PointerButton.left(this); }
194
+ get right() { return PointerButton.right(this); }
195
+ get middle() { return PointerButton.middle(this); }
196
+ constructor(params) {
197
+ super(params.type);
198
+ this.bubbles = true;
199
+ Object.assign(this, params);
200
+ }
201
+ getPage() {
202
+ return this.current.getPagePoint(this);
203
+ }
204
+ getInner(relative) {
205
+ if (!relative)
206
+ relative = this.current;
207
+ return relative.getInnerPoint(this);
208
+ }
209
+ getLocal(relative) {
210
+ if (!relative)
211
+ relative = this.current;
212
+ return relative.getLocalPoint(this);
213
+ }
214
+ static changeName(oldName, newName) {
215
+ EventCreator.changeName(oldName, newName);
216
+ }
217
+ }
218
+
219
+ let PointerEvent = class PointerEvent extends UIEvent {
220
+ };
221
+ PointerEvent.POINTER = 'pointer';
222
+ PointerEvent.BEFORE_DOWN = 'pointer.before_down';
223
+ PointerEvent.BEFORE_MOVE = 'pointer.before_move';
224
+ PointerEvent.BEFORE_UP = 'pointer.before_up';
225
+ PointerEvent.DOWN = 'pointer.down';
226
+ PointerEvent.MOVE = 'pointer.move';
227
+ PointerEvent.UP = 'pointer.up';
228
+ PointerEvent.OVER = 'pointer.over';
229
+ PointerEvent.OUT = 'pointer.out';
230
+ PointerEvent.ENTER = 'pointer.enter';
231
+ PointerEvent.LEAVE = 'pointer.leave';
232
+ PointerEvent.TAP = 'tap';
233
+ PointerEvent.DOUBLE_TAP = 'double_tap';
234
+ PointerEvent.CLICK = 'click';
235
+ PointerEvent.DOUBLE_CLICK = 'double_click';
236
+ PointerEvent.LONG_PRESS = 'long_press';
237
+ PointerEvent.LONG_TAP = 'long_tap';
238
+ PointerEvent.MENU = 'pointer.menu';
239
+ PointerEvent.MENU_TAP = 'pointer.menu_tap';
240
+ PointerEvent = __decorate([
241
+ registerUIEvent()
242
+ ], PointerEvent);
243
+
244
+ const move = {};
245
+ let DragEvent = class DragEvent extends PointerEvent {
246
+ static setList(data) {
247
+ this.list = data instanceof LeafList ? data : new LeafList(data);
248
+ }
249
+ static setData(data) {
250
+ this.data = data;
251
+ }
252
+ getPageMove(total) {
253
+ this.assignMove(total);
254
+ return this.current.getPagePoint(move, null, true);
255
+ }
256
+ getInnerMove(relative, total) {
257
+ if (!relative)
258
+ relative = this.current;
259
+ this.assignMove(total);
260
+ return relative.getInnerPoint(move, null, true);
261
+ }
262
+ getLocalMove(relative, total) {
263
+ if (!relative)
264
+ relative = this.current;
265
+ this.assignMove(total);
266
+ return relative.getLocalPoint(move, null, true);
267
+ }
268
+ getPageTotal() {
269
+ return this.getPageMove(true);
270
+ }
271
+ getInnerTotal(relative) {
272
+ return this.getInnerMove(relative, true);
273
+ }
274
+ getLocalTotal(relative) {
275
+ return this.getLocalMove(relative, true);
276
+ }
277
+ assignMove(total) {
278
+ move.x = total ? this.totalX : this.moveX;
279
+ move.y = total ? this.totalY : this.moveY;
280
+ }
281
+ };
282
+ DragEvent.BEFORE_DRAG = 'drag.before_drag';
283
+ DragEvent.START = 'drag.start';
284
+ DragEvent.DRAG = 'drag';
285
+ DragEvent.END = 'drag.end';
286
+ DragEvent.OVER = 'drag.over';
287
+ DragEvent.OUT = 'drag.out';
288
+ DragEvent.ENTER = 'drag.enter';
289
+ DragEvent.LEAVE = 'drag.leave';
290
+ DragEvent = __decorate([
291
+ registerUIEvent()
292
+ ], DragEvent);
293
+
294
+ let DropEvent = class DropEvent extends PointerEvent {
295
+ static setList(data) {
296
+ DragEvent.setList(data);
297
+ }
298
+ static setData(data) {
299
+ DragEvent.setData(data);
300
+ }
301
+ };
302
+ DropEvent.DROP = 'drop';
303
+ DropEvent = __decorate([
304
+ registerUIEvent()
305
+ ], DropEvent);
306
+
307
+ let MoveEvent = class MoveEvent extends DragEvent {
308
+ };
309
+ MoveEvent.BEFORE_MOVE = 'move.before_move';
310
+ MoveEvent.START = 'move.start';
311
+ MoveEvent.MOVE = 'move';
312
+ MoveEvent.END = 'move.end';
313
+ MoveEvent = __decorate([
314
+ registerUIEvent()
315
+ ], MoveEvent);
316
+
317
+ let RotateEvent = class RotateEvent extends UIEvent {
318
+ };
319
+ RotateEvent.BEFORE_ROTATE = 'rotate.before_rotate';
320
+ RotateEvent.START = 'rotate.start';
321
+ RotateEvent.ROTATE = 'rotate';
322
+ RotateEvent.END = 'rotate.end';
323
+ RotateEvent = __decorate([
324
+ registerUIEvent()
325
+ ], RotateEvent);
326
+
327
+ let SwipeEvent = class SwipeEvent extends DragEvent {
328
+ };
329
+ SwipeEvent.SWIPE = 'swipe';
330
+ SwipeEvent.LEFT = 'swipe.left';
331
+ SwipeEvent.RIGHT = 'swipe.right';
332
+ SwipeEvent.UP = 'swipe.up';
333
+ SwipeEvent.DOWN = 'swipe.down';
334
+ SwipeEvent = __decorate([
335
+ registerUIEvent()
336
+ ], SwipeEvent);
337
+
338
+ let ZoomEvent = class ZoomEvent extends UIEvent {
339
+ };
340
+ ZoomEvent.BEFORE_ZOOM = 'zoom.before_zoom';
341
+ ZoomEvent.START = 'zoom.start';
342
+ ZoomEvent.ZOOM = 'zoom';
343
+ ZoomEvent.END = 'zoom.end';
344
+ ZoomEvent = __decorate([
345
+ registerUIEvent()
346
+ ], ZoomEvent);
347
+
348
+ let KeyEvent = class KeyEvent extends UIEvent {
349
+ };
350
+ KeyEvent.DOWN = 'key.down';
351
+ KeyEvent.HOLD = 'key.hold';
352
+ KeyEvent.UP = 'key.up';
353
+ KeyEvent = __decorate([
354
+ registerUIEvent()
355
+ ], KeyEvent);
356
+
357
+ function design(leafer) {
358
+ if (leafer.isApp)
359
+ return;
360
+ leafer.__eventIds.push(leafer.on_(MoveEvent.BEFORE_MOVE, (e) => leafer.zoomLayer.move(e.moveX, e.moveY)), leafer.on_(ZoomEvent.BEFORE_ZOOM, (e) => {
361
+ const { zoomLayer } = leafer;
362
+ const { scaleX } = zoomLayer.__, { min, max } = leafer.app.config.zoom;
363
+ let { scale } = e, absScale = Math.abs(scaleX * scale);
364
+ if (absScale < min)
365
+ scale = min / scaleX;
366
+ else if (absScale > max)
367
+ scale = max / scaleX;
368
+ if (scale !== 1) {
369
+ PointHelper.scaleOf(zoomLayer, e, scale);
370
+ zoomLayer.scale = scaleX * scale;
371
+ }
372
+ }));
373
+ }
374
+
375
+ const debug$1 = Debug.get('LeaferTypeCreator');
376
+ const LeaferTypeCreator = {
377
+ list: {},
378
+ register(name, fn) {
379
+ if (list[name]) {
380
+ debug$1.repeat(name);
381
+ }
382
+ else {
383
+ list[name] = fn;
384
+ }
385
+ },
386
+ run(name, leafer) {
387
+ const fn = list[name];
388
+ if (fn) {
389
+ fn(leafer);
390
+ }
391
+ else {
392
+ debug$1.error('no', name);
393
+ }
394
+ }
395
+ };
396
+ const { list, register } = LeaferTypeCreator;
397
+ register('draw', draw);
398
+ register('design', design);
399
+
400
+ Leafer.prototype.initType = function (type) {
401
+ LeaferTypeCreator.run(type, this);
402
+ };
403
+
404
+ class Transformer {
405
+ constructor(interaction) {
406
+ this.interaction = interaction;
407
+ }
408
+ move(data) {
409
+ const { interaction } = this;
410
+ if (!this.moveData) {
411
+ const { path } = interaction.selector.getByPoint(data, interaction.hitRadius);
412
+ data.path = path;
413
+ this.moveData = Object.assign(Object.assign({}, data), { moveX: 0, moveY: 0 });
414
+ interaction.emit(MoveEvent.START, this.moveData);
415
+ }
416
+ data.path = this.moveData.path;
417
+ interaction.emit(MoveEvent.BEFORE_MOVE, data);
418
+ interaction.emit(MoveEvent.MOVE, data);
419
+ this.transformEndWait();
420
+ }
421
+ zoom(data) {
422
+ const { interaction } = this;
423
+ if (!this.zoomData) {
424
+ const { path } = interaction.selector.getByPoint(data, interaction.hitRadius);
425
+ data.path = path;
426
+ this.zoomData = Object.assign(Object.assign({}, data), { scale: 1 });
427
+ interaction.emit(ZoomEvent.START, this.zoomData);
428
+ }
429
+ data.path = this.zoomData.path;
430
+ interaction.emit(ZoomEvent.BEFORE_ZOOM, data);
431
+ interaction.emit(ZoomEvent.ZOOM, data);
432
+ this.transformEndWait();
433
+ }
434
+ rotate(data) {
435
+ const { interaction } = this;
436
+ if (!this.rotateData) {
437
+ const { path } = interaction.selector.getByPoint(data, interaction.hitRadius);
438
+ data.path = path;
439
+ this.rotateData = Object.assign(Object.assign({}, data), { rotation: 0 });
440
+ interaction.emit(RotateEvent.START, this.rotateData);
441
+ }
442
+ data.path = this.rotateData.path;
443
+ interaction.emit(RotateEvent.BEFORE_ROTATE, data);
444
+ interaction.emit(RotateEvent.ROTATE, data);
445
+ this.transformEndWait();
446
+ }
447
+ transformEndWait() {
448
+ clearTimeout(this.transformTimer);
449
+ this.transformTimer = setTimeout(() => {
450
+ this.transformEnd();
451
+ }, this.interaction.config.pointer.transformTime);
452
+ }
453
+ transformEnd() {
454
+ this.moveEnd();
455
+ this.zoomEnd();
456
+ this.rotateEnd();
457
+ }
458
+ moveEnd() {
459
+ if (this.moveData) {
460
+ this.interaction.emit(MoveEvent.END, this.moveData);
461
+ this.moveData = null;
462
+ }
463
+ }
464
+ zoomEnd() {
465
+ if (this.zoomData) {
466
+ this.interaction.emit(ZoomEvent.END, this.zoomData);
467
+ this.zoomData = null;
468
+ }
469
+ }
470
+ rotateEnd() {
471
+ if (this.rotateData) {
472
+ this.interaction.emit(RotateEvent.END, this.rotateData);
473
+ this.rotateData = null;
474
+ }
475
+ }
476
+ destroy() {
477
+ this.zoomData = this.moveData = this.rotateData = null;
478
+ }
479
+ }
480
+
481
+ const InteractionHelper = {
482
+ getMoveEventData(center, move, event) {
483
+ return Object.assign(Object.assign({}, event), { x: center.x, y: center.y, moveX: move.x, moveY: move.y });
484
+ },
485
+ getRotateEventData(center, angle, event) {
486
+ return Object.assign(Object.assign({}, event), { x: center.x, y: center.y, rotation: angle });
487
+ },
488
+ getZoomEventData(center, scale, event) {
489
+ return Object.assign(Object.assign({}, event), { x: center.x, y: center.y, scale });
490
+ },
491
+ getDragEventData(startPoint, lastPoint, event) {
492
+ return Object.assign(Object.assign({}, event), { x: event.x, y: event.y, moveX: event.x - lastPoint.x, moveY: event.y - lastPoint.y, totalX: event.x - startPoint.x, totalY: event.y - startPoint.y });
493
+ },
494
+ getDropEventData(event, list, data) {
495
+ return Object.assign(Object.assign({}, event), { list,
496
+ data });
497
+ },
498
+ getSwipeDirection(angle) {
499
+ if (angle < -45 && angle > -135) {
500
+ return SwipeEvent.UP;
501
+ }
502
+ else if (angle > 45 && angle < 135) {
503
+ return SwipeEvent.DOWN;
504
+ }
505
+ else if (angle <= 45 && angle >= -45) {
506
+ return SwipeEvent.RIGHT;
507
+ }
508
+ else {
509
+ return SwipeEvent.LEFT;
510
+ }
511
+ },
512
+ getSwipeEventData(startPoint, lastDragData, event) {
513
+ return Object.assign(Object.assign({}, event), { moveX: lastDragData.moveX, moveY: lastDragData.moveY, totalX: event.x - startPoint.x, totalY: event.y - startPoint.y, type: I.getSwipeDirection(PointHelper.getAngle(startPoint, event)) });
514
+ },
515
+ getBase(e) {
516
+ const pointerUpButtons = e.button === 1 ? 4 : e.button;
517
+ return {
518
+ altKey: e.altKey,
519
+ ctrlKey: e.ctrlKey,
520
+ shiftKey: e.shiftKey,
521
+ metaKey: e.metaKey,
522
+ buttons: e.buttons === undefined ? 1 : (e.buttons === 0 ? pointerUpButtons : e.buttons),
523
+ origin: e
524
+ };
525
+ },
526
+ pathHasEventType(path, type) {
527
+ const { list } = path;
528
+ for (let i = 0, len = list.length; i < len; i++) {
529
+ if (list[i].hasEvent(type))
530
+ return true;
531
+ }
532
+ return false;
533
+ },
534
+ filterPathByEventType(path, type) {
535
+ const find = new LeafList();
536
+ const { list } = path;
537
+ for (let i = 0, len = list.length; i < len; i++) {
538
+ if (list[i].hasEvent(type))
539
+ find.add(list[i]);
540
+ }
541
+ return find;
542
+ }
543
+ };
544
+ const I = InteractionHelper;
545
+
546
+ const emptyList = new LeafList();
547
+ const { getDragEventData, getDropEventData, getSwipeEventData } = InteractionHelper;
548
+ class Dragger {
549
+ constructor(interaction) {
550
+ this.interaction = interaction;
551
+ }
552
+ setDragData(data) {
553
+ if (this.animateWait)
554
+ this.dragEndReal();
555
+ this.downData = this.interaction.downData;
556
+ this.dragData = getDragEventData(data, data, data);
557
+ }
558
+ getList() {
559
+ const { proxy } = this.interaction.selector;
560
+ return this.dragging && (!proxy || !proxy.list.length) ? (DragEvent.list || this.dragableList || emptyList) : emptyList;
561
+ }
562
+ checkDrag(data, canDrag) {
563
+ const { interaction } = this;
564
+ if (this.moving && data.buttons < 1) {
565
+ this.canAnimate = false;
566
+ interaction.pointerCancel();
567
+ return;
568
+ }
569
+ else {
570
+ this.canAnimate = true;
571
+ }
572
+ if (!this.moving && canDrag) {
573
+ if (this.moving = interaction.moveMode || interaction.isHoldRightKey)
574
+ interaction.emit(MoveEvent.START, this.dragData);
575
+ }
576
+ if (!this.moving) {
577
+ this.dragStart(data, canDrag);
578
+ }
579
+ this.drag(data);
580
+ }
581
+ dragStart(data, canDrag) {
582
+ if (!this.dragging) {
583
+ this.dragging = canDrag && PointerButton.left(data);
584
+ if (this.dragging) {
585
+ this.interaction.emit(DragEvent.START, this.dragData);
586
+ this.getDragableList(this.dragData.path);
587
+ }
588
+ }
589
+ }
590
+ getDragableList(path) {
591
+ let leaf;
592
+ for (let i = 0, len = path.length; i < len; i++) {
593
+ leaf = path.list[i];
594
+ if ((leaf.__.draggable || leaf.__.editable) && leaf.__.hitSelf && !leaf.__.locked) {
595
+ this.dragableList = new LeafList(leaf);
596
+ break;
597
+ }
598
+ }
599
+ }
600
+ drag(data) {
601
+ const { interaction, dragData, downData } = this;
602
+ const { path, throughPath } = downData;
603
+ this.dragData = getDragEventData(downData, dragData, data);
604
+ if (throughPath)
605
+ this.dragData.throughPath = throughPath;
606
+ this.dragData.path = path;
607
+ if (this.moving) {
608
+ interaction.emit(MoveEvent.BEFORE_MOVE, this.dragData);
609
+ interaction.emit(MoveEvent.MOVE, this.dragData);
610
+ }
611
+ else if (this.dragging) {
612
+ this.dragReal();
613
+ interaction.emit(DragEvent.BEFORE_DRAG, this.dragData);
614
+ interaction.emit(DragEvent.DRAG, this.dragData);
615
+ }
616
+ }
617
+ dragReal() {
618
+ const { running } = this.interaction;
619
+ const list = this.getList();
620
+ if (list.length && running) {
621
+ const { moveX, moveY } = this.dragData;
622
+ list.forEach(leaf => LeafHelper.moveWorld(leaf, moveX, moveY));
623
+ }
624
+ }
625
+ dragOverOrOut(data) {
626
+ const { interaction } = this;
627
+ const { dragOverPath } = this;
628
+ const { path } = data;
629
+ if (dragOverPath) {
630
+ if (path.indexAt(0) !== dragOverPath.indexAt(0)) {
631
+ interaction.emit(DragEvent.OUT, data, dragOverPath);
632
+ interaction.emit(DragEvent.OVER, data, path);
633
+ }
634
+ }
635
+ else {
636
+ interaction.emit(DragEvent.OVER, data, path);
637
+ }
638
+ this.dragOverPath = path;
639
+ }
640
+ dragEnterOrLeave(data) {
641
+ const { interaction } = this;
642
+ const { dragEnterPath } = this;
643
+ const { path } = data;
644
+ interaction.emit(DragEvent.LEAVE, data, dragEnterPath, path);
645
+ interaction.emit(DragEvent.ENTER, data, path, dragEnterPath);
646
+ this.dragEnterPath = path;
647
+ }
648
+ dragEnd(data, speed) {
649
+ if (!this.dragData)
650
+ return;
651
+ const { moveX, moveY } = this.dragData;
652
+ if (this.canAnimate && this.moving && (Math.abs(moveX) > 1 || Math.abs(moveY) > 1)) {
653
+ data = Object.assign({}, data);
654
+ speed = (speed || (data.pointerType === 'touch' ? 2 : 1)) * 0.9;
655
+ PointHelper.move(data, moveX * speed, moveY * speed);
656
+ this.drag(data);
657
+ this.animate(() => { this.dragEnd(data, 1); });
658
+ }
659
+ else {
660
+ this.dragEndReal(data);
661
+ }
662
+ }
663
+ dragEndReal(data) {
664
+ const { interaction, downData, dragData } = this;
665
+ if (!data)
666
+ data = dragData;
667
+ const { path, throughPath } = downData;
668
+ const endDragData = getDragEventData(downData, data, data);
669
+ if (throughPath)
670
+ endDragData.throughPath = throughPath;
671
+ endDragData.path = path;
672
+ if (this.moving)
673
+ interaction.emit(MoveEvent.END, endDragData);
674
+ if (this.dragging) {
675
+ interaction.emit(DragEvent.END, endDragData);
676
+ this.swipe(data, endDragData);
677
+ this.drop(data);
678
+ }
679
+ this.autoMoveCancel();
680
+ this.dragReset();
681
+ this.animate(null, 'off');
682
+ }
683
+ animate(func, off) {
684
+ const animateWait = func || this.animateWait;
685
+ if (animateWait)
686
+ this.interaction.target.nextRender(animateWait, off);
687
+ this.animateWait = func;
688
+ }
689
+ swipe(data, endDragData) {
690
+ const { interaction } = this;
691
+ const { downData } = interaction;
692
+ if (PointHelper.getDistance(downData, data) > interaction.config.pointer.swipeDistance) {
693
+ const swipeData = getSwipeEventData(downData, this.dragData, endDragData);
694
+ this.interaction.emit(swipeData.type, swipeData);
695
+ }
696
+ }
697
+ drop(data) {
698
+ const dropData = getDropEventData(data, this.getList(), DragEvent.data);
699
+ dropData.path = this.dragEnterPath;
700
+ this.interaction.emit(DropEvent.DROP, dropData);
701
+ this.interaction.emit(DragEvent.LEAVE, data, this.dragEnterPath);
702
+ }
703
+ dragReset() {
704
+ DragEvent.list = DragEvent.data = this.dragableList = this.dragData = this.downData = this.dragOverPath = this.dragEnterPath = null;
705
+ this.dragging = this.moving = false;
706
+ }
707
+ checkDragOut(data) {
708
+ const { interaction } = this;
709
+ this.autoMoveCancel();
710
+ if (this.dragging && !interaction.shrinkCanvasBounds.hitPoint(data))
711
+ this.autoMoveOnDragOut(data);
712
+ }
713
+ autoMoveOnDragOut(data) {
714
+ const { interaction, downData } = this;
715
+ const { autoDistance, dragOut } = interaction.config.move;
716
+ if (!dragOut || !autoDistance)
717
+ return;
718
+ const bounds = interaction.shrinkCanvasBounds;
719
+ const { x, y } = bounds;
720
+ const right = BoundsHelper.maxX(bounds);
721
+ const bottom = BoundsHelper.maxY(bounds);
722
+ const moveX = data.x < x ? autoDistance : (right < data.x ? -autoDistance : 0);
723
+ const moveY = data.y < y ? autoDistance : (bottom < data.y ? -autoDistance : 0);
724
+ let totalX = 0, totalY = 0;
725
+ this.autoMoveTimer = setInterval(() => {
726
+ totalX += moveX;
727
+ totalY += moveY;
728
+ PointHelper.move(downData, moveX, moveY);
729
+ PointHelper.move(this.dragData, moveX, moveY);
730
+ interaction.move(Object.assign(Object.assign({}, data), { moveX, moveY, totalX, totalY }));
731
+ interaction.pointerMoveReal(data);
732
+ }, 10);
733
+ }
734
+ autoMoveCancel() {
735
+ if (this.autoMoveTimer) {
736
+ clearInterval(this.autoMoveTimer);
737
+ this.autoMoveTimer = 0;
738
+ }
739
+ }
740
+ destroy() {
741
+ this.dragReset();
742
+ }
743
+ }
744
+
745
+ const debug = Debug.get('emit');
746
+ function emit(type, data, path, excludePath) {
747
+ if (!path && !data.path)
748
+ return;
749
+ let leaf;
750
+ data.type = type;
751
+ if (path) {
752
+ data = Object.assign(Object.assign({}, data), { path });
753
+ }
754
+ else {
755
+ path = data.path;
756
+ }
757
+ data.target = path.indexAt(0);
758
+ try {
759
+ for (let i = path.length - 1; i > -1; i--) {
760
+ leaf = path.list[i];
761
+ if (emitEvent(leaf, type, data, true, excludePath))
762
+ return;
763
+ if (leaf.isApp)
764
+ emitAppChildren(leaf, type, data, true, excludePath);
765
+ }
766
+ for (let i = 0, len = path.length; i < len; i++) {
767
+ leaf = path.list[i];
768
+ if (leaf.isApp)
769
+ emitAppChildren(leaf, type, data, false, excludePath);
770
+ if (emitEvent(leaf, type, data, false, excludePath))
771
+ return;
772
+ }
773
+ }
774
+ catch (e) {
775
+ debug.error(e);
776
+ }
777
+ }
778
+ const allowTypes = ['move', 'zoom', 'rotate', 'key'];
779
+ function emitAppChildren(leaf, type, data, capture, excludePath) {
780
+ if (allowTypes.some(name => type.startsWith(name)) && leaf.__.hitChildren && !exclude(leaf, excludePath)) {
781
+ let child;
782
+ for (let i = 0, len = leaf.children.length; i < len; i++) {
783
+ child = leaf.children[i];
784
+ if (!data.path.has(child) && child.__.hittable)
785
+ emitEvent(child, type, data, capture, excludePath);
786
+ }
787
+ }
788
+ }
789
+ function emitEvent(leaf, type, data, capture, excludePath) {
790
+ if (leaf.destroyed)
791
+ return true;
792
+ if (leaf.__.hitSelf && leaf.hasEvent(type, capture) && !exclude(leaf, excludePath)) {
793
+ data.phase = capture ? 1 : ((leaf === data.target) ? 2 : 3);
794
+ const event = EventCreator.get(type, data);
795
+ leaf.emitEvent(event, capture);
796
+ if (event.isStop)
797
+ return true;
798
+ }
799
+ return false;
800
+ }
801
+ function exclude(leaf, excludePath) {
802
+ return excludePath && excludePath.has(leaf);
803
+ }
804
+
805
+ const MultiTouchHelper = {
806
+ getData(list) {
807
+ const a = list[0];
808
+ const b = list[1];
809
+ const lastCenter = PointHelper.getCenter(a.from, b.from);
810
+ const center = PointHelper.getCenter(a.to, b.to);
811
+ const move = { x: center.x - lastCenter.x, y: center.y - lastCenter.y };
812
+ const lastDistance = PointHelper.getDistance(a.from, b.from);
813
+ const distance = PointHelper.getDistance(a.to, b.to);
814
+ const scale = distance / lastDistance;
815
+ const angle = PointHelper.getRotation(a.from, b.from, a.to, b.to);
816
+ return { move, scale, angle, center };
817
+ }
818
+ };
819
+
820
+ const config = {
821
+ wheel: {
822
+ zoomMode: false,
823
+ zoomSpeed: 0.5,
824
+ moveSpeed: 0.5,
825
+ rotateSpeed: 0.5,
826
+ delta: { x: 80 / 4, y: 8.0 },
827
+ preventDefault: true
828
+ },
829
+ pointer: {
830
+ hitRadius: 5,
831
+ through: false,
832
+ tapTime: 120,
833
+ longPressTime: 800,
834
+ transformTime: 500,
835
+ dragHover: true,
836
+ dragDistance: 2,
837
+ swipeDistance: 20,
838
+ ignoreMove: false,
839
+ preventDefaultMenu: true
840
+ },
841
+ cursor: {}
842
+ };
843
+
844
+ const { pathHasEventType, getMoveEventData, getZoomEventData, getRotateEventData } = InteractionHelper;
845
+ class InteractionBase {
846
+ get dragging() { return this.dragger.dragging; }
847
+ get isDragEmpty() { return this.config.move.dragEmpty && (this.hoverData && this.hoverData.path.list[0].isLeafer) && (!this.downData || this.downData.path.list[0].isLeafer); }
848
+ get isHoldRightKey() { return this.config.move.holdRightKey && this.downData && PointerButton.right(this.downData); }
849
+ get moveMode() { return this.config.move.drag || (this.config.move.holdSpaceKey && Keyboard.isHoldSpaceKey()) || (this.downData && ((this.config.move.holdMiddleKey && PointerButton.middle(this.downData)) || (this.isHoldRightKey && this.dragger.moving))) || this.isDragEmpty; }
850
+ get hitRadius() { return this.config.pointer.hitRadius; }
851
+ constructor(target, canvas, selector, userConfig) {
852
+ this.config = config;
853
+ this.tapCount = 0;
854
+ this.downKeyMap = {};
855
+ this.target = target;
856
+ this.canvas = canvas;
857
+ this.selector = selector;
858
+ this.defaultPath = new LeafList(target);
859
+ this.transformer = new Transformer(this);
860
+ this.dragger = new Dragger(this);
861
+ if (userConfig)
862
+ this.config = DataHelper.default(userConfig, this.config);
863
+ this.__listenEvents();
864
+ }
865
+ start() {
866
+ this.running = true;
867
+ }
868
+ stop() {
869
+ this.running = false;
870
+ }
871
+ receive(_event) { }
872
+ pointerDown(data, useDefaultPath) {
873
+ if (!data)
874
+ data = this.hoverData;
875
+ if (!data)
876
+ return;
877
+ PointerButton.defaultLeft(data);
878
+ this.updateDownData(data);
879
+ if (useDefaultPath)
880
+ data.path = this.defaultPath;
881
+ this.emit(PointerEvent.BEFORE_DOWN, data);
882
+ this.emit(PointerEvent.DOWN, data);
883
+ this.downTime = Date.now();
884
+ this.dragger.setDragData(data);
885
+ if (PointerButton.left(data)) {
886
+ this.tapWait();
887
+ this.longPressWait(data);
888
+ }
889
+ else if (PointerButton.right(data)) {
890
+ this.waitMenuTap = true;
891
+ }
892
+ this.updateCursor(data);
893
+ }
894
+ pointerMove(data) {
895
+ if (!data)
896
+ data = this.hoverData;
897
+ if (!data)
898
+ return;
899
+ if (this.downData)
900
+ PointerButton.defaultLeft(data);
901
+ const hit = this.canvas.bounds.hitPoint(data);
902
+ if (hit || this.downData) {
903
+ if (hit && !this.downData && PointerButton.left(data))
904
+ this.pointerDown(data, true);
905
+ this.pointerMoveReal(data);
906
+ this.dragger.checkDragOut(data);
907
+ }
908
+ }
909
+ pointerMoveReal(data) {
910
+ this.emit(PointerEvent.BEFORE_MOVE, data, this.defaultPath);
911
+ if (this.downData) {
912
+ const canDrag = PointHelper.getDistance(this.downData, data) > this.config.pointer.dragDistance;
913
+ if (canDrag) {
914
+ if (this.waitTap)
915
+ this.pointerWaitCancel();
916
+ this.waitMenuTap = false;
917
+ }
918
+ this.dragger.checkDrag(data, canDrag);
919
+ }
920
+ if (!this.dragger.moving) {
921
+ this.updateHoverData(data);
922
+ this.emit(PointerEvent.MOVE, data);
923
+ this.pointerOverOrOut(data);
924
+ this.pointerEnterOrLeave(data);
925
+ if (this.dragger.dragging) {
926
+ this.dragger.dragOverOrOut(data);
927
+ this.dragger.dragEnterOrLeave(data);
928
+ }
929
+ }
930
+ this.updateCursor(this.downData || data);
931
+ }
932
+ pointerUp(data) {
933
+ if (!data)
934
+ data = this.downData;
935
+ if (!this.downData)
936
+ return;
937
+ PointerButton.defaultLeft(data);
938
+ this.findPath(data);
939
+ this.emit(PointerEvent.BEFORE_UP, data);
940
+ this.emit(PointerEvent.UP, data);
941
+ if (this.oldDownData)
942
+ this.emit(PointerEvent.UP, this.oldDownData, undefined, data.path);
943
+ this.emit(PointerEvent.UP, this.downData, undefined, data.path);
944
+ this.touchLeave(data);
945
+ this.tap(data);
946
+ this.menuTap(data);
947
+ this.dragger.dragEnd(data);
948
+ this.downData = this.oldDownData = null;
949
+ this.updateCursor(data);
950
+ }
951
+ pointerCancel() {
952
+ this.pointerUp(this.dragger.dragData);
953
+ }
954
+ multiTouch(data, list) {
955
+ const { move, angle, scale, center } = MultiTouchHelper.getData(list);
956
+ this.rotate(getRotateEventData(center, angle, data));
957
+ this.zoom(getZoomEventData(center, scale, data));
958
+ this.move(getMoveEventData(center, move, data));
959
+ }
960
+ menu(data) {
961
+ this.findPath(data);
962
+ this.emit(PointerEvent.MENU, data);
963
+ }
964
+ menuTap(data) {
965
+ if (this.waitMenuTap)
966
+ this.emit(PointerEvent.MENU_TAP, data);
967
+ }
968
+ move(data) {
969
+ this.transformer.move(data);
970
+ }
971
+ zoom(data) {
972
+ this.transformer.zoom(data);
973
+ }
974
+ rotate(data) {
975
+ this.transformer.rotate(data);
976
+ }
977
+ transformEnd() {
978
+ this.transformer.transformEnd();
979
+ }
980
+ keyDown(data) {
981
+ const { code } = data;
982
+ if (!this.downKeyMap[code]) {
983
+ this.downKeyMap[code] = true;
984
+ Keyboard.setDownCode(code);
985
+ this.emit(KeyEvent.HOLD, data, this.defaultPath);
986
+ if (this.moveMode)
987
+ this.updateCursor();
988
+ }
989
+ this.emit(KeyEvent.DOWN, data, this.defaultPath);
990
+ }
991
+ keyUp(data) {
992
+ const { code } = data;
993
+ this.downKeyMap[code] = false;
994
+ Keyboard.setUpCode(code);
995
+ this.emit(KeyEvent.UP, data, this.defaultPath);
996
+ if (this.cursor === 'grab')
997
+ this.updateCursor();
998
+ }
999
+ pointerOverOrOut(data) {
1000
+ if (this.dragger.moving)
1001
+ return;
1002
+ if (this.dragging && !this.config.pointer.dragHover)
1003
+ return;
1004
+ const { path } = data;
1005
+ if (this.overPath) {
1006
+ if (path.indexAt(0) !== this.overPath.indexAt(0)) {
1007
+ this.emit(PointerEvent.OUT, data, this.overPath);
1008
+ this.emit(PointerEvent.OVER, data, path);
1009
+ }
1010
+ }
1011
+ else {
1012
+ this.emit(PointerEvent.OVER, data, path);
1013
+ }
1014
+ this.overPath = path;
1015
+ }
1016
+ pointerEnterOrLeave(data) {
1017
+ if (this.dragger.moving)
1018
+ return;
1019
+ if (this.dragging && !this.config.pointer.dragHover)
1020
+ return;
1021
+ const { path } = data;
1022
+ this.emit(PointerEvent.LEAVE, data, this.enterPath, path);
1023
+ this.emit(PointerEvent.ENTER, data, path, this.enterPath);
1024
+ this.enterPath = path;
1025
+ }
1026
+ touchLeave(data) {
1027
+ if (data.pointerType === 'touch') {
1028
+ if (this.enterPath) {
1029
+ this.emit(PointerEvent.LEAVE, data);
1030
+ if (this.dragger.dragging)
1031
+ this.emit(DropEvent.LEAVE, data);
1032
+ }
1033
+ }
1034
+ }
1035
+ tap(data) {
1036
+ const { pointer } = this.config;
1037
+ const longTap = this.longTap(data);
1038
+ if (!pointer.tapMore && longTap)
1039
+ return;
1040
+ if (!this.waitTap)
1041
+ return;
1042
+ if (pointer.tapMore)
1043
+ this.emitTap(data);
1044
+ const useTime = Date.now() - this.downTime;
1045
+ const hasDouble = [PointerEvent.DOUBLE_TAP, PointerEvent.DOUBLE_CLICK].some(type => pathHasEventType(data.path, type));
1046
+ if (useTime < pointer.tapTime + 50 && hasDouble) {
1047
+ this.tapCount++;
1048
+ if (this.tapCount === 2) {
1049
+ this.tapWaitCancel();
1050
+ this.emitDoubleTap(data);
1051
+ }
1052
+ else {
1053
+ clearTimeout(this.tapTimer);
1054
+ this.tapTimer = setTimeout(() => {
1055
+ if (!pointer.tapMore) {
1056
+ this.tapWaitCancel();
1057
+ this.emitTap(data);
1058
+ }
1059
+ }, pointer.tapTime);
1060
+ }
1061
+ }
1062
+ else {
1063
+ if (!pointer.tapMore) {
1064
+ this.tapWaitCancel();
1065
+ this.emitTap(data);
1066
+ }
1067
+ }
1068
+ }
1069
+ findPath(data, options) {
1070
+ const { hitRadius, through } = this.config.pointer;
1071
+ const find = this.selector.getByPoint(data, hitRadius, options || { through });
1072
+ if (find.throughPath)
1073
+ data.throughPath = find.throughPath;
1074
+ data.path = find.path;
1075
+ return find.path;
1076
+ }
1077
+ isDrag(leaf) {
1078
+ return this.dragger.getList().has(leaf);
1079
+ }
1080
+ updateDownData(data, options) {
1081
+ const { downData } = this;
1082
+ if (!data && downData)
1083
+ data = Object.assign({}, downData);
1084
+ if (!data)
1085
+ return;
1086
+ this.oldDownData = downData;
1087
+ this.findPath(data, options);
1088
+ this.downData = data;
1089
+ }
1090
+ updateHoverData(data) {
1091
+ if (!data)
1092
+ data = this.hoverData;
1093
+ if (!data)
1094
+ return;
1095
+ this.findPath(data, { exclude: this.dragger.getList(), name: PointerEvent.MOVE });
1096
+ this.hoverData = data;
1097
+ }
1098
+ updateCursor(data) {
1099
+ if (this.config.cursor.stop)
1100
+ return;
1101
+ if (!data) {
1102
+ this.updateHoverData();
1103
+ data = this.downData || this.hoverData;
1104
+ }
1105
+ if (this.dragger.moving) {
1106
+ return this.setCursor('grabbing');
1107
+ }
1108
+ else if (this.moveMode) {
1109
+ return this.setCursor(this.downData ? 'grabbing' : 'grab');
1110
+ }
1111
+ else if (!data)
1112
+ return;
1113
+ let leaf;
1114
+ let cursor;
1115
+ const { path } = data;
1116
+ for (let i = 0, len = path.length; i < len; i++) {
1117
+ leaf = path.list[i];
1118
+ cursor = leaf.cursor;
1119
+ if (cursor)
1120
+ break;
1121
+ }
1122
+ this.setCursor(cursor);
1123
+ }
1124
+ setCursor(cursor) {
1125
+ this.cursor = cursor;
1126
+ }
1127
+ emitTap(data) {
1128
+ this.emit(PointerEvent.TAP, data);
1129
+ this.emit(PointerEvent.CLICK, data);
1130
+ }
1131
+ emitDoubleTap(data) {
1132
+ this.emit(PointerEvent.DOUBLE_TAP, data);
1133
+ this.emit(PointerEvent.DOUBLE_CLICK, data);
1134
+ }
1135
+ pointerWaitCancel() {
1136
+ this.tapWaitCancel();
1137
+ this.longPressWaitCancel();
1138
+ }
1139
+ tapWait() {
1140
+ clearTimeout(this.tapTimer);
1141
+ this.waitTap = true;
1142
+ }
1143
+ tapWaitCancel() {
1144
+ clearTimeout(this.tapTimer);
1145
+ this.waitTap = false;
1146
+ this.tapCount = 0;
1147
+ }
1148
+ longPressWait(data) {
1149
+ clearTimeout(this.longPressTimer);
1150
+ this.longPressTimer = setTimeout(() => {
1151
+ this.longPressed = true;
1152
+ this.emit(PointerEvent.LONG_PRESS, data);
1153
+ }, this.config.pointer.longPressTime);
1154
+ }
1155
+ longTap(data) {
1156
+ let longTap;
1157
+ if (this.longPressed) {
1158
+ this.emit(PointerEvent.LONG_TAP, data);
1159
+ if (pathHasEventType(data.path, PointerEvent.LONG_TAP))
1160
+ longTap = true;
1161
+ }
1162
+ this.longPressWaitCancel();
1163
+ return longTap;
1164
+ }
1165
+ longPressWaitCancel() {
1166
+ clearTimeout(this.longPressTimer);
1167
+ this.longPressed = false;
1168
+ }
1169
+ __onResize() {
1170
+ this.shrinkCanvasBounds = new Bounds(this.canvas.bounds);
1171
+ this.shrinkCanvasBounds.spread(-2);
1172
+ }
1173
+ __listenEvents() {
1174
+ const { target } = this;
1175
+ this.__eventIds = [target.on_(ResizeEvent.RESIZE, this.__onResize, this)];
1176
+ target.once(LeaferEvent.READY, () => this.__onResize());
1177
+ }
1178
+ __removeListenEvents() {
1179
+ this.target.off_(this.__eventIds);
1180
+ this.__eventIds.length = 0;
1181
+ }
1182
+ emit(type, data, path, excludePath) {
1183
+ if (this.running)
1184
+ emit(type, data, path, excludePath);
1185
+ }
1186
+ destroy() {
1187
+ if (this.__eventIds.length) {
1188
+ this.stop();
1189
+ this.__removeListenEvents();
1190
+ this.dragger.destroy();
1191
+ this.transformer.destroy();
1192
+ this.downData = this.overPath = this.enterPath = null;
1193
+ }
1194
+ }
1195
+ }
1196
+
1197
+ class Cursor {
1198
+ static set(name, value) {
1199
+ this.custom[name] = value;
1200
+ }
1201
+ static get(name) {
1202
+ return this.custom[name];
1203
+ }
1204
+ }
1205
+ Cursor.custom = {};
1206
+
1207
+ class HitCanvasManager extends CanvasManager {
1208
+ constructor() {
1209
+ super(...arguments);
1210
+ this.pathTypeList = new LeafList();
1211
+ this.imageTypeList = new LeafList();
1212
+ }
1213
+ getImageType(leaf, size) {
1214
+ this.imageTypeList.add(leaf);
1215
+ return Creator.hitCanvas(size);
1216
+ }
1217
+ getPathType(leaf) {
1218
+ this.pathTypeList.add(leaf);
1219
+ return Creator.hitCanvas();
1220
+ }
1221
+ clearImageType() {
1222
+ this.__clearLeafList(this.imageTypeList);
1223
+ }
1224
+ clearPathType() {
1225
+ this.__clearLeafList(this.pathTypeList);
1226
+ }
1227
+ __clearLeafList(leafList) {
1228
+ if (leafList.length) {
1229
+ leafList.forEach(leaf => {
1230
+ if (leaf.__hitCanvas) {
1231
+ leaf.__hitCanvas.destroy();
1232
+ leaf.__hitCanvas = null;
1233
+ }
1234
+ });
1235
+ leafList.reset();
1236
+ }
1237
+ }
1238
+ clear() {
1239
+ this.clearPathType();
1240
+ this.clearImageType();
1241
+ }
1242
+ }
1243
+
1244
+ const { toInnerRadiusPointOf, copy, setRadius } = PointHelper$1;
1245
+ const inner = {};
1246
+ Leaf.prototype.__hitWorld = function (point) {
1247
+ if (this.__layout.hitCanvasChanged || !this.__hitCanvas) {
1248
+ this.__updateHitCanvas();
1249
+ this.__layout.hitCanvasChanged = false;
1250
+ }
1251
+ if (this.__.hitRadius) {
1252
+ copy(inner, point), point = inner;
1253
+ setRadius(point, this.__.hitRadius);
1254
+ }
1255
+ toInnerRadiusPointOf(point, this.__world, inner);
1256
+ if (this.__.hitBox) {
1257
+ if (BoundsHelper$1.hitRadiusPoint(this.__layout.boxBounds, inner))
1258
+ return true;
1259
+ }
1260
+ return this.__hit(inner);
1261
+ };
1262
+
1263
+ UI.prototype.__updateHitCanvas = function () {
1264
+ if (!this.__hitCanvas)
1265
+ this.__hitCanvas = this.leafer.hitCanvasManager.getPathType(this);
1266
+ const h = this.__hitCanvas;
1267
+ this.__drawHitPath(h);
1268
+ h.setStrokeOptions(this.__);
1269
+ };
1270
+ UI.prototype.__hit = function (inner) {
1271
+ if (Platform.name === 'miniapp')
1272
+ this.__drawHitPath(this.__hitCanvas);
1273
+ const { fill, hitFill, windingRule } = this.__;
1274
+ const needHitFill = (fill && hitFill === 'path') || hitFill === 'all';
1275
+ const isHitFill = this.__hitFill(inner, windingRule);
1276
+ if (needHitFill && isHitFill)
1277
+ return true;
1278
+ const { stroke, hitStroke, __strokeWidth, strokeAlign } = this.__;
1279
+ const needHitStroke = (stroke && hitStroke === 'path') || hitStroke === 'all';
1280
+ const radiusWidth = inner.radiusX * 2;
1281
+ let hitWidth = radiusWidth;
1282
+ if (needHitStroke) {
1283
+ switch (strokeAlign) {
1284
+ case 'inside':
1285
+ hitWidth += __strokeWidth * 2;
1286
+ if (!needHitFill && (isHitFill && this.__hitStroke(inner, hitWidth)))
1287
+ return true;
1288
+ hitWidth = radiusWidth;
1289
+ break;
1290
+ case 'center':
1291
+ hitWidth += __strokeWidth;
1292
+ break;
1293
+ case 'outside':
1294
+ hitWidth += __strokeWidth * 2;
1295
+ if (!needHitFill) {
1296
+ if (!isHitFill && this.__hitStroke(inner, hitWidth))
1297
+ return true;
1298
+ hitWidth = radiusWidth;
1299
+ }
1300
+ break;
1301
+ }
1302
+ }
1303
+ return hitWidth ? this.__hitStroke(inner, hitWidth) : false;
1304
+ };
1305
+
1306
+ const ui = new UI();
1307
+ Rect.prototype.__updateHitCanvas = function () {
1308
+ if (this.stroke || this.cornerRadius)
1309
+ ui.__updateHitCanvas.call(this);
1310
+ };
1311
+ Rect.prototype.__hitFill = function (inner, windingRule) {
1312
+ return this.__hitCanvas ? ui.__hitFill.call(this, inner, windingRule) : BoundsHelper$1.hitRadiusPoint(this.__layout.boxBounds, inner);
1313
+ };
1314
+
1315
+ UI.prototype.find = function (condition, options) {
1316
+ return this.leafer ? this.leafer.selector.getBy(condition, this, false, options) : [];
1317
+ };
1318
+ UI.prototype.findOne = function (condition, options) {
1319
+ return this.leafer ? this.leafer.selector.getBy(condition, this, true, options) : null;
1320
+ };
1321
+ Group.prototype.pick = function (hitPoint, options) {
1322
+ this.__layout.update();
1323
+ if (!options)
1324
+ options = {};
1325
+ return this.leafer ? this.leafer.selector.getByPoint(hitPoint, options.hitRadius || 0, Object.assign(Object.assign({}, options), { target: this })) : null;
1326
+ };
1327
+
1328
+ export { App, Cursor, DragEvent, DropEvent, HitCanvasManager, InteractionBase, InteractionHelper, KeyEvent, Keyboard, LeaferTypeCreator, MoveEvent, MultiTouchHelper, PointerButton, PointerEvent, RotateEvent, SwipeEvent, UIEvent, ZoomEvent };