@leafer-in/export 1.8.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 +186 -145
- package/dist/export.esm.js +184 -143
- package/dist/export.esm.min.js +1 -1
- package/dist/export.esm.min.js.map +1 -1
- package/dist/export.js +171 -149
- 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 +6 -5
- 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,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
|
|
67
|
-
const { download
|
|
68
|
-
if (fileType ===
|
|
69
|
-
isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)),
|
|
70
|
-
result = {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
|
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 =
|
|
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 ?
|
|
86
|
-
}
|
|
87
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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(
|
|
119
|
+
renderBounds = leaf.getBounds("render", relative);
|
|
113
120
|
}
|
|
114
|
-
const scaleData = {
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
|
131
|
-
const oldFill = leaf.get(
|
|
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 = {
|
|
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({
|
|
153
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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(
|
|
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
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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(
|
|
257
|
+
}).catch(e => {
|
|
222
258
|
debug.error(e);
|
|
223
259
|
resolve(null);
|
|
224
260
|
});
|
|
225
261
|
});
|
|
226
262
|
};
|
|
227
|
-
|
|
263
|
+
|
|
264
|
+
canvas.toDataURL = function(type, quality) {
|
|
228
265
|
return draw.Platform.origin.canvasToDataURL(this.view, type, quality);
|
|
229
266
|
};
|
|
230
|
-
|
|
231
|
-
|
|
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(
|
|
272
|
+
}).catch(e => {
|
|
235
273
|
debug.error(e);
|
|
236
274
|
resolve(false);
|
|
237
275
|
});
|
|
238
276
|
});
|
|
239
277
|
};
|
|
240
278
|
|
|
241
|
-
draw.Plugin.add(
|
|
279
|
+
draw.Plugin.add("export");
|
|
280
|
+
|
|
242
281
|
Object.assign(draw.Export, ExportModule);
|
|
243
|
-
|
|
282
|
+
|
|
283
|
+
draw.UI.prototype.export = function(filename, options) {
|
|
244
284
|
return draw.Export.export(this, filename, options);
|
|
245
285
|
};
|
|
246
|
-
|
|
286
|
+
|
|
287
|
+
draw.UI.prototype.syncExport = function(filename, options) {
|
|
247
288
|
return draw.Export.syncExport(this, filename, options);
|
|
248
289
|
};
|