@uppy/thumbnail-generator
Advanced tools
Comparing version 1.0.2 to 1.1.0
146
lib/index.js
@@ -1,8 +0,8 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _class, _temp; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
@@ -13,5 +13,6 @@ var _require = require('@uppy/core'), | ||
var dataURItoBlob = require('@uppy/utils/lib/dataURItoBlob'); | ||
var isObjectURL = require('@uppy/utils/lib/isObjectURL'); | ||
var isPreviewSupported = require('@uppy/utils/lib/isPreviewSupported'); | ||
/** | ||
@@ -21,10 +22,12 @@ * The Thumbnail Generator plugin | ||
module.exports = function (_Plugin) { | ||
_inherits(ThumbnailGenerator, _Plugin); | ||
module.exports = (_temp = _class = | ||
/*#__PURE__*/ | ||
function (_Plugin) { | ||
_inheritsLoose(ThumbnailGenerator, _Plugin); | ||
function ThumbnailGenerator(uppy, opts) { | ||
_classCallCheck(this, ThumbnailGenerator); | ||
var _this; | ||
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts)); | ||
_this = _Plugin.call(this, uppy, opts) || this; | ||
_this.type = 'thumbnail'; | ||
@@ -36,3 +39,2 @@ _this.id = _this.opts.id || 'ThumbnailGenerator'; | ||
_this.defaultThumbnailDimension = 200; | ||
var defaultOptions = { | ||
@@ -42,11 +44,8 @@ thumbnailWidth: null, | ||
}; | ||
_this.opts = _extends({}, defaultOptions, opts); | ||
_this.onFileAdded = _this.onFileAdded.bind(_this); | ||
_this.onFileRemoved = _this.onFileRemoved.bind(_this); | ||
_this.onRestored = _this.onRestored.bind(_this); | ||
_this.onFileAdded = _this.onFileAdded.bind(_assertThisInitialized(_this)); | ||
_this.onFileRemoved = _this.onFileRemoved.bind(_assertThisInitialized(_this)); | ||
_this.onRestored = _this.onRestored.bind(_assertThisInitialized(_this)); | ||
return _this; | ||
} | ||
/** | ||
@@ -61,7 +60,8 @@ * Create a thumbnail for the given Uppy file object. | ||
ThumbnailGenerator.prototype.createThumbnail = function createThumbnail(file, targetWidth, targetHeight) { | ||
var _proto = ThumbnailGenerator.prototype; | ||
_proto.createThumbnail = function createThumbnail(file, targetWidth, targetHeight) { | ||
var _this2 = this; | ||
var originalUrl = URL.createObjectURL(file.data); | ||
var onload = new Promise(function (resolve, reject) { | ||
@@ -79,6 +79,7 @@ var image = new Image(); | ||
}); | ||
return onload.then(function (image) { | ||
var dimensions = _this2.getProportionalDimensions(image, targetWidth, targetHeight); | ||
var canvas = _this2.resizeImage(image, dimensions.width, dimensions.height); | ||
return _this2.canvasToBlob(canvas, 'image/png'); | ||
@@ -88,4 +89,3 @@ }).then(function (blob) { | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -97,5 +97,5 @@ * Get the new calculated dimensions for the given image and a target width | ||
*/ | ||
; | ||
ThumbnailGenerator.prototype.getProportionalDimensions = function getProportionalDimensions(img, width, height) { | ||
_proto.getProportionalDimensions = function getProportionalDimensions(img, width, height) { | ||
var aspect = img.width / img.height; | ||
@@ -121,4 +121,3 @@ | ||
}; | ||
}; | ||
} | ||
/** | ||
@@ -128,10 +127,9 @@ * Make sure the image doesn’t exceed browser/device canvas limits. | ||
*/ | ||
; | ||
ThumbnailGenerator.prototype.protect = function protect(image) { | ||
_proto.protect = function protect(image) { | ||
// https://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element | ||
var ratio = image.width / image.height; | ||
var maxSquare = 5000000; // ios max canvas square | ||
var maxSquare = 5000000; // ios max canvas square | ||
var maxSize = 4096; // ie max canvas dimensions | ||
@@ -141,2 +139,3 @@ | ||
var maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio)); | ||
if (maxW > maxSize) { | ||
@@ -146,2 +145,3 @@ maxW = maxSize; | ||
} | ||
if (maxH > maxSize) { | ||
@@ -151,2 +151,3 @@ maxH = maxSize; | ||
} | ||
if (image.width > maxW) { | ||
@@ -161,4 +162,3 @@ var canvas = document.createElement('canvas'); | ||
return image; | ||
}; | ||
} | ||
/** | ||
@@ -169,16 +169,16 @@ * Resize an image to the target `width` and `height`. | ||
*/ | ||
; | ||
ThumbnailGenerator.prototype.resizeImage = function resizeImage(image, targetWidth, targetHeight) { | ||
_proto.resizeImage = function resizeImage(image, targetWidth, targetHeight) { | ||
// Resizing in steps refactored to use a solution from | ||
// https://blog.uploadcare.com/image-resize-in-browsers-is-broken-e38eed08df01 | ||
image = this.protect(image); // Use the Polyfill for Math.log2() since IE doesn't support log2 | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2#Polyfill | ||
image = this.protect(image); | ||
var steps = Math.ceil(Math.log(image.width / targetWidth) * Math.LOG2E); | ||
// Use the Polyfill for Math.log2() since IE doesn't support log2 | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2#Polyfill | ||
var steps = Math.ceil(Math.log(image.width / targetWidth) * Math.LOG2E); | ||
if (steps < 1) { | ||
steps = 1; | ||
} | ||
var sW = targetWidth * Math.pow(2, steps - 1); | ||
@@ -194,3 +194,2 @@ var sH = targetHeight * Math.pow(2, steps - 1); | ||
image = canvas; | ||
sW = Math.round(sW / x); | ||
@@ -201,4 +200,3 @@ sH = Math.round(sH / x); | ||
return image; | ||
}; | ||
} | ||
/** | ||
@@ -210,5 +208,5 @@ * Save a <canvas> element's content to a Blob object. | ||
*/ | ||
; | ||
ThumbnailGenerator.prototype.canvasToBlob = function canvasToBlob(canvas, type, quality) { | ||
_proto.canvasToBlob = function canvasToBlob(canvas, type, quality) { | ||
try { | ||
@@ -229,21 +227,31 @@ canvas.getContext('2d').getImageData(0, 0, 1, 1); | ||
} | ||
return blob; | ||
}); | ||
} | ||
return Promise.resolve().then(function () { | ||
return dataURItoBlob(canvas.toDataURL(type, quality), {}); | ||
}).then(function (blob) { | ||
if (blob === null) { | ||
throw new Error('could not extract blob, probably an old browser'); | ||
} | ||
return blob; | ||
}); | ||
}; | ||
} | ||
/** | ||
* Set the preview URL for a file. | ||
*/ | ||
; | ||
ThumbnailGenerator.prototype.setPreviewURL = function setPreviewURL(fileID, preview) { | ||
this.uppy.setFileState(fileID, { preview: preview }); | ||
_proto.setPreviewURL = function setPreviewURL(fileID, preview) { | ||
this.uppy.setFileState(fileID, { | ||
preview: preview | ||
}); | ||
}; | ||
ThumbnailGenerator.prototype.addToQueue = function addToQueue(item) { | ||
_proto.addToQueue = function addToQueue(item) { | ||
this.queue.push(item); | ||
if (this.queueProcessing === false) { | ||
@@ -254,6 +262,7 @@ this.processQueue(); | ||
ThumbnailGenerator.prototype.processQueue = function processQueue() { | ||
_proto.processQueue = function processQueue() { | ||
var _this3 = this; | ||
this.queueProcessing = true; | ||
if (this.queue.length > 0) { | ||
@@ -272,3 +281,3 @@ var current = this.queue.shift(); | ||
ThumbnailGenerator.prototype.requestThumbnail = function requestThumbnail(file) { | ||
_proto.requestThumbnail = function requestThumbnail(file) { | ||
var _this4 = this; | ||
@@ -279,14 +288,19 @@ | ||
_this4.setPreviewURL(file.id, preview); | ||
_this4.uppy.log('[ThumbnailGenerator] Generated thumbnail for ' + file.id); | ||
_this4.uppy.log("[ThumbnailGenerator] Generated thumbnail for " + file.id); | ||
_this4.uppy.emit('thumbnail:generated', _this4.uppy.getFile(file.id), preview); | ||
}).catch(function (err) { | ||
_this4.uppy.log('[ThumbnailGenerator] Failed thumbnail for ' + file.id + ':', 'warning'); | ||
_this4.uppy.log("[ThumbnailGenerator] Failed thumbnail for " + file.id + ":", 'warning'); | ||
_this4.uppy.log(err, 'warning'); | ||
_this4.uppy.emit('thumbnail:error', _this4.uppy.getFile(file.id), err); | ||
}); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
ThumbnailGenerator.prototype.onFileAdded = function onFileAdded(file) { | ||
_proto.onFileAdded = function onFileAdded(file) { | ||
if (!file.preview) { | ||
@@ -297,9 +311,10 @@ this.addToQueue(file); | ||
ThumbnailGenerator.prototype.onFileRemoved = function onFileRemoved(file) { | ||
_proto.onFileRemoved = function onFileRemoved(file) { | ||
var index = this.queue.indexOf(file); | ||
if (index !== -1) { | ||
this.queue.splice(index, 1); | ||
} | ||
} // Clean up object URLs. | ||
// Clean up object URLs. | ||
if (file.preview && isObjectURL(file.preview)) { | ||
@@ -310,7 +325,7 @@ URL.revokeObjectURL(file.preview); | ||
ThumbnailGenerator.prototype.onRestored = function onRestored() { | ||
_proto.onRestored = function onRestored() { | ||
var _this5 = this; | ||
var _uppy$getState = this.uppy.getState(), | ||
files = _uppy$getState.files; | ||
var _this$uppy$getState = this.uppy.getState(), | ||
files = _this$uppy$getState.files; | ||
@@ -320,4 +335,5 @@ var fileIDs = Object.keys(files); | ||
var file = _this5.uppy.getFile(fileID); | ||
if (!file.isRestored) return; | ||
// Only add blob URLs; they are likely invalid after being restored. | ||
if (!file.isRestored) return; // Only add blob URLs; they are likely invalid after being restored. | ||
if (!file.preview || isObjectURL(file.preview)) { | ||
@@ -329,3 +345,3 @@ _this5.addToQueue(file); | ||
ThumbnailGenerator.prototype.install = function install() { | ||
_proto.install = function install() { | ||
this.uppy.on('file-added', this.onFileAdded); | ||
@@ -336,3 +352,3 @@ this.uppy.on('file-removed', this.onFileRemoved); | ||
ThumbnailGenerator.prototype.uninstall = function uninstall() { | ||
_proto.uninstall = function uninstall() { | ||
this.uppy.off('file-added', this.onFileAdded); | ||
@@ -344,2 +360,2 @@ this.uppy.off('file-removed', this.onFileRemoved); | ||
return ThumbnailGenerator; | ||
}(Plugin); | ||
}(Plugin), _class.VERSION = "1.1.0", _temp); |
{ | ||
"name": "@uppy/thumbnail-generator", | ||
"description": "Uppy plugin that generates small previews of images to show on your upload UI.", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -25,6 +25,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@uppy/utils": "1.0.2" | ||
"@uppy/utils": "1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@uppy/core": "1.0.2", | ||
"@uppy/core": "1.1.0", | ||
"namespace-emitter": "^2.0.1" | ||
@@ -35,3 +35,3 @@ }, | ||
}, | ||
"gitHead": "5c9e1a836df858254657a59ab8bd4b90035cf8b4" | ||
"gitHead": "28d235fe2fb57d87a399c20883fd6590aa49f4f4" | ||
} |
@@ -11,2 +11,4 @@ const { Plugin } = require('@uppy/core') | ||
module.exports = class ThumbnailGenerator extends Plugin { | ||
static VERSION = require('../package.json').version | ||
constructor (uppy, opts) { | ||
@@ -108,4 +110,4 @@ super(uppy, opts) | ||
var maxSquare = 5000000 // ios max canvas square | ||
var maxSize = 4096 // ie max canvas dimensions | ||
var maxSquare = 5000000 // ios max canvas square | ||
var maxSize = 4096 // ie max canvas dimensions | ||
@@ -195,2 +197,7 @@ var maxW = Math.floor(Math.sqrt(maxSquare * ratio)) | ||
return dataURItoBlob(canvas.toDataURL(type, quality), {}) | ||
}).then((blob) => { | ||
if (blob === null) { | ||
throw new Error('could not extract blob, probably an old browser') | ||
} | ||
return blob | ||
}) | ||
@@ -197,0 +204,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
979
38740
+ Added@uppy/utils@1.1.0(transitive)
- Removed@uppy/utils@1.0.2(transitive)
Updated@uppy/utils@1.1.0