@leafer/miniapp-core 2.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/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +33 -0
- package/src/index.ts +150 -0
- package/types/index.d.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, Chao (Leafer) Wan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @leafer/miniapp-core
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leafer/miniapp-core",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "@leafer/miniapp-core",
|
|
5
|
+
"author": "Chao (Leafer) Wan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"types",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/leaferjs/leafer.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/leaferjs/leafer/tree/main/packages/core/miniapp-core",
|
|
19
|
+
"bugs": "https://github.com/leaferjs/leafer/issues",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"leafer miniapp",
|
|
22
|
+
"leafer-miniapp",
|
|
23
|
+
"leafer",
|
|
24
|
+
"leaferjs"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@leafer/core": "2.0.2",
|
|
28
|
+
"@leafer/canvas-miniapp": "2.0.2",
|
|
29
|
+
"@leafer/image-miniapp": "2.0.2",
|
|
30
|
+
"@leafer/partner": "2.0.2",
|
|
31
|
+
"@leafer/interface": "2.0.2"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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, IResponseType, IFunction, IObject, IMiniappSelect, IMiniappSizeView, IBoundsData, IImageCrossOrigin, ILeaferImage } 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
|
+
Object.assign(Creator, {
|
|
14
|
+
canvas: (options?, manager?) => new LeaferCanvas(options, manager),
|
|
15
|
+
image: (options) => new LeaferImage(options)
|
|
16
|
+
} as ICreator)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export function useCanvas(_canvasType: ICanvasType, app?: IObject): void {
|
|
20
|
+
Platform.origin = {
|
|
21
|
+
createCanvas: (width: number, height: number, _format?: string) => {
|
|
22
|
+
const data = { type: '2d', width, height }
|
|
23
|
+
return app.createOffscreenCanvas ? app.createOffscreenCanvas(data) : app.createOffScreenCanvas(data) // fix: 微信小游戏居然使用的是Screen大写的 wx.createOffScreenCanvas() [踩坑]
|
|
24
|
+
},
|
|
25
|
+
canvasToDataURL: (canvas: IObject, type?: IExportImageType, quality?: number) => canvas.toDataURL(FileHelper.mimeType(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(FileHelper.mimeType(FileHelper.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 })
|
|
48
|
+
resolve()
|
|
49
|
+
})
|
|
50
|
+
} else {
|
|
51
|
+
resolve()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
},
|
|
55
|
+
fail(error: any) {
|
|
56
|
+
reject(error)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
},
|
|
61
|
+
loadImage(src: string, _crossOrigin?: IImageCrossOrigin, _leaferImage?: ILeaferImage): 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
|
+
loadContent(url: string, responseType: IResponseType = 'text'): Promise<any> {
|
|
70
|
+
return new Promise((resolve, reject) =>
|
|
71
|
+
app.request({
|
|
72
|
+
url,
|
|
73
|
+
responseType: responseType === 'arrayBuffer' ? 'arraybuffer' : 'text',
|
|
74
|
+
success: (res: any) => resolve(responseType === 'json' && typeof res.data === 'string' ? JSON.parse(res.data) : res.data),
|
|
75
|
+
fail: reject
|
|
76
|
+
})
|
|
77
|
+
)
|
|
78
|
+
},
|
|
79
|
+
noRepeat: 'repeat-x' // fix: 微信小程序 createPattern 直接使用 no-repeat 有bug,导致无法显示
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Platform.miniapp = {
|
|
83
|
+
select(name: string): IMiniappSelect {
|
|
84
|
+
return app.createSelectorQuery().select(name)
|
|
85
|
+
},
|
|
86
|
+
getBounds(select: IMiniappSelect): Promise<IBoundsData> {
|
|
87
|
+
return new Promise((resolve) => {
|
|
88
|
+
select.boundingClientRect().exec((res: any) => {
|
|
89
|
+
const rect = res[1]
|
|
90
|
+
resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height })
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
},
|
|
94
|
+
getSizeView(select: IMiniappSelect): Promise<IMiniappSizeView> {
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
select.fields({ node: true, size: true }).exec((res: any) => {
|
|
97
|
+
const data = res[0]
|
|
98
|
+
resolve({ view: data.node, width: data.width, height: data.height })
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
saveToAlbum(path: string): Promise<any> {
|
|
103
|
+
return new Promise((resolve) => {
|
|
104
|
+
app.getSetting({
|
|
105
|
+
success: (res: any) => {
|
|
106
|
+
if (res.authSetting['scope.writePhotosAlbum']) {
|
|
107
|
+
app.saveImageToPhotosAlbum({
|
|
108
|
+
filePath: path,
|
|
109
|
+
success() { resolve(true) }
|
|
110
|
+
})
|
|
111
|
+
} else {
|
|
112
|
+
app.authorize({
|
|
113
|
+
scope: 'scope.writePhotosAlbum',
|
|
114
|
+
success: () => {
|
|
115
|
+
app.saveImageToPhotosAlbum({
|
|
116
|
+
filePath: path,
|
|
117
|
+
success() { resolve(true) }
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
fail: () => { }
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
},
|
|
127
|
+
onWindowResize(fun: IFunction): void {
|
|
128
|
+
app.onWindowResize(fun)
|
|
129
|
+
},
|
|
130
|
+
offWindowResize(fun: IFunction): void {
|
|
131
|
+
app.offWindowResize(fun)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
Platform.event = {
|
|
136
|
+
stopDefault(_origin: IObject): void { },
|
|
137
|
+
stopNow(_origin: IObject): void { },
|
|
138
|
+
stop(_origin: IObject): void { }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
Platform.canvas = Creator.canvas()
|
|
142
|
+
Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
Platform.name = 'miniapp'
|
|
146
|
+
Platform.requestRender = function (render: IFunction): void {
|
|
147
|
+
const { view } = Platform.renderCanvas || Platform.canvas
|
|
148
|
+
view.requestAnimationFrame ? view.requestAnimationFrame(render) : setTimeout(render, 16) // fix 抖音小程序
|
|
149
|
+
}
|
|
150
|
+
defineKey(Platform, 'devicePixelRatio', { get() { return Math.max(1, wx.getWindowInfo ? wx.getWindowInfo().pixelRatio : wx.getSystemInfoSync().pixelRatio) } })
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from '@leafer/core';
|
|
2
|
+
export * from '@leafer/canvas-miniapp';
|
|
3
|
+
export * from '@leafer/image-miniapp';
|
|
4
|
+
import { ICanvasType, IObject } from '@leafer/interface';
|
|
5
|
+
|
|
6
|
+
declare function useCanvas(_canvasType: ICanvasType, app?: IObject): void;
|
|
7
|
+
|
|
8
|
+
export { useCanvas };
|