@leafer-ui/app 1.0.0-alpha.21 → 1.0.0-alpha.23

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.
Files changed (3) hide show
  1. package/package.json +5 -5
  2. package/src/App.ts +55 -30
  3. package/src/Leafer.ts +112 -49
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/app",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "@leafer-ui/app",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,11 +19,11 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/core": "1.0.0-alpha.21",
23
- "@leafer-ui/display": "1.0.0-alpha.21"
22
+ "@leafer/core": "1.0.0-alpha.23",
23
+ "@leafer-ui/display": "1.0.0-alpha.23"
24
24
  },
25
25
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-alpha.21",
27
- "@leafer-ui/interface": "1.0.0-alpha.21"
26
+ "@leafer/interface": "1.0.0-alpha.23",
27
+ "@leafer-ui/interface": "1.0.0-alpha.23"
28
28
  }
29
29
  }
package/src/App.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { ILeaferConfig, IResizeEvent, ILeaferCanvas, IRenderOptions, IApp } from '@leafer/interface'
2
- import { DataHelper, LayoutEvent, RenderEvent } from '@leafer/core'
1
+ import { ILeaferConfig, IResizeEvent, ILeaferCanvas, IRenderOptions, IApp, __Value } from '@leafer/interface'
2
+ import { DataHelper, LayoutEvent, RenderEvent, defineLeafAttr } from '@leafer/core'
3
3
 
4
4
  import { Leafer } from './Leafer'
5
5
 
@@ -8,39 +8,60 @@ export class App extends Leafer implements IApp {
8
8
 
9
9
  public get isApp(): boolean { return true }
10
10
 
11
+ public view: unknown
12
+
13
+ public virtualCanvas: boolean
14
+
11
15
  public children: Leafer[] = []
12
16
 
13
- constructor(userConfig?: ILeaferConfig) {
14
- super(userConfig)
17
+ @hitType(true)
18
+ public hittable: boolean
19
+
20
+ protected __setApp(): void {
21
+ if (this.config.view === this.canvas.view) {
22
+ this.virtualCanvas = true
23
+ } else {
24
+ this.canvas.unloadView()
25
+ this.view = this.canvas.parentView
26
+ }
15
27
  }
16
28
 
17
29
  public start(): void {
18
- this.children.forEach(leafer => { leafer.start() })
19
- this.renderer.start()
20
- this.running = true
30
+ if (!this.running) {
31
+ this.children.forEach(leafer => { leafer.start() })
32
+ this.renderer.start()
33
+ this.running = true
34
+ }
21
35
  }
22
36
 
23
37
  public stop(): void {
24
- this.children.forEach(leafer => { leafer.stop() })
25
- this.renderer.stop()
26
- this.running = false
38
+ if (this.running) {
39
+ this.children.forEach(leafer => { leafer.stop() })
40
+ this.renderer.stop()
41
+ this.running = false
42
+ }
27
43
  }
28
44
 
29
45
  public addLeafer(merge?: ILeaferConfig): Leafer {
30
- const leafer = new Leafer(this.__getChildConfig(merge), this)
46
+ const leafer = new Leafer(merge)
31
47
  this.add(leafer)
32
-
33
- const { renderer } = this
34
-
35
- this.__eventIds.push(
36
- leafer.on__(LayoutEvent.END, this.__onChildLayoutEnd, this),
37
- leafer.on__(RenderEvent.END, renderer.update, renderer),
38
- this.on__(LayoutEvent.REQUEST, renderer.mergeBlocks, renderer),
39
- )
40
-
41
48
  return leafer
42
49
  }
43
50
 
51
+ public add(leafer: Leafer): void {
52
+ if (!leafer.view) leafer.init(this.__getChildConfig(leafer.userConfig), this)
53
+ super.add(leafer)
54
+
55
+ if (this.virtualCanvas) {
56
+ const { renderer } = this
57
+ this.__eventIds.push(
58
+ leafer.on__(LayoutEvent.END, this.__onChildLayoutEnd, this),
59
+ leafer.on__(RenderEvent.END, renderer.update, renderer),
60
+ this.on__(LayoutEvent.REQUEST, renderer.mergeBlocks, renderer)
61
+ )
62
+ }
63
+ }
64
+
44
65
  protected __onChildLayoutEnd(event: LayoutEvent): void {
45
66
  const { renderer } = this
46
67
  if ((event.current as Leafer).config.usePartRender) {
@@ -62,7 +83,8 @@ export class App extends Leafer implements IApp {
62
83
  protected __getChildConfig(userConfig?: ILeaferConfig): ILeaferConfig {
63
84
  let old = { ...this.config }
64
85
  userConfig = userConfig ? DataHelper.default(userConfig, old) : old
65
- userConfig.view = null
86
+ userConfig.view = this.virtualCanvas ? null : this.canvas.parentView
87
+ userConfig.virtualCanvas = null
66
88
  if (this.autoLayout) {
67
89
  userConfig.width = this.width
68
90
  userConfig.height = this.height
@@ -71,14 +93,17 @@ export class App extends Leafer implements IApp {
71
93
  return userConfig
72
94
  }
73
95
 
74
- public destory(): void {
75
- super.destory()
76
-
77
- const { children } = this
78
- if (children.length) {
79
- children.forEach(leafer => leafer.destory())
80
- children.length = 0
81
- }
96
+ }
97
+
98
+ export function hitType(defaultValue?: __Value) {
99
+ return (target: IApp, key: string) => {
100
+ defineLeafAttr(target, key, defaultValue, {
101
+ set(value: __Value) {
102
+ this.__setAttr(key, value)
103
+ this.children.forEach(item => {
104
+ item.hittable = value as boolean
105
+ })
106
+ }
107
+ })
82
108
  }
83
-
84
109
  }
package/src/Leafer.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { IApp, ILeafer, ILeaferCanvas, IRenderer, ILayouter, ICreator, ISelector, IWatcher, IInteraction, ILeaferConfig, ICanvasManager, IHitCanvasManager, IImageManager, IAutoBounds, IScreenSizeData, IResizeEvent, IObject, ILeaf, IEventListenerId, ITransformEventData } from '@leafer/interface'
2
- import { AutoBounds, LayoutEvent, ResizeEvent, MoveEvent, ZoomEvent, CanvasManager, HitCanvasManager, ImageManager, DataHelper, LeafHelper, Creator, Run } from '@leafer/core'
1
+ import { IApp, ILeafer, ILeaferCanvas, IRenderer, ILayouter, ICreator, ISelector, IWatcher, IInteraction, ILeaferConfig, ICanvasManager, IHitCanvasManager, IImageManager, IAutoBounds, IScreenSizeData, IResizeEvent, IObject, ILeaf, IEventListenerId, ITransformEventData, ILeaferType, IBounds } from '@leafer/interface'
2
+ import { AutoBounds, LayoutEvent, ResizeEvent, MoveEvent, ZoomEvent, LeaferEvent, CanvasManager, HitCanvasManager, ImageManager, DataHelper, LeafHelper, Creator, Run, RenderEvent } from '@leafer/core'
3
3
 
4
4
  import { Group } from '@leafer-ui/display'
5
5
 
@@ -15,6 +15,9 @@ export class Leafer extends Group implements ILeafer {
15
15
  public parent?: App
16
16
 
17
17
  public running: boolean
18
+ public ready: boolean
19
+
20
+ public startTimer: number
18
21
 
19
22
  @resizeType()
20
23
  public width: number
@@ -25,18 +28,20 @@ export class Leafer extends Group implements ILeafer {
25
28
  @resizeType()
26
29
  public pixelRatio: number
27
30
 
31
+ public get bounds(): IBounds { return this.canvas.bounds }
32
+
33
+ public userConfig: ILeaferConfig
28
34
  public config: ILeaferConfig = {
29
- useZoom: true,
30
- useMove: true,
31
- autoStart: true,
32
- hittable: true,
33
- pixelRatio: devicePixelRatio
35
+ type: 'design',
36
+ start: true,
37
+ hittable: true
34
38
  }
35
39
 
36
40
  public autoLayout?: IAutoBounds
37
41
 
38
- // manager
42
+ public view: unknown
39
43
 
44
+ // manager
40
45
  public canvas: ILeaferCanvas
41
46
  public renderer: IRenderer
42
47
 
@@ -56,10 +61,16 @@ export class Leafer extends Group implements ILeafer {
56
61
 
57
62
  protected __eventIds: IEventListenerId[] = []
58
63
 
59
- constructor(userConfig?: ILeaferConfig, app?: IApp) {
64
+
65
+
66
+ constructor(userConfig?: ILeaferConfig) {
60
67
  super()
68
+ this.userConfig = userConfig
69
+ if (userConfig?.view) this.init(userConfig)
70
+ }
61
71
 
62
- this.__setAsLeafer()
72
+ public init(userConfig?: ILeaferConfig, app?: IApp): void {
73
+ this.__setLeafer(this)
63
74
  this.__setConfig(userConfig)
64
75
 
65
76
  const { config } = this
@@ -69,11 +80,12 @@ export class Leafer extends Group implements ILeafer {
69
80
  // render
70
81
  this.canvas = Creator.canvas(config)
71
82
  this.renderer = Creator.renderer(this, this.canvas, config)
83
+ this.view = this.canvas.view
72
84
 
73
- // layout
74
85
  if (this.isApp) {
75
- this.__level = 1
86
+ this.__setApp()
76
87
  } else {
88
+ // layout
77
89
  this.watcher = Creator.watcher(this)
78
90
  this.layouter = Creator.layouter(this)
79
91
  }
@@ -82,46 +94,86 @@ export class Leafer extends Group implements ILeafer {
82
94
 
83
95
  // interaction / manager
84
96
  if (app) {
85
-
86
- app.selector?.defaultPath.unshift(this)
87
-
88
- this.selector = app.selector
89
- if (config.hittable) this.interaction = app.interaction
90
-
91
- this.canvasManager = app.canvasManager
92
- this.hitCanvasManager = app.hitCanvasManager
93
- this.imageManager = app.imageManager
94
-
95
- if (app.running) setTimeout(this.start.bind(this))
96
-
97
+ this.__bindApp(app)
97
98
  } else {
98
-
99
99
  this.selector = Creator.selector(this)
100
- if (config.hittable) this.interaction = Creator.interaction(this, this.canvas, this.selector, config)
100
+ if (config.hittable) this.interaction = Creator.interaction(this, this, this.selector, config)
101
101
 
102
- this.canvasManager = new CanvasManager(this)
103
- this.hitCanvasManager = new HitCanvasManager(this)
102
+ this.canvasManager = new CanvasManager()
103
+ this.hitCanvasManager = new HitCanvasManager()
104
104
  this.imageManager = new ImageManager(this, config)
105
- Run.start('FullCreate')
106
- if (config.autoStart) setTimeout(this.start.bind(this))
107
105
 
106
+ if (config.start) this.startTimer = setTimeout(this.start.bind(this))
108
107
  }
109
108
 
110
109
  this.canvasManager.add(this.canvas)
111
- this.__listenEvents()
112
110
 
111
+ this.__listenEvents()
113
112
  }
114
113
 
115
114
  protected __listenEvents(): void {
116
- this.once(LayoutEvent.END, this.__setAsRoot.bind(this))
115
+ Run.start('FirstCreate')
116
+ this.once(LeaferEvent.START, () => {
117
+ Run.endOfName('FirstCreate')
118
+ Run.start('FirstLayout')
119
+ })
120
+
121
+ this.once(LayoutEvent.END, () => {
122
+ Run.endOfName('FirstLayout')
123
+ Run.start('FirstRender')
124
+
125
+ this.ready = true
126
+
127
+ this.emit(LeaferEvent.BEFORE_READY)
128
+ this.emit(LeaferEvent.READY)
129
+ this.emit(LeaferEvent.AFTER_READY)
130
+ })
131
+
132
+ this.once(RenderEvent.END, () => {
133
+ Run.endOfName('FirstRender')
134
+ this.emit(LeaferEvent.VIEW_READY)
135
+ })
117
136
  }
118
137
 
119
138
  protected __removeListenEvents(): void {
120
139
  this.off__(this.__eventIds)
121
140
  }
122
141
 
142
+ protected __setApp(): void { }
143
+
144
+ protected __bindApp(app: IApp): void {
145
+ app.selector?.defaultPath.unshift(this)
146
+
147
+ this.selector = app.selector
148
+ if (this.config.hittable) this.interaction = app.interaction
149
+
150
+ this.canvasManager = app.canvasManager
151
+ this.hitCanvasManager = app.hitCanvasManager
152
+ this.imageManager = app.imageManager
153
+
154
+ if (app.running) this.startTimer = setTimeout(this.start.bind(this))
155
+ }
156
+
157
+
158
+ public __setLeafer(leafer: ILeafer): void {
159
+ this.leafer = leafer
160
+ this.isLeafer = !!leafer
161
+ this.__level = 1
162
+ }
163
+
123
164
  protected __setConfig(userConfig?: ILeaferConfig): void {
124
- if (userConfig) this.config = DataHelper.default(userConfig, this.config)
165
+ if (userConfig) {
166
+ this.config = DataHelper.default(userConfig, this.config)
167
+ this.__setAppType(this.config.type)
168
+ }
169
+ }
170
+
171
+ protected __setAppType(type: ILeaferType): void {
172
+ if (type === 'user') {
173
+ const { config } = this
174
+ if (!config.pointer) config.pointer = {}
175
+ config.pointer.autoMoveDistance = 0
176
+ }
125
177
  }
126
178
 
127
179
  protected __checkAutoLayout(config: ILeaferConfig): void {
@@ -133,11 +185,18 @@ export class Leafer extends Group implements ILeafer {
133
185
 
134
186
  public start(): void {
135
187
  if (!this.running) {
136
- Run.endOfName('FullCreate')
188
+
189
+ if (this.ready) {
190
+ this.emit(LeaferEvent.RESTART)
191
+ } else {
192
+ this.emit(LeaferEvent.START)
193
+ }
194
+
137
195
  if (this.interaction) {
138
196
  this.__interactiveWindow()
139
197
  this.interaction.start()
140
198
  }
199
+
141
200
  this.renderer.start()
142
201
  this.layouter.start()
143
202
  this.watcher.start()
@@ -152,16 +211,16 @@ export class Leafer extends Group implements ILeafer {
152
211
  this.layouter.stop()
153
212
  this.renderer.stop()
154
213
  this.running = false
214
+ this.emit(LeaferEvent.STOP)
155
215
  }
156
216
  }
157
217
 
158
218
  protected __interactiveWindow(): void {
159
- const { useZoom, useMove } = this.config
160
- if (useMove) {
219
+ if (this.config.type !== 'user') {
161
220
  const { MOVE } = MoveEvent
162
221
  const { ZOOM } = ZoomEvent
163
- if (!this.hasEvent(MOVE)) this.__eventIds.push(this.on__(MOVE, (e: MoveEvent) => { LeafHelper.moveOfWorld(this, e.moveX, e.moveY) }))
164
- if (useZoom && !this.hasEvent(ZOOM)) this.__eventIds.push(this.on__(ZOOM, (e: ZoomEvent) => { LeafHelper.zoomOfWorld(this, e.scale, e) }))
222
+ if (!this.hasEvent(MOVE)) this.__eventIds.push(this.on__(MOVE, (e: MoveEvent) => { LeafHelper.move(this, e.moveX, e.moveY) }))
223
+ if (!this.hasEvent(ZOOM)) this.__eventIds.push(this.on__(ZOOM, (e: ZoomEvent) => { LeafHelper.zoomOf(this, e, e.scale) }))
165
224
  }
166
225
  }
167
226
 
@@ -180,22 +239,24 @@ export class Leafer extends Group implements ILeafer {
180
239
 
181
240
  public destory(): void {
182
241
  if (this.canvas) {
183
- super.destroy()
184
242
 
243
+ clearTimeout(this.startTimer)
244
+ this.emit(LeaferEvent.END)
245
+
246
+ this.stop()
185
247
  this.__removeListenEvents()
186
248
 
187
- if (!this.parent) {
188
- this.interaction?.destroy()
189
- this.selector.destroy()
190
249
 
191
- this.renderer.destroy()
192
- this.watcher.destroy()
193
- this.layouter.destroy()
250
+ this.interaction?.destroy()
251
+ this.selector.destroy()
194
252
 
195
- this.canvasManager.destory()
196
- this.hitCanvasManager.destory()
197
- this.imageManager.destory()
198
- }
253
+ this.renderer.destroy()
254
+ this.watcher?.destroy()
255
+ this.layouter?.destroy()
256
+
257
+ this.canvasManager.destory()
258
+ this.hitCanvasManager.destory()
259
+ this.imageManager.destory()
199
260
 
200
261
  this.canvas.destroy()
201
262
  this.canvas = null
@@ -219,6 +280,8 @@ export class Leafer extends Group implements ILeafer {
219
280
  this.moveLayer = undefined
220
281
 
221
282
  this.parent = undefined
283
+
284
+ super.destroy()
222
285
  }
223
286
  }
224
287
  }