@leafer-ui/external 1.0.0-beta → 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 +4 -4
  2. package/src/Export.ts +79 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/external",
3
- "version": "1.0.0-beta",
3
+ "version": "1.0.0-beta.10",
4
4
  "description": "@leafer-ui/external",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,10 +19,10 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer-ui/paint": "1.0.0-beta",
23
- "@leafer-ui/effect": "1.0.0-beta"
22
+ "@leafer-ui/paint": "1.0.0-beta.10",
23
+ "@leafer-ui/effect": "1.0.0-beta.10"
24
24
  },
25
25
  "devDependencies": {
26
- "@leafer-ui/interface": "1.0.0-beta"
26
+ "@leafer-ui/interface": "1.0.0-beta.10"
27
27
  }
28
28
  }
package/src/Export.ts CHANGED
@@ -1,3 +1,81 @@
1
- export const Export = {
1
+ import { ILeaf, IExportFileType, IBlob, IFunction } from '@leafer/interface'
2
+ import { TaskProcessor } from '@leafer/core'
2
3
 
4
+ import { IExportModule, IExportOptions, IExportResult, IExportResultFunction } from '@leafer-ui/interface'
5
+
6
+
7
+ export const Export: IExportModule = {
8
+
9
+ export(leaf: ILeaf, filename: IExportFileType | string, options?: IExportOptions | number | boolean): Promise<IExportResult> {
10
+
11
+ return addTask((success: IExportResultFunction) =>
12
+
13
+ new Promise((resolve: IFunction) => {
14
+
15
+ const { leafer } = leaf
16
+ if (leafer) {
17
+
18
+ leafer.waitViewLoaded(async () => {
19
+
20
+ let quality, blob: boolean
21
+ let { canvas } = leafer
22
+ let { unreal } = canvas
23
+
24
+ if (unreal) {
25
+ canvas = canvas.getSameCanvas()
26
+ canvas.backgroundColor = leafer.config.fill
27
+ leafer.__render(canvas, {})
28
+ }
29
+
30
+ switch (typeof options) {
31
+ case 'object':
32
+ if (options.quality) quality = options.quality
33
+ if (options.blob) blob = true
34
+ break
35
+ case 'number':
36
+ quality = options
37
+ break
38
+ case 'boolean':
39
+ blob = options
40
+ }
41
+
42
+ let data: IBlob | string | boolean
43
+
44
+ if (filename.includes('.')) {
45
+ data = await canvas.saveAs(filename, quality)
46
+ } else if (blob) {
47
+ data = await canvas.toBlob(filename, quality)
48
+ } else {
49
+ data = canvas.toDataURL(filename, quality)
50
+ }
51
+
52
+ success({ data })
53
+ resolve()
54
+
55
+ if (unreal) canvas.recycle()
56
+
57
+ })
58
+
59
+ } else {
60
+ success({ data: false })
61
+ resolve()
62
+ }
63
+
64
+ })
65
+
66
+ )
67
+
68
+ }
69
+
70
+ }
71
+
72
+
73
+ let tasker: TaskProcessor
74
+
75
+ function addTask(task: IFunction): Promise<IExportResult> {
76
+ if (!tasker) tasker = new TaskProcessor()
77
+
78
+ return new Promise((resolve: IExportResultFunction) => {
79
+ tasker.add(async () => await task(resolve), null, true)
80
+ })
3
81
  }