@leafer/layout 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,28 +1,31 @@
1
1
  {
2
2
  "name": "@leafer/layout",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-rc.10",
4
4
  "description": "@leafer/layout",
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/layout",
18
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/display-module/layout",
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",
23
- "@leafer/platform": "1.0.0-beta.9"
25
+ "@leafer/math": "1.0.0-rc.10",
26
+ "@leafer/platform": "1.0.0-rc.10"
24
27
  },
25
28
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-beta.9"
29
+ "@leafer/interface": "1.0.0-rc.10"
27
30
  }
28
31
  }
package/src/LeafLayout.ts CHANGED
@@ -1,21 +1,25 @@
1
- import { ILeaf, ILeafLayout, ILayoutLocationType, ILayoutBoundsType, IBoundsData, IMatrixData, IMatrixDecompositionData } from '@leafer/interface'
2
- import { BoundsHelper, MatrixHelper } from '@leafer/math'
1
+ import { ILeaf, ILeafLayout, ILocationType, IBoundsType, IBoundsData, IMatrixData, ILayoutBoundsData, IPointData } from '@leafer/interface'
2
+ import { Bounds, BoundsHelper, Matrix, MatrixHelper, PointHelper } from '@leafer/math'
3
3
  import { Platform } from '@leafer/platform'
4
4
 
5
5
 
6
- const { toOuterOf } = BoundsHelper
6
+ const tempMatrix = new Matrix()
7
+ const { toOuterOf, getPoints, copy } = BoundsHelper
7
8
 
8
9
  export class LeafLayout implements ILeafLayout {
9
10
 
10
11
  public leaf: ILeaf
11
12
 
12
- public useZoomProxy: boolean
13
+ public proxyZoom: boolean
13
14
 
14
- // local
15
+ // inner
15
16
 
16
17
  public boxBounds: IBoundsData
17
- public strokeBounds: IBoundsData
18
- public renderBounds: IBoundsData
18
+ public get strokeBounds(): IBoundsData { return this._strokeBounds || this.boxBounds }
19
+ public get renderBounds(): IBoundsData { return this._renderBounds || this.boxBounds }
20
+
21
+ public _strokeBounds: IBoundsData
22
+ public _renderBounds: IBoundsData
19
23
 
20
24
  // auto layout
21
25
  public marginBounds: IBoundsData
@@ -23,8 +27,11 @@ export class LeafLayout implements ILeafLayout {
23
27
 
24
28
  // local
25
29
 
26
- public localStrokeBounds: IBoundsData
27
- public localRenderBounds: IBoundsData
30
+ public get localStrokeBounds(): IBoundsData { return this._localStrokeBounds || this }
31
+ public get localRenderBounds(): IBoundsData { return this._localRenderBounds || this }
32
+
33
+ protected _localStrokeBounds?: IBoundsData
34
+ protected _localRenderBounds?: IBoundsData
28
35
 
29
36
  // world temp
30
37
  protected _worldContentBounds: IBoundsData
@@ -33,9 +40,11 @@ export class LeafLayout implements ILeafLayout {
33
40
 
34
41
  // state
35
42
 
43
+ public resized: boolean
44
+ public waitAutoLayout: boolean
45
+
36
46
  // matrix changed
37
47
  public matrixChanged: boolean
38
- public positionChanged: boolean
39
48
  public scaleChanged: boolean
40
49
  public rotationChanged: boolean
41
50
 
@@ -59,95 +68,184 @@ export class LeafLayout implements ILeafLayout {
59
68
  // keep state
60
69
  public affectScaleOrRotation: boolean
61
70
  public affectRotation: boolean
71
+ public affectChildrenSort?: boolean
62
72
 
63
73
  public strokeSpread: number
64
74
  public renderSpread: number
65
75
  public strokeBoxSpread: number
66
76
  public renderShapeSpread: number
67
77
 
78
+ // temp local
79
+ public get a() { return 1 }
80
+ public get b() { return 0 }
81
+ public get c() { return 0 }
82
+ public get d() { return 1 }
83
+ public get e() { return this.leaf.__.x }
84
+ public get f() { return this.leaf.__.y }
85
+ public get x() { return this.e + this.boxBounds.x }
86
+ public get y() { return this.f + this.boxBounds.y }
87
+ public get width() { return this.boxBounds.width }
88
+ public get height() { return this.boxBounds.height }
89
+
68
90
 
69
91
  constructor(leaf: ILeaf) {
70
92
  this.leaf = leaf
71
- this.renderBounds = this.strokeBounds = this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
72
- this.localRenderBounds = this.localStrokeBounds = leaf.__local
93
+ this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
94
+ if (this.leaf.__local) this._localRenderBounds = this._localStrokeBounds = this.leaf.__local
73
95
  this.boxChange()
74
- this.positionChange()
96
+ this.matrixChange()
75
97
  }
76
98
 
99
+ public createLocal(): void {
100
+ const local = this.leaf.__local = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
101
+ if (!this._localStrokeBounds) this._localStrokeBounds = local
102
+ if (!this._localRenderBounds) this._localRenderBounds = local
103
+ }
77
104
 
78
- public checkUpdate(force?: boolean): void {
105
+ public update(): void {
79
106
  const { leafer } = this.leaf
80
107
  if (leafer) {
81
108
  if (leafer.ready) {
82
- if ((Platform.realtimeLayout || force) && leafer.watcher.changed) leafer.layouter.layout()
109
+ if (leafer.watcher.changed) leafer.layouter.layout()
83
110
  } else {
84
111
  leafer.start()
85
112
  }
86
113
  } else {
87
114
  let root = this.leaf
88
- while (root.parent) { root = root.parent }
115
+ while (root.parent && !root.parent.leafer) { root = root.parent }
89
116
  Platform.layout(root)
90
117
  }
91
118
  }
92
119
 
93
- public getTransform(locationType: ILayoutLocationType): IMatrixData {
94
- this.checkUpdate()
95
- return locationType === 'world' ? this.leaf.__world : this.leaf.__local
120
+ public getTransform(relative: ILocationType | ILeaf = 'world'): IMatrixData {
121
+ this.update()
122
+ switch (relative) {
123
+ case 'world':
124
+ return this.leaf.__world
125
+ case 'local':
126
+ return this.leaf.__localMatrix
127
+ case 'inner':
128
+ return MatrixHelper.defaultMatrix
129
+ default:
130
+ return new Matrix(this.leaf.__world).divideParent(relative.__world)
131
+ }
96
132
  }
97
133
 
98
- public decomposeTransform(locationType: ILayoutLocationType): IMatrixDecompositionData {
99
- this.checkUpdate()
100
- return MatrixHelper.decompose(locationType === 'world' ? this.leaf.__world : this.leaf.__local)
134
+ public getBounds(type?: IBoundsType, relative: ILocationType | ILeaf = 'world'): IBoundsData {
135
+ this.update()
136
+ switch (relative) {
137
+ case 'world':
138
+ return this.getWorldBounds(type)
139
+ case 'local':
140
+ return this.getLocalBounds(type)
141
+ case 'inner':
142
+ return this.getInnerBounds(type)
143
+ default:
144
+ return new Bounds(this.getInnerBounds(type)).toOuterOf(this.getTransform(relative))
145
+ }
101
146
  }
102
147
 
103
- public getBounds(type: ILayoutBoundsType, locationType: ILayoutLocationType): IBoundsData {
148
+ public getInnerBounds(type: IBoundsType = 'box'): IBoundsData {
149
+ switch (type) {
150
+ case 'render':
151
+ return this.renderBounds
152
+ case 'content':
153
+ if (this.contentBounds) return this.contentBounds
154
+ case 'margin':
155
+ case 'box':
156
+ return this.boxBounds
157
+ case 'stroke':
158
+ return this.strokeBounds
159
+ }
160
+ }
104
161
 
105
- this.checkUpdate()
162
+ public getLocalBounds(type: IBoundsType = 'box'): IBoundsData {
163
+ switch (type) {
164
+ case 'render':
165
+ return this.localRenderBounds
166
+ case 'stroke':
167
+ return this.localStrokeBounds
168
+ case 'margin':
169
+ case 'content':
170
+ case 'box':
171
+ return this.leaf.__localBoxBounds
172
+ }
173
+ }
106
174
 
107
- if (locationType === 'world') {
175
+ public getWorldBounds(type: IBoundsType = 'box'): IBoundsData {
176
+ switch (type) {
177
+ case 'render':
178
+ return this.leaf.__world
179
+ case 'content':
180
+ if (this.contentBounds) return this.getWorldContentBounds()
181
+ case 'margin':
182
+ case 'box':
183
+ return this.getWorldBoxBounds()
184
+ case 'margin':
185
+ case 'stroke':
186
+ return this.getWorldStrokeBounds()
187
+ }
188
+ }
108
189
 
109
- switch (type) {
110
- case 'render':
111
- return this.leaf.__world
112
- case 'content':
113
- if (this.contentBounds) return this.getWorldContentBounds()
114
- case 'margin':
115
- case 'box':
116
- return this.getWorldBoxBounds()
117
- case 'margin':
118
- case 'stroke':
119
- return this.getWorldStrokeBounds()
120
- }
190
+ public getLayoutBounds(type?: IBoundsType, relative: ILocationType | ILeaf = 'world', unscale?: boolean): ILayoutBoundsData {
191
+ const { leaf } = this
192
+ let point: IPointData, matrix: IMatrixData, bounds: IBoundsData = this.getInnerBounds(type)
193
+
194
+ switch (relative) {
195
+ case 'world':
196
+ point = leaf.getWorldPoint(bounds)
197
+ matrix = leaf.__world
198
+ break
199
+ case 'local':
200
+ point = leaf.getLocalPointByInner(bounds)
201
+ matrix = leaf.__localMatrix
202
+ break
203
+ case 'inner':
204
+ point = bounds
205
+ matrix = MatrixHelper.defaultMatrix
206
+ break
207
+ default:
208
+ point = leaf.getWorldPoint(bounds, relative)
209
+ matrix = tempMatrix.set(leaf.__world).divideParent(relative.__world)
210
+ }
121
211
 
122
- } else if (locationType === 'inner') {
123
-
124
- switch (type) {
125
- case 'render':
126
- return this.renderBounds
127
- case 'content':
128
- if (this.contentBounds) return this.contentBounds
129
- case 'margin':
130
- case 'box':
131
- return this.boxBounds
132
- case 'stroke':
133
- return this.strokeBounds
212
+ const layoutBounds = MatrixHelper.getLayout(matrix) as ILayoutBoundsData
213
+ copy(layoutBounds, bounds)
214
+ PointHelper.copy(layoutBounds, point)
215
+
216
+ if (unscale) {
217
+ const { scaleX, scaleY } = layoutBounds
218
+ const uScaleX = Math.abs(scaleX)
219
+ const uScaleY = Math.abs(scaleY)
220
+ if (uScaleX !== 1 || uScaleY !== 1) {
221
+ layoutBounds.scaleX /= uScaleX
222
+ layoutBounds.scaleY /= uScaleY
223
+ layoutBounds.width *= uScaleX
224
+ layoutBounds.height *= uScaleY
134
225
  }
226
+ }
135
227
 
136
- } else {
137
-
138
- switch (type) {
139
- case 'render':
140
- return this.localRenderBounds
141
- case 'margin':
142
- case 'content':
143
- case 'box':
144
- return this.leaf.__local
145
- case 'stroke':
146
- return this.localStrokeBounds
147
- }
228
+ return layoutBounds
229
+ }
148
230
 
231
+ public getLayoutPoints(type?: IBoundsType, relative: ILocationType | ILeaf = 'world'): IPointData[] {
232
+ const { leaf } = this
233
+ const points = getPoints(this.getInnerBounds(type))
234
+ let relativeLeaf: ILeaf
235
+ switch (relative) {
236
+ case 'world':
237
+ relativeLeaf = null
238
+ break
239
+ case 'local':
240
+ relativeLeaf = leaf.parent
241
+ break
242
+ case 'inner':
243
+ break
244
+ default:
245
+ relativeLeaf = relative
149
246
  }
150
-
247
+ if (relativeLeaf !== undefined) points.forEach(point => leaf.innerToWorld(point, null, false, relativeLeaf))
248
+ return points
151
249
  }
152
250
 
153
251
  protected getWorldContentBounds(): IBoundsData {
@@ -172,25 +270,25 @@ export class LeafLayout implements ILeafLayout {
172
270
 
173
271
  public spreadStrokeCancel(): void {
174
272
  const same = this.renderBounds === this.strokeBounds
175
- this.strokeBounds = this.boxBounds
176
- this.localStrokeBounds = this.leaf.__local
273
+ this._strokeBounds = this.boxBounds
274
+ this._localStrokeBounds = this.leaf.__localBoxBounds
177
275
  if (same) this.spreadRenderCancel()
178
276
  }
179
277
  public spreadRenderCancel(): void {
180
- this.renderBounds = this.strokeBounds
181
- this.localRenderBounds = this.localStrokeBounds
278
+ this._renderBounds = this._strokeBounds
279
+ this._localRenderBounds = this._localStrokeBounds
182
280
  }
183
281
 
184
282
  public spreadStroke(): void {
185
283
  const { x, y, width, height } = this.strokeBounds
186
- this.strokeBounds = { x, y, width, height }
187
- this.localStrokeBounds = { x, y, width, height }
284
+ this._strokeBounds = { x, y, width, height }
285
+ this._localStrokeBounds = { x, y, width, height }
188
286
  if (!this.renderSpread) this.spreadRenderCancel()
189
287
  }
190
288
  public spreadRender(): void {
191
289
  const { x, y, width, height } = this.renderBounds
192
- this.renderBounds = { x, y, width, height }
193
- this.localRenderBounds = { x, y, width, height }
290
+ this._renderBounds = { x, y, width, height }
291
+ this._localRenderBounds = { x, y, width, height }
194
292
  }
195
293
 
196
294
 
@@ -223,12 +321,6 @@ export class LeafLayout implements ILeafLayout {
223
321
 
224
322
  // matrix
225
323
 
226
- public positionChange(): void {
227
- this.positionChanged = true
228
- this.matrixChanged = true
229
- this.localBoxChanged || this.localBoxChange()
230
- }
231
-
232
324
  public scaleChange(): void {
233
325
  this.scaleChanged = true
234
326
  this._scaleOrRotationChange()
@@ -242,6 +334,11 @@ export class LeafLayout implements ILeafLayout {
242
334
 
243
335
  protected _scaleOrRotationChange() {
244
336
  this.affectScaleOrRotation = true
337
+ this.matrixChange()
338
+ if (!this.leaf.__local) this.createLocal()
339
+ }
340
+
341
+ public matrixChange(): void {
245
342
  this.matrixChanged = true
246
343
  this.localBoxChanged || this.localBoxChange()
247
344
  }
@@ -258,8 +355,13 @@ export class LeafLayout implements ILeafLayout {
258
355
  this.surfaceChanged || this.surfaceChange()
259
356
  }
260
357
 
261
- public destroy(): void {
262
- this.leaf = null
358
+ public childrenSortChange(): void {
359
+ if (!this.childrenSortChanged) {
360
+ this.childrenSortChanged = true
361
+ this.leaf.forceUpdate('surface')
362
+ }
263
363
  }
264
364
 
365
+ public destroy(): void { }
366
+
265
367
  }
@@ -0,0 +1,82 @@
1
+ import { ILeafLayout, ILeaf, IBoundsData, ILocationType, IMatrixData, IBoundsType, ILayoutBoundsData, IPointData } from '@leafer/interface';
2
+
3
+ declare class LeafLayout implements ILeafLayout {
4
+ leaf: ILeaf;
5
+ proxyZoom: boolean;
6
+ boxBounds: IBoundsData;
7
+ get strokeBounds(): IBoundsData;
8
+ get renderBounds(): IBoundsData;
9
+ _strokeBounds: IBoundsData;
10
+ _renderBounds: IBoundsData;
11
+ marginBounds: IBoundsData;
12
+ contentBounds: IBoundsData;
13
+ get localStrokeBounds(): IBoundsData;
14
+ get localRenderBounds(): IBoundsData;
15
+ protected _localStrokeBounds?: IBoundsData;
16
+ protected _localRenderBounds?: IBoundsData;
17
+ protected _worldContentBounds: IBoundsData;
18
+ protected _worldBoxBounds: IBoundsData;
19
+ protected _worldStrokeBounds: IBoundsData;
20
+ resized: boolean;
21
+ waitAutoLayout: boolean;
22
+ matrixChanged: boolean;
23
+ scaleChanged: boolean;
24
+ rotationChanged: boolean;
25
+ boundsChanged: boolean;
26
+ boxChanged: boolean;
27
+ strokeChanged: boolean;
28
+ renderChanged: boolean;
29
+ localBoxChanged: boolean;
30
+ surfaceChanged: boolean;
31
+ opacityChanged: boolean;
32
+ hitCanvasChanged: boolean;
33
+ childrenSortChanged?: boolean;
34
+ affectScaleOrRotation: boolean;
35
+ affectRotation: boolean;
36
+ affectChildrenSort?: boolean;
37
+ strokeSpread: number;
38
+ renderSpread: number;
39
+ strokeBoxSpread: number;
40
+ renderShapeSpread: number;
41
+ get a(): number;
42
+ get b(): number;
43
+ get c(): number;
44
+ get d(): number;
45
+ get e(): number;
46
+ get f(): number;
47
+ get x(): number;
48
+ get y(): number;
49
+ get width(): number;
50
+ get height(): number;
51
+ constructor(leaf: ILeaf);
52
+ createLocal(): void;
53
+ update(): void;
54
+ getTransform(relative?: ILocationType | ILeaf): IMatrixData;
55
+ getBounds(type?: IBoundsType, relative?: ILocationType | ILeaf): IBoundsData;
56
+ getInnerBounds(type?: IBoundsType): IBoundsData;
57
+ getLocalBounds(type?: IBoundsType): IBoundsData;
58
+ getWorldBounds(type?: IBoundsType): IBoundsData;
59
+ getLayoutBounds(type?: IBoundsType, relative?: ILocationType | ILeaf, unscale?: boolean): ILayoutBoundsData;
60
+ getLayoutPoints(type?: IBoundsType, relative?: ILocationType | ILeaf): IPointData[];
61
+ protected getWorldContentBounds(): IBoundsData;
62
+ protected getWorldBoxBounds(): IBoundsData;
63
+ protected getWorldStrokeBounds(): IBoundsData;
64
+ spreadStrokeCancel(): void;
65
+ spreadRenderCancel(): void;
66
+ spreadStroke(): void;
67
+ spreadRender(): void;
68
+ boxChange(): void;
69
+ localBoxChange(): void;
70
+ strokeChange(): void;
71
+ renderChange(): void;
72
+ scaleChange(): void;
73
+ rotationChange(): void;
74
+ protected _scaleOrRotationChange(): void;
75
+ matrixChange(): void;
76
+ surfaceChange(): void;
77
+ opacityChange(): void;
78
+ childrenSortChange(): void;
79
+ destroy(): void;
80
+ }
81
+
82
+ export { LeafLayout };