@leafer/helper 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.
package/package.json CHANGED
@@ -1,27 +1,30 @@
1
1
  {
2
2
  "name": "@leafer/helper",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-rc.10",
4
4
  "description": "@leafer/helper",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
+ "types": "types/index.d.ts",
8
9
  "files": [
9
- "src"
10
+ "src",
11
+ "types",
12
+ "dist"
10
13
  ],
11
14
  "repository": {
12
15
  "type": "git",
13
16
  "url": "https://github.com/leaferjs/leafer.git"
14
17
  },
15
- "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/helper",
18
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/display-module/helper",
16
19
  "bugs": "https://github.com/leaferjs/leafer/issues",
17
20
  "keywords": [
18
21
  "leafer",
19
22
  "leaferjs"
20
23
  ],
21
24
  "dependencies": {
22
- "@leafer/math": "1.0.0-beta.9"
25
+ "@leafer/math": "1.0.0-rc.10"
23
26
  },
24
27
  "devDependencies": {
25
- "@leafer/interface": "1.0.0-beta.9"
28
+ "@leafer/interface": "1.0.0-rc.10"
26
29
  }
27
30
  }
@@ -1,10 +1,8 @@
1
- import { ILeaf, ILeafList } from '@leafer/interface'
1
+ import { ILeaf, ILeafLevelList, ILeafList } from '@leafer/interface'
2
2
 
3
+ import { LeafHelper } from './LeafHelper'
3
4
 
4
- interface ILeafPushList {
5
- push(item: ILeaf): void
6
- }
7
-
5
+ const { updateBounds } = LeafHelper
8
6
 
9
7
  export const BranchHelper = {
10
8
 
@@ -14,7 +12,7 @@ export const BranchHelper = {
14
12
 
15
13
  // push
16
14
 
17
- pushAllChildBranch(branch: ILeaf, pushList: ILeafPushList): void {
15
+ pushAllChildBranch(branch: ILeaf, leafList: ILeafList | ILeafLevelList): void {
18
16
  branch.__tempNumber = 1 // 标识需要更新子Leaf元素的WorldBounds分支 Layouter需使用
19
17
 
20
18
  if (branch.__.__childBranchNumber) {
@@ -23,19 +21,19 @@ export const BranchHelper = {
23
21
  branch = children[i]
24
22
  if (branch.isBranch) {
25
23
  branch.__tempNumber = 1
26
- pushList.push(branch)
27
- pushAllChildBranch(branch, pushList)
24
+ leafList.add(branch)
25
+ pushAllChildBranch(branch, leafList)
28
26
  }
29
27
  }
30
28
  }
31
29
  },
32
30
 
33
- pushAllParent(leaf: ILeaf, pushList: ILeafPushList): void {
34
- const { keys } = (pushList as ILeafList)
31
+ pushAllParent(leaf: ILeaf, leafList: ILeafList | ILeafLevelList): void {
32
+ const { keys } = (leafList as ILeafList)
35
33
  if (keys) {
36
34
  while (leaf.parent) {
37
35
  if (keys[leaf.parent.innerId] === undefined) {
38
- pushList.push(leaf.parent)
36
+ leafList.add(leaf.parent)
39
37
  leaf = leaf.parent
40
38
  } else {
41
39
  break
@@ -43,7 +41,7 @@ export const BranchHelper = {
43
41
  }
44
42
  } else {
45
43
  while (leaf.parent) {
46
- pushList.push(leaf.parent)
44
+ leafList.add(leaf.parent)
47
45
  leaf = leaf.parent
48
46
  }
49
47
  }
@@ -64,16 +62,24 @@ export const BranchHelper = {
64
62
  }
65
63
  },
66
64
 
67
- updateWorldBoundsByBranchStack(branchStack: ILeaf[]): void {
68
- let branch: ILeaf
65
+ updateBounds(branch: ILeaf, exclude?: ILeaf): void {
66
+ const branchStack: ILeaf[] = [branch]
67
+ pushAllBranchStack(branch, branchStack)
68
+ updateBoundsByBranchStack(branchStack, exclude)
69
+ },
70
+
71
+ updateBoundsByBranchStack(branchStack: ILeaf[], exclude?: ILeaf): void {
72
+ let branch: ILeaf, children: ILeaf[]
69
73
  for (let i = branchStack.length - 1; i > -1; i--) {
70
74
  branch = branchStack[i]
71
- for (let j = 0, len = branch.children.length; j < len; j++) {
72
- branch.children[j].__updateWorldBounds()
75
+ children = branch.children
76
+ for (let j = 0, len = children.length; j < len; j++) {
77
+ updateBounds(children[j])
73
78
  }
79
+ if (exclude && exclude === branch) continue
80
+ updateBounds(branch)
74
81
  }
75
- branch.__updateWorldBounds()
76
82
  }
77
83
  }
78
84
 
79
- const { pushAllChildBranch, pushAllBranchStack } = BranchHelper
85
+ const { pushAllChildBranch, pushAllBranchStack, updateBoundsByBranchStack } = BranchHelper
@@ -1,4 +1,4 @@
1
- import { ILeaf, IBoundsData } from '@leafer/interface'
1
+ import { ILeaf, IBoundsData, IRenderOptions } from '@leafer/interface'
2
2
 
3
3
 
4
4
  export const LeafBoundsHelper = {
@@ -8,27 +8,33 @@ export const LeafBoundsHelper = {
8
8
  },
9
9
 
10
10
  localBoxBounds(target: ILeaf): IBoundsData {
11
- return target.__.isEraser ? null : target.__local
11
+ return target.__.eraser ? null : (target.__local || target.__layout)
12
12
  },
13
13
 
14
- localEventBounds(target: ILeaf): IBoundsData {
15
- return target.__.isEraser ? null : target.__layout.localStrokeBounds
14
+ localStrokeBounds(target: ILeaf): IBoundsData {
15
+ return target.__.eraser ? null : target.__layout.localStrokeBounds
16
16
  },
17
17
 
18
18
  localRenderBounds(target: ILeaf): IBoundsData {
19
- return target.__.isEraser ? null : target.__layout.localRenderBounds
19
+ return target.__.eraser ? null : target.__layout.localRenderBounds
20
20
  },
21
21
 
22
22
  maskLocalBoxBounds(target: ILeaf): IBoundsData {
23
- return target.__.isMask ? target.__local : null
23
+ return target.__.mask ? target.__localBoxBounds : null
24
24
  },
25
25
 
26
- maskLocalEventBounds(target: ILeaf): IBoundsData {
27
- return target.__.isMask ? target.__layout.localStrokeBounds : null
26
+ maskLocalStrokeBounds(target: ILeaf): IBoundsData {
27
+ return target.__.mask ? target.__layout.localStrokeBounds : null
28
28
  },
29
29
 
30
30
  maskLocalRenderBounds(target: ILeaf): IBoundsData {
31
- return target.__.isMask ? target.__layout.localRenderBounds : null
31
+ return target.__.mask ? target.__layout.localRenderBounds : null
32
+ },
33
+
34
+ excludeRenderBounds(child: ILeaf, options: IRenderOptions): boolean {
35
+ if (options.bounds && !options.bounds.hit(child.__world, options.matrix)) return true
36
+ if (options.hideBounds && options.hideBounds.includes(child.__world, options.matrix)) return true
37
+ return false
32
38
  }
33
39
 
34
40
  }
package/src/LeafHelper.ts CHANGED
@@ -1,20 +1,47 @@
1
- import { IBranch, ILeaf, IPointData } from '@leafer/interface'
2
- import { Matrix, MatrixHelper, PointHelper } from '@leafer/math'
1
+ import { ILeaf, IMatrixData, IPointData } from '@leafer/interface'
2
+ import { MathHelper, MatrixHelper, PointHelper } from '@leafer/math'
3
3
 
4
4
 
5
+ const { copy, toInnerPoint, scaleOfOuter, rotateOfOuter, skewOfOuter, multiplyParent, divideParent, getLayout } = MatrixHelper
6
+ const matrix = {} as IMatrixData
7
+
5
8
  export const LeafHelper = {
6
9
 
7
- updateAllWorldMatrix(leaf: ILeaf): void {
8
- leaf.__updateWorldMatrix()
10
+ updateAllMatrix(leaf: ILeaf, checkAutoLayout?: boolean, waitAutoLayout?: boolean): void {
11
+ if (checkAutoLayout && leaf.__hasAutoLayout && leaf.__layout.matrixChanged) waitAutoLayout = true
12
+
13
+ updateMatrix(leaf, checkAutoLayout, waitAutoLayout)
9
14
 
10
15
  if (leaf.isBranch) {
11
16
  const { children } = leaf
12
17
  for (let i = 0, len = children.length; i < len; i++) {
13
- updateAllWorldMatrix(children[i])
18
+ updateAllMatrix(children[i], checkAutoLayout, waitAutoLayout)
14
19
  }
15
20
  }
16
21
  },
17
22
 
23
+ updateMatrix(leaf: ILeaf, checkAutoLayout?: boolean, waitAutoLayout?: boolean): void {
24
+ const layout = leaf.__layout
25
+
26
+ if (checkAutoLayout) {
27
+ if (waitAutoLayout) {
28
+ layout.waitAutoLayout = true
29
+ if (leaf.__hasAutoLayout) layout.matrixChanged = false // wait updateAutoLayout
30
+ }
31
+ } else if (layout.waitAutoLayout) {
32
+ layout.waitAutoLayout = false
33
+ }
34
+
35
+ if (layout.matrixChanged) leaf.__updateLocalMatrix()
36
+ if (!layout.waitAutoLayout) leaf.__updateWorldMatrix()
37
+ },
38
+
39
+ updateBounds(leaf: ILeaf): void {
40
+ const layout = leaf.__layout
41
+ if (layout.boundsChanged) leaf.__updateLocalBounds()
42
+ if (!layout.waitAutoLayout) leaf.__updateWorldBounds()
43
+ },
44
+
18
45
  updateAllWorldOpacity(leaf: ILeaf): void {
19
46
  leaf.__updateWorldOpacity()
20
47
 
@@ -27,7 +54,6 @@ export const LeafHelper = {
27
54
  },
28
55
 
29
56
  updateAllChange(leaf: ILeaf): void {
30
-
31
57
  updateAllWorldOpacity(leaf)
32
58
 
33
59
  leaf.__updateChange()
@@ -41,11 +67,9 @@ export const LeafHelper = {
41
67
  },
42
68
 
43
69
  worldHittable(t: ILeaf): boolean {
44
- if (!t.__.hittable) return false
45
- let { parent } = t
46
- while (parent) {
47
- if (!parent.__.hittable || !parent.__.hitChildren) return false
48
- parent = parent.parent
70
+ while (t) {
71
+ if (!t.__.hittable) return false
72
+ t = t.parent
49
73
  }
50
74
  return true
51
75
  },
@@ -54,53 +78,105 @@ export const LeafHelper = {
54
78
 
55
79
  moveWorld(t: ILeaf, x: number, y: number): void {
56
80
  const local = { x, y }
57
- if (t.parent) MatrixHelper.toInnerPoint(t.parent.__world, local, local, true)
81
+ if (t.parent) toInnerPoint(t.parent.worldTransform, local, local, true)
58
82
  L.moveLocal(t, local.x, local.y)
59
83
  },
60
84
 
61
85
  moveLocal(t: ILeaf, x: number, y: number = 0): void {
62
- t.x = t.__.x + x
63
- t.y = t.__.y + y
86
+ t.x += x
87
+ t.y += y
88
+ },
89
+
90
+ zoomOfWorld(t: ILeaf, origin: IPointData, scaleX: number, scaleY?: number, resize?: boolean): void {
91
+ L.zoomOfLocal(t, getTempLocal(t, origin), scaleX, scaleY, resize)
92
+ },
93
+
94
+ zoomOfLocal(t: ILeaf, origin: IPointData, scaleX: number, scaleY: number = scaleX, resize?: boolean): void {
95
+ copy(matrix, t.__localMatrix)
96
+ scaleOfOuter(matrix, origin, scaleX, scaleY)
97
+ moveByMatrix(t, matrix)
98
+ t.scaleResize(scaleX, scaleY, resize !== true)
99
+ },
100
+
101
+ rotateOfWorld(t: ILeaf, origin: IPointData, angle: number): void {
102
+ L.rotateOfLocal(t, getTempLocal(t, origin), angle)
64
103
  },
65
104
 
66
- zoomOfWorld(t: ILeaf, origin: IPointData, scaleX: number, scaleY?: number, moveLayer?: ILeaf): void {
67
- const local = t.parent ? PointHelper.tempToInnerOf(origin, t.parent.__world) : origin
68
- this.zoomOfLocal(t, local, scaleX, scaleY, moveLayer)
105
+ rotateOfLocal(t: ILeaf, origin: IPointData, angle: number): void {
106
+ copy(matrix, t.__localMatrix)
107
+ rotateOfOuter(matrix, origin, angle)
108
+ moveByMatrix(t, matrix)
109
+ t.rotation = MathHelper.formatRotation(t.rotation + angle)
69
110
  },
70
111
 
71
- zoomOfLocal(t: ILeaf, origin: IPointData, scaleX: number, scaleY?: number, moveLayer?: ILeaf): void {
72
- if (!scaleY) scaleY = scaleX
73
- if (!moveLayer) moveLayer = t
74
- const { x, y } = moveLayer.__
75
- const matrix = new Matrix().translate(x, y).scaleOfOuter(origin, scaleX, scaleY)
76
- moveLayer.x = matrix.e
77
- moveLayer.y = matrix.f
78
- t.scaleX = t.__.scaleX * scaleX
79
- t.scaleY = t.__.scaleY * scaleY
112
+ skewOfWorld(t: ILeaf, origin: IPointData, skewX: number, skewY?: number, resize?: boolean): void {
113
+ L.skewOfLocal(t, getTempLocal(t, origin), skewX, skewY, resize)
80
114
  },
81
115
 
82
- rotateOfWorld(t: ILeaf, origin: IPointData, angle: number, moveLayer?: ILeaf): void {
83
- const local = t.parent ? PointHelper.tempToInnerOf(origin, t.parent.__world) : origin
84
- this.rotateOfLocal(t, local, angle, moveLayer)
116
+ skewOfLocal(t: ILeaf, origin: IPointData, skewX: number, skewY: number = 0, resize?: boolean): void {
117
+ copy(matrix, t.__localMatrix)
118
+ skewOfOuter(matrix, origin, skewX, skewY)
119
+ L.setTransform(t, matrix, resize)
85
120
  },
86
121
 
87
- rotateOfLocal(t: ILeaf, origin: IPointData, angle: number, moveLayer?: ILeaf): void {
88
- if (!moveLayer) moveLayer = t
89
- const { x, y } = moveLayer.__
90
- const matrix = new Matrix().translate(x, y).rotateOfOuter(origin, angle)
91
- moveLayer.x = matrix.e
92
- moveLayer.y = matrix.f
93
- t.rotation = t.__.rotation + angle
122
+ transformWorld(t: ILeaf, transform: IMatrixData, resize?: boolean): void {
123
+ copy(matrix, t.worldTransform)
124
+ multiplyParent(matrix, transform)
125
+ if (t.parent) divideParent(matrix, t.parent.worldTransform)
126
+ L.setTransform(t, matrix, resize)
94
127
  },
95
128
 
96
- drop(t: ILeaf, parent: IBranch): void {
97
- const position = { x: t.x, y: t.y }
98
- t.localToWorld(position)
99
- parent.worldToInner(position)
100
- t.set(position)
101
- parent.add(t)
129
+ transform(t: ILeaf, transform: IMatrixData, resize?: boolean): void {
130
+ copy(matrix, t.localTransform)
131
+ multiplyParent(matrix, transform)
132
+ L.setTransform(t, matrix, resize)
133
+ },
134
+
135
+ setTransform(t: ILeaf, transform: IMatrixData, resize?: boolean): void {
136
+ const layout = getLayout(transform)
137
+ if (resize) {
138
+ t.scaleResize(layout.scaleX / t.scaleX, layout.scaleY / t.scaleY, resize !== true)
139
+ delete layout.scaleX
140
+ delete layout.scaleY
141
+ }
142
+ t.set(layout)
143
+ },
144
+
145
+
146
+ drop(t: ILeaf, parent: ILeaf, index?: number, resize?: boolean): void {
147
+ copy(matrix, t.worldTransform)
148
+ divideParent(matrix, parent.worldTransform)
149
+ t.setTransform(matrix, resize)
150
+ parent.add(t, index)
151
+ },
152
+
153
+ hasParent(p: ILeaf, parent: ILeaf): boolean | void {
154
+ if (!parent) return false
155
+ while (p) {
156
+ if (parent === p) return true
157
+ p = p.parent
158
+ }
159
+ },
160
+
161
+ hasParentAutoLayout(p: ILeaf): boolean | void {
162
+ while (p.parent) {
163
+ p = p.parent
164
+ if (p.__hasAutoLayout) return true
165
+ }
102
166
  }
103
167
 
104
168
  }
169
+
105
170
  const L = LeafHelper
106
- const { updateAllWorldMatrix, updateAllWorldOpacity, updateAllChange } = L
171
+ const { updateAllMatrix, updateMatrix, updateAllWorldOpacity, updateAllChange } = L
172
+
173
+ function moveByMatrix(t: ILeaf, matrix: IMatrixData): void {
174
+ const { e, f } = t.__localMatrix
175
+ t.x += matrix.e - e
176
+ t.y += matrix.f - f
177
+ }
178
+
179
+ function getTempLocal(t: ILeaf, world: IPointData): IPointData {
180
+ t.__layout.update()
181
+ return t.parent ? PointHelper.tempToInnerOf(world, t.parent.__world) : world
182
+ }
package/src/WaitHelper.ts CHANGED
@@ -2,7 +2,10 @@ import { IFunction } from '@leafer/interface'
2
2
 
3
3
  export const WaitHelper = {
4
4
  run(wait: IFunction[]): void {
5
- for (let i = 0, len = wait.length; i < len; i++) { wait[i]() }
6
- wait.length = 0
5
+ if (wait.length) {
6
+ const len = wait.length
7
+ for (let i = 0; i < len; i++) { wait[i]() }
8
+ wait.length === len ? wait.length = 0 : wait.splice(0, len)
9
+ }
7
10
  }
8
11
  }
@@ -0,0 +1,50 @@
1
+ import { ILeaf, IPointData, IMatrixData, IBoundsData, IRenderOptions, ILeafList, ILeafLevelList, IFunction } from '@leafer/interface';
2
+
3
+ declare const LeafHelper: {
4
+ updateAllMatrix(leaf: ILeaf, checkAutoLayout?: boolean, waitAutoLayout?: boolean): void;
5
+ updateMatrix(leaf: ILeaf, checkAutoLayout?: boolean, waitAutoLayout?: boolean): void;
6
+ updateBounds(leaf: ILeaf): void;
7
+ updateAllWorldOpacity(leaf: ILeaf): void;
8
+ updateAllChange(leaf: ILeaf): void;
9
+ worldHittable(t: ILeaf): boolean;
10
+ moveWorld(t: ILeaf, x: number, y: number): void;
11
+ moveLocal(t: ILeaf, x: number, y?: number): void;
12
+ zoomOfWorld(t: ILeaf, origin: IPointData, scaleX: number, scaleY?: number, resize?: boolean): void;
13
+ zoomOfLocal(t: ILeaf, origin: IPointData, scaleX: number, scaleY?: number, resize?: boolean): void;
14
+ rotateOfWorld(t: ILeaf, origin: IPointData, angle: number): void;
15
+ rotateOfLocal(t: ILeaf, origin: IPointData, angle: number): void;
16
+ skewOfWorld(t: ILeaf, origin: IPointData, skewX: number, skewY?: number, resize?: boolean): void;
17
+ skewOfLocal(t: ILeaf, origin: IPointData, skewX: number, skewY?: number, resize?: boolean): void;
18
+ transformWorld(t: ILeaf, transform: IMatrixData, resize?: boolean): void;
19
+ transform(t: ILeaf, transform: IMatrixData, resize?: boolean): void;
20
+ setTransform(t: ILeaf, transform: IMatrixData, resize?: boolean): void;
21
+ drop(t: ILeaf, parent: ILeaf, index?: number, resize?: boolean): void;
22
+ hasParent(p: ILeaf, parent: ILeaf): boolean | void;
23
+ hasParentAutoLayout(p: ILeaf): boolean | void;
24
+ };
25
+
26
+ declare const LeafBoundsHelper: {
27
+ worldBounds(target: ILeaf): IBoundsData;
28
+ localBoxBounds(target: ILeaf): IBoundsData;
29
+ localStrokeBounds(target: ILeaf): IBoundsData;
30
+ localRenderBounds(target: ILeaf): IBoundsData;
31
+ maskLocalBoxBounds(target: ILeaf): IBoundsData;
32
+ maskLocalStrokeBounds(target: ILeaf): IBoundsData;
33
+ maskLocalRenderBounds(target: ILeaf): IBoundsData;
34
+ excludeRenderBounds(child: ILeaf, options: IRenderOptions): boolean;
35
+ };
36
+
37
+ declare const BranchHelper: {
38
+ sort(a: ILeaf, b: ILeaf): number;
39
+ pushAllChildBranch(branch: ILeaf, leafList: ILeafList | ILeafLevelList): void;
40
+ pushAllParent(leaf: ILeaf, leafList: ILeafList | ILeafLevelList): void;
41
+ pushAllBranchStack(branch: ILeaf, pushList: ILeaf[]): void;
42
+ updateBounds(branch: ILeaf, exclude?: ILeaf): void;
43
+ updateBoundsByBranchStack(branchStack: ILeaf[], exclude?: ILeaf): void;
44
+ };
45
+
46
+ declare const WaitHelper: {
47
+ run(wait: IFunction[]): void;
48
+ };
49
+
50
+ export { BranchHelper, LeafBoundsHelper, LeafHelper, WaitHelper };