diff --git a/index.html b/index.html index 005734a..8776306 100644 --- a/index.html +++ b/index.html @@ -5,22 +5,33 @@ Note to PNG Converter @@ -29,57 +40,52 @@

Display a Supernote File

+
diff --git a/worker-impl.js b/worker-impl.js new file mode 100644 index 0000000..71e98fd --- /dev/null +++ b/worker-impl.js @@ -0,0 +1,20 @@ +import { toImage } from 'supernote-typescript' + +self.onmessage = async function(e) { + const { pageIndex, note } = e.data + try { + const [image] = await toImage(note, [pageIndex]) + const imageBuffer = await image.toBuffer('image/png') + postMessage({ + pageIndex, + imageData: imageBuffer, + status: 'success' + }) + } catch (error) { + postMessage({ + pageIndex, + error: error.message, + status: 'error' + }) + } +} diff --git a/worker.js b/worker.js new file mode 100644 index 0000000..4930e8d --- /dev/null +++ b/worker.js @@ -0,0 +1,20 @@ +export default class SupernoteWorker { + constructor() { + this.worker = new Worker( + new URL('./worker-impl.js?v=' + Date.now(), import.meta.url), + { type: 'module' } + ) + } + + process(note, pageIndex) { + this.worker.postMessage({ note, pageIndex }) + } + + onMessage(callback) { + this.worker.onmessage = (e) => callback(e.data) + } + + terminate() { + this.worker.terminate() + } +}