@leafer/layout 1.0.0-rc.3 → 1.0.0-rc.30

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