@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 +214 -165
- package/dist/export.esm.js +212 -163
- package/dist/export.esm.min.js +1 -1
- package/dist/export.esm.min.js.map +1 -1
- package/dist/export.js +199 -169
- package/dist/export.min.cjs +1 -1
- package/dist/export.min.cjs.map +1 -1
- package/dist/export.min.js +1 -1
- package/dist/export.min.js.map +1 -1
- package/package.json +4 -4
- package/src/export.ts +108 -99
- package/src/trim.ts +5 -2
- package/types/index.d.ts +1 -1
package/dist/export.cjs
CHANGED
|
@@ -1,43 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var draw = require(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
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
|
-
|
|
52
|
-
|
|
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
|
-
|
|
62
|
+
draw.Export.running = true;
|
|
58
63
|
let result;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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
|
-
|
|
104
|
+
|
|
105
|
+
case "world":
|
|
97
106
|
scaleX = 1;
|
|
98
107
|
scaleY = 1;
|
|
99
108
|
break;
|
|
100
|
-
|
|
109
|
+
|
|
110
|
+
case "page":
|
|
101
111
|
relative = leafer || leaf;
|
|
102
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
x += clip.x, y += clip.y, width = clip.width, height = clip.height;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
canvas.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
205
|
+
draw.Export.running = false;
|
|
159
206
|
return result;
|
|
160
207
|
},
|
|
161
208
|
export(leaf, filename, options) {
|
|
162
|
-
|
|
163
|
-
return addTask(
|
|
164
|
-
const getResult = () => __awaiter(this, void 0, void 0, function*
|
|
165
|
-
if (!draw.Resource.isComplete)
|
|
166
|
-
|
|
167
|
-
|
|
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
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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(
|
|
257
|
+
}).catch(e => {
|
|
214
258
|
debug.error(e);
|
|
215
259
|
resolve(null);
|
|
216
260
|
});
|
|
217
261
|
});
|
|
218
262
|
};
|
|
219
|
-
|
|
263
|
+
|
|
264
|
+
canvas.toDataURL = function(type, quality) {
|
|
220
265
|
return draw.Platform.origin.canvasToDataURL(this.view, type, quality);
|
|
221
266
|
};
|
|
222
|
-
|
|
223
|
-
|
|
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(
|
|
272
|
+
}).catch(e => {
|
|
227
273
|
debug.error(e);
|
|
228
274
|
resolve(false);
|
|
229
275
|
});
|
|
230
276
|
});
|
|
231
277
|
};
|
|
232
278
|
|
|
233
|
-
draw.Plugin.add(
|
|
279
|
+
draw.Plugin.add("export");
|
|
280
|
+
|
|
234
281
|
Object.assign(draw.Export, ExportModule);
|
|
235
|
-
|
|
282
|
+
|
|
283
|
+
draw.UI.prototype.export = function(filename, options) {
|
|
236
284
|
return draw.Export.export(this, filename, options);
|
|
237
285
|
};
|
|
238
|
-
|
|
286
|
+
|
|
287
|
+
draw.UI.prototype.syncExport = function(filename, options) {
|
|
239
288
|
return draw.Export.syncExport(this, filename, options);
|
|
240
289
|
};
|