@leafer/miniapp 1.0.0-bate → 1.0.0-beta.10

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 +12 -2
  2. package/src/index.ts +133 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/miniapp",
3
- "version": "1.0.0-bate",
3
+ "version": "1.0.0-beta.10",
4
4
  "description": "@leafer/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/core": "1.0.0-beta.10",
23
+ "@leafer/partner": "1.0.0-beta.10",
24
+ "@leafer/canvas-miniapp": "1.0.0-beta.10",
25
+ "@leafer/interaction-miniapp": "1.0.0-beta.10",
26
+ "@leafer/image-miniapp": "1.0.0-beta.10"
27
+ },
28
+ "devDependencies": {
29
+ "@leafer/interface": "1.0.0-beta.10"
30
+ }
21
31
  }
package/src/index.ts CHANGED
@@ -0,0 +1,133 @@
1
+ export * from '@leafer/core'
2
+ export * from '@leafer/partner'
3
+
4
+ export * from '@leafer/canvas-miniapp'
5
+ export * from '@leafer/image-miniapp'
6
+
7
+ import { ICanvasType, ICreator, IExportFileType, IExportImageType, IFunction, IObject, IMiniappSelect, IMiniappSizeView, IBoundsData } from '@leafer/interface'
8
+ import { Platform, Creator, FileHelper } from '@leafer/core'
9
+
10
+ import { LeaferCanvas } from '@leafer/canvas-miniapp'
11
+ import { LeaferImage } from '@leafer/image-miniapp'
12
+ import { Interaction } from '@leafer/interaction-miniapp'
13
+
14
+
15
+ const { mineType, fileType } = FileHelper
16
+
17
+
18
+ Object.assign(Creator, {
19
+ canvas: (options?, manager?) => new LeaferCanvas(options, manager),
20
+ image: (options) => new LeaferImage(options),
21
+ hitCanvas: (options?, manager?) => new LeaferCanvas(options, manager),
22
+
23
+ interaction: (target, canvas, selector, options?) => { return new Interaction(target, canvas, selector, options) }
24
+ } as ICreator)
25
+
26
+
27
+ export function useCanvas(_canvasType: ICanvasType, app?: IObject): void {
28
+ if (!Platform.origin) {
29
+ Platform.origin = {
30
+ createCanvas: (width: number, height: number, _format?: string) => app.createOffscreenCanvas({ type: '2d', width, height }),
31
+ canvasToDataURL: (canvas: IObject, type?: IExportImageType, quality?: number) => canvas.toDataURL(mineType(type), quality),
32
+ canvasToBolb: (canvas: IObject, type?: IExportFileType, quality?: number) => canvas.toBuffer(type, { quality }),
33
+ canvasSaveAs: (canvas: IObject, filePath: string, quality?: any) => {
34
+ return new Promise((resolve, reject) => {
35
+ let data: string = canvas.toDataURL(mineType(fileType(filePath)), quality)
36
+ data = data.substring(data.indexOf('64,') + 3)
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
+ })
52
+ }
53
+ resolve()
54
+ },
55
+ fail(error: any) {
56
+ reject(error)
57
+ }
58
+ })
59
+ })
60
+ },
61
+ loadImage(url: 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 = url
67
+ })
68
+ },
69
+ noRepeat: 'repeat-x' // fix: 微信小程序 createPattern 直接使用 no-repeat 有bug,导致无法显示
70
+ }
71
+
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 })
81
+ })
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 })
89
+ })
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
+ })
112
+ }
113
+ }
114
+ })
115
+ })
116
+ },
117
+ onWindowResize(fun: IFunction): void {
118
+ app.onWindowResize(fun)
119
+ },
120
+ offWindowResize(fun: IFunction): void {
121
+ app.offWindowResize(fun)
122
+ }
123
+ }
124
+
125
+ Platform.canvas = Creator.canvas()
126
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
127
+ }
128
+ }
129
+
130
+ Platform.name = 'miniapp'
131
+ Platform.requestRender = function (render: IFunction): void { Platform.canvas.view.requestAnimationFrame(render) }
132
+ Platform.devicePixelRatio = wx.getSystemInfoSync().pixelRatio
133
+ Platform.realtimeLayout = true