@leafer/canvas-miniapp 1.0.0-beta.7 → 1.0.0-beta.8

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/canvas-miniapp",
3
- "version": "1.0.0-beta.7",
3
+ "version": "1.0.0-beta.8",
4
4
  "description": "@leafer/canvas-miniapp",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -17,5 +17,15 @@
17
17
  "keywords": [
18
18
  "leafer",
19
19
  "leaferjs"
20
- ]
20
+ ],
21
+ "dependencies": {
22
+ "@leafer/canvas": "1.0.0-beta.8",
23
+ "@leafer/data": "1.0.0-beta.8",
24
+ "@leafer/event": "1.0.0-beta.8",
25
+ "@leafer/platform": "1.0.0-beta.8",
26
+ "@leafer/debug": "1.0.0-beta.8"
27
+ },
28
+ "devDependencies": {
29
+ "@leafer/interface": "1.0.0-beta.8"
30
+ }
21
31
  }
@@ -0,0 +1,99 @@
1
+ import { IResizeEventListener, IAutoBounds, IScreenSizeData, IFunction, IMiniappSelect } from '@leafer/interface'
2
+ import { LeaferCanvasBase, canvasSizeAttrs } from '@leafer/canvas'
3
+ import { Platform } from '@leafer/platform'
4
+ import { DataHelper } from '@leafer/data'
5
+ import { ResizeEvent } from '@leafer/event'
6
+
7
+ export class LeaferCanvas extends LeaferCanvasBase {
8
+
9
+ public get allowBackgroundColor(): boolean { return false }
10
+
11
+ protected viewSelect: IMiniappSelect
12
+ protected resizeListener: IResizeEventListener
13
+
14
+ public init(): void {
15
+ let { view } = this.config
16
+ if (view) {
17
+ if (typeof view === 'string') {
18
+ if (view[0] !== '#') view = '#' + view
19
+ this.viewSelect = Platform.miniapp.select(view)
20
+ } else if (view.fields) {
21
+ this.viewSelect = view
22
+ } else {
23
+ this.initView(view)
24
+ }
25
+
26
+ if (this.viewSelect) Platform.miniapp.getSizeView(this.viewSelect).then(sizeView => {
27
+ this.initView(sizeView)
28
+ })
29
+ } else {
30
+ this.initView()
31
+ }
32
+ }
33
+
34
+ protected initView(view?: any): void {
35
+ if (!view) {
36
+ view = {}
37
+ this.__createView()
38
+ } else {
39
+ this.view = view.view || view
40
+ }
41
+ this.__createContext()
42
+ const { width, height, pixelRatio } = this.config
43
+ const size = { width: width || view.width, height: height || view.height, pixelRatio }
44
+ this.resize(size)
45
+ }
46
+
47
+ protected __createView(): void {
48
+ this.view = Platform.origin.createCanvas(1, 1)
49
+ }
50
+
51
+ public updateViewSize(): void {
52
+ const { width, height, pixelRatio } = this
53
+
54
+ this.view.width = width * pixelRatio
55
+ this.view.height = height * pixelRatio
56
+ }
57
+
58
+ public updateClientBounds(callback?: IFunction): void {
59
+ if (this.viewSelect) Platform.miniapp.getBounds(this.viewSelect).then(bounds => {
60
+ this.clientBounds = bounds
61
+ if (callback) callback()
62
+ })
63
+ }
64
+
65
+
66
+ public startAutoLayout(_autoBounds: IAutoBounds, listener: IResizeEventListener): void {
67
+ if (!this.offscreen) {
68
+ this.resizeListener = listener
69
+ this.checkSize = this.checkSize.bind(this)
70
+ Platform.miniapp.onWindowResize(this.checkSize)
71
+ }
72
+ }
73
+
74
+ public checkSize(): void {
75
+ if (this.viewSelect) {
76
+ setTimeout(() => {
77
+ this.updateClientBounds(() => {
78
+ const { width, height } = this.clientBounds
79
+ const { pixelRatio } = this
80
+ const size = { width, height, pixelRatio }
81
+ if (!this.isSameSize(size)) {
82
+ const oldSize = {} as IScreenSizeData
83
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
84
+ this.resize(size)
85
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
86
+ }
87
+ })
88
+ }, 500)
89
+ }
90
+ }
91
+
92
+ public stopAutoLayout(): void {
93
+ this.autoLayout = false
94
+ this.resizeListener = null
95
+ Platform.miniapp.offWindowResize(this.checkSize)
96
+ }
97
+
98
+
99
+ }
package/src/index.ts CHANGED
@@ -0,0 +1 @@
1
+ export { LeaferCanvas } from './LeaferCanvas'