@scaleflex/widget-thumbnail-generator 0.0.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/CHANGELOG.md +6835 -0
- package/LICENSE +21 -0
- package/README.md +181 -0
- package/assets/CHECKERED_BACKGROUND.svg +2507 -0
- package/lib/defaultLocale.js +4 -0
- package/lib/index.js +481 -0
- package/package.json +25 -0
- package/types/index.d.ts +16 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
5
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
6
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
7
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
8
|
+
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
9
|
+
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
10
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
11
|
+
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
12
|
+
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
13
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
14
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
import { Plugin } from '@scaleflex/widget-core';
|
|
18
|
+
import Translator from '@scaleflex/widget-utils/lib/Translator';
|
|
19
|
+
import dataURItoBlob from '@scaleflex/widget-utils/lib/dataURItoBlob';
|
|
20
|
+
import isObjectURL from '@scaleflex/widget-utils/lib/isObjectURL';
|
|
21
|
+
import isThumbnailLocallySupported from '@scaleflex/widget-utils/lib/isThumbnailLocallySupported';
|
|
22
|
+
import isPhotoshopFile from '@scaleflex/widget-utils/lib/isPhotoshopFile';
|
|
23
|
+
import generatePsdThumbnail from '@scaleflex/widget-utils/lib/generatePsdThumbnail/generatePsdThumbnail';
|
|
24
|
+
import MathLog2 from 'math-log2';
|
|
25
|
+
import IMAGE from '../assets/CHECKERED_BACKGROUND.svg';
|
|
26
|
+
import defaultLocale from './defaultLocale';
|
|
27
|
+
// TODO: find a way to show version of the current plugin
|
|
28
|
+
// why solution below isn't good?
|
|
29
|
+
// first import doesn't work with webpack 5 as it was deprecated
|
|
30
|
+
// second import fixes webpack 5 issue as it was mentioned in their docs
|
|
31
|
+
// but it exposes our package.json to the client and it is mentioned as security rist in mutiple places
|
|
32
|
+
// https://github.com/axelpale/genversion
|
|
33
|
+
// https://stackoverflow.com/questions/64993118/error-should-not-import-the-named-export-version-imported-as-version
|
|
34
|
+
// https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#10855054
|
|
35
|
+
// import { version } from '../package.json'
|
|
36
|
+
// import packageInfo from '../package.json'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The Thumbnail Generator plugin
|
|
40
|
+
*/
|
|
41
|
+
var ThumbnailGenerator = /*#__PURE__*/function (_Plugin) {
|
|
42
|
+
// static VERSION = packageInfo.version
|
|
43
|
+
|
|
44
|
+
function ThumbnailGenerator(filerobot) {
|
|
45
|
+
var _this;
|
|
46
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
47
|
+
_classCallCheck(this, ThumbnailGenerator);
|
|
48
|
+
_this = _callSuper(this, ThumbnailGenerator, [filerobot, opts]);
|
|
49
|
+
_defineProperty(_this, "onFileAdded", function (file) {
|
|
50
|
+
if (!file.preview && isThumbnailLocallySupported(file) && !file.isRemote && file.id) {
|
|
51
|
+
_this.addToQueue(file.id);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
_defineProperty(_this, "onThumbnailUpdateRequest", function (file) {
|
|
55
|
+
if (isThumbnailLocallySupported(file) && !file.isRemote) {
|
|
56
|
+
if (file.preview && isObjectURL(file.preview)) {
|
|
57
|
+
URL.revokeObjectURL(file.preview);
|
|
58
|
+
}
|
|
59
|
+
_this.addToQueue(file.id);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* Cancel a lazy request for a thumbnail if the thumbnail has not yet been generated.
|
|
64
|
+
*/
|
|
65
|
+
_defineProperty(_this, "onCancelRequest", function (file) {
|
|
66
|
+
var index = _this.queue.indexOf(file.id);
|
|
67
|
+
if (index !== -1) {
|
|
68
|
+
_this.queue.splice(index, 1);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* Clean up the thumbnail for a file. Cancel lazy requests and free the thumbnail URL.
|
|
73
|
+
*/
|
|
74
|
+
_defineProperty(_this, "onFileRemoved", function (file) {
|
|
75
|
+
var index = _this.queue.indexOf(file.id);
|
|
76
|
+
if (index !== -1) {
|
|
77
|
+
_this.queue.splice(index, 1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Clean up object URLs.
|
|
81
|
+
if (file.preview && isObjectURL(file.preview)) {
|
|
82
|
+
URL.revokeObjectURL(file.preview);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
_defineProperty(_this, "onRestored", function () {
|
|
86
|
+
var fileIds = _this.filerobot.getUploads();
|
|
87
|
+
fileIds.forEach(function (fileId) {
|
|
88
|
+
var file = _this.filerobot.getFileBeforeUpload(fileId);
|
|
89
|
+
if (!file.isRestored) return;
|
|
90
|
+
// Only add blob URLs; they are likely invalid after being restored.
|
|
91
|
+
if (!file.preview || isObjectURL(file.preview)) {
|
|
92
|
+
_this.addToQueue(file.id);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
_defineProperty(_this, "waitUntilAllProcessed", function (fileIds) {
|
|
97
|
+
fileIds.forEach(function (fileId) {
|
|
98
|
+
var file = _this.filerobot.getFileBeforeUpload(fileId);
|
|
99
|
+
_this.filerobot.emit('preprocess-progress', file, {
|
|
100
|
+
mode: 'indeterminate',
|
|
101
|
+
message: _this.i18n('thumbnailGeneratorGeneratingThumbnailsMessage')
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
var emitPreprocessCompleteForAll = function emitPreprocessCompleteForAll() {
|
|
105
|
+
fileIds.forEach(function (fileId) {
|
|
106
|
+
var file = _this.filerobot.getFileBeforeUpload(fileId);
|
|
107
|
+
_this.filerobot.emit('preprocess-complete', file);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
return new Promise(function (resolve, reject) {
|
|
111
|
+
if (_this.queueProcessing) {
|
|
112
|
+
_this.filerobot.once('thumbnail:all-generated', function () {
|
|
113
|
+
emitPreprocessCompleteForAll();
|
|
114
|
+
resolve();
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
emitPreprocessCompleteForAll();
|
|
118
|
+
resolve();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
_this.type = 'modifier';
|
|
123
|
+
_this.invisible = true;
|
|
124
|
+
_this.id = _this.opts.id || 'ThumbnailGenerator';
|
|
125
|
+
_this.title = 'Thumbnail Generator';
|
|
126
|
+
_this.queue = [];
|
|
127
|
+
_this.queueProcessing = false;
|
|
128
|
+
_this.transparencyBackground = null;
|
|
129
|
+
_this.defaultLocale = {
|
|
130
|
+
strings: defaultLocale
|
|
131
|
+
};
|
|
132
|
+
var defaultOptions = {
|
|
133
|
+
thumbnailWidth: 200,
|
|
134
|
+
thumbnailHeight: 170,
|
|
135
|
+
waitForThumbnailsBeforeUpload: false,
|
|
136
|
+
lazy: false
|
|
137
|
+
};
|
|
138
|
+
_this.opts = _objectSpread(_objectSpread({}, defaultOptions), opts);
|
|
139
|
+
if (_this.opts.lazy && _this.opts.waitForThumbnailsBeforeUpload) {
|
|
140
|
+
throw new Error('ThumbnailGenerator: The `lazy` and `waitForThumbnailsBeforeUpload` options are mutually exclusive. Please ensure at most one of them is set to `true`.');
|
|
141
|
+
}
|
|
142
|
+
_this.i18nInit();
|
|
143
|
+
return _this;
|
|
144
|
+
}
|
|
145
|
+
_inherits(ThumbnailGenerator, _Plugin);
|
|
146
|
+
return _createClass(ThumbnailGenerator, [{
|
|
147
|
+
key: "i18nInit",
|
|
148
|
+
value: function i18nInit() {
|
|
149
|
+
this.translator = new Translator([this.defaultLocale, this.filerobot.locale, this.opts.locale]);
|
|
150
|
+
this.i18n = this.translator.translate.bind(this.translator);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Create a thumbnail for the given Filerobot file object.
|
|
155
|
+
*
|
|
156
|
+
* @param {{data: Blob}} file
|
|
157
|
+
* @param {number} targetWidth
|
|
158
|
+
* @param {number} targetHeight
|
|
159
|
+
* @returns {Promise}
|
|
160
|
+
*/
|
|
161
|
+
}, {
|
|
162
|
+
key: "createThumbnail",
|
|
163
|
+
value: function createThumbnail(file, targetWidth, targetHeight) {
|
|
164
|
+
var _this2 = this;
|
|
165
|
+
var originalUrl = isPhotoshopFile(file) ? generatePsdThumbnail(file.data).then(function (thumbnail) {
|
|
166
|
+
return thumbnail;
|
|
167
|
+
})["catch"](function (err) {
|
|
168
|
+
return err.message;
|
|
169
|
+
}) : Promise.resolve(URL.createObjectURL(file.data));
|
|
170
|
+
var loadPreviewImg = function loadPreviewImg(previewUrl) {
|
|
171
|
+
return new Promise(function (resolve, reject) {
|
|
172
|
+
var image = new Image();
|
|
173
|
+
image.src = previewUrl;
|
|
174
|
+
image.addEventListener('load', function () {
|
|
175
|
+
URL.revokeObjectURL(originalUrl);
|
|
176
|
+
resolve(image);
|
|
177
|
+
});
|
|
178
|
+
image.addEventListener('error', function (event) {
|
|
179
|
+
URL.revokeObjectURL(originalUrl);
|
|
180
|
+
reject(event.error || new Error('Could not create thumbnail'));
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
return originalUrl.then(loadPreviewImg).then(function (image) {
|
|
185
|
+
var dimensions = _this2.getProportionalDimensions(image, targetWidth, targetHeight);
|
|
186
|
+
return _this2.addBackground(file, image, dimensions.width, dimensions.height).then(function (canvas) {
|
|
187
|
+
return _this2.canvasToBlob(canvas, 'image/png', 80);
|
|
188
|
+
});
|
|
189
|
+
}).then(function (blob) {
|
|
190
|
+
return URL.createObjectURL(blob);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Get the new calculated dimensions for the given image and a target width
|
|
196
|
+
* or height. If both width and height are given, only width is taken into
|
|
197
|
+
* account. If neither width nor height are given, the default dimension
|
|
198
|
+
* is used.
|
|
199
|
+
*/
|
|
200
|
+
}, {
|
|
201
|
+
key: "getProportionalDimensions",
|
|
202
|
+
value: function getProportionalDimensions(img, width, height) {
|
|
203
|
+
var aspectRatio = img.width / img.height;
|
|
204
|
+
if (img.width >= img.height) {
|
|
205
|
+
return {
|
|
206
|
+
width: width,
|
|
207
|
+
height: Math.round(width / aspectRatio)
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
if (img.height > img.width) {
|
|
211
|
+
return {
|
|
212
|
+
width: Math.round(height * aspectRatio),
|
|
213
|
+
height: height
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Make sure the image doesn’t exceed browser/device canvas limits.
|
|
220
|
+
* For ios with 256 RAM and ie
|
|
221
|
+
*/
|
|
222
|
+
}, {
|
|
223
|
+
key: "protect",
|
|
224
|
+
value: function protect(image) {
|
|
225
|
+
// https://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element
|
|
226
|
+
|
|
227
|
+
var ratio = image.width / image.height;
|
|
228
|
+
var maxSquare = 5000000; // ios max canvas square
|
|
229
|
+
var maxSize = 4096; // ie max canvas dimensions
|
|
230
|
+
|
|
231
|
+
var maxW = Math.floor(Math.sqrt(maxSquare * ratio));
|
|
232
|
+
var maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio));
|
|
233
|
+
if (maxW > maxSize) {
|
|
234
|
+
maxW = maxSize;
|
|
235
|
+
maxH = Math.round(maxW / ratio);
|
|
236
|
+
}
|
|
237
|
+
if (maxH > maxSize) {
|
|
238
|
+
maxH = maxSize;
|
|
239
|
+
maxW = Math.round(ratio * maxH);
|
|
240
|
+
}
|
|
241
|
+
if (image.width > maxW) {
|
|
242
|
+
var canvas = document.createElement('canvas');
|
|
243
|
+
canvas.width = maxW;
|
|
244
|
+
canvas.height = maxH;
|
|
245
|
+
canvas.getContext('2d').drawImage(image, 0, 0, maxW, maxH);
|
|
246
|
+
image = canvas;
|
|
247
|
+
}
|
|
248
|
+
return image;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Resize an image to the target `width` and `height`.
|
|
253
|
+
*
|
|
254
|
+
* Returns a Canvas with the resized image on it.
|
|
255
|
+
*/
|
|
256
|
+
}, {
|
|
257
|
+
key: "resizeImage",
|
|
258
|
+
value: function resizeImage(image, targetWidth, targetHeight) {
|
|
259
|
+
// Resizing in steps refactored to use a solution from
|
|
260
|
+
// https://blog.uploadcare.com/image-resize-in-browsers-is-broken-e38eed08df01
|
|
261
|
+
|
|
262
|
+
image = this.protect(image);
|
|
263
|
+
var steps = Math.ceil(MathLog2(image.width / targetWidth));
|
|
264
|
+
if (steps < 1) {
|
|
265
|
+
steps = 1;
|
|
266
|
+
}
|
|
267
|
+
var sW = targetWidth * Math.pow(2, steps - 1);
|
|
268
|
+
var sH = targetHeight * Math.pow(2, steps - 1);
|
|
269
|
+
var x = 2;
|
|
270
|
+
while (steps--) {
|
|
271
|
+
var canvas = document.createElement('canvas');
|
|
272
|
+
canvas.width = sW;
|
|
273
|
+
canvas.height = sH;
|
|
274
|
+
canvas.getContext('2d').drawImage(image, 0, 0, sW, sH);
|
|
275
|
+
image = canvas;
|
|
276
|
+
sW = Math.round(sW / x);
|
|
277
|
+
sH = Math.round(sH / x);
|
|
278
|
+
}
|
|
279
|
+
return image;
|
|
280
|
+
}
|
|
281
|
+
}, {
|
|
282
|
+
key: "draw",
|
|
283
|
+
value: function draw(image, background) {
|
|
284
|
+
var imgWidth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|
285
|
+
var imgHeight = arguments.length > 3 ? arguments[3] : undefined;
|
|
286
|
+
var canvas = arguments.length > 4 ? arguments[4] : undefined;
|
|
287
|
+
var ctx = arguments.length > 5 ? arguments[5] : undefined;
|
|
288
|
+
ctx.drawImage(background, 0, 0, this.opts.thumbnailWidth, this.opts.thumbnailHeight);
|
|
289
|
+
if (ctx.filter === 'blur(6px)') {
|
|
290
|
+
ctx.filter = 'none';
|
|
291
|
+
}
|
|
292
|
+
ctx.drawImage(image, canvas.width / 2 - imgWidth / 2, canvas.height / 2 - imgHeight / 2, imgWidth, imgHeight);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Makes sure that the image dimensions wouldn't exceed the limits &
|
|
297
|
+
* Adds a blurry background of the image to the target `width` and `height`.
|
|
298
|
+
*
|
|
299
|
+
* adds a Canvas with the image and checkered or blurry background.
|
|
300
|
+
*/
|
|
301
|
+
}, {
|
|
302
|
+
key: "addBackground",
|
|
303
|
+
value: function addBackground(file, image, targetWidth, targetHeight) {
|
|
304
|
+
var _this3 = this;
|
|
305
|
+
return new Promise(function (resolve, reject) {
|
|
306
|
+
image = _this3.protect(image);
|
|
307
|
+
var imgWidth = image.width;
|
|
308
|
+
var imgHeight = image.height;
|
|
309
|
+
var canvas = document.createElement('canvas');
|
|
310
|
+
canvas.width = _this3.opts.thumbnailWidth;
|
|
311
|
+
canvas.height = _this3.opts.thumbnailHeight;
|
|
312
|
+
var ctx = canvas.getContext('2d');
|
|
313
|
+
if (imgWidth > targetWidth || imgHeight > targetHeight) {
|
|
314
|
+
var imgRatio = imgWidth / imgHeight;
|
|
315
|
+
if (imgHeight > imgWidth) {
|
|
316
|
+
imgHeight = targetHeight;
|
|
317
|
+
imgWidth = imgHeight * imgRatio;
|
|
318
|
+
} else {
|
|
319
|
+
imgWidth = targetWidth;
|
|
320
|
+
imgHeight = imgWidth / imgRatio;
|
|
321
|
+
}
|
|
322
|
+
image = _this3.resizeImage(image, targetWidth, targetHeight);
|
|
323
|
+
imgHeight = image.height;
|
|
324
|
+
imgWidth = image.width;
|
|
325
|
+
}
|
|
326
|
+
if (file.extension === 'svg' || file.extension === 'png') {
|
|
327
|
+
if (!_this3.transparencyBackground) {
|
|
328
|
+
_this3.transparencyBackground = new Image();
|
|
329
|
+
_this3.transparencyBackground.crossOrigin = 'Anonymous';
|
|
330
|
+
_this3.transparencyBackground.src = IMAGE;
|
|
331
|
+
_this3.transparencyBackground.onload = function () {
|
|
332
|
+
_this3.draw(image, _this3.transparencyBackground, imgWidth, imgHeight, canvas, ctx);
|
|
333
|
+
resolve(canvas);
|
|
334
|
+
};
|
|
335
|
+
_this3.transparencyBackground.onerror = function () {
|
|
336
|
+
reject(new Error(_this3.i18n('thumbnailGeneratorThumbnailGenerationFailed', {
|
|
337
|
+
file_name: (file === null || file === void 0 ? void 0 : file.name) || ''
|
|
338
|
+
})));
|
|
339
|
+
};
|
|
340
|
+
} else {
|
|
341
|
+
_this3.draw(image, _this3.transparencyBackground, imgWidth, imgHeight, canvas, ctx);
|
|
342
|
+
resolve(canvas);
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
ctx.filter = 'blur(6px)';
|
|
346
|
+
_this3.draw(image, image, imgWidth, imgHeight, canvas, ctx);
|
|
347
|
+
resolve(canvas);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Save a <canvas> element's content to a Blob object.
|
|
354
|
+
*
|
|
355
|
+
* @param {HTMLCanvasElement} canvas
|
|
356
|
+
* @returns {Promise}
|
|
357
|
+
*/
|
|
358
|
+
}, {
|
|
359
|
+
key: "canvasToBlob",
|
|
360
|
+
value: function canvasToBlob(canvas, type, quality) {
|
|
361
|
+
try {
|
|
362
|
+
canvas.getContext('2d').getImageData(0, 0, 1, 1);
|
|
363
|
+
} catch (err) {
|
|
364
|
+
if (err.code === 18) {
|
|
365
|
+
return Promise.reject(new Error('cannot read image, probably an svg with external resources'));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (canvas.toBlob) {
|
|
369
|
+
return new Promise(function (resolve) {
|
|
370
|
+
canvas.toBlob(resolve, type, quality);
|
|
371
|
+
}).then(function (blob) {
|
|
372
|
+
if (blob === null) {
|
|
373
|
+
throw new Error('cannot read image, probably an svg with external resources');
|
|
374
|
+
}
|
|
375
|
+
return blob;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return Promise.resolve().then(function () {
|
|
379
|
+
return dataURItoBlob(canvas.toDataURL(type, quality), {});
|
|
380
|
+
}).then(function (blob) {
|
|
381
|
+
if (blob === null) {
|
|
382
|
+
throw new Error('could not extract blob, probably an old browser');
|
|
383
|
+
}
|
|
384
|
+
return blob;
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Set the preview URL for a file.
|
|
390
|
+
*/
|
|
391
|
+
}, {
|
|
392
|
+
key: "setPreviewURL",
|
|
393
|
+
value: function setPreviewURL(fileId, preview) {
|
|
394
|
+
this.filerobot.setFileStateBeforeUpload(fileId, {
|
|
395
|
+
preview: preview
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}, {
|
|
399
|
+
key: "addToQueue",
|
|
400
|
+
value: function addToQueue(item) {
|
|
401
|
+
this.queue.push(item);
|
|
402
|
+
if (this.queueProcessing === false) {
|
|
403
|
+
this.processQueue();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}, {
|
|
407
|
+
key: "processQueue",
|
|
408
|
+
value: function processQueue() {
|
|
409
|
+
var _this4 = this;
|
|
410
|
+
this.queueProcessing = true;
|
|
411
|
+
if (this.queue.length > 0) {
|
|
412
|
+
var current = this.filerobot.getFileBeforeUpload(this.queue.shift());
|
|
413
|
+
if (!current) {
|
|
414
|
+
this.filerobot.log('[ThumbnailGenerator] file was removed before a thumbnail could be generated, but not removed from the queue. This is probably a bug', 'error');
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
return this.requestThumbnail(current)["catch"](function (err) {
|
|
418
|
+
return err;
|
|
419
|
+
}) // eslint-disable-line handle-callback-err
|
|
420
|
+
.then(function () {
|
|
421
|
+
return _this4.processQueue();
|
|
422
|
+
});
|
|
423
|
+
} else {
|
|
424
|
+
this.queueProcessing = false;
|
|
425
|
+
this.filerobot.log('[ThumbnailGenerator] Emptied thumbnail queue');
|
|
426
|
+
this.filerobot.emit('thumbnail:all-generated');
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}, {
|
|
430
|
+
key: "requestThumbnail",
|
|
431
|
+
value: function requestThumbnail(file) {
|
|
432
|
+
var _this5 = this;
|
|
433
|
+
if (isThumbnailLocallySupported(file) && !file.isRemote) {
|
|
434
|
+
return this.createThumbnail(file, this.opts.thumbnailWidth, this.opts.thumbnailHeight).then(function (preview) {
|
|
435
|
+
_this5.setPreviewURL(file.id, preview);
|
|
436
|
+
_this5.filerobot.log("[ThumbnailGenerator] Generated thumbnail for ".concat(file.id));
|
|
437
|
+
_this5.filerobot.emit('thumbnail:generated', _this5.filerobot.getFileBeforeUpload(file.id), preview);
|
|
438
|
+
})["catch"](function (err) {
|
|
439
|
+
_this5.filerobot.log("[ThumbnailGenerator] Failed thumbnail for ".concat(file.id, ":"), 'warning');
|
|
440
|
+
_this5.filerobot.log(err, 'warning');
|
|
441
|
+
_this5.filerobot.info(err.message, 'warning');
|
|
442
|
+
_this5.filerobot.emit('thumbnail:error', _this5.filerobot.getFileBeforeUpload(file.id), err);
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
return Promise.resolve();
|
|
446
|
+
}
|
|
447
|
+
}, {
|
|
448
|
+
key: "install",
|
|
449
|
+
value: function install() {
|
|
450
|
+
this.filerobot.on('file-removed', this.onFileRemoved);
|
|
451
|
+
this.filerobot.on('thumbnail:update', this.onThumbnailUpdateRequest);
|
|
452
|
+
if (this.opts.lazy) {
|
|
453
|
+
this.filerobot.on('thumbnail:request', this.onFileAdded);
|
|
454
|
+
this.filerobot.on('thumbnail:cancel', this.onCancelRequest);
|
|
455
|
+
} else {
|
|
456
|
+
this.filerobot.on('file-added', this.onFileAdded);
|
|
457
|
+
this.filerobot.on('restored', this.onRestored);
|
|
458
|
+
}
|
|
459
|
+
if (this.opts.waitForThumbnailsBeforeUpload) {
|
|
460
|
+
this.filerobot.addPreProcessor(this.waitUntilAllProcessed);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}, {
|
|
464
|
+
key: "uninstall",
|
|
465
|
+
value: function uninstall() {
|
|
466
|
+
this.filerobot.off('file-removed', this.onFileRemoved);
|
|
467
|
+
if (this.opts.lazy) {
|
|
468
|
+
this.filerobot.off('thumbnail:request', this.onFileAdded);
|
|
469
|
+
this.filerobot.off('thumbnail:cancel', this.onCancelRequest);
|
|
470
|
+
} else {
|
|
471
|
+
this.filerobot.off('file-added', this.onFileAdded);
|
|
472
|
+
this.filerobot.off('thumbnail:update', this.onThumbnailUpdateRequest);
|
|
473
|
+
this.filerobot.off('restored', this.onRestored);
|
|
474
|
+
}
|
|
475
|
+
if (this.opts.waitForThumbnailsBeforeUpload) {
|
|
476
|
+
this.filerobot.removePreProcessor(this.waitUntilAllProcessed);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}]);
|
|
480
|
+
}(Plugin);
|
|
481
|
+
export { ThumbnailGenerator as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scaleflex/widget-thumbnail-generator",
|
|
3
|
+
"description": "Scaleflex plugin that generates small previews of images to show on your upload UI.",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "types/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"/dist",
|
|
10
|
+
"/lib",
|
|
11
|
+
"/types",
|
|
12
|
+
"/assets"
|
|
13
|
+
],
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@scaleflex/widget-utils": "^0.0.1",
|
|
19
|
+
"math-log2": "^2.0.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@scaleflex/widget-core": "^0.0.0"
|
|
23
|
+
},
|
|
24
|
+
"gitHead": "64ea82e745b7deda36d6794863350e6671e9010d"
|
|
25
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Filerobot = require("@scaleflex/widget-core");
|
|
2
|
+
import ThumbnailGeneratorLocale = require("./generatedLocale");
|
|
3
|
+
|
|
4
|
+
declare module ThumbnailGenerator {
|
|
5
|
+
interface ThumbnailGeneratorOptions extends Filerobot.PluginOptions {
|
|
6
|
+
thumbnailWidth?: number;
|
|
7
|
+
thumbnailHeight?: number;
|
|
8
|
+
waitForThumbnailsBeforeUpload?: boolean;
|
|
9
|
+
lazy?: boolean;
|
|
10
|
+
locale?: ThumbnailGeneratorLocale;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare class ThumbnailGenerator extends Filerobot.Plugin<ThumbnailGenerator.ThumbnailGeneratorOptions> {}
|
|
15
|
+
|
|
16
|
+
export = ThumbnailGenerator;
|