@leafer-ui/external 1.0.0-beta.7 → 1.0.0-beta.9

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