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

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.9",
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.9",
23
+ "@leafer/data": "1.0.0-beta.9",
24
+ "@leafer/event": "1.0.0-beta.9",
25
+ "@leafer/platform": "1.0.0-beta.9",
26
+ "@leafer/debug": "1.0.0-beta.9"
27
+ },
28
+ "devDependencies": {
29
+ "@leafer/interface": "1.0.0-beta.9"
30
+ }
21
31
  }
@@ -0,0 +1,108 @@
1
+ import { IResizeEventListener, IAutoBounds, IScreenSizeData, IFunction, IMiniappSelect, IObject } from '@leafer/interface'
2
+ import { LeaferCanvasBase, canvasPatch, 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?: IObject): void {
35
+ if (!view) {
36
+ view = {}
37
+ this.__createView()
38
+ } else {
39
+ this.view = view.view || view
40
+ Platform.canvas = this
41
+ }
42
+ this.__createContext()
43
+ const { width, height, pixelRatio } = this.config
44
+ const size = { width: width || view.width, height: height || view.height, pixelRatio }
45
+ this.resize(size)
46
+
47
+ // fix roundRect
48
+ if (this.context.roundRect) {
49
+ this.roundRect = function (x: number, y: number, width: number, height: number, radius?: number | number[]): void {
50
+ this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius)
51
+ }
52
+ }
53
+ canvasPatch((this.context as IObject).__proto__)
54
+
55
+ }
56
+
57
+ protected __createView(): void {
58
+ this.view = Platform.origin.createCanvas(1, 1)
59
+ }
60
+
61
+ public updateViewSize(): void {
62
+ const { width, height, pixelRatio } = this
63
+
64
+ this.view.width = width * pixelRatio
65
+ this.view.height = height * pixelRatio
66
+ }
67
+
68
+ public updateClientBounds(callback?: IFunction): void {
69
+ if (this.viewSelect) Platform.miniapp.getBounds(this.viewSelect).then(bounds => {
70
+ this.clientBounds = bounds
71
+ if (callback) callback()
72
+ })
73
+ }
74
+
75
+
76
+ public startAutoLayout(_autoBounds: IAutoBounds, listener: IResizeEventListener): void {
77
+ if (!this.offscreen) {
78
+ this.resizeListener = listener
79
+ this.checkSize = this.checkSize.bind(this)
80
+ Platform.miniapp.onWindowResize(this.checkSize)
81
+ }
82
+ }
83
+
84
+ public checkSize(): void {
85
+ if (this.viewSelect) {
86
+ setTimeout(() => {
87
+ this.updateClientBounds(() => {
88
+ const { width, height } = this.clientBounds
89
+ const { pixelRatio } = this
90
+ const size = { width, height, pixelRatio }
91
+ if (!this.isSameSize(size)) {
92
+ const oldSize = {} as IScreenSizeData
93
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
94
+ this.resize(size)
95
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
96
+ }
97
+ })
98
+ }, 500)
99
+ }
100
+ }
101
+
102
+ public stopAutoLayout(): void {
103
+ this.autoLayout = false
104
+ this.resizeListener = null
105
+ Platform.miniapp.offWindowResize(this.checkSize)
106
+ }
107
+
108
+ }
package/src/index.ts CHANGED
@@ -0,0 +1 @@
1
+ export { LeaferCanvas } from './LeaferCanvas'