@leafer-ui/interaction 1.0.0-rc.17 → 1.0.0-rc.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/interaction",
3
- "version": "1.0.0-rc.17",
3
+ "version": "1.0.0-rc.18",
4
4
  "description": "@leafer-ui/interaction",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.0.0-rc.17",
26
- "@leafer-ui/event": "1.0.0-rc.17"
25
+ "@leafer/core": "1.0.0-rc.18",
26
+ "@leafer-ui/draw": "1.0.0-rc.18",
27
+ "@leafer-ui/event": "1.0.0-rc.18"
27
28
  },
28
29
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.17"
30
+ "@leafer/interface": "1.0.0-rc.18"
30
31
  }
31
32
  }
package/src/Dragger.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IPointerEvent, IDragEvent, ILeaf, ILeafList, ITimer, IFunction } from '@leafer/interface'
2
- import { BoundsHelper, PointHelper, LeafHelper, LeafList } from '@leafer/core'
2
+ import { BoundsHelper, PointHelper, LeafList } from '@leafer/core'
3
3
 
4
4
  import { MoveEvent, DragEvent, DropEvent, PointerButton } from '@leafer-ui/event'
5
5
 
@@ -26,7 +26,8 @@ export class Dragger {
26
26
 
27
27
  protected autoMoveTimer: ITimer
28
28
 
29
- protected canAnimate: boolean
29
+ public canAnimate: boolean
30
+ public canDragOut: boolean
30
31
  protected animateWait: IFunction
31
32
 
32
33
  constructor(interaction: InteractionBase) {
@@ -37,6 +38,7 @@ export class Dragger {
37
38
  if (this.animateWait) this.dragEndReal()
38
39
  this.downData = this.interaction.downData
39
40
  this.dragData = getDragEventData(data, data, data)
41
+ this.canAnimate = this.canDragOut = true
40
42
  }
41
43
 
42
44
  public getList(): ILeafList {
@@ -51,8 +53,6 @@ export class Dragger {
51
53
  this.canAnimate = false // 防止dragEnd动画
52
54
  interaction.pointerCancel() // 按住中键/右键拖出页面后的up事件接收不到
53
55
  return
54
- } else {
55
- this.canAnimate = true
56
56
  }
57
57
 
58
58
  if (!this.moving && canDrag) {
@@ -109,7 +109,7 @@ export class Dragger {
109
109
  const list = this.getList()
110
110
  if (list.length && running) {
111
111
  const { moveX, moveY } = this.dragData
112
- list.forEach(leaf => LeafHelper.moveWorld(leaf, moveX, moveY))
112
+ list.forEach(leaf => leaf.moveWorld(moveX, moveY))
113
113
  }
114
114
  }
115
115
 
@@ -117,6 +117,7 @@ export class Dragger {
117
117
  const { interaction } = this
118
118
  const { dragOverPath } = this
119
119
  const { path } = data
120
+ this.dragOverPath = path
120
121
 
121
122
  if (dragOverPath) {
122
123
  if (path.indexAt(0) !== dragOverPath.indexAt(0)) {
@@ -126,7 +127,7 @@ export class Dragger {
126
127
  } else {
127
128
  interaction.emit(DragEvent.OVER, data, path)
128
129
  }
129
- this.dragOverPath = path
130
+
130
131
  }
131
132
 
132
133
  public dragEnterOrLeave(data: IPointerEvent): void {
@@ -143,7 +144,7 @@ export class Dragger {
143
144
  if (!this.dragData) return
144
145
 
145
146
  const { moveX, moveY } = this.dragData
146
- if (this.canAnimate && this.moving && (Math.abs(moveX) > 1 || Math.abs(moveY) > 1)) {
147
+ if (this.interaction.config.move.dragAnimate && this.canAnimate && this.moving && (Math.abs(moveX) > 1 || Math.abs(moveY) > 1)) {
147
148
  data = { ...data }
148
149
  speed = (speed || (data.pointerType === 'touch' ? 2 : 1)) * 0.9
149
150
  PointHelper.move(data, moveX * speed, moveY * speed)
@@ -179,14 +180,13 @@ export class Dragger {
179
180
 
180
181
  protected animate(func?: IFunction, off?: 'off'): void { // dragEnd animation
181
182
  const animateWait = func || this.animateWait
182
- if (animateWait) this.interaction.target.nextRender(animateWait, off)
183
+ if (animateWait) this.interaction.target.nextRender(animateWait, null, off)
183
184
  this.animateWait = func
184
185
  }
185
186
 
186
187
 
187
188
  protected swipe(data: IPointerEvent, endDragData: IDragEvent): void {
188
- const { interaction } = this
189
- const { downData } = interaction
189
+ const { interaction, downData } = this
190
190
  if (PointHelper.getDistance(downData, data) > interaction.config.pointer.swipeDistance) {
191
191
  const swipeData = getSwipeEventData(downData, this.dragData, endDragData)
192
192
  this.interaction.emit(swipeData.type, swipeData)
@@ -214,9 +214,9 @@ export class Dragger {
214
214
 
215
215
 
216
216
  protected autoMoveOnDragOut(data: IPointerEvent): void {
217
- const { interaction, downData } = this
217
+ const { interaction, downData, canDragOut } = this
218
218
  const { autoDistance, dragOut } = interaction.config.move
219
- if (!dragOut || !autoDistance) return
219
+ if (!dragOut || !canDragOut || !autoDistance) return
220
220
 
221
221
  const bounds = interaction.shrinkCanvasBounds
222
222
  const { x, y } = bounds
@@ -35,8 +35,11 @@ export class InteractionBase implements IInteraction {
35
35
  public downData: IPointerEvent
36
36
  protected oldDownData?: IPointerEvent // 通过updateDownData强制更新下来的数据
37
37
  public hoverData: IPointerEvent
38
+ public focusData: ILeaf
38
39
 
39
40
  public downTime: number
41
+ protected downed: boolean
42
+
40
43
  protected overPath: LeafList
41
44
  protected enterPath: LeafList
42
45
 
@@ -89,33 +92,39 @@ export class InteractionBase implements IInteraction {
89
92
  this.updateDownData(data)
90
93
  if (useDefaultPath) data.path = this.defaultPath
91
94
 
92
- this.emit(PointerEvent.BEFORE_DOWN, data)
93
- this.emit(PointerEvent.DOWN, data)
94
-
95
95
  this.downTime = Date.now()
96
96
 
97
- this.dragger.setDragData(data)
97
+ if (this.downed = !this.moveMode) {
98
+ this.emit(PointerEvent.BEFORE_DOWN, data) // downData maybe changed
99
+ this.emit(PointerEvent.DOWN, data)
98
100
 
99
- if (PointerButton.left(data)) {
100
- this.tapWait()
101
- this.longPressWait(data)
102
- } else if (PointerButton.right(data)) {
103
- this.waitMenuTap = true
101
+ if (PointerButton.left(data)) {
102
+ this.tapWait()
103
+ this.longPressWait(data)
104
+ } else if (PointerButton.right(data)) {
105
+ this.waitMenuTap = true
106
+ }
104
107
  }
105
108
 
109
+ this.dragger.setDragData(data) // must after down event
106
110
  this.updateCursor(data)
107
111
  }
108
112
 
109
113
  public pointerMove(data?: IPointerEvent): void {
110
114
  if (!data) data = this.hoverData
111
115
  if (!data) return
112
- if (this.downData) PointerButton.defaultLeft(data)
116
+
117
+ const { downData } = this
118
+ if (downData) PointerButton.defaultLeft(data)
113
119
 
114
120
  const hit = this.canvas.bounds.hitPoint(data)
115
- if (hit || this.downData) {
116
- if (hit && !this.downData && PointerButton.left(data)) this.pointerDown(data, true) // 从外部拖拽内容进入,需要先模拟down事件
121
+ if (hit || downData) {
122
+ if (hit && !downData && PointerButton.left(data)) {
123
+ this.pointerDown(data, true) // 从外部拖拽内容进入,需要先模拟down事件
124
+ this.dragger.canDragOut = false
125
+ }
117
126
  this.pointerMoveReal(data)
118
- this.dragger.checkDragOut(data)
127
+ if (downData) this.dragger.checkDragOut(data)
119
128
  }
120
129
  }
121
130
 
@@ -134,10 +143,12 @@ export class InteractionBase implements IInteraction {
134
143
 
135
144
  if (!this.dragger.moving) {
136
145
  this.updateHoverData(data)
146
+ if (this.moveMode) data.path = this.defaultPath
147
+
137
148
  this.emit(PointerEvent.MOVE, data)
138
149
 
139
- this.pointerOverOrOut(data)
140
- this.pointerEnterOrLeave(data)
150
+ if (!(this.dragging && !this.config.pointer.dragHover)) this.pointerHover(data)
151
+
141
152
  if (this.dragger.dragging) {
142
153
  this.dragger.dragOverOrOut(data)
143
154
  this.dragger.dragEnterOrLeave(data)
@@ -148,21 +159,25 @@ export class InteractionBase implements IInteraction {
148
159
  }
149
160
 
150
161
  public pointerUp(data?: IPointerEvent): void {
151
- if (!data) data = this.downData
152
- if (!this.downData) return
162
+ const { downData, oldDownData } = this
163
+ if (!data) data = downData
164
+ if (!downData) return
153
165
  PointerButton.defaultLeft(data)
154
166
 
155
167
  this.findPath(data)
156
168
 
157
- this.emit(PointerEvent.BEFORE_UP, data)
158
- this.emit(PointerEvent.UP, data)
159
- if (this.oldDownData) this.emit(PointerEvent.UP, this.oldDownData, undefined, data.path) // oldDownPath必须触发up
160
- this.emit(PointerEvent.UP, this.downData, undefined, data.path) // downPath必须触发up
169
+ if (this.downed) {
170
+ this.downed = false
171
+ this.emit(PointerEvent.BEFORE_UP, data)
172
+ this.emit(PointerEvent.UP, data)
173
+ if (oldDownData) this.emit(PointerEvent.UP, oldDownData, undefined, data.path) // oldDownPath必须触发up
174
+ this.emit(PointerEvent.UP, downData, undefined, data.path) // downPath必须触发up
161
175
 
162
- this.touchLeave(data)
176
+ this.touchLeave(data)
163
177
 
164
- this.tap(data)
165
- this.menuTap(data)
178
+ this.tap(data)
179
+ this.menuTap(data)
180
+ }
166
181
 
167
182
  this.dragger.dragEnd(data)
168
183
 
@@ -222,7 +237,10 @@ export class InteractionBase implements IInteraction {
222
237
  Keyboard.setDownCode(code)
223
238
 
224
239
  this.emit(KeyEvent.HOLD, data, this.defaultPath)
225
- if (this.moveMode) this.updateCursor()
240
+ if (this.moveMode) {
241
+ this.cancelHover()
242
+ this.updateCursor()
243
+ }
226
244
  }
227
245
  this.emit(KeyEvent.DOWN, data, this.defaultPath)
228
246
  }
@@ -238,30 +256,39 @@ export class InteractionBase implements IInteraction {
238
256
 
239
257
 
240
258
  // helper
241
- protected pointerOverOrOut(data: IPointerEvent): void {
242
- if (this.dragger.moving) return
243
- if (this.dragging && !this.config.pointer.dragHover) return
259
+ protected pointerHover(data: IPointerEvent): void {
260
+ this.pointerOverOrOut(data)
261
+ this.pointerEnterOrLeave(data)
262
+ }
244
263
 
264
+ protected pointerOverOrOut(data: IPointerEvent): void {
245
265
  const { path } = data
246
- if (this.overPath) {
247
- if (path.indexAt(0) !== this.overPath.indexAt(0)) {
248
- this.emit(PointerEvent.OUT, data, this.overPath)
266
+ const { overPath } = this
267
+ this.overPath = path
268
+
269
+ if (overPath) {
270
+ if (path.indexAt(0) !== overPath.indexAt(0)) {
271
+ this.emit(PointerEvent.OUT, data, overPath)
249
272
  this.emit(PointerEvent.OVER, data, path)
250
273
  }
251
274
  } else {
252
275
  this.emit(PointerEvent.OVER, data, path)
253
276
  }
254
- this.overPath = path
255
277
  }
256
278
 
257
279
  protected pointerEnterOrLeave(data: IPointerEvent): void {
258
- if (this.dragger.moving) return
259
- if (this.dragging && !this.config.pointer.dragHover) return
280
+ let { path } = data
260
281
 
261
- const { path } = data
262
- this.emit(PointerEvent.LEAVE, data, this.enterPath, path)
263
- this.emit(PointerEvent.ENTER, data, path, this.enterPath)
282
+ if (this.downData && !this.moveMode) {
283
+ path = path.clone()
284
+ this.downData.path.forEach(leaf => path.add(leaf))
285
+ }
286
+
287
+ const { enterPath } = this
264
288
  this.enterPath = path
289
+
290
+ this.emit(PointerEvent.LEAVE, data, enterPath, path)
291
+ this.emit(PointerEvent.ENTER, data, path, enterPath)
265
292
  }
266
293
 
267
294
  protected touchLeave(data: IPointerEvent): void {
@@ -321,10 +348,34 @@ export class InteractionBase implements IInteraction {
321
348
  return find.path
322
349
  }
323
350
 
351
+
324
352
  public isDrag(leaf: ILeaf): boolean {
325
353
  return this.dragger.getList().has(leaf)
326
354
  }
327
355
 
356
+ public isPress(leaf: ILeaf): boolean {
357
+ const { downData, oldDownData } = this
358
+ return this.downed && ((downData && downData.path.has(leaf)) || (oldDownData && oldDownData.path.has(leaf)))
359
+ }
360
+
361
+ public isHover(leaf: ILeaf): boolean {
362
+ return this.enterPath && this.enterPath.has(leaf)
363
+ }
364
+
365
+ public isFocus(leaf: ILeaf): boolean {
366
+ return this.focusData === leaf
367
+ }
368
+
369
+
370
+ public cancelHover(): void {
371
+ const { hoverData } = this
372
+ if (hoverData) {
373
+ hoverData.path = this.defaultPath
374
+ this.pointerHover(hoverData)
375
+ }
376
+ }
377
+
378
+
328
379
  public updateDownData(data?: IPointerEvent, options?: IPickOptions): void {
329
380
  const { downData } = this
330
381
  if (!data && downData) data = { ...downData }
@@ -355,8 +406,7 @@ export class InteractionBase implements IInteraction {
355
406
  return this.setCursor(this.downData ? 'grabbing' : 'grab')
356
407
  } else if (!data) return
357
408
 
358
- let leaf: ILeaf
359
- let cursor: ICursorType | ICursorType[]
409
+ let leaf: ILeaf, cursor: ICursorType | ICursorType[]
360
410
 
361
411
  const { path } = data
362
412
  for (let i = 0, len = path.length; i < len; i++) {
@@ -372,6 +422,7 @@ export class InteractionBase implements IInteraction {
372
422
  this.cursor = cursor
373
423
  }
374
424
 
425
+
375
426
  protected emitTap(data: IPointerEvent) {
376
427
  this.emit(PointerEvent.TAP, data)
377
428
  this.emit(PointerEvent.CLICK, data)
@@ -23,6 +23,7 @@ export class Transformer {
23
23
  const { path } = interaction.selector.getByPoint(data, interaction.hitRadius)
24
24
  data.path = path
25
25
  this.moveData = { ...data, moveX: 0, moveY: 0 }
26
+ interaction.cancelHover()
26
27
  interaction.emit(MoveEvent.START, this.moveData)
27
28
  }
28
29
 
@@ -40,6 +41,7 @@ export class Transformer {
40
41
  const { path } = interaction.selector.getByPoint(data, interaction.hitRadius)
41
42
  data.path = path
42
43
  this.zoomData = { ...data, scale: 1 }
44
+ interaction.cancelHover()
43
45
  interaction.emit(ZoomEvent.START, this.zoomData)
44
46
  }
45
47
 
@@ -57,6 +59,7 @@ export class Transformer {
57
59
  const { path } = interaction.selector.getByPoint(data, interaction.hitRadius)
58
60
  data.path = path
59
61
  this.rotateData = { ...data, rotation: 0 }
62
+ interaction.cancelHover()
60
63
  interaction.emit(RotateEvent.START, this.rotateData)
61
64
  }
62
65
 
package/src/config.ts CHANGED
@@ -2,7 +2,6 @@ import { IInteractionConfig } from '@leafer/interface'
2
2
 
3
3
  export const config: IInteractionConfig = {
4
4
  wheel: {
5
- zoomMode: false,
6
5
  zoomSpeed: 0.5,
7
6
  moveSpeed: 0.5,
8
7
  rotateSpeed: 0.5,
@@ -11,14 +10,12 @@ export const config: IInteractionConfig = {
11
10
  },
12
11
  pointer: {
13
12
  hitRadius: 5,
14
- through: false,
15
13
  tapTime: 120,
16
14
  longPressTime: 800,
17
15
  transformTime: 500,
18
16
  dragHover: true,
19
17
  dragDistance: 2,
20
18
  swipeDistance: 20,
21
- ignoreMove: false,
22
19
  preventDefaultMenu: true
23
20
  },
24
21
  cursor: {}
package/src/emit.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { IUIEvent, ILeaf, ILeafList } from '@leafer/interface'
2
2
  import { EventCreator, Debug } from '@leafer/core'
3
+ import { State } from '@leafer-ui/draw'
4
+
3
5
 
4
6
  const debug = Debug.get('emit')
5
7
 
@@ -50,11 +52,16 @@ function emitAppChildren(leaf: ILeaf, type: string, data: IUIEvent, capture?: bo
50
52
 
51
53
  function emitEvent(leaf: ILeaf, type: string, data: IUIEvent, capture?: boolean, excludePath?: ILeafList): boolean {
52
54
  if (leaf.destroyed) return true
53
- if (leaf.__.hitSelf && leaf.hasEvent(type, capture) && !exclude(leaf, excludePath)) {
54
- data.phase = capture ? 1 : ((leaf === data.target) ? 2 : 3)
55
- const event = EventCreator.get(type, data)
56
- leaf.emitEvent(event, capture)
57
- if (event.isStop) return true
55
+ if (leaf.__.hitSelf && !exclude(leaf, excludePath)) {
56
+
57
+ if (State.updateEventStyle) State.updateEventStyle(leaf, type) // hoverStyle / pressStyle
58
+
59
+ if (leaf.hasEvent(type, capture)) {
60
+ data.phase = capture ? 1 : ((leaf === data.target) ? 2 : 3)
61
+ const event = EventCreator.get(type, data)
62
+ leaf.emitEvent(event, capture)
63
+ if (event.isStop) return true
64
+ }
58
65
  }
59
66
  return false
60
67
  }
package/types/index.d.ts CHANGED
@@ -29,7 +29,8 @@ declare class Dragger {
29
29
  protected dragOverPath: ILeafList;
30
30
  protected dragEnterPath: ILeafList;
31
31
  protected autoMoveTimer: ITimer;
32
- protected canAnimate: boolean;
32
+ canAnimate: boolean;
33
+ canDragOut: boolean;
33
34
  protected animateWait: IFunction;
34
35
  constructor(interaction: InteractionBase);
35
36
  setDragData(data: IPointerEvent): void;
@@ -69,7 +70,9 @@ declare class InteractionBase implements IInteraction {
69
70
  downData: IPointerEvent;
70
71
  protected oldDownData?: IPointerEvent;
71
72
  hoverData: IPointerEvent;
73
+ focusData: ILeaf;
72
74
  downTime: number;
75
+ protected downed: boolean;
73
76
  protected overPath: LeafList;
74
77
  protected enterPath: LeafList;
75
78
  protected waitMenuTap: boolean;
@@ -101,12 +104,17 @@ declare class InteractionBase implements IInteraction {
101
104
  transformEnd(): void;
102
105
  keyDown(data: IKeyEvent): void;
103
106
  keyUp(data: IKeyEvent): void;
107
+ protected pointerHover(data: IPointerEvent): void;
104
108
  protected pointerOverOrOut(data: IPointerEvent): void;
105
109
  protected pointerEnterOrLeave(data: IPointerEvent): void;
106
110
  protected touchLeave(data: IPointerEvent): void;
107
111
  protected tap(data: IPointerEvent): void;
108
112
  findPath(data: IPointerEvent, options?: IPickOptions): ILeafList;
109
113
  isDrag(leaf: ILeaf): boolean;
114
+ isPress(leaf: ILeaf): boolean;
115
+ isHover(leaf: ILeaf): boolean;
116
+ isFocus(leaf: ILeaf): boolean;
117
+ cancelHover(): void;
110
118
  updateDownData(data?: IPointerEvent, options?: IPickOptions): void;
111
119
  updateHoverData(data?: IPointerEvent): void;
112
120
  updateCursor(data?: IPointerEvent): void;