@leafer-ui/event 1.9.9 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/event",
3
- "version": "1.9.9",
3
+ "version": "1.9.11",
4
4
  "description": "@leafer-ui/event",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.9.9"
25
+ "@leafer/core": "1.9.11"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.9.9"
28
+ "@leafer/interface": "1.9.11"
29
29
  }
30
30
  }
@@ -0,0 +1,147 @@
1
+ import { IPointData, IBoundsData, IDragBoundsType, ILeaf, ISide } from '@leafer/interface'
2
+ import { Bounds, BoundsHelper, MathHelper, isFinite } from '@leafer/core'
3
+
4
+
5
+ const { min, max, abs } = Math, { float, sign } = MathHelper, { minX, maxX, minY, maxY } = BoundsHelper
6
+ const tempContent = new Bounds(), tempDragBounds = new Bounds()
7
+
8
+ export const DragBoundsHelper = {
9
+
10
+ // 拖拽区域内移动
11
+ limitMove(leaf: ILeaf, move: IPointData): void {
12
+ const { dragBounds, dragBoundsType } = leaf
13
+ if (dragBounds) D.getValidMove(leaf.__localBoxBounds, D.getDragBounds(leaf), dragBoundsType, move, true)
14
+ D.axisMove(leaf, move)
15
+ },
16
+
17
+ // 拖拽区域内缩放
18
+ limitScaleOf(leaf: ILeaf, origin: IPointData, scale: IPointData, lockRatio?: boolean): void {
19
+ const { dragBounds, dragBoundsType } = leaf
20
+ if (dragBounds) D.getValidScaleOf(leaf.__localBoxBounds, D.getDragBounds(leaf), dragBoundsType, leaf.getLocalPointByInner(leaf.getInnerPointByBox(origin)), scale, lockRatio, true)
21
+ },
22
+
23
+ // 按轴移动
24
+ axisMove(leaf: ILeaf, move: IPointData) {
25
+ const { draggable } = leaf
26
+ if (draggable === 'x') move.y = 0
27
+ if (draggable === 'y') move.x = 0
28
+ },
29
+
30
+ getDragBounds(leaf: ILeaf): IBoundsData {
31
+ const { dragBounds } = leaf
32
+ return dragBounds === 'parent' ? leaf.parent.boxBounds : dragBounds
33
+ },
34
+
35
+ isInnerMode(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, sideType: ISide): boolean {
36
+ return dragBoundsType === 'inner' || (dragBoundsType === 'auto' && content[sideType] > dragBounds[sideType])
37
+ },
38
+
39
+ getValidMove(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, move: IPointData, change?: boolean): IPointData {
40
+ const x = content.x + move.x, y = content.y + move.y, right = x + content.width, bottom = y + content.height
41
+ const boundsRight = dragBounds.x + dragBounds.width, boundsBottom = dragBounds.y + dragBounds.height
42
+
43
+ if (!change) move = { ...move }
44
+
45
+ if (D.isInnerMode(content, dragBounds, dragBoundsType, 'width')) { // inner 模式
46
+ if (x > dragBounds.x) move.x += dragBounds.x - x
47
+ else if (right < boundsRight) move.x += boundsRight - right
48
+ } else { // outer 模式
49
+ if (x < dragBounds.x) move.x += dragBounds.x - x
50
+ else if (right > boundsRight) move.x += boundsRight - right
51
+ }
52
+
53
+ if (D.isInnerMode(content, dragBounds, dragBoundsType, 'height')) { // inner 模式
54
+ if (y > dragBounds.y) move.y += dragBounds.y - y
55
+ else if (bottom < boundsBottom) move.y += boundsBottom - bottom
56
+ } else { // outer 模式
57
+ if (y < dragBounds.y) move.y += dragBounds.y - y
58
+ else if (bottom > boundsBottom) move.y += boundsBottom - bottom
59
+ }
60
+
61
+ // 避免出现为0的抖动小数
62
+ move.x = float(move.x)
63
+ move.y = float(move.y)
64
+
65
+ return move
66
+ },
67
+
68
+ getValidScaleOf(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, origin: IPointData, scale: IPointData, lockRatio?: boolean, change?: boolean): IPointData {
69
+ if (!change) scale = { ...scale }
70
+
71
+ tempDragBounds.set(dragBounds)
72
+ tempContent.set(content).scaleOf(origin, scale.x, scale.y)
73
+
74
+ const originLeftScale = (origin.x - content.x) / content.width, originRightScale = 1 - originLeftScale
75
+ const originTopScale = (origin.y - content.y) / content.height, originBottomScale = 1 - originTopScale
76
+
77
+ let correctScaleX = 1, correctScaleY = 1, aScale: number, bScale: number, aSize: number, bSize: number
78
+
79
+ if (D.isInnerMode(content, dragBounds, dragBoundsType, 'width')) { // inner 模式
80
+
81
+ if (scale.x < 0) tempContent.scaleOf(origin, correctScaleX = 1 / scale.x, 1) // 阻止镜像
82
+
83
+ aSize = float(tempContent.minX - tempDragBounds.minX)
84
+ bSize = float(tempDragBounds.maxX - tempContent.maxX)
85
+
86
+ aScale = originLeftScale && aSize > 0 ? 1 + aSize / (originLeftScale * tempContent.width) : 1
87
+ bScale = originRightScale && bSize > 0 ? 1 + bSize / (originRightScale * tempContent.width) : 1
88
+ correctScaleX *= max(aScale, bScale)
89
+
90
+ } else { // outer 模式
91
+
92
+ if (scale.x < 0) {
93
+ if (float(minX(content) - minX(dragBounds)) <= 0 || float(maxX(dragBounds) - maxX(content)) <= 0) tempContent.scaleOf(origin, correctScaleX = 1 / scale.x, 1) // 到达边界时阻止镜像
94
+ tempContent.unsign()
95
+ }
96
+
97
+ aSize = float(tempDragBounds.minX - tempContent.minX)
98
+ bSize = float(tempContent.maxX - tempDragBounds.maxX)
99
+
100
+ aScale = originLeftScale && aSize > 0 ? 1 - aSize / (originLeftScale * tempContent.width) : 1
101
+ bScale = originRightScale && bSize > 0 ? 1 - bSize / (originRightScale * tempContent.width) : 1
102
+ correctScaleX *= min(aScale, bScale)
103
+
104
+ }
105
+
106
+ if (D.isInnerMode(content, dragBounds, dragBoundsType, 'height')) { // inner 模式
107
+
108
+ if (scale.y < 0) tempContent.scaleOf(origin, 1, correctScaleY = 1 / scale.y) // 阻止镜像
109
+
110
+ aSize = float(tempContent.minY - tempDragBounds.minY)
111
+ bSize = float(tempDragBounds.maxY - tempContent.maxY)
112
+
113
+ aScale = originTopScale && aSize > 0 ? 1 + aSize / (originTopScale * tempContent.height) : 1
114
+ bScale = originBottomScale && bSize > 0 ? 1 + bSize / (originBottomScale * tempContent.height) : 1
115
+
116
+ correctScaleY *= max(aScale, bScale)
117
+
118
+ if (lockRatio) {
119
+ aScale = max(abs(correctScaleX), abs(correctScaleY))
120
+ correctScaleX = sign(correctScaleX) * aScale
121
+ correctScaleY = sign(correctScaleY) * aScale
122
+ }
123
+
124
+ } else { // outer 模式
125
+
126
+ if (scale.y < 0) {
127
+ if (float(minY(content) - minY(dragBounds)) <= 0 || float(maxY(dragBounds) - maxY(content)) <= 0) tempContent.scaleOf(origin, 1, correctScaleY = 1 / scale.y) // 到达边界时阻止镜像
128
+ tempContent.unsign()
129
+ }
130
+
131
+ aSize = float(tempDragBounds.minY - tempContent.minY)
132
+ bSize = float(tempContent.maxY - tempDragBounds.maxY)
133
+
134
+ aScale = originTopScale && aSize > 0 ? 1 - aSize / (originTopScale * tempContent.height) : 1
135
+ bScale = originBottomScale && bSize > 0 ? 1 - bSize / (originBottomScale * tempContent.height) : 1
136
+ correctScaleY *= min(aScale, bScale)
137
+
138
+ }
139
+
140
+ scale.x *= isFinite(correctScaleX) ? correctScaleX : 1
141
+ scale.y *= isFinite(correctScaleY) ? correctScaleY : 1
142
+
143
+ return scale
144
+ }
145
+ }
146
+
147
+ const D = DragBoundsHelper
package/src/DragEvent.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { IDragEvent, IPointData, ILeaf, ILeafList, IObject, IBoundsData } from '@leafer/interface'
2
2
  import { registerUIEvent, LeafList, BoundsHelper, PointHelper } from '@leafer/core'
