@leafer/layouter 1.0.0-alpha.21 → 1.0.0-alpha.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/layouter",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.30",
4
4
  "description": "@leafer/canvas",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,14 +19,14 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/event": "1.0.0-alpha.21",
23
- "@leafer/math": "1.0.0-alpha.21",
24
- "@leafer/list": "1.0.0-alpha.21",
25
- "@leafer/data": "1.0.0-alpha.21",
26
- "@leafer/helper": "1.0.0-alpha.21",
27
- "@leafer/debug": "1.0.0-alpha.21"
22
+ "@leafer/event": "1.0.0-alpha.30",
23
+ "@leafer/math": "1.0.0-alpha.30",
24
+ "@leafer/list": "1.0.0-alpha.30",
25
+ "@leafer/data": "1.0.0-alpha.30",
26
+ "@leafer/helper": "1.0.0-alpha.30",
27
+ "@leafer/debug": "1.0.0-alpha.30"
28
28
  },
29
29
  "devDependencies": {
30
- "@leafer/interface": "1.0.0-alpha.21"
30
+ "@leafer/interface": "1.0.0-alpha.30"
31
31
  }
32
32
  }
package/src/Layouter.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { ILayouter, ILeaf, ILayoutBlockData, IEventListenerId, ILayouterConfig, ILeafList } from '@leafer/interface'
2
- import { LayoutEvent, RenderEvent, WatchEvent } from '@leafer/event'
2
+ import { LayoutEvent, WatchEvent } from '@leafer/event'
3
3
  import { LeafLevelList, LeafList } from '@leafer/list'
4
4
  import { BranchHelper, LeafHelper } from '@leafer/helper'
5
5
  import { DataHelper } from '@leafer/data'
6
- import { Run } from '@leafer/debug'
6
+ import { Run, Debug } from '@leafer/debug'
7
7
 
8
8
  import { updateBounds, updateMatrix, updateChange } from './LayouterHelper'
9
9
  import { LayoutBlockData } from './LayoutBlockData'
@@ -12,6 +12,8 @@ import { LayoutBlockData } from './LayoutBlockData'
12
12
  const { updateAllWorldMatrix, updateAllChange } = LeafHelper
13
13
  const { pushAllBranchStack, updateWorldBoundsByBranchStack } = BranchHelper
14
14
 
15
+ const debug = Debug.get('Layouter')
16
+
15
17
  export class Layouter implements ILayouter {
16
18
 
17
19
  public target: ILeaf
@@ -20,14 +22,13 @@ export class Layouter implements ILayouter {
20
22
  public totalTimes: number = 0
21
23
  public times: number
22
24
 
23
- public changed: boolean = true
25
+ public disabled: boolean
24
26
  public running: boolean
27
+ public layouting: boolean
25
28
 
26
- public config: ILayouterConfig = {
27
- partLayout: {
28
- maxTimes: 3
29
- }
30
- }
29
+ public waitAgain: boolean
30
+
31
+ public config: ILayouterConfig = {}
31
32
 
32
33
  protected __updateList: ILeafList
33
34
  protected __levelList: LeafLevelList = new LeafLevelList()
@@ -40,6 +41,7 @@ export class Layouter implements ILayouter {
40
41
  }
41
42
 
42
43
  public start(): void {
44
+ if (this.disabled) return
43
45
  this.running = true
44
46
  }
45
47
 
@@ -47,34 +49,61 @@ export class Layouter implements ILayouter {
47
49
  this.running = false
48
50
  }
49
51
 
50
- public update(): void {
51
- this.changed = true
52
+ public disable(): void {
53
+ this.stop()
54
+ this.__removeListenEvents()
55
+ this.disabled = true
52
56
  }
53
57
 
54
58
  public layout(): void {
55
59
  if (!this.running) return
56
60
  const { target } = this
57
- const { LAYOUT, END } = LayoutEvent
58
61
  this.times = 0
59
- this.changed = false
60
- target.emit(LayoutEvent.START)
61
- this.layoutOnce()
62
- target.emitEvent(new LayoutEvent(LAYOUT, this.layoutedBlocks))
63
- target.emitEvent(new LayoutEvent(END, this.layoutedBlocks))
62
+
63
+ try {
64
+ target.emit(LayoutEvent.START)
65
+ this.layoutOnce()
66
+ target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times))
67
+ } catch (e) {
68
+ debug.error(e)
69
+ }
70
+
64
71
  this.layoutedBlocks = null
65
72
  }
66
73
 
74
+ public layoutAgain(): void {
75
+ if (this.layouting) {
76
+ this.waitAgain = true
77
+ } else {
78
+ this.layoutOnce()
79
+ }
80
+ }
81
+
67
82
  public layoutOnce(): void {
68
83
 
69
- this.totalTimes++
84
+ if (this.layouting) return debug.warn('layouting')
85
+ if (this.times > 3) return debug.warn('layout max times')
86
+
70
87
  this.times++
88
+ this.totalTimes++
89
+
90
+ this.layouting = true
71
91
 
72
92
  this.target.emit(WatchEvent.REQUEST)
93
+
73
94
  if (this.totalTimes > 1) {
74
95
  this.partLayout()
75
96
  } else {
76
97
  this.fullLayout()
77
98
  }
99
+
100
+ this.layouting = false
101
+
102
+ if (this.waitAgain) {
103
+ this.waitAgain = false
104
+ this.layoutOnce()
105
+ }
106
+
78
107
  }
79
108
 
80
109
  public partLayout(): void {
@@ -82,11 +111,11 @@ export class Layouter implements ILayouter {
82
111
 
83
112
  const t = Run.start('PartLayout')
84
113
  const { target, __updateList: updateList } = this
85
- const { BEFORE_ONCE, ONCE, AFTER_ONCE } = LayoutEvent
114
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent
86
115
 
87
116
  const blocks = this.getBlocks(updateList)
88
117
  blocks.forEach(item => { item.setBefore() })
89
- target.emitEvent(new LayoutEvent(BEFORE_ONCE, blocks))
118
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))
90
119
 
91
120
  updateList.sort()
92
121
  updateMatrix(updateList, this.__levelList)
@@ -95,44 +124,42 @@ export class Layouter implements ILayouter {
95
124
 
96
125
  blocks.forEach(item => item.setAfter())
97
126
 
98
- target.emitEvent(new LayoutEvent(ONCE, blocks))
99
- target.emitEvent(new LayoutEvent(AFTER_ONCE, blocks))
127
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))
128
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))
100
129
 
101
- this.setBlocks(blocks)
130
+ this.addBlocks(blocks)
102
131
 
103
132
  this.__levelList.reset()
104
133
  this.__updateList = null
105
134
  Run.end(t)
106
135
 
107
- this.__checkAgain()
108
136
  }
109
137
 
110
138
  public fullLayout(): void {
111
139
  const t = Run.start('FullLayout')
112
140
 
113
141
  const { target } = this
114
- const { BEFORE_ONCE, ONCE, AFTER_ONCE } = LayoutEvent
142
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent
115
143
 
116
144
  const blocks = this.getBlocks(new LeafList(target))
117
- target.emitEvent(new LayoutEvent(BEFORE_ONCE, blocks))
145
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))
118
146
 
119
147
  Layouter.fullLayout(target)
120
148
 
121
149
  blocks.forEach(item => { item.setAfter() })
122
- target.emitEvent(new LayoutEvent(ONCE, blocks))
123
- target.emitEvent(new LayoutEvent(AFTER_ONCE, blocks))
150
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))
151
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))
124
152
 
125
- this.setBlocks(blocks)
153
+ this.addBlocks(blocks)
126
154
 
127
155
  Run.end(t)
128
156
 
129
- this.__checkAgain()
130
157
  }
131
158
 
132
159
  static fullLayout(target: ILeaf): void {
133
160
  updateAllWorldMatrix(target)
134
161
 
135
- if (target.__isBranch) {
162
+ if (target.isBranch) {
136
163
  const branchStack: ILeaf[] = [target]
137
164
  pushAllBranchStack(target, branchStack)
138
165
  updateWorldBoundsByBranchStack(branchStack)
@@ -152,14 +179,10 @@ export class Layouter implements ILayouter {
152
179
  return [this.createBlock(list)]
153
180
  }
154
181
 
155
- public setBlocks(current: ILayoutBlockData[]) {
182
+ public addBlocks(current: ILayoutBlockData[]) {
156
183
  this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current
157
184
  }
158
185
 
159
- protected __checkAgain(): void {
160
- if (this.changed && this.times <= this.config.partLayout.maxTimes) this.target.emit(LayoutEvent.AGAIN) // 防止更新布局过程中产生了属性修改
161
- }
162
-
163
186
  protected __onReceiveWatchData(event: WatchEvent): void {
164
187
  this.__updateList = event.data.updatedList
165
188
  }
@@ -168,9 +191,8 @@ export class Layouter implements ILayouter {
168
191
  const { target } = this
169
192
  this.__eventIds = [
170
193
  target.on__(LayoutEvent.REQUEST, this.layout, this),
171
- target.on__(LayoutEvent.AGAIN, this.layoutOnce, this),
172
- target.on__(WatchEvent.DATA, this.__onReceiveWatchData, this),
173
- target.on__(RenderEvent.REQUEST, this.update, this),
194
+ target.on__(LayoutEvent.AGAIN, this.layoutAgain, this),
195
+ target.on__(WatchEvent.DATA, this.__onReceiveWatchData, this)
174
196
  ]
175
197
  }
176
198
 
@@ -180,8 +202,10 @@ export class Layouter implements ILayouter {
180
202
 
181
203
  public destroy(): void {
182
204
  if (this.target) {
205
+ this.stop()
183
206
  this.__removeListenEvents()
184
207
  this.target = null
208
+ this.config = null
185
209
  }
186
210
  }
187
211
 
@@ -11,20 +11,20 @@ export function updateMatrix(updateList: ILeafList, levelList: ILeafLevelList):
11
11
  let layout: ILeafLayout
12
12
  updateList.list.forEach(leaf => { // 更新矩阵, 所有子元素,和父元素都需要更新bounds
13
13
  layout = leaf.__layout
14
- if (levelList.without(leaf)) { // 防止重复, 子元素可能已经被父元素更新过
14
+ if (levelList.without(leaf) && !layout.useZoomProxy) { // 防止重复, 子元素可能已经被父元素更新过
15
15
 
16
16
  if (layout.matrixChanged) {
17
17
 
18
18
  updateAllWorldMatrix(leaf)
19
19
 
20
20
  levelList.push(leaf)
21
- if (leaf.__isBranch) pushAllChildBranch(leaf, levelList)
21
+ if (leaf.isBranch) pushAllChildBranch(leaf, levelList)
22
22
  pushAllParent(leaf, levelList)
23
23
 
24
24
  } else if (layout.boundsChanged) {
25
25
 
26
26
  levelList.push(leaf)
27
- if (leaf.__isBranch) leaf.__tempNumber = 0 // 标识需要更新子Leaf元素的WorldBounds分支, 0表示不需要更新
27
+ if (leaf.isBranch) leaf.__tempNumber = 0 // 标识需要更新子Leaf元素的WorldBounds分支, 0表示不需要更新
28
28
  pushAllParent(leaf, levelList)
29
29
  }
30
30
  }
@@ -43,9 +43,9 @@ export function updateBounds(boundsList: ILeafLevelList): void {
43
43
  branch = itemList[i]
44
44
 
45
45
  // 标识了需要更新子元素
46
- if (branch.__isBranch && branch.__tempNumber) {
46
+ if (branch.isBranch && branch.__tempNumber) {
47
47
  for (let j = 0, jLen = branch.children.length; j < jLen; j++) {
48
- if (!branch.children[j].__isBranch) {
48
+ if (!branch.children[j].isBranch) {
49
49
  branch.children[j].__updateWorldBounds()
50
50
  }
51
51
  }