diff --git a/Preview/build/debug/main.js b/Preview/build/debug/main.js index 7655643..f3aa5c6 100644 --- a/Preview/build/debug/main.js +++ b/Preview/build/debug/main.js @@ -78,49 +78,51 @@ } renderPDF(file) { - var q, status; - status = `${file.info.name} (${file.info.size} Kb)`; - q = this._api.mid(); - return file.read("binary").then((d) => { - this._api.loading(q, "RENDERING"); - ($(this.view)).removeClass(); - return PDFJS.getDocument({ - data: d - }).then((pdf) => { - var fn; - fn = (p) => { - if (p > pdf.numPages) { - this.setStatus(`${status} - loaded`); - return this._api.loaded(q, "OK"); - } - return pdf.getPage(p).then((page) => { - var canvas, context, div, renderContext, scale, viewport; - scale = 1.5; - viewport = page.getViewport(scale); - div = ($("
")).attr("id", "page-" + (page.pageIndex + 1)); - ($(this.view)).append(div); - canvas = ($(""))[0]; - div.append(canvas); - context = canvas.getContext('2d'); - canvas.height = viewport.height; - canvas.width = viewport.width; - renderContext = { - canvasContext: context, - viewport: viewport - }; - page.render(renderContext); - this.setStatus(`${status} - ${p}/${pdf.numPages} loaded`); - return fn(p + 1); - }); - }; - return fn(1); - }).catch((err) => { - this.error(__("Cannot render the PDF file")); - return this._api.loaded(q, "FAIL"); + return this.load(new Promise((resolve, reject) => { + var status; + status = `${file.info.name} (${file.info.size} Kb)`; + return file.read("binary").then((d) => { + ($(this.view)).removeClass(); + return PDFJS.getDocument({ + data: d + }).then((pdf) => { + var fn; + fn = (p) => { + if (p > pdf.numPages) { + this.setStatus(`${status} - loaded`); + return resolve(); + } + return pdf.getPage(p).then((page) => { + var canvas, context, div, renderContext, scale, viewport; + scale = 1.5; + viewport = page.getViewport(scale); + div = ($("
")).attr("id", "page-" + (page.pageIndex + 1)); + ($(this.view)).append(div); + canvas = ($(""))[0]; + div.append(canvas); + context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; + renderContext = { + canvasContext: context, + viewport: viewport + }; + page.render(renderContext); + this.setStatus(`${status} - ${p}/${pdf.numPages} loaded`); + return fn(p + 1); + }).catch(function(e) { + return reject(e); + }); + }; + return fn(1); + }).catch(function(e) { + return reject(e); + }); + }).catch(function(e) { + return reject(e); }); - }).catch((e) => { - this._api.loaded(q, "FAIL"); - return this.error(__("Unable to read file: {}", file.path), e); + })).catch((e) => { + return this.error(__("Unable to view file: {}", file.path), e); }); } diff --git a/Preview/build/release/Preview.zip b/Preview/build/release/Preview.zip index 31e1a57..a3fa766 100644 Binary files a/Preview/build/release/Preview.zip and b/Preview/build/release/Preview.zip differ diff --git a/Preview/main.coffee b/Preview/main.coffee index 6f76da2..36a9e12 100644 --- a/Preview/main.coffee +++ b/Preview/main.coffee @@ -58,40 +58,37 @@ class Preview extends this.OS.GUI.BaseApplication ($ @status).html t renderPDF: (file) -> - status = "#{file.info.name} (#{file.info.size} Kb)" - q = @_api.mid() - file.read("binary").then (d) => - @_api.loading q, "RENDERING" - ($ @view).removeClass() - PDFJS.getDocument { data: d } - .then (pdf) => - fn = (p) => - if p > pdf.numPages - @setStatus "#{status} - loaded" - return @_api.loaded q, "OK" - pdf.getPage(p).then (page) => - scale = 1.5 - viewport = page.getViewport scale - div = ($ "
").attr("id", "page-" + (page.pageIndex + 1)) - ($ @view).append div - canvas = ($ "")[0] - div.append canvas - context = canvas.getContext '2d' - canvas.height = viewport.height - canvas.width = viewport.width - renderContext = - canvasContext: context - viewport: viewport - page.render renderContext - @setStatus "#{status} - #{p}/#{pdf.numPages} loaded" - fn(p+1) - fn(1) - .catch (err) => - @error __("Cannot render the PDF file") - @_api.loaded q, "FAIL" - .catch (e) => - @_api.loaded q, "FAIL" - @error __("Unable to read file: {}", file.path), e + @load new Promise (resolve, reject) => + status = "#{file.info.name} (#{file.info.size} Kb)" + file.read("binary").then (d) => + ($ @view).removeClass() + PDFJS.getDocument { data: d } + .then (pdf) => + fn = (p) => + if p > pdf.numPages + @setStatus "#{status} - loaded" + return resolve() + pdf.getPage(p).then (page) => + scale = 1.5 + viewport = page.getViewport scale + div = ($ "
").attr("id", "page-" + (page.pageIndex + 1)) + ($ @view).append div + canvas = ($ "")[0] + div.append canvas + context = canvas.getContext '2d' + canvas.height = viewport.height + canvas.width = viewport.width + renderContext = + canvasContext: context + viewport: viewport + page.render renderContext + @setStatus "#{status} - #{p}/#{pdf.numPages} loaded" + fn(p+1) + .catch (e) -> reject e + fn(1) + .catch (e) -> reject e + .catch (e) -> reject e + .catch (e) => @error __("Unable to view file: {}", file.path), e renderSVG: (file) -> ($ @view).attr("class", "image") diff --git a/Preview/package.json b/Preview/package.json index 1759a43..c39ac21 100644 --- a/Preview/package.json +++ b/Preview/package.json @@ -6,7 +6,7 @@ "author": "Xuan Sang LE", "email": "xsang.le@gmail.com" }, - "version":"0.0.1-a", + "version":"0.0.2-a", "category":"Utils", "iconclass":"fa fa-eye", "mimes":["[^\/]*/.*pdf", "image/.*"] diff --git a/packages.json b/packages.json index 6502fb3..7084f9a 100644 --- a/packages.json +++ b/packages.json @@ -32,7 +32,7 @@ "description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Preview/README.md", "category": "Utils", "author": "Xuan Sang LE", - "version": "0.0.1-a", + "version": "0.0.2-a", "download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Preview/build/release/Preview.zip" }, {