3
- import { DragBoundsHelper } from '@leafer-ui/draw'
4
3
 
4
+ import { DragBoundsHelper } from './DragBoundsHelper'
5
5
  import { PointerEvent } from './PointerEvent'
6
6
 
7
7
 
package/src/index.ts CHANGED
@@ -9,4 +9,5 @@ export { ZoomEvent } from './ZoomEvent'
9
9
  export { KeyEvent } from './KeyEvent'
10
10
 
11
11
  export { Keyboard } from './Keyboard'
12
- export { PointerButton } from './PointerButton'
12
+ export { PointerButton } from './PointerButton'
13
+ export { DragBoundsHelper } from './DragBoundsHelper'
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IUIEvent, ILeafList, ILeaf, IShortcutKeysCheck, IShortcutKeys, IPointData, IPointerEvent, PointerType, IDragEvent, IObject, IBoundsData, IDropEvent, IMoveEvent, IRotateEvent, ISwipeEvent, IZoomEvent, IKeyEvent, IKeyCodes, IShortcutKeyCodes } from '@leafer/interface';
1
+ import { IUIEvent, ILeafList, ILeaf, IShortcutKeysCheck, IShortcutKeys, IPointData, IPointerEvent, PointerType, IDragEvent, IObject, IBoundsData, IDropEvent, IMoveEvent, IRotateEvent, ISwipeEvent, IZoomEvent, IKeyEvent, IKeyCodes, IShortcutKeyCodes, IDragBoundsType, ISide } from '@leafer/interface';
2
2
  import { Event } from '@leafer/core';
3
3
 
4
4
  declare class UIEvent extends Event implements IUIEvent {
@@ -164,4 +164,14 @@ declare const PointerButton: {
164
164
  middle(event: IUIEvent): boolean;
165
165
  };
166
166
 
167
- export { DragEvent, DropEvent, KeyEvent, Keyboard, MoveEvent, MyDragEvent, MyPointerEvent, PointerButton, PointerEvent, RotateEvent, SwipeEvent, UIEvent, ZoomEvent };
167
+ declare const DragBoundsHelper: {
168
+ limitMove(leaf: ILeaf, move: IPointData): void;
169
+ limitScaleOf(leaf: ILeaf, origin: IPointData, scale: IPointData, lockRatio?: boolean): void;
170
+ axisMove(leaf: ILeaf, move: IPointData): void;
171
+ getDragBounds(leaf: ILeaf): IBoundsData;
172
+ isInnerMode(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, sideType: ISide): boolean;
173
+ getValidMove(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, move: IPointData, change?: boolean): IPointData;
174
+ getValidScaleOf(content: IBoundsData, dragBounds: IBoundsData, dragBoundsType: IDragBoundsType, origin: IPointData, scale: IPointData, lockRatio?: boolean, change?: boolean): IPointData;
175
+ };
176
+
177
+ export { DragBoundsHelper, DragEvent, DropEvent, KeyEvent, Keyboard, MoveEvent, MyDragEvent, MyPointerEvent, PointerButton, PointerEvent, RotateEvent, SwipeEvent, UIEvent, ZoomEvent };