@leafer/helper 1.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/helper
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@leafer/helper",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/helper",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/helper",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/math": "1.0.0-alpha.1"
21
+ },
22
+ "devDependencies": {
23
+ "@leafer/interface": "1.0.0-alpha.1"
24
+ }
25
+ }
@@ -0,0 +1,79 @@
1
+ import { ILeaf, ILeafList } from '@leafer/interface'
2
+
3
+
4
+ interface ILeafPushList {
5
+ push(item: ILeaf): void
6
+ }
7
+
8
+
9
+ export const BranchHelper = {
10
+
11
+ sort(a: ILeaf, b: ILeaf): number {
12
+ return (a.__.zIndex === b.__.zIndex) ? (a.__tempNumber - b.__tempNumber) : (a.__.zIndex - b.__.zIndex)
13
+ },
14
+
15
+ // push
16
+
17
+ pushAllChildBranch(branch: ILeaf, pushList: ILeafPushList): void {
18
+ branch.__tempNumber = 1 // 标识需要更新子Leaf元素的WorldBounds分支 Layouter需使用
19
+
20
+ if (branch.__childBranchNumber) {
21
+ const { children } = branch
22
+ for (let i = 0, len = children.length; i < len; i++) {
23
+ branch = children[i]
24
+ if (branch.__isBranch) {
25
+ branch.__tempNumber = 1
26
+ pushList.push(branch)
27
+ pushAllChildBranch(branch, pushList)
28
+ }
29
+ }
30
+ }
31
+ },
32
+
33
+ pushAllParent(leaf: ILeaf, pushList: ILeafPushList): void {
34
+ const { keys } = (pushList as ILeafList)
35
+ if (keys) {
36
+ while (leaf.parent) {
37
+ if (keys[leaf.parent.innerId] === undefined) {
38
+ pushList.push(leaf.parent)
39
+ leaf = leaf.parent
40
+ } else {
41
+ break
42
+ }
43
+ }
44
+ } else {
45
+ while (leaf.parent) {
46
+ pushList.push(leaf.parent)
47
+ leaf = leaf.parent
48
+ }
49
+ }
50
+ },
51
+
52
+ pushAllBranchStack(branch: ILeaf, pushList: ILeaf[]): void {
53
+ let start = pushList.length
54
+ const { children } = branch
55
+ for (let i = 0, len = children.length; i < len; i++) {
56
+ if (children[i].__isBranch) {
57
+ pushList.push(children[i])
58
+ }
59
+ }
60
+
61
+ // 找子级
62
+ for (let i = start, len = pushList.length; i < len; i++) {
63
+ pushAllBranchStack(pushList[i], pushList)
64
+ }
65
+ },
66
+
67
+ updateWorldBoundsByBranchStack(branchStack: ILeaf[]): void {
68
+ let branch: ILeaf
69
+ for (let i = branchStack.length - 1; i > -1; i--) {
70
+ branch = branchStack[i]
71
+ for (let j = 0, len = branch.children.length; j < len; j++) {
72
+ branch.children[j].__updateWorldBounds()
73
+ }
74
+ }
75
+ branch.__updateWorldBounds()
76
+ }
77
+ }
78
+
79
+ const { pushAllChildBranch, pushAllBranchStack } = BranchHelper
@@ -0,0 +1,22 @@
1
+ import { ILeaf, IBoundsData } from '@leafer/interface'
2
+
3
+
4
+ export const LeafBoundsHelper = {
5
+
6
+ worldBounds(target: ILeaf): IBoundsData {
7
+ return target.__world
8
+ },
9
+
10
+ relativeBoxBounds(target: ILeaf): IBoundsData {
11
+ return target.__relative
12
+ },
13
+
14
+ relativeEventBounds(target: ILeaf): IBoundsData {
15
+ return target.__layout.relativeEventBounds
16
+ },
17
+
18
+ relativeRenderBounds(target: ILeaf): IBoundsData {
19
+ return target.__layout.relativeRenderBounds
20
+ }
21
+
22
+ }
@@ -0,0 +1,94 @@
1
+ import { ILeaf, IPointData } from '@leafer/interface'
2
+ import { Matrix, MatrixHelper, PointHelper } from '@leafer/math'
3
+
4
+
5
+ export const LeafHelper = {
6
+
7
+ updateAllWorldMatrix(leaf: ILeaf): void {
8
+ leaf.__updateWorldMatrix()
9
+
10
+ if (leaf.__isBranch) {
11
+ const { children } = leaf
12
+ for (let i = 0, len = children.length; i < len; i++) {
13
+ updateAllWorldMatrix(children[i])
14
+ }
15
+ }
16
+ },
17
+
18
+ updateAllWorldOpacity(leaf: ILeaf): void {
19
+ leaf.__updateWorldOpacity()
20
+
21
+ if (leaf.__isBranch) {
22
+ const { children } = leaf
23
+ for (let i = 0, len = children.length; i < len; i++) {
24
+ updateAllWorldOpacity(children[i])
25
+ }
26
+ }
27
+ },
28
+
29
+ updateAllChange(leaf: ILeaf): void {
30
+
31
+ updateAllWorldOpacity(leaf)
32
+
33
+ leaf.__updateChange()
34
+
35
+ if (leaf.__isBranch) {
36
+ const { children } = leaf
37
+ for (let i = 0, len = children.length; i < len; i++) {
38
+ updateAllChange(children[i])
39
+ }
40
+ }
41
+ },
42
+
43
+
44
+ // transform
45
+
46
+ moveByWorld(t: ILeaf, worldX: number, worldY: number): void {
47
+ const local = { x: worldX, y: worldY }
48
+ if (t.parent) MatrixHelper.toLocalPoint(t.parent.__world, local, local, true)
49
+ L.move(t, local.x, local.y)
50
+ },
51
+
52
+ move(t: ILeaf, x: number, y: number): void {
53
+ t.x = t.__.x + x
54
+ t.y = t.__.y + y
55
+ },
56
+
57
+ zoomByWorld(t: ILeaf, scale: number, world: IPointData, moveLayer?: ILeaf): void {
58
+ const local = t.parent ? PointHelper.tempToLocal(world, t.parent.__world) : world
59
+ this.zoom(t, scale, local, moveLayer)
60
+ },
61
+
62
+ zoom(t: ILeaf, scale: number, local: IPointData, moveLayer?: ILeaf): void {
63
+ if (!moveLayer) moveLayer = t
64
+ const { x, y } = moveLayer.__
65
+ const { scaleX, scaleY } = t.__
66
+ const centerX = local.x - x
67
+ const centerY = local.y - y
68
+ const matrix = new Matrix().translate(centerX, centerY).scale(scale).translate(-centerX, -centerY)
69
+ moveLayer.x = x - matrix.e
70
+ moveLayer.y = y - matrix.f
71
+ t.scaleX = scaleX * scale
72
+ t.scaleY = scaleY * scale
73
+ },
74
+
75
+ rotateByWorld(t: ILeaf, angle: number, world: IPointData, moveLayer?: ILeaf): void {
76
+ const local = t.parent ? PointHelper.tempToLocal(world, t.parent.__world) : world
77
+ this.rotate(t, angle, local, moveLayer)
78
+ },
79
+
80
+ rotate(t: ILeaf, angle: number, local: IPointData, moveLayer?: ILeaf): void {
81
+ if (!moveLayer) moveLayer = t
82
+ const { x, y } = moveLayer.__
83
+ const { rotation } = t.__
84
+ const centerX = local.x - x
85
+ const centerY = local.y - y
86
+ const matrix = new Matrix().translate(centerX, centerY).rotate(angle).translate(-centerX, -centerY)
87
+ moveLayer.x = x - matrix.e
88
+ moveLayer.y = y - matrix.f
89
+ t.rotation = rotation + angle
90
+ }
91
+
92
+ }
93
+ const L = LeafHelper
94
+ const { updateAllWorldMatrix, updateAllWorldOpacity, updateAllChange } = L
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { LeafHelper } from './LeafHelper'
2
+ export { LeafBoundsHelper } from './LeafBoundsHelper'
3
+ export { BranchHelper } from './BranchHelper'