@leafer/miniapp 1.0.0 → 1.0.2

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