@leafer/layout 1.0.0-rc.1 → 1.0.0-rc.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/layout",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0-rc.11",
4
4
  "description": "@leafer/layout",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -15,17 +15,17 @@
15
15
  "type": "git",
16
16
  "url": "https://github.com/leaferjs/leafer.git"
17
17
  },
18
- "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/layout",
18
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/display-module/layout",
19
19
  "bugs": "https://github.com/leaferjs/leafer/issues",
20
20
  "keywords": [
21
21
  "leafer",
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/math": "1.0.0-rc.1",
26
- "@leafer/platform": "1.0.0-rc.1"
25
+ "@leafer/math": "1.0.0-rc.11",
26
+ "@leafer/platform": "1.0.0-rc.11"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.1"
29
+ "@leafer/interface": "1.0.0-rc.11"
30
30
  }
31
31
  }
package/src/LeafLayout.ts CHANGED
@@ -1,21 +1,25 @@
1
- import { ILeaf, ILeafLayout, ILayoutLocationType, ILayoutBoundsType, IBoundsData, IMatrixData } from '@leafer/interface'
2
- import { BoundsHelper } 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
 
@@ -66,84 +75,177 @@ export class LeafLayout implements ILeafLayout {
66
75
  public strokeBoxSpread: number
67
76
  public renderShapeSpread: number
68
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
+
69
90
 
70
91
  constructor(leaf: ILeaf) {
71
92
  this.leaf = leaf
72
- this.renderBounds = this.strokeBounds = this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
73
- 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
74
95
  this.boxChange()
75
- this.positionChange()
96
+ this.matrixChange()
76
97
  }
77
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
+ }
78
104
 
79
- public checkUpdate(force?: boolean): void {
105
+ public update(): void {
80
106
  const { leafer } = this.leaf
81
107
  if (leafer) {
82
108
  if (leafer.ready) {
83
- if ((Platform.realtimeLayout || force) && leafer.watcher.changed) leafer.layouter.layout()
109
+ if (leafer.watcher.changed) leafer.layouter.layout()
84
110
  } else {
85
111
  leafer.start()
86
112
  }
87
113
  } else {
88
114
  let root = this.leaf
89
- while (root.parent) { root = root.parent }
115
+ while (root.parent && !root.parent.leafer) { root = root.parent }
90
116
  Platform.layout(root)
91
117
  }
92
118
  }
93
119
 
94
- public getTransform(locationType: ILayoutLocationType): IMatrixData {
95
- this.checkUpdate()
96
- 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
+ }
97
132
  }
98
133
 
99
- public getBounds(type: ILayoutBoundsType, locationType: ILayoutLocationType): IBoundsData {
100
-
101
- this.checkUpdate()
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
+ }
146
+ }
102
147
 
103
- if (locationType === 'world') {
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
- switch (type) {
106
- case 'render':
107
- return this.leaf.__world
108
- case 'content':
109
- if (this.contentBounds) return this.getWorldContentBounds()
110
- case 'margin':
111
- case 'box':
112
- return this.getWorldBoxBounds()
113
- case 'margin':
114
- case 'stroke':
115
- return this.getWorldStrokeBounds()
116
- }
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
+ }
117
174
 
118
- } else if (locationType === 'inner') {
119
-
120
- switch (type) {
121
- case 'render':
122
- return this.renderBounds
123
- case 'content':
124
- if (this.contentBounds) return this.contentBounds
125
- case 'margin':
126
- case 'box':
127
- return this.boxBounds
128
- case 'stroke':
129
- return this.strokeBounds
130
- }
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
+ }
131
189
 
132
- } else {
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
+ }
133
211
 
134
- switch (type) {
135
- case 'render':
136
- return this.localRenderBounds
137
- case 'margin':
138
- case 'content':
139
- case 'box':
140
- return this.leaf.__local
141
- case 'stroke':
142
- return this.localStrokeBounds
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
143
225
  }
144
-
145
226
  }
146
227
 
228
+ return layoutBounds
229
+ }
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
246
+ }
247
+ if (relativeLeaf !== undefined) points.forEach(point => leaf.innerToWorld(point, null, false, relativeLeaf))
248
+ return points
147
249
  }
148
250
 
149
251
  protected getWorldContentBounds(): IBoundsData {
@@ -168,25 +270,25 @@ export class LeafLayout implements ILeafLayout {
168
270
 
169
271
  public spreadStrokeCancel(): void {
170
272
  const same = this.renderBounds === this.strokeBounds
171
- this.strokeBounds = this.boxBounds
172
- this.localStrokeBounds = this.leaf.__local
273
+ this._strokeBounds = this.boxBounds
274
+ this._localStrokeBounds = this.leaf.__localBoxBounds
173
275
  if (same) this.spreadRenderCancel()
174
276
  }
175
277
  public spreadRenderCancel(): void {
176
- this.renderBounds = this.strokeBounds
177
- this.localRenderBounds = this.localStrokeBounds
278
+ this._renderBounds = this._strokeBounds
279
+ this._localRenderBounds = this._localStrokeBounds
178
280
  }
179
281
 
180
282
  public spreadStroke(): void {
181
283
  const { x, y, width, height } = this.strokeBounds
182
- this.strokeBounds = { x, y, width, height }
183
- this.localStrokeBounds = { x, y, width, height }
284
+ this._strokeBounds = { x, y, width, height }
285
+ this._localStrokeBounds = { x, y, width, height }
184
286
  if (!this.renderSpread) this.spreadRenderCancel()
185
287
  }
186
288
  public spreadRender(): void {
187
289
  const { x, y, width, height } = this.renderBounds
188
- this.renderBounds = { x, y, width, height }
189
- this.localRenderBounds = { x, y, width, height }
290
+ this._renderBounds = { x, y, width, height }
291
+ this._localRenderBounds = { x, y, width, height }
190
292
  }
191
293
 
192
294
 
@@ -219,12 +321,6 @@ export class LeafLayout implements ILeafLayout {
219
321
 
220
322
  // matrix
221
323
 
222
- public positionChange(): void {
223
- this.positionChanged = true
224
- this.matrixChanged = true
225
- this.localBoxChanged || this.localBoxChange()
226
- }
227
-
228
324
  public scaleChange(): void {
229
325
  this.scaleChanged = true
230
326
  this._scaleOrRotationChange()
@@ -238,6 +334,11 @@ export class LeafLayout implements ILeafLayout {
238
334
 
239
335
  protected _scaleOrRotationChange() {
240
336
  this.affectScaleOrRotation = true
337
+ this.matrixChange()
338
+ if (!this.leaf.__local) this.createLocal()
339
+ }
340
+
341
+ public matrixChange(): void {
241
342
  this.matrixChanged = true
242
343
  this.localBoxChanged || this.localBoxChange()
243
344
  }
package/types/index.d.ts CHANGED
@@ -1,20 +1,25 @@
1
- import { ILeafLayout, ILeaf, IBoundsData, ILayoutLocationType, IMatrixData, ILayoutBoundsType } from '@leafer/interface';
1
+ import { ILeafLayout, ILeaf, IBoundsData, ILocationType, IMatrixData, IBoundsType, ILayoutBoundsData, IPointData } from '@leafer/interface';
2
2
 
3
3
  declare class LeafLayout implements ILeafLayout {
4
4
  leaf: ILeaf;
5
- useZoomProxy: boolean;
5
+ proxyZoom: boolean;
6
6
  boxBounds: IBoundsData;
7
- strokeBounds: IBoundsData;
8
- renderBounds: IBoundsData;
7
+ get strokeBounds(): IBoundsData;
8
+ get renderBounds(): IBoundsData;
9
+ _strokeBounds: IBoundsData;
10
+ _renderBounds: IBoundsData;
9
11
  marginBounds: IBoundsData;
10
12
  contentBounds: IBoundsData;
11
- localStrokeBounds: IBoundsData;
12
- localRenderBounds: IBoundsData;
13
+ get localStrokeBounds(): IBoundsData;
14
+ get localRenderBounds(): IBoundsData;
15
+ protected _localStrokeBounds?: IBoundsData;
16
+ protected _localRenderBounds?: IBoundsData;
13
17
  protected _worldContentBounds: IBoundsData;
14
18
  protected _worldBoxBounds: IBoundsData;
15
19
  protected _worldStrokeBounds: IBoundsData;
20
+ resized: boolean;
21
+ waitAutoLayout: boolean;
16
22
  matrixChanged: boolean;
17
- positionChanged: boolean;
18
23
  scaleChanged: boolean;
19
24
  rotationChanged: boolean;
20
25
  boundsChanged: boolean;
@@ -33,10 +38,26 @@ declare class LeafLayout implements ILeafLayout {
33
38
  renderSpread: number;
34
39
  strokeBoxSpread: number;
35
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;
36
51
  constructor(leaf: ILeaf);
37
- checkUpdate(force?: boolean): void;
38
- getTransform(locationType: ILayoutLocationType): IMatrixData;
39
- getBounds(type: ILayoutBoundsType, locationType: ILayoutLocationType): IBoundsData;
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[];
40
61
  protected getWorldContentBounds(): IBoundsData;
41
62
  protected getWorldBoxBounds(): IBoundsData;
42
63
  protected getWorldStrokeBounds(): IBoundsData;
@@ -48,10 +69,10 @@ declare class LeafLayout implements ILeafLayout {
48
69
  localBoxChange(): void;
49
70
  strokeChange(): void;
50
71
  renderChange(): void;
51
- positionChange(): void;
52
72
  scaleChange(): void;
53
73
  rotationChange(): void;
54
74
  protected _scaleOrRotationChange(): void;
75
+ matrixChange(): void;
55
76
  surfaceChange(): void;
56
77
  opacityChange(): void;
57
78
  childrenSortChange(): void;