@leafer/miniapp 1.0.1 → 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.
- package/package.json +5 -5
- package/src/index.ts +104 -104
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/miniapp",
|
|
3
|
-
"version": "1.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.
|
|
26
|
-
"@leafer/canvas-miniapp": "1.0.
|
|
27
|
-
"@leafer/image-miniapp": "1.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.
|
|
30
|
+
"@leafer/interface": "1.0.2"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/src/index.ts
CHANGED
|
@@ -20,121 +20,121 @@ Object.assign(Creator, {
|
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
export function useCanvas(_canvasType: ICanvasType, app?: IObject): void {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
133
|
-
|
|
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 {
|
|
139
|
-
|
|
140
|
-
|
|
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) } })
|