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