@leafer-ui/app 1.0.0-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-ui/app
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@leafer-ui/app",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer-ui/app",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/ui.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/ui/tree/main/packages/app",
14
+ "bugs": "https://github.com/leaferjs/ui/issues",
15
+ "keywords": [
16
+ "leaferui",
17
+ "leafer-ui",
18
+ "leaferjs"
19
+ ],
20
+ "dependencies": {
21
+ "@leafer/core": "1.0.0-alpha.1",
22
+ "@leafer-ui/display": "1.0.0-alpha.1"
23
+ },
24
+ "devDependencies": {
25
+ "@leafer/interface": "1.0.0-alpha.1",
26
+ "@leafer-ui/interface": "1.0.0-alpha.1"
27
+ }
28
+ }
package/src/Leafer.ts ADDED
@@ -0,0 +1,240 @@
1
+ import { ILayouter, ILeafer, ILeaferCanvas, IRenderer, ICreator, ISelector, IWatcher, IInteraction, ILeaferConfig, ICanvasManager, IHitCanvasManager, IImageManager, IAutoBounds, IScreenSizeData, IResizeEvent, IObject, ILeaf, IEventListenerId, ITransformEventData } from '@leafer/interface'
2
+ import { registerUI, AutoBounds, LayoutEvent, ResizeEvent, MoveEvent, ZoomEvent, CanvasManager, HitCanvasManager, ImageManager, DataHelper, LeafHelper, Creator } from '@leafer/core'
3
+
4
+ import { Group } from '@leafer-ui/display'
5
+
6
+ import { SuperLeafer } from './SuperLeafer'
7
+
8
+
9
+ @registerUI()
10
+ export class Leafer extends Group implements ILeafer {
11
+
12
+ public creator: ICreator
13
+
14
+ public get isSupperLeafer(): boolean { return false }
15
+
16
+ public parent?: SuperLeafer
17
+
18
+ public running: boolean
19
+
20
+ @resizeType()
21
+ public width: number
22
+
23
+ @resizeType()
24
+ public height: number
25
+
26
+ @resizeType()
27
+ public pixelRatio: number
28
+
29
+ public config: ILeaferConfig = {
30
+ zoom: true,
31
+ move: true,
32
+ start: true,
33
+ pixelRatio: devicePixelRatio
34
+ }
35
+
36
+ public autoLayout?: IAutoBounds
37
+
38
+ // manager
39
+
40
+ public canvas: ILeaferCanvas
41
+ public renderer: IRenderer
42
+
43
+ public watcher: IWatcher
44
+ public layouter: ILayouter
45
+
46
+ public selector?: ISelector
47
+ public interaction?: IInteraction
48
+
49
+ public canvasManager: ICanvasManager
50
+ public hitCanvasManager?: IHitCanvasManager
51
+ public imageManager: IImageManager
52
+
53
+ public zoomLayer?: ILeaf
54
+ public moveLayer?: ILeaf
55
+ public transformData?: ITransformEventData
56
+
57
+ protected __eventIds: IEventListenerId[] = []
58
+
59
+ constructor(userConfig?: ILeaferConfig, parentLeafer?: ILeafer) {
60
+ super()
61
+
62
+ this.creator = Creator
63
+
64
+ const { config } = this
65
+
66
+ this.__setAsLeafer()
67
+ this.__setConfig(userConfig)
68
+
69
+ // render
70
+ this.canvas = Creator.canvas(config)
71
+ this.renderer = Creator.renderer(this, this.canvas, config)
72
+
73
+ // layout
74
+ if (this.isSupperLeafer) {
75
+ this.__level = 1
76
+ } else {
77
+
78
+ this.watcher = Creator.watcher(this)
79
+ this.layouter = Creator.layouter(this)
80
+
81
+ }
82
+
83
+ this.__checkAutoLayout(config)
84
+
85
+ // interaction / manager
86
+ if (parentLeafer) {
87
+
88
+ parentLeafer.selector?.defaultPath.unshift(this)
89
+
90
+ this.selector = parentLeafer.selector
91
+ this.interaction = parentLeafer.interaction
92
+
93
+ this.canvasManager = parentLeafer.canvasManager
94
+ this.hitCanvasManager = parentLeafer.hitCanvasManager
95
+ this.imageManager = parentLeafer.imageManager
96
+
97
+ if (parentLeafer.running) setTimeout(this.start.bind(this))
98
+
99
+ } else {
100
+
101
+ this.selector = Creator.selector(this)
102
+ this.interaction = Creator.interaction(this, this.canvas, this.selector, config)
103
+
104
+ this.canvasManager = new CanvasManager(this)
105
+ this.hitCanvasManager = new HitCanvasManager(this)
106
+ this.imageManager = new ImageManager(this, config)
107
+
108
+ if (config.start) setTimeout(this.start.bind(this))
109
+
110
+ }
111
+
112
+ this.canvasManager.add(this.canvas)
113
+ this.__listenEvents()
114
+
115
+ }
116
+
117
+ protected __listenEvents(): void {
118
+ this.once(LayoutEvent.END, this.__setAsRoot.bind(this))
119
+ }
120
+
121
+ protected __removeListenEvents(): void {
122
+ this.off__(this.__eventIds)
123
+ }
124
+
125
+ protected __setConfig(userConfig?: ILeaferConfig): void {
126
+ if (userConfig) this.config = DataHelper.default(userConfig, this.config)
127
+ }
128
+
129
+ protected __checkAutoLayout(config: ILeaferConfig): void {
130
+ if (!config.width || !config.height) {
131
+ this.autoLayout = new AutoBounds(config)
132
+ this.canvas.autoLayout(this.autoLayout, this.__onResize.bind(this))
133
+ }
134
+ }
135
+
136
+ public start(): void {
137
+ if (!this.running) {
138
+ this.__interactiveWindow()
139
+ this.interaction?.start()
140
+ this.renderer.start()
141
+ this.layouter.start()
142
+ this.watcher.start()
143
+ this.running = true
144
+ }
145
+ }
146
+
147
+ public stop(): void {
148
+ if (this.running) {
149
+ this.interaction?.stop()
150
+ this.watcher.stop()
151
+ this.layouter.stop()
152
+ this.renderer.stop()
153
+ this.running = false
154
+ }
155
+ }
156
+
157
+ protected __interactiveWindow(): void {
158
+ const { zoom, move } = this.config
159
+ if (move) {
160
+ const { MOVE } = MoveEvent
161
+ const { ZOOM } = ZoomEvent
162
+ if (!this.hasEvent(MOVE)) this.__eventIds.push(this.on__(MOVE, (e: MoveEvent) => { LeafHelper.moveByWorld(this, e.moveX, e.moveY) }))
163
+ if (zoom && !this.hasEvent(ZOOM)) this.__eventIds.push(this.on__(ZOOM, (e: ZoomEvent) => { LeafHelper.zoomByWorld(this, e.scale, e) }))
164
+ }
165
+ }
166
+
167
+ public resize(size: IScreenSizeData): void {
168
+ if (this.canvas.isSameSize(size)) return
169
+ const { width, height, pixelRatio } = this.canvas
170
+ const oldSize = { width, height, pixelRatio }
171
+ this.canvas.resize(size)
172
+ this.__onResize(new ResizeEvent(size, oldSize))
173
+ }
174
+
175
+ protected __onResize(event: IResizeEvent): void {
176
+ this.emitEvent(event)
177
+ setTimeout(() => { this.canvasManager.clearRecycled() }, 0)
178
+ }
179
+
180
+ public destory(): void {
181
+ if (this.canvas) {
182
+ super.destroy()
183
+
184
+ this.__removeListenEvents()
185
+
186
+ if (!this.parent) {
187
+ this.interaction?.destroy()
188
+ this.selector.destroy()
189
+
190
+ this.renderer.destroy()
191
+ this.watcher.destroy()
192
+ this.layouter.destroy()
193
+
194
+ this.canvasManager.destory()
195
+ this.hitCanvasManager.destory()
196
+ this.imageManager.destory()
197
+ }
198
+
199
+ this.canvas.destroy()
200
+ this.canvas = undefined
201
+
202
+ this.creator = undefined
203
+ this.config = undefined
204
+
205
+ this.interaction = undefined
206
+ this.selector = undefined
207
+
208
+
209
+ this.renderer = undefined
210
+ this.watcher = undefined
211
+ this.layouter = undefined
212
+
213
+ this.canvasManager = undefined
214
+ this.hitCanvasManager = undefined
215
+ this.imageManager = undefined
216
+
217
+ this.zoomLayer = undefined
218
+ this.moveLayer = undefined
219
+
220
+ this.parent = undefined
221
+ }
222
+ }
223
+ }
224
+
225
+ function resizeType() {
226
+ return (target: Leafer, key: string) => {
227
+ const property: IObject & ThisType<Leafer> = {
228
+ get() {
229
+ return (this.canvas as IObject)[key]
230
+ },
231
+ set(value: number) {
232
+ const { width, height, pixelRatio } = this.canvas
233
+ const data = { width, height, pixelRatio } as IObject
234
+ data[key] = value
235
+ this.resize(data as IScreenSizeData)
236
+ }
237
+ }
238
+ Object.defineProperty(target, key, property)
239
+ }
240
+ }
@@ -0,0 +1,68 @@
1
+ import { ILeaferConfig, IResizeEvent, ILeaferCanvas, IRenderOptions } from '@leafer/interface'
2
+ import { registerUI, DataHelper, RenderEvent } from '@leafer/core'
3
+
4
+ import { IGroup } from '@leafer-ui/interface'
5
+
6
+ import { Leafer } from './Leafer'
7
+
8
+
9
+ @registerUI()
10
+ export class SuperLeafer extends Leafer implements IGroup {
11
+
12
+ public get isSupperLeafer(): boolean { return true }
13
+
14
+ public children: Leafer[] = []
15
+
16
+ public start(): void {
17
+ if (!this.running) {
18
+ this.children.forEach(leafer => { leafer.start() })
19
+ this.running = false
20
+ }
21
+ }
22
+
23
+ public stop(): void {
24
+ if (this.running) {
25
+ this.children.forEach(leafer => { leafer.stop() })
26
+ this.running = false
27
+ }
28
+ }
29
+
30
+ public addLeafer(merge?: ILeaferConfig): Leafer {
31
+ const leafer = new Leafer(this.__getChildConfig(merge), this)
32
+ this.add(leafer)
33
+ this.__eventIds.push(leafer.on__(RenderEvent.END, this.renderer.start, this.renderer))
34
+ return leafer
35
+ }
36
+
37
+ public __render(canvas: ILeaferCanvas, options: IRenderOptions): void {
38
+ this.children.forEach(leafer => { canvas.copy(leafer.canvas) })
39
+ }
40
+
41
+ public __onResize(event: IResizeEvent): void {
42
+ this.emitEvent(event)
43
+ this.children.forEach(leafer => { leafer.resize(event) })
44
+ }
45
+
46
+ protected __getChildConfig(userConfig?: ILeaferConfig): ILeaferConfig {
47
+ let old = { ...this.config }
48
+ userConfig = userConfig ? DataHelper.default(userConfig, old) : old
49
+ userConfig.view = undefined
50
+ if (this.autoLayout) {
51
+ userConfig.width = this.width
52
+ userConfig.height = this.height
53
+ userConfig.pixelRatio = this.pixelRatio
54
+ }
55
+ return userConfig
56
+ }
57
+
58
+ public destory(): void {
59
+ if (this.children.length) {
60
+ super.destory()
61
+ this.children.forEach(leafer => {
62
+ leafer.destory()
63
+ })
64
+ this.children.length = 0
65
+ }
66
+ }
67
+
68
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { Leafer } from './Leafer'
2
+ export { SuperLeafer } from './SuperLeafer'