Update preview package

This commit is contained in:
Xuan Sang LE 2020-05-21 14:51:47 +02:00
parent 5df3106328
commit dd58a998d8
5 changed files with 77 additions and 78 deletions

View File

@ -78,49 +78,51 @@
} }
renderPDF(file) { renderPDF(file) {
var q, status; return this.load(new Promise((resolve, reject) => {
status = `${file.info.name} (${file.info.size} Kb)`; var status;
q = this._api.mid(); status = `${file.info.name} (${file.info.size} Kb)`;
return file.read("binary").then((d) => { return file.read("binary").then((d) => {
this._api.loading(q, "RENDERING"); ($(this.view)).removeClass();
($(this.view)).removeClass(); return PDFJS.getDocument({
return PDFJS.getDocument({ data: d
data: d }).then((pdf) => {
}).then((pdf) => { var fn;
var fn; fn = (p) => {
fn = (p) => { if (p > pdf.numPages) {
if (p > pdf.numPages) { this.setStatus(`${status} - loaded`);
this.setStatus(`${status} - loaded`); return resolve();
return this._api.loaded(q, "OK"); }
} return pdf.getPage(p).then((page) => {
return pdf.getPage(p).then((page) => { var canvas, context, div, renderContext, scale, viewport;
var canvas, context, div, renderContext, scale, viewport; scale = 1.5;
scale = 1.5; viewport = page.getViewport(scale);
viewport = page.getViewport(scale); div = ($("<div/>")).attr("id", "page-" + (page.pageIndex + 1));
div = ($("<div/>")).attr("id", "page-" + (page.pageIndex + 1)); ($(this.view)).append(div);
($(this.view)).append(div); canvas = ($("<canvas>"))[0];
canvas = ($("<canvas>"))[0]; div.append(canvas);
div.append(canvas); context = canvas.getContext('2d');
context = canvas.getContext('2d'); canvas.height = viewport.height;
canvas.height = viewport.height; canvas.width = viewport.width;
canvas.width = viewport.width; renderContext = {
renderContext = { canvasContext: context,
canvasContext: context, viewport: viewport
viewport: viewport };
}; page.render(renderContext);
page.render(renderContext); this.setStatus(`${status} - ${p}/${pdf.numPages} loaded`);
this.setStatus(`${status} - ${p}/${pdf.numPages} loaded`); return fn(p + 1);
return fn(p + 1); }).catch(function(e) {
}); return reject(e);
}; });
return fn(1); };
}).catch((err) => { return fn(1);
this.error(__("Cannot render the PDF file")); }).catch(function(e) {
return this._api.loaded(q, "FAIL"); return reject(e);
});
}).catch(function(e) {
return reject(e);
}); });
}).catch((e) => { })).catch((e) => {
this._api.loaded(q, "FAIL"); return this.error(__("Unable to view file: {}", file.path), e);
return this.error(__("Unable to read file: {}", file.path), e);
}); });
} }

Binary file not shown.

View File

@ -58,40 +58,37 @@ class Preview extends this.OS.GUI.BaseApplication
($ @status).html t ($ @status).html t
renderPDF: (file) -> renderPDF: (file) ->
status = "#{file.info.name} (#{file.info.size} Kb)" @load new Promise (resolve, reject) =>
q = @_api.mid() status = "#{file.info.name} (#{file.info.size} Kb)"
file.read("binary").then (d) => file.read("binary").then (d) =>
@_api.loading q, "RENDERING" ($ @view).removeClass()
($ @view).removeClass() PDFJS.getDocument { data: d }
PDFJS.getDocument { data: d } .then (pdf) =>
.then (pdf) => fn = (p) =>
fn = (p) => if p > pdf.numPages
if p > pdf.numPages @setStatus "#{status} - loaded"
@setStatus "#{status} - loaded" return resolve()
return @_api.loaded q, "OK" pdf.getPage(p).then (page) =>
pdf.getPage(p).then (page) => scale = 1.5
scale = 1.5 viewport = page.getViewport scale
viewport = page.getViewport scale div = ($ "<div/>").attr("id", "page-" + (page.pageIndex + 1))
div = ($ "<div/>").attr("id", "page-" + (page.pageIndex + 1)) ($ @view).append div
($ @view).append div canvas = ($ "<canvas>")[0]
canvas = ($ "<canvas>")[0] div.append canvas
div.append canvas context = canvas.getContext '2d'
context = canvas.getContext '2d' canvas.height = viewport.height
canvas.height = viewport.height canvas.width = viewport.width
canvas.width = viewport.width renderContext =
renderContext = canvasContext: context
canvasContext: context viewport: viewport
viewport: viewport page.render renderContext
page.render renderContext @setStatus "#{status} - #{p}/#{pdf.numPages} loaded"
@setStatus "#{status} - #{p}/#{pdf.numPages} loaded" fn(p+1)
fn(p+1) .catch (e) -> reject e
fn(1) fn(1)
.catch (err) => .catch (e) -> reject e
@error __("Cannot render the PDF file") .catch (e) -> reject e
@_api.loaded q, "FAIL" .catch (e) => @error __("Unable to view file: {}", file.path), e
.catch (e) =>
@_api.loaded q, "FAIL"
@error __("Unable to read file: {}", file.path), e
renderSVG: (file) -> renderSVG: (file) ->
($ @view).attr("class", "image") ($ @view).attr("class", "image")

View File

@ -6,7 +6,7 @@
"author": "Xuan Sang LE", "author": "Xuan Sang LE",
"email": "xsang.le@gmail.com" "email": "xsang.le@gmail.com"
}, },
"version":"0.0.1-a", "version":"0.0.2-a",
"category":"Utils", "category":"Utils",
"iconclass":"fa fa-eye", "iconclass":"fa fa-eye",
"mimes":["[^\/]*/.*pdf", "image/.*"] "mimes":["[^\/]*/.*pdf", "image/.*"]

View File

@ -32,7 +32,7 @@
"description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Preview/README.md", "description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Preview/README.md",
"category": "Utils", "category": "Utils",
"author": "Xuan Sang LE", "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" "download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Preview/build/release/Preview.zip"
}, },
{ {