@leafer/miniapp 1.0.0-beta.6 → 1.0.0-beta.8

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 +7 -7
  2. package/src/index.ts +132 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/miniapp",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.8",
4
4
  "description": "@leafer/miniapp",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,13 +19,13 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/core": "1.0.0-beta.6",
23
- "@leafer/partner": "1.0.0-beta.6",
24
- "@leafer/canvas-miniapp": "1.0.0-beta.6",
25
- "@leafer/interaction-miniapp": "1.0.0-beta.6",
26
- "@leafer/image-miniapp": "1.0.0-beta.6"
22
+ "@leafer/core": "1.0.0-beta.8",
23
+ "@leafer/partner": "1.0.0-beta.8",
24
+ "@leafer/canvas-miniapp": "1.0.0-beta.8",
25
+ "@leafer/interaction-miniapp": "1.0.0-beta.8",
26
+ "@leafer/image-miniapp": "1.0.0-beta.8"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-beta.6"
29
+ "@leafer/interface": "1.0.0-beta.8"
30
30
  }
31
31
  }
package/src/index.ts CHANGED
@@ -0,0 +1,132 @@
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(src: any): 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 = src
67
+ })
68
+ }
69
+ }
70
+
71
+ Platform.miniapp = {
72
+ select(name: string): IMiniappSelect {
73
+ return app.createSelectorQuery().select(name)
74
+ },
75
+ getBounds(select: IMiniappSelect): Promise<IBoundsData> {
76
+ return new Promise((resolve) => {
77
+ select.boundingClientRect().exec((res: any) => {
78
+ const rect = res[1]
79
+ resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height })
80
+ })
81
+ })
82
+ },
83
+ getSizeView(select: IMiniappSelect): Promise<IMiniappSizeView> {
84
+ return new Promise((resolve) => {
85
+ select.fields({ node: true, size: true }).exec((res: any) => {
86
+ const data = res[0]
87
+ resolve({ view: data.node, width: data.width, height: data.height })
88
+ })
89
+ })
90
+ },
91
+ saveToAlbum(path: string): Promise<any> {
92
+ return new Promise((resolve) => {
93
+ app.getSetting({
94
+ success: (res: any) => {
95
+ if (res.authSetting['scope.writePhotosAlbum']) {
96
+ app.saveImageToPhotosAlbum({
97
+ filePath: path,
98
+ success() { resolve(true) }
99
+ })
100
+ } else {
101
+ app.authorize({
102
+ scope: 'scope.writePhotosAlbum',
103
+ success: () => {
104
+ app.saveImageToPhotosAlbum({
105
+ filePath: path,
106
+ success() { resolve(true) }
107
+ })
108
+ },
109
+ fail: () => { }
110
+ })
111
+ }
112
+ }
113
+ })
114
+ })
115
+ },
116
+ onWindowResize(fun: IFunction): void {
117
+ app.onWindowResize(fun)
118
+ },
119
+ offWindowResize(fun: IFunction): void {
120
+ app.offWindowResize(fun)
121
+ }
122
+ }
123
+
124
+ Platform.canvas = Creator.canvas()
125
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
126
+ }
127
+ }
128
+
129
+ Platform.name = 'miniapp'
130
+ Platform.requestRender = function (render: IFunction): void { Platform.canvas.view.requestAnimationFrame(render) }
131
+ Platform.devicePixelRatio = wx.getSystemInfoSync().pixelRatio
132
+ Platform.realtimeLayout = true