@leafer-in/export 1.8.0 → 1.9.1

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,7 +49,7 @@ function getTrimBounds(canvas) {
47
49
  }
48
50
  index++;
49
51
  }
50
- const bounds = new draw.Bounds();
52
+ const bounds = new draw.Bounds;
51
53
  if (pointBounds) {
52
54
  toBounds(pointBounds, bounds);
53
55
  bounds.scale(1 / canvas.pixelRatio).ceil();
@@ -61,64 +63,80 @@ const ExportModule = {
61
63
  let result;
62
64
  try {
63
65
  const fileType = draw.FileHelper.fileType(filename);
64
- const isDownload = filename.includes('.');
66
+ const isDownload = filename.includes(".");
65
67
  options = draw.FileHelper.getExportOptions(options);
66
- const { toURL } = draw.Platform;
67
- const { download } = draw.Platform.origin;
68
- if (fileType === 'json') {
69
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
70
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
71
- }
72
- else if (fileType === 'svg') {
73
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
74
- result = { data: isDownload ? true : leaf.toSVG() };
75
- }
76
- else {
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 {
77
81
  let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
78
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
79
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
80
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
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;
81
85
  const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
82
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
83
- const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
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;
84
88
  if (screenshot) {
85
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
86
- }
87
- else {
88
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
89
+ renderBounds = screenshot === true ? isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds : screenshot;
90
+ } else {
91
+ let relative = options.relative || (isLeafer ? "inner" : "local");
89
92
  scaleX = worldTransform.scaleX;
90
93
  scaleY = worldTransform.scaleY;
91
94
  switch (relative) {
92
- case 'inner':
93
- matrix.set(worldTransform);
94
- break;
95
- case 'local':
96
- matrix.set(worldTransform).divide(leaf.localTransform);
97
- scaleX /= leaf.scaleX;
98
- scaleY /= leaf.scaleY;
99
- break;
100
- case 'world':
101
- scaleX = 1;
102
- scaleY = 1;
103
- break;
104
- case 'page':
105
- relative = leafer || leaf;
106
- default:
107
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
108
- const l = relative.worldTransform;
109
- scaleX /= scaleX / l.scaleX;
110
- scaleY /= scaleY / l.scaleY;
95
+ case "inner":
96
+ matrix.set(worldTransform);
97
+ break;
98
+
99
+ case "local":
100
+ matrix.set(worldTransform).divide(leaf.localTransform);
101
+ scaleX /= leaf.scaleX;
102
+ scaleY /= leaf.scaleY;
103
+ break;
104
+
105
+ case "world":
106
+ scaleX = 1;
107
+ scaleY = 1;
108
+ break;
109
+
110
+ case "page":
111
+ relative = leafer || leaf;
112
+
113
+ default:
114
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
115
+ const l = relative.worldTransform;
116
+ scaleX /= scaleX / l.scaleX;
117
+ scaleY /= scaleY / l.scaleY;
111
118
  }
112
- renderBounds = leaf.getBounds('render', relative);
119
+ renderBounds = leaf.getBounds("render", relative);
113
120
  }
114
- const scaleData = { scaleX: 1, scaleY: 1 };
121
+ const scaleData = {
122
+ scaleX: 1,
123
+ scaleY: 1
124
+ };
115
125
  draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
116
126
  let pixelRatio = options.pixelRatio || 1;
117
- let { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
118
- if (clip)
119
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
120
- 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) };
121
- let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
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
+ });
122
140
  let sliceLeaf;
123
141
  if (slice) {
124
142
  sliceLeaf = leaf;
@@ -127,122 +145,145 @@ const ExportModule = {
127
145
  renderOptions.bounds = canvas.bounds;
128
146
  }
129
147
  canvas.save();
130
- if (isFrame && fill !== undefined) {
131
- const oldFill = leaf.get('fill');
132
- leaf.fill = '';
148
+ if (isFrame && !draw.isUndefined(fill)) {
149
+ const oldFill = leaf.get("fill");
150
+ leaf.fill = "";
133
151
  leaf.__render(canvas, renderOptions);
134
152
  leaf.fill = oldFill;
135
- }
136
- else {
153
+ } else {
137
154
  leaf.__render(canvas, renderOptions);
138
155
  }
139
156
  canvas.restore();
140
- if (sliceLeaf)
141
- sliceLeaf.__updateWorldOpacity();
157
+ if (sliceLeaf) sliceLeaf.__updateWorldOpacity();
142
158
  if (trim) {
143
159
  trimBounds = getTrimBounds(canvas);
144
- const old = canvas, { width, height } = trimBounds;
145
- const config = { x: 0, y: 0, width, height, pixelRatio };
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
+ };
146
168
  canvas = draw.Creator.canvas(config);
147
169
  canvas.copyWorld(old, trimBounds, config);
170
+ old.destroy();
148
171
  }
149
172
  if (padding) {
150
173
  const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
151
- const old = canvas, { width, height } = old;
152
- canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
153
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
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();
154
187
  }
155
- if (needFill)
156
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
157
- if (onCanvas)
158
- onCanvas(canvas);
159
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
160
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
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
+ };
161
198
  }
162
- }
163
- catch (error) {
164
- result = { data: '', error };
199
+ } catch (error) {
200
+ result = {
201
+ data: "",
202
+ error: error
203
+ };
165
204
  }
166
205
  draw.Export.running = false;
167
206
  return result;
168
207
  },
169
208
  export(leaf, filename, options) {
170
209
  draw.Export.running = true;
171
- return addTask((success) => new Promise((resolve) => {
172
- const getResult = () => __awaiter(this, void 0, void 0, function* () {
173
- if (!draw.Resource.isComplete)
174
- return draw.Platform.requestRender(getResult);
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);
175
213
  const result = draw.Export.syncExport(leaf, filename, options);
176
- if (result.data instanceof Promise)
177
- result.data = yield result.data;
214
+ if (result.data instanceof Promise) result.data = yield result.data;
178
215
  success(result);
179
216
  resolve();
180
217
  });
181
218
  leaf.updateLayout();
182
219
  checkLazy(leaf);
183
- const { leafer } = leaf;
184
- if (leafer)
185
- leafer.waitViewCompleted(getResult);
186
- else
187
- getResult();
220
+ const {leafer: leafer} = leaf;
221
+ if (leafer) leafer.waitViewCompleted(getResult); else getResult();
188
222
  }));
189
223
  }
190
224
  };
225
+
191
226
  let tasker;
227
+
192
228
  function addTask(task) {
193
- if (!tasker)
194
- tasker = new draw.TaskProcessor();
195
- return new Promise((resolve) => {
196
- 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
+ });
197
236
  });
198
237
  }
238
+
199
239
  function checkLazy(leaf) {
200
- if (leaf.__.__needComputePaint)
201
- leaf.__.__computePaint();
202
- if (leaf.isBranch)
203
- leaf.children.forEach(child => checkLazy(child));
240
+ if (leaf.__.__needComputePaint) leaf.__.__computePaint();
241
+ if (leaf.isBranch) leaf.children.forEach(child => checkLazy(child));
204
242
  }
205
243
 
206
244
  const canvas = draw.LeaferCanvasBase.prototype;
207
- const debug = draw.Debug.get('@leafer-in/export');
208
- canvas.export = function (filename, options) {
209
- const { quality, blob } = draw.FileHelper.getExportOptions(options);
210
- if (filename.includes('.'))
211
- return this.saveAs(filename, quality);
212
- else if (blob)
213
- return this.toBlob(filename, quality);
214
- else
215
- 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);
216
251
  };
217
- canvas.toBlob = function (type, quality) {
218
- return new Promise((resolve) => {
219
- 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 => {
220
256
  resolve(blob);
221
- }).catch((e) => {
257
+ }).catch(e => {
222
258
  debug.error(e);
223
259
  resolve(null);
224
260
  });
225
261
  });
226
262
  };
227
- canvas.toDataURL = function (type, quality) {
263
+
264
+ canvas.toDataURL = function(type, quality) {
228
265
  return draw.Platform.origin.canvasToDataURL(this.view, type, quality);
229
266
  };
230
- canvas.saveAs = function (filename, quality) {
231
- return new Promise((resolve) => {
267
+
268
+ canvas.saveAs = function(filename, quality) {
269
+ return new Promise(resolve => {
232
270
  draw.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
233
271
  resolve(true);
234
- }).catch((e) => {
272
+ }).catch(e => {
235
273
  debug.error(e);
236
274
  resolve(false);
237
275
  });
238
276
  });
239
277
  };
240
278
 
241
- draw.Plugin.add('export');
279
+ draw.Plugin.add("export");
280
+
242
281
  Object.assign(draw.Export, ExportModule);
243
- draw.UI.prototype.export = function (filename, options) {
282
+
283
+ draw.UI.prototype.export = function(filename, options) {
244
284
  return draw.Export.export(this, filename, options);
245
285
  };
246
- draw.UI.prototype.syncExport = function (filename, options) {
286
+
287
+ draw.UI.prototype.syncExport = function(filename, options) {
247
288
  return draw.Export.syncExport(this, filename, options);
248
289
  };