@leafer/layouter 1.0.0-alpha.23 → 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.23",
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.23",
23
- "@leafer/math": "1.0.0-alpha.23",
24
- "@leafer/list": "1.0.0-alpha.23",
25
- "@leafer/data": "1.0.0-alpha.23",
26
- "@leafer/helper": "1.0.0-alpha.23",
27
- "@leafer/debug": "1.0.0-alpha.23"
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.23"
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,32 +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
61
  this.times = 0
58
- this.changed = false
59
- target.emit(LayoutEvent.START)
60
- this.layoutOnce()
61
- target.emitEvent(new LayoutEvent(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
+
62
71
  this.layoutedBlocks = null
63
72
  }
64
73
 
74
+ public layoutAgain(): void {
75
+ if (this.layouting) {
76
+ this.waitAgain = true
77
+ } else {
78
+ this.layoutOnce()
79
+ }
80
+ }
81
+
65
82
  public layoutOnce(): void {
66
83
 
67
- this.totalTimes++
84
+ if (this.layouting) return debug.warn('layouting')
85
+ if (this.times > 3) return debug.warn('layout max times')
86
+
68
87
  this.times++
88
+ this.totalTimes++
89
+
90
+ this.layouting = true
69
91
 
70
92
  this.target.emit(WatchEvent.REQUEST)
93
+
71
94
  if (this.totalTimes > 1) {
72
95
  this.partLayout()
73
96
  } else {
74
97
  this.fullLayout()
75
98
  }
99
+
100
+ this.layouting = false
101
+
102
+ if (this.waitAgain) {
103
+ this.waitAgain = false
104
+ this.layoutOnce()
105
+ }
106
+
76
107
  }
77
108
 
78
109
  public partLayout(): void {
@@ -84,7 +115,7 @@ export class Layouter implements ILayouter {
84
115
 
85
116
  const blocks = this.getBlocks(updateList)
86
117
  blocks.forEach(item => { item.setBefore() })
87
- target.emitEvent(new LayoutEvent(BEFORE, blocks))
118
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))
88
119
 
89
120
  updateList.sort()
90
121
  updateMatrix(updateList, this.__levelList)
@@ -93,16 +124,15 @@ export class Layouter implements ILayouter {
93
124
 
94
125
  blocks.forEach(item => item.setAfter())
95
126
 
96
- target.emitEvent(new LayoutEvent(LAYOUT, blocks))
97
- target.emitEvent(new LayoutEvent(AFTER, blocks))
127
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))
128
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))
98
129
 
99
- this.setBlocks(blocks)
130
+ this.addBlocks(blocks)
100
131
 
101
132
  this.__levelList.reset()
102
133
  this.__updateList = null
103
134
  Run.end(t)
104
135
 
105
- this.__checkAgain()
106
136
  }
107
137
 
108
138
  public fullLayout(): void {
@@ -112,25 +142,24 @@ export class Layouter implements ILayouter {
112
142
  const { BEFORE, LAYOUT, AFTER } = LayoutEvent
113
143
 
114
144
  const blocks = this.getBlocks(new LeafList(target))
115
- target.emitEvent(new LayoutEvent(BEFORE, blocks))
145
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))
116
146
 
117
147
  Layouter.fullLayout(target)
118
148
 
119
149
  blocks.forEach(item => { item.setAfter() })
120
- target.emitEvent(new LayoutEvent(LAYOUT, blocks))
121
- target.emitEvent(new LayoutEvent(AFTER, blocks))
150
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))
151
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))
122
152
 
123
- this.setBlocks(blocks)
153
+ this.addBlocks(blocks)
124
154
 
125
155
  Run.end(t)
126
156
 
127
- this.__checkAgain()
128
157
  }
129
158
 
130
159
  static fullLayout(target: ILeaf): void {
131
160
  updateAllWorldMatrix(target)
132
161
 
133
- if (target.__isBranch) {
162
+ if (target.isBranch) {
134
163
  const branchStack: ILeaf[] = [target]
135
164
  pushAllBranchStack(target, branchStack)
136
165
  updateWorldBoundsByBranchStack(branchStack)
@@ -150,14 +179,10 @@ export class Layouter implements ILayouter {
150
179
  return [this.createBlock(list)]
151
180
  }
152
181
 
153
- public setBlocks(current: ILayoutBlockData[]) {
182
+ public addBlocks(current: ILayoutBlockData[]) {
154
183
  this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current
155
184
  }
156
185
 
157
- protected __checkAgain(): void {
158
- //if (this.changed && this.times <= this.config.partLayout.maxTimes) this.target.emit(LayoutEvent.AGAIN) // 防止更新布局过程中产生了属性修改
159
- }
160
-
161
186
  protected __onReceiveWatchData(event: WatchEvent): void {
162
187
  this.__updateList = event.data.updatedList
163
188
  }
@@ -166,9 +191,8 @@ export class Layouter implements ILayouter {
166
191
  const { target } = this
167
192
  this.__eventIds = [
168
193
  target.on__(LayoutEvent.REQUEST, this.layout, this),
169
- target.on__(LayoutEvent.AGAIN, this.layoutOnce, this),
170
- target.on__(WatchEvent.DATA, this.__onReceiveWatchData, this),
171
- target.on__(RenderEvent.REQUEST, this.update, this),
194
+ target.on__(LayoutEvent.AGAIN, this.layoutAgain, this),
195
+ target.on__(WatchEvent.DATA, this.__onReceiveWatchData, this)
172
196
  ]
173
197
  }
174
198
 
@@ -181,6 +205,7 @@ export class Layouter implements ILayouter {
181
205
  this.stop()
182
206
  this.__removeListenEvents()
183
207
  this.target = null
208
+ this.config = null
184
209
  }
185
210
  }
186
211
 
@@ -18,13 +18,13 @@ export function updateMatrix(updateList: ILeafList, levelList: ILeafLevelList):
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
  }