@leafer/layout 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 +21 -0
- package/README.md +1 -0
- package/package.json +25 -0
- package/src/LeafLayout.ts +234 -0
- package/src/index.ts +1 -0
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/layout
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leafer/layout",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "@leafer/layout",
|
|
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/layout",
|
|
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,234 @@
|
|
|
1
|
+
import { ILeaf, ILeafLayout, ILayoutLocationType, ILayoutBoundsType, IBoundsData, IMatrixData, } from '@leafer/interface'
|
|
2
|
+
import { BoundsHelper } from '@leafer/math'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const { setByBoundsTimesMatrix } = BoundsHelper
|
|
6
|
+
|
|
7
|
+
export class LeafLayout implements ILeafLayout {
|
|
8
|
+
|
|
9
|
+
public leaf: ILeaf
|
|
10
|
+
|
|
11
|
+
public useZoomProxy: boolean
|
|
12
|
+
|
|
13
|
+
// local
|
|
14
|
+
|
|
15
|
+
public boxBounds: IBoundsData // | content + padding |
|
|
16
|
+
public eventBounds: IBoundsData // | boxBounds + border |
|
|
17
|
+
public renderBounds: IBoundsData // | eventBounds + shadow |
|
|
18
|
+
|
|
19
|
+
// auto layout
|
|
20
|
+
public marginBounds: IBoundsData // | eventBounds + margin |
|
|
21
|
+
public contentBounds: IBoundsData // | content |
|
|
22
|
+
|
|
23
|
+
// relative
|
|
24
|
+
|
|
25
|
+
//relativeBoxBounds: IBoundsData = leaf.__relative
|
|
26
|
+
public relativeEventBounds: IBoundsData
|
|
27
|
+
public relativeRenderBounds: IBoundsData
|
|
28
|
+
|
|
29
|
+
// world temp
|
|
30
|
+
protected _worldBoxBounds: IBoundsData
|
|
31
|
+
protected _worldEventBounds: IBoundsData
|
|
32
|
+
// worldRenderBounds: IBoundsData = leaf.__world
|
|
33
|
+
|
|
34
|
+
// state
|
|
35
|
+
|
|
36
|
+
// matrix changed
|
|
37
|
+
public matrixChanged: boolean // include positionChanged scaleChanged skewChanged
|
|
38
|
+
public positionChanged: boolean // x, y
|
|
39
|
+
public scaleChanged: boolean // scaleX scaleY
|
|
40
|
+
public rotationChanged: boolean // rotaiton, skewX scaleY
|
|
41
|
+
|
|
42
|
+
// bounds
|
|
43
|
+
public boundsChanged: boolean
|
|
44
|
+
|
|
45
|
+
public boxBoundsChanged: boolean
|
|
46
|
+
public eventBoundsChanged: boolean
|
|
47
|
+
public renderBoundsChanged: boolean
|
|
48
|
+
|
|
49
|
+
public localBoxBoundsChanged: boolean
|
|
50
|
+
|
|
51
|
+
// face
|
|
52
|
+
public surfaceChanged: boolean
|
|
53
|
+
public opacityChanged: boolean
|
|
54
|
+
|
|
55
|
+
public hitCanvasChanged: boolean
|
|
56
|
+
|
|
57
|
+
public childrenSortChanged?: boolean
|
|
58
|
+
|
|
59
|
+
// keep state
|
|
60
|
+
public affectScaleOrRotation: boolean
|
|
61
|
+
public affectRotation: boolean
|
|
62
|
+
public eventBoundsSpreadWidth: number
|
|
63
|
+
public renderBoundsSpreadWidth: number
|
|
64
|
+
public renderShapeBoundsSpreadWidth: number
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
constructor(leaf: ILeaf) {
|
|
68
|
+
this.leaf = leaf
|
|
69
|
+
this.renderBounds = this.eventBounds = this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
|
|
70
|
+
this.relativeRenderBounds = this.relativeEventBounds = leaf.__relative
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
public update(): void {
|
|
75
|
+
const { leafer } = this.leaf
|
|
76
|
+
if (leafer && leafer.watcher.changed) {
|
|
77
|
+
if (!leafer.running) leafer.start()
|
|
78
|
+
leafer.layouter.layout()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public getTransform(type: ILayoutLocationType): IMatrixData {
|
|
83
|
+
this.update()
|
|
84
|
+
return type === 'world' ? this.leaf.__world : this.leaf.__relative
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public getBounds(type: ILayoutLocationType, boundsType: ILayoutBoundsType): IBoundsData {
|
|
88
|
+
|
|
89
|
+
this.update()
|
|
90
|
+
|
|
91
|
+
if (type === 'world') {
|
|
92
|
+
|
|
93
|
+
switch (boundsType) {
|
|
94
|
+
case 'render':
|
|
95
|
+
return this.leaf.__world
|
|
96
|
+
case 'box':
|
|
97
|
+
return this.getWorldBoxBounds()
|
|
98
|
+
case 'event':
|
|
99
|
+
return this.getWorldEventBounds()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
} else if (type === 'local') {
|
|
103
|
+
|
|
104
|
+
switch (boundsType) {
|
|
105
|
+
case 'render':
|
|
106
|
+
return this.renderBounds
|
|
107
|
+
case 'box':
|
|
108
|
+
return this.boxBounds
|
|
109
|
+
case 'event':
|
|
110
|
+
return this.eventBounds
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
} else {
|
|
114
|
+
|
|
115
|
+
switch (boundsType) {
|
|
116
|
+
case 'render':
|
|
117
|
+
return this.relativeRenderBounds
|
|
118
|
+
case 'box':
|
|
119
|
+
return this.leaf.__relative
|
|
120
|
+
case 'event':
|
|
121
|
+
return this.relativeEventBounds
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
return this.leaf.__world
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
protected getWorldBoxBounds(): IBoundsData {
|
|
130
|
+
this._worldBoxBounds || (this._worldBoxBounds = {} as IBoundsData)
|
|
131
|
+
setByBoundsTimesMatrix(this._worldBoxBounds, this.boxBounds, this.leaf.__world)
|
|
132
|
+
return this._worldBoxBounds
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
protected getWorldEventBounds(): IBoundsData {
|
|
136
|
+
this._worldEventBounds || (this._worldEventBounds = {} as IBoundsData)
|
|
137
|
+
setByBoundsTimesMatrix(this._worldEventBounds, this.eventBounds, this.leaf.__world)
|
|
138
|
+
return this._worldEventBounds
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 独立 / 引用 boxBounds
|
|
142
|
+
|
|
143
|
+
public eventBoundsSpreadCancel(): void {
|
|
144
|
+
const same = this.renderBounds === this.eventBounds
|
|
145
|
+
this.eventBounds = this.boxBounds
|
|
146
|
+
this.relativeEventBounds = this.leaf.__relative
|
|
147
|
+
if (same) this.renderBoundsSpreadCancel()
|
|
148
|
+
}
|
|
149
|
+
public renderBoundsSpreadCancel(): void {
|
|
150
|
+
this.renderBounds = this.eventBounds
|
|
151
|
+
this.relativeRenderBounds = this.relativeEventBounds
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public eventBoundsSpread(): void {
|
|
155
|
+
const { x, y, width, height } = this.eventBounds
|
|
156
|
+
this.eventBounds = { x, y, width, height }
|
|
157
|
+
this.relativeEventBounds = { x, y, width, height }
|
|
158
|
+
if (!this.renderBoundsSpreadWidth) this.renderBoundsSpreadCancel()
|
|
159
|
+
}
|
|
160
|
+
public renderBoundsSpread(): void {
|
|
161
|
+
const { x, y, width, height } = this.renderBounds
|
|
162
|
+
this.renderBounds = { x, y, width, height }
|
|
163
|
+
this.relativeRenderBounds = { x, y, width, height }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
// bounds
|
|
168
|
+
|
|
169
|
+
public boxBoundsChange(): void {
|
|
170
|
+
this.boxBoundsChanged = true
|
|
171
|
+
this.localBoxBoundsChanged || this.localBoxBoundsChange()
|
|
172
|
+
this.hitCanvasChanged || (this.hitCanvasChanged = true)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public localBoxBoundsChange(): void {
|
|
176
|
+
this.localBoxBoundsChanged = true
|
|
177
|
+
this.boundsChanged || (this.boundsChanged = true)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public eventBoundsChange(): void {
|
|
181
|
+
this.eventBoundsChanged = true
|
|
182
|
+
this.eventBoundsSpreadWidth || (this.eventBoundsSpreadWidth = 1)
|
|
183
|
+
this.boundsChanged || (this.boundsChanged = true)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public renderBoundsChange(): void {
|
|
187
|
+
this.renderBoundsChanged = true
|
|
188
|
+
this.renderBoundsSpreadWidth || (this.renderBoundsSpreadWidth = 1)
|
|
189
|
+
this.boundsChanged || (this.boundsChanged = true)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
// matrix
|
|
194
|
+
|
|
195
|
+
public positionChange(): void {
|
|
196
|
+
this.positionChanged = true
|
|
197
|
+
this.matrixChanged || (this.matrixChanged = true)
|
|
198
|
+
this.localBoxBoundsChanged || this.localBoxBoundsChange()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public scaleChange(): void {
|
|
202
|
+
this.scaleChanged = true
|
|
203
|
+
this._scaleOrRotationChange()
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public rotationChange(): void {
|
|
207
|
+
this.rotationChanged = true
|
|
208
|
+
this.affectRotation || (this.affectRotation = true)
|
|
209
|
+
this._scaleOrRotationChange()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
protected _scaleOrRotationChange() {
|
|
213
|
+
this.affectScaleOrRotation || (this.affectScaleOrRotation = true)
|
|
214
|
+
this.matrixChanged || (this.matrixChanged = true)
|
|
215
|
+
this.localBoxBoundsChanged || this.localBoxBoundsChange()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
// face
|
|
220
|
+
|
|
221
|
+
public surfaceChange(): void {
|
|
222
|
+
this.surfaceChanged = true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public opacityChange(): void {
|
|
226
|
+
this.opacityChanged = true
|
|
227
|
+
this.surfaceChanged || this.surfaceChange()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public destroy(): void {
|
|
231
|
+
this.leaf = undefined
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { LeafLayout } from "./LeafLayout"
|