@leafer-in/export 1.7.0 → 1.9.0

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/dist/export.cjs CHANGED
@@ -1,43 +1,45 @@
1
- 'use strict';
2
-
3
- var draw = require('@leafer-ui/draw');
4
-
5
- /******************************************************************************
6
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
- PERFORMANCE OF THIS SOFTWARE.
18
- ***************************************************************************** */
19
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
20
-
21
-
22
- function __awaiter(thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- }
31
-
32
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
33
- var e = new Error(message);
34
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1
+ "use strict";
2
+
3
+ var draw = require("@leafer-ui/draw");
4
+
5
+ function __awaiter(thisArg, _arguments, P, generator) {
6
+ function adopt(value) {
7
+ return value instanceof P ? value : new P(function(resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function(resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator["throw"](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
28
+ }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
34
+ var e = new Error(message);
35
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
35
36
  };
36
37
 
37
- const { setPoint, addPoint, toBounds } = draw.TwoPointBoundsHelper;
38
+ const {setPoint: setPoint, addPoint: addPoint, toBounds: toBounds} = draw.TwoPointBoundsHelper;
39
+
38
40
  function getTrimBounds(canvas) {
39
- const { width, height } = canvas.view;
40
- const { data } = canvas.context.getImageData(0, 0, width, height);
41
+ const {width: width, height: height} = canvas.view;
42
+ const {data: data} = canvas.context.getImageData(0, 0, width, height);
41
43
  let x, y, pointBounds, index = 0;
42
44
  for (let i = 0; i < data.length; i += 4) {
43
45
  if (data[i + 3] !== 0) {
@@ -47,194 +49,241 @@ function getTrimBounds(canvas) {
47
49
  }
48
50
  index++;
49
51
  }
50
- const bounds = new draw.Bounds();
51
- toBounds(pointBounds, bounds);
52
- return bounds.scale(1 / canvas.pixelRatio).ceil();
52
+ const bounds = new draw.Bounds;
53
+ if (pointBounds) {
54
+ toBounds(pointBounds, bounds);
55
+ bounds.scale(1 / canvas.pixelRatio).ceil();
56
+ }
57
+ return bounds;
53
58
  }
54
59
 
55
60
  const ExportModule = {
56
61
  syncExport(leaf, filename, options) {
57
- this.running = true;
62
+ draw.Export.running = true;
58
63
  let result;
59
- const fileType = draw.FileHelper.fileType(filename);
60
- const isDownload = filename.includes('.');
61
- options = draw.FileHelper.getExportOptions(options);
62
- const { toURL } = draw.Platform;
63
- const { download } = draw.Platform.origin;
64
- if (fileType === 'json') {
65
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
66
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
67
- }
68
- else if (fileType === 'svg') {
69
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
70
- result = { data: isDownload ? true : leaf.toSVG() };
71
- }
72
- else {
73
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
74
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
75
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
76
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
77
- const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
78
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
79
- const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
80
- if (screenshot) {
81
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
82
- }
83
- else {
84
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
85
- scaleX = worldTransform.scaleX;
86
- scaleY = worldTransform.scaleY;
87
- switch (relative) {
88
- case 'inner':
64
+ try {
65
+ const fileType = draw.FileHelper.fileType(filename);
66
+ const isDownload = filename.includes(".");
67
+ options = draw.FileHelper.getExportOptions(options);
68
+ const {toURL: toURL} = draw.Platform;
69
+ const {download: download} = draw.Platform.origin;
70
+ if (fileType === "json") {
71
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), "text"), filename);
72
+ result = {
73
+ data: isDownload ? true : leaf.toJSON(options.json)
74
+ };
75
+ } else if (fileType === "svg") {
76
+ isDownload && download(toURL(leaf.toSVG(), "svg"), filename);
77
+ result = {
78
+ data: isDownload ? true : leaf.toSVG()
79
+ };
80
+ } else {
81
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
82
+ const {worldTransform: worldTransform, isLeafer: isLeafer, leafer: leafer, isFrame: isFrame} = leaf;
83
+ const {slice: slice, clip: clip, trim: trim, screenshot: screenshot, padding: padding, onCanvas: onCanvas} = options;
84
+ const smooth = draw.isUndefined(options.smooth) ? leafer ? leafer.config.smooth : true : options.smooth;
85
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
86
+ const fill = isLeafer && screenshot ? draw.isUndefined(options.fill) ? leaf.fill : options.fill : options.fill;
87
+ const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix;
88
+ if (screenshot) {
89
+ renderBounds = screenshot === true ? isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds : screenshot;
90
+ } else {
91
+ let relative = options.relative || (isLeafer ? "inner" : "local");
92
+ scaleX = worldTransform.scaleX;
93
+ scaleY = worldTransform.scaleY;
94
+ switch (relative) {
95
+ case "inner":
89
96
  matrix.set(worldTransform);
90
97
  break;
91
- case 'local':
98
+
99
+ case "local":
92
100
  matrix.set(worldTransform).divide(leaf.localTransform);
93
101
  scaleX /= leaf.scaleX;
94
102
  scaleY /= leaf.scaleY;
95
103
  break;
96
- case 'world':
104
+
105
+ case "world":
97
106
  scaleX = 1;
98
107
  scaleY = 1;
99
108
  break;
100
- case 'page':
109
+
110
+ case "page":
101
111
  relative = leafer || leaf;
102
- default:
112
+
113
+ default:
103
114
  matrix.set(worldTransform).divide(leaf.getTransform(relative));
104
115
  const l = relative.worldTransform;
105
116
  scaleX /= scaleX / l.scaleX;
106
117
  scaleY /= scaleY / l.scaleY;
118
+ }
119
+ renderBounds = leaf.getBounds("render", relative);
107
120
  }
108
- renderBounds = leaf.getBounds('render', relative);
109
- }
110
- const scaleData = { scaleX: 1, scaleY: 1 };
111
- draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
112
- let pixelRatio = options.pixelRatio || 1;
113
- let { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
114
- if (clip)
115
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
116
- const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
117
- let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
118
- let sliceLeaf;
119
- if (slice) {
120
- sliceLeaf = leaf;
121
- sliceLeaf.__worldOpacity = 0;
122
- leaf = leafer || leaf;
123
- renderOptions.bounds = canvas.bounds;
124
- }
125
- canvas.save();
126
- if (isFrame && fill !== undefined) {
127
- const oldFill = leaf.get('fill');
128
- leaf.fill = '';
129
- leaf.__render(canvas, renderOptions);
130
- leaf.fill = oldFill;
131
- }
132
- else {
133
- leaf.__render(canvas, renderOptions);
134
- }
135
- canvas.restore();
136
- if (sliceLeaf)
137
- sliceLeaf.__updateWorldOpacity();
138
- if (trim) {
139
- trimBounds = getTrimBounds(canvas);
140
- const old = canvas, { width, height } = trimBounds;
141
- const config = { x: 0, y: 0, width, height, pixelRatio };
142
- canvas = draw.Creator.canvas(config);
143
- canvas.copyWorld(old, trimBounds, config);
144
- }
145
- if (padding) {
146
- const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
147
- const old = canvas, { width, height } = old;
148
- canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
149
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
121
+ const scaleData = {
122
+ scaleX: 1,
123
+ scaleY: 1
124
+ };
125
+ draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
126
+ let pixelRatio = options.pixelRatio || 1;
127
+ let {x: x, y: y, width: width, height: height} = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
128
+ if (clip) x += clip.x, y += clip.y, width = clip.width, height = clip.height;
129
+ const renderOptions = {
130
+ exporting: true,
131
+ matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY)
132
+ };
133
+ let canvas = draw.Creator.canvas({
134
+ width: Math.floor(width),
135
+ height: Math.floor(height),
136
+ pixelRatio: pixelRatio,
137
+ smooth: smooth,
138
+ contextSettings: contextSettings
139
+ });
140
+ let sliceLeaf;
141
+ if (slice) {
142
+ sliceLeaf = leaf;
143
+ sliceLeaf.__worldOpacity = 0;
144
+ leaf = leafer || leaf;
145
+ renderOptions.bounds = canvas.bounds;
146
+ }
147
+ canvas.save();
148
+ if (isFrame && !draw.isUndefined(fill)) {
149
+ const oldFill = leaf.get("fill");
150
+ leaf.fill = "";
151
+ leaf.__render(canvas, renderOptions);
152
+ leaf.fill = oldFill;
153
+ } else {
154
+ leaf.__render(canvas, renderOptions);
155
+ }
156
+ canvas.restore();
157
+ if (sliceLeaf) sliceLeaf.__updateWorldOpacity();
158
+ if (trim) {
159
+ trimBounds = getTrimBounds(canvas);
160
+ const old = canvas, {width: width, height: height} = trimBounds;
161
+ const config = {
162
+ x: 0,
163
+ y: 0,
164
+ width: width,
165
+ height: height,
166
+ pixelRatio: pixelRatio
167
+ };
168
+ canvas = draw.Creator.canvas(config);
169
+ canvas.copyWorld(old, trimBounds, config);
170
+ old.destroy();
171
+ }
172
+ if (padding) {
173
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
174
+ const old = canvas, {width: width, height: height} = old;
175
+ canvas = draw.Creator.canvas({
176
+ width: width + left + right,
177
+ height: height + top + bottom,
178
+ pixelRatio: pixelRatio
179
+ });
180
+ canvas.copyWorld(old, old.bounds, {
181
+ x: left,
182
+ y: top,
183
+ width: width,
184
+ height: height
185
+ });
186
+ old.destroy();
187
+ }
188
+ if (needFill) canvas.fillWorld(canvas.bounds, fill || "#FFFFFF", "destination-over");
189
+ if (onCanvas) onCanvas(canvas);
190
+ const data = filename === "canvas" ? canvas : canvas.export(filename, options);
191
+ result = {
192
+ data: data,
193
+ width: canvas.pixelWidth,
194
+ height: canvas.pixelHeight,
195
+ renderBounds: renderBounds,
196
+ trimBounds: trimBounds
197
+ };
150
198
  }
151
- if (needFill)
152
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
153
- if (onCanvas)
154
- onCanvas(canvas);
155
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
156
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
199
+ } catch (error) {
200
+ result = {
201
+ data: "",
202
+ error: error
203
+ };
157
204
  }
158
- this.running = false;
205
+ draw.Export.running = false;
159
206
  return result;
160
207
  },
161
208
  export(leaf, filename, options) {
162
- this.running = true;
163
- return addTask((success) => new Promise((resolve) => {
164
- const getResult = () => __awaiter(this, void 0, void 0, function* () {
165
- if (!draw.Resource.isComplete)
166
- return draw.Platform.requestRender(getResult);
167
- const result = ExportModule.syncExport(leaf, filename, options);
168
- if (result.data instanceof Promise)
169
- result.data = yield result.data;
209
+ draw.Export.running = true;
210
+ return addTask(success => new Promise(resolve => {
211
+ const getResult = () => __awaiter(this, void 0, void 0, function*() {
212
+ if (!draw.Resource.isComplete) return draw.Platform.requestRender(getResult);
213
+ const result = draw.Export.syncExport(leaf, filename, options);
214
+ if (result.data instanceof Promise) result.data = yield result.data;
170
215
  success(result);
171
216
  resolve();
172
217
  });
173
218
  leaf.updateLayout();
174
219
  checkLazy(leaf);
175
- const { leafer } = leaf;
176
- if (leafer)
177
- leafer.waitViewCompleted(getResult);
178
- else
179
- getResult();
220
+ const {leafer: leafer} = leaf;
221
+ if (leafer) leafer.waitViewCompleted(getResult); else getResult();
180
222
  }));
181
223
  }
182
224
  };
225
+
183
226
  let tasker;
227
+
184
228
  function addTask(task) {
185
- if (!tasker)
186
- tasker = new draw.TaskProcessor();
187
- return new Promise((resolve) => {
188
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
229
+ if (!tasker) tasker = new draw.TaskProcessor;
230
+ return new Promise(resolve => {
231
+ tasker.add(() => __awaiter(this, void 0, void 0, function*() {
232
+ return yield task(resolve);
233
+ }), {
234
+ parallel: false
235
+ });
189
236
  });
190
237
  }
238
+
191
239
  function checkLazy(leaf) {
192
- if (leaf.__.__needComputePaint)
193
- leaf.__.__computePaint();
194
- if (leaf.isBranch)
195
- leaf.children.forEach(child => checkLazy(child));
240
+ if (leaf.__.__needComputePaint) leaf.__.__computePaint();
241
+ if (leaf.isBranch) leaf.children.forEach(child => checkLazy(child));
196
242
  }
197
243
 
198
244
  const canvas = draw.LeaferCanvasBase.prototype;
199
- const debug = draw.Debug.get('@leafer-in/export');
200
- canvas.export = function (filename, options) {
201
- const { quality, blob } = draw.FileHelper.getExportOptions(options);
202
- if (filename.includes('.'))
203
- return this.saveAs(filename, quality);
204
- else if (blob)
205
- return this.toBlob(filename, quality);
206
- else
207
- return this.toDataURL(filename, quality);
245
+
246
+ const debug = draw.Debug.get("@leafer-in/export");
247
+
248
+ canvas.export = function(filename, options) {
249
+ const {quality: quality, blob: blob} = draw.FileHelper.getExportOptions(options);
250
+ if (filename.includes(".")) return this.saveAs(filename, quality); else if (blob) return this.toBlob(filename, quality); else return this.toDataURL(filename, quality);
208
251
  };
209
- canvas.toBlob = function (type, quality) {
210
- return new Promise((resolve) => {
211
- draw.Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
252
+
253
+ canvas.toBlob = function(type, quality) {
254
+ return new Promise(resolve => {
255
+ draw.Platform.origin.canvasToBolb(this.view, type, quality).then(blob => {
212
256
  resolve(blob);
213
- }).catch((e) => {
257
+ }).catch(e => {
214
258
  debug.error(e);
215
259
  resolve(null);
216
260
  });
217
261
  });
218
262
  };
219
- canvas.toDataURL = function (type, quality) {
263
+
264
+ canvas.toDataURL = function(type, quality) {
220
265
  return draw.Platform.origin.canvasToDataURL(this.view, type, quality);
221
266
  };
222
- canvas.saveAs = function (filename, quality) {
223
- return new Promise((resolve) => {
267
+
268
+ canvas.saveAs = function(filename, quality) {
269
+ return new Promise(resolve => {
224
270
  draw.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
225
271
  resolve(true);
226
- }).catch((e) => {
272
+ }).catch(e => {
227
273
  debug.error(e);
228
274
  resolve(false);
229
275
  });
230
276
  });
231
277
  };
232
278
 
233
- draw.Plugin.add('export');
279
+ draw.Plugin.add("export");
280
+
234
281
  Object.assign(draw.Export, ExportModule);
235
- draw.UI.prototype.export = function (filename, options) {
282
+
283
+ draw.UI.prototype.export = function(filename, options) {
236
284
  return draw.Export.export(this, filename, options);
237
285
  };
238
- draw.UI.prototype.syncExport = function (filename, options) {
286
+
287
+ draw.UI.prototype.syncExport = function(filename, options) {
239
288
  return draw.Export.syncExport(this, filename, options);
240
289
  };