@leafer-ui/miniapp 1.5.2 → 1.6.0

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-ui/miniapp",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
4
4
  "description": "@leafer-ui/miniapp",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -32,15 +32,16 @@
32
32
  "leaferjs"
33
33
  ],
34
34
  "dependencies": {
35
- "@leafer/core": "1.5.2",
36
- "@leafer/miniapp": "1.5.2",
37
- "@leafer/partner": "1.5.2",
38
- "@leafer/interface": "1.5.2",
39
- "@leafer-ui/draw": "1.5.2",
40
- "@leafer-ui/core": "1.5.2",
41
- "@leafer-ui/interaction-miniapp": "1.5.2",
42
- "@leafer-ui/partner": "1.5.2",
43
- "@leafer-ui/interface": "1.5.2",
44
- "@leafer-in/interface": "1.5.2"
35
+ "@leafer/core": "1.6.0",
36
+ "@leafer/canvas-miniapp": "1.6.0",
37
+ "@leafer/image-miniapp": "1.6.0",
38
+ "@leafer/partner": "1.6.0",
39
+ "@leafer/interface": "1.6.0",
40
+ "@leafer-ui/draw": "1.6.0",
41
+ "@leafer-ui/core": "1.6.0",
42
+ "@leafer-ui/interaction-miniapp": "1.6.0",
43
+ "@leafer-ui/partner": "1.6.0",
44
+ "@leafer-ui/interface": "1.6.0",
45
+ "@leafer-in/interface": "1.6.0"
45
46
  }
46
47
  }
package/src/core.ts ADDED
@@ -0,0 +1,143 @@
1
+ export * from '@leafer/core'
2
+
3
+ export * from '@leafer/canvas-miniapp'
4
+ export * from '@leafer/image-miniapp'
5
+
6
+ import { ICanvasType, ICreator, IExportFileType, IExportImageType, IFunction, IObject, IMiniappSelect, IMiniappSizeView, IBoundsData } from '@leafer/interface'
7
+ import { Platform, Creator, FileHelper, defineKey } from '@leafer/core'
8
+
9
+ import { LeaferCanvas } from '@leafer/canvas-miniapp'
10
+ import { LeaferImage } from '@leafer/image-miniapp'
11
+
12
+
13
+ const { mineType, fileType } = FileHelper
14
+
15
+
16
+ Object.assign(Creator, {
17
+ canvas: (options?, manager?) => new LeaferCanvas(options, manager),
18
+ image: (options) => new LeaferImage(options)
19
+ } as ICreator)
20
+
21
+
22
+ export function useCanvas(_canvasType: ICanvasType, app?: IObject): void {
23
+ Platform.origin = {
24
+ createCanvas: (width: number, height: number, _format?: string) => {
25
+ const data = { type: '2d', width, height }
26
+ return app.createOffscreenCanvas ? app.createOffscreenCanvas(data) : app.createOffScreenCanvas(data) // fix: 微信小游戏居然使用的是Screen大写的 wx.createOffScreenCanvas() [踩坑]
27
+ },
28
+ canvasToDataURL: (canvas: IObject, type?: IExportImageType, quality?: number) => canvas.toDataURL(mineType(type), quality),
29
+ canvasToBolb: (canvas: IObject, type?: IExportFileType, quality?: number) => canvas.toBuffer(type, { quality }),
30
+ canvasSaveAs: (canvas: IObject, filePath: string, quality?: any) => {
31
+ let data: string = canvas.toDataURL(mineType(fileType(filePath)), quality)
32
+ data = data.substring(data.indexOf('64,') + 3)
33
+ return Platform.origin.download(data, filePath)
34
+ },
35
+ download(data: string, filePath: string): Promise<void> {
36
+ return new Promise((resolve, reject) => {
37
+ let toAlbum: boolean
38
+ if (!filePath.includes('/')) {
39
+ filePath = `${app.env.USER_DATA_PATH}/` + filePath
40
+ toAlbum = true
41
+ }
42
+ const fs = app.getFileSystemManager()
43
+ fs.writeFile({
44
+ filePath,
45
+ data,
46
+ encoding: 'base64',
47
+ success() {
48
+ if (toAlbum) {
49
+ Platform.miniapp.saveToAlbum(filePath).then(() => {
50
+ fs.unlink({ filePath })
51
+ resolve()
52
+ })
53
+ } else {
54
+ resolve()
55
+ }
56
+
57
+ },
58
+ fail(error: any) {
59
+ reject(error)
60
+ }
61
+ })
62
+ })
63
+ },
64
+ loadImage(src: string): Promise<HTMLImageElement> {
65
+ return new Promise((resolve, reject) => {
66
+ const img = Platform.canvas.view.createImage()
67
+ img.onload = () => { resolve(img) }
68
+ img.onerror = (error: any) => { reject(error) }
69
+ img.src = Platform.image.getRealURL(src)
70
+ })
71
+ },
72
+ noRepeat: 'repeat-x' // fix: 微信小程序 createPattern 直接使用 no-repeat 有bug,导致无法显示
73
+ }
74
+
75
+ Platform.miniapp = {
76
+ select(name: string): IMiniappSelect {
77
+ return app.createSelectorQuery().select(name)
78
+ },
79
+ getBounds(select: IMiniappSelect): Promise<IBoundsData> {
80
+ return new Promise((resolve) => {
81
+ select.boundingClientRect().exec((res: any) => {
82
+ const rect = res[1]
83
+ resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height })
84
+ })
85
+ })
86
+ },
87
+ getSizeView(select: IMiniappSelect): Promise<IMiniappSizeView> {
88
+ return new Promise((resolve) => {
89
+ select.fields({ node: true, size: true }).exec((res: any) => {
90
+ const data = res[0]
91
+ resolve({ view: data.node, width: data.width, height: data.height })
92
+ })
93
+ })
94
+ },
95
+ saveToAlbum(path: string): Promise<any> {
96
+ return new Promise((resolve) => {
97
+ app.getSetting({
98
+ success: (res: any) => {
99
+ if (res.authSetting['scope.writePhotosAlbum']) {
100
+ app.saveImageToPhotosAlbum({
101
+ filePath: path,
102
+ success() { resolve(true) }
103
+ })
104
+ } else {
105
+ app.authorize({
106
+ scope: 'scope.writePhotosAlbum',
107
+ success: () => {
108
+ app.saveImageToPhotosAlbum({
109
+ filePath: path,
110
+ success() { resolve(true) }
111
+ })
112
+ },
113
+ fail: () => { }
114
+ })
115
+ }
116
+ }
117
+ })
118
+ })
119
+ },
120
+ onWindowResize(fun: IFunction): void {
121
+ app.onWindowResize(fun)
122
+ },
123
+ offWindowResize(fun: IFunction): void {
124
+ app.offWindowResize(fun)
125
+ }
126
+ }
127
+
128
+ Platform.event = {
129
+ stopDefault(_origin: IObject): void { },
130
+ stopNow(_origin: IObject): void { },
131
+ stop(_origin: IObject): void { }
132
+ }
133
+
134
+ Platform.canvas = Creator.canvas()
135
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
136
+ }
137
+
138
+ Platform.name = 'miniapp'
139
+ Platform.requestRender = function (render: IFunction): void {
140
+ const { view } = Platform.renderCanvas || Platform.canvas
141
+ view.requestAnimationFrame ? view.requestAnimationFrame(render) : setTimeout(render, 16) // fix 抖音小程序
142
+ }
143
+ defineKey(Platform, 'devicePixelRatio', { get() { return Math.max(1, wx.getSystemInfoSync().pixelRatio) } })
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from '@leafer-ui/interface'
2
2
 
3
- export * from '@leafer/miniapp'
3
+ export * from './core'
4
4
  export * from '@leafer/partner'
5
5
 
6
6
  export * from '@leafer-ui/core'
@@ -8,7 +8,7 @@ export * from '@leafer-ui/interaction-miniapp'
8
8
  export * from '@leafer-ui/partner'
9
9
 
10
10
  import { ICreator } from '@leafer/interface'
11
- import { useCanvas, Creator, LeaferCanvas } from '@leafer/miniapp'
11
+ import { useCanvas, Creator, LeaferCanvas } from './core'
12
12
  import { Leafer } from '@leafer-ui/draw'
13
13
  import { HitCanvasManager } from '@leafer-ui/core'
14
14
  import { Interaction } from '@leafer-ui/interaction-miniapp'
package/types/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  export * from '@leafer-ui/interface';
2
- export * from '@leafer/miniapp';
2
+ import { ICanvasType, IObject } from '@leafer/interface';
3
3
  export * from '@leafer/partner';
4
4
  export * from '@leafer-ui/core';
5
5
  export * from '@leafer-ui/interaction-miniapp';
6
6
  export * from '@leafer-ui/partner';
7
+ export * from '@leafer/core';
8
+ export * from '@leafer/canvas-miniapp';
9
+ export * from '@leafer/image-miniapp';
10
+
11
+ declare function useCanvas(_canvasType: ICanvasType, app?: IObject): void;
12
+
13
+ export { useCanvas };