From b9406a515fda792065ed66c1141ae726fbd2e438 Mon Sep 17 00:00:00 2001 From: lelinhtinh Date: Sun, 21 Jun 2020 16:48:36 +0700 Subject: [PATCH] Add onUpdate callback --- README.md | 3 ++- demo/script.js | 5 ++++- src/jepub.js | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e3e96c2..296a1ac 100644 --- a/README.md +++ b/README.md @@ -118,10 +118,11 @@ Place `<%= image[IMG_ID] %>` inside the chapter's content *(HTML string only)*, ### Generate EPUB `*` ```typescript -jepub.generate(type = 'blob') +jepub.generate(type = 'blob', onUpdate?: metadata => void) ``` - **type**: The type of EPUB to return. See [JSZip type option](https://stuk.github.io/jszip/documentation/api_jszip/generate_async.html#type-option). +- **onUpdate**: _(optional)_ Callback function. See [JSZip onUpdate callback](https://stuk.github.io/jszip/documentation/api_jszip/generate_async.html#onupdate-callback). ### Static methods `+` diff --git a/demo/script.js b/demo/script.js index c9fb1d6..7bc3806 100644 --- a/demo/script.js +++ b/demo/script.js @@ -42,7 +42,10 @@ ]); console.log(jepub); - const blob = await jepub.generate(); + const blob = await jepub.generate('blob', metadata => { + console.log('progression: ' + metadata.percent.toFixed(2) + ' %'); + if (metadata.currentFile) console.log('current file = ' + metadata.currentFile); + }); console.log(blob); let url = URL.createObjectURL(blob); diff --git a/src/jepub.js b/src/jepub.js index 31aa75a..06113f2 100644 --- a/src/jepub.js +++ b/src/jepub.js @@ -188,7 +188,7 @@ export default class jEpub { } } - generate(type = 'blob') { + generate(type = 'blob', onUpdate) { if (!JSZip.support[type]) throw `This browser does not support ${type}`; let notes = this._Zip.file('OEBPS/notes.html'); @@ -236,6 +236,6 @@ export default class jEpub { compressionOptions: { level: 9 } - }); + }, onUpdate); } }