From 18b5b20d2788e48e895a0c92a2d84d24afba34f0 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 24 Oct 2017 14:31:42 +0200 Subject: [PATCH 1/3] Add thumbnails for PDFs --- www/common/common-thumbnail.js | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/www/common/common-thumbnail.js b/www/common/common-thumbnail.js index ea0ce1d54..25cf92bc0 100644 --- a/www/common/common-thumbnail.js +++ b/www/common/common-thumbnail.js @@ -11,7 +11,8 @@ define([ 'image/jpeg', 'image/jpg', 'image/gif', - 'video/' + 'video/', + 'application/pdf' ]; Thumb.isSupportedType = function (type) { @@ -122,10 +123,43 @@ define([ cb('ERROR'); }); }; + Thumb.fromPdfBlob = function (blob, cb) { + require.config({paths: {'pdfjs-dist': '/common/pdfjs'}}); + require(['pdfjs-dist/build/pdf'], function (PDFJS) { + var url = URL.createObjectURL(blob); + var makeThumb = function (page) { + var vp = page.getViewport(1); + var canvas = document.createElement("canvas"); + canvas.width = canvas.height = Thumb.dimension; + var scale = Math.min(canvas.width / vp.width, canvas.height / vp.height); + canvas.width = Math.floor(vp.width * scale); + canvas.height = Math.floor(vp.height * scale); + return page.render({ + canvasContext: canvas.getContext("2d"), + viewport: page.getViewport(scale) + }).promise.then(function () { + return canvas; + }); + }; + PDFJS.getDocument(url).promise + .then(function (doc) { + return doc.getPage(1).then(makeThumb).then(function (canvas) { + canvas.toBlob(function (blob) { + cb(void 0, blob); + }); + }); + }).catch(function (err) { + cb('ERROR'); + }); + }); + }; Thumb.fromBlob = function (blob, cb) { if (blob.type.indexOf('video/') !== -1) { return void Thumb.fromVideoBlob(blob, cb); } + if (blob.type.indexOf('application/pdf') !== -1) { + return void Thumb.fromPdfBlob(blob, cb); + } Thumb.fromImageBlob(blob, cb); }; From bac10472f367b46f7696e578f13f62fbc709f2c4 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 24 Oct 2017 14:32:47 +0200 Subject: [PATCH 2/3] lint compliance --- www/common/common-thumbnail.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/common/common-thumbnail.js b/www/common/common-thumbnail.js index 9bd97e8aa..18e963a4e 100644 --- a/www/common/common-thumbnail.js +++ b/www/common/common-thumbnail.js @@ -149,7 +149,7 @@ define([ cb(void 0, blob); }); }); - }).catch(function (err) { + }).catch(function () { cb('ERROR'); }); }); From dc908110901f0ad6e8c18ccaec053c3ca8db5c5e Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 24 Oct 2017 16:56:08 +0200 Subject: [PATCH 3/3] only call onReady once in sframe-listmap --- www/common/sframe-chainpad-listmap.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/www/common/sframe-chainpad-listmap.js b/www/common/sframe-chainpad-listmap.js index 22b7e6216..a4c63641d 100644 --- a/www/common/sframe-chainpad-listmap.js +++ b/www/common/sframe-chainpad-listmap.js @@ -685,7 +685,9 @@ define([ }); }; + var ready = false; realtimeOptions.onReady = function (info) { + if (ready) { return; } // create your patcher if (realtime !== info.realtime) { realtime = rt.realtime = info.realtime; @@ -709,6 +711,7 @@ define([ DeepProxy.checkLocalChange(proxy, onLocal); initializing = false; + ready = true; }; realtimeOptions.onAbort = function (info) {