@leafer/canvas-miniapp 1.0.0-beta → 1.0.0-beta.11

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