From 43b704379e0842357526cd7935a51d5dc9456cf4 Mon Sep 17 00:00:00 2001 From: Erik Koopmans Date: Sun, 18 Feb 2018 15:14:24 +1100 Subject: [PATCH] Move html2pdf initialization code out of Worker --- src/index.js | 18 ++++++++++++++++-- src/worker.js | 32 +++++++------------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/index.js b/src/index.js index 2dfeb62..33def39 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,9 @@ import 'es6-promise/auto'; import jsPDF from 'jspdf'; import html2canvas from 'html2canvas'; + +import Worker from './worker.js'; import './plugin/jspdf-plugin.js'; -import { objType, createElement, cloneNode, unitConvert } from './utils.js'; /** * Generate a PDF from an HTML element or string using html2canvas and jsPDF. @@ -12,6 +13,20 @@ import { objType, createElement, cloneNode, unitConvert } from './utils.js'; * 'image' ('type' and 'quality'), and 'html2canvas' / 'jspdf', which are * sent as settings to their corresponding functions. */ +var html2pdf = function html2pdf(src, opt) { + // Create a new worker with the given options. + var worker = new html2pdf.Worker(opt); + + if (src) { + // If src is specified, perform the traditional 'simple' operation. + return worker.from(src).save(); + } else { + // Otherwise, return the worker for new Promise-based operation. + return worker; + } +} +html2pdf.Worker = Worker; + var html2pdf = function(source, opt) { // Render the canvas and pass the result to makePDF. var onRendered = opt.html2canvas.onrendered || function() {}; @@ -24,6 +39,5 @@ var html2pdf = function(source, opt) { html2canvas(container, opt.html2canvas).then(done); }; - // Expose the html2pdf function. export default html2pdf; diff --git a/src/worker.js b/src/worker.js index 0b5f255..4c5cd08 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,28 +1,3 @@ -/* ----- BELOW CODE BELONGS IN INDEX.JS ----- */ - -import Worker from './worker.js'; - -var html2pdf = function html2pdf(src, opt) { - // opt = src ? Object.assign(opt || {}, {src: src}) : opt; - var worker = new html2pdf.Worker(opt); - - if (src) { - // If src is specified, perform the traditional 'simple' operation. - worker.from(src).save(); - } else { - // Otherwise, return the worker for new Promise-based operation. - return worker; - } -} -html2pdf.Worker = Worker; - -/* ----- ABOVE CODE BELONGS IN INDEX.JS ----- */ - - - - - - import { objType, createElement, cloneNode, unitConvert } from './utils.js'; /* ----- CONSTRUCTOR ----- */ @@ -431,9 +406,16 @@ Worker.prototype.error = function error(msg) { }); }; + /* ----- ALIASES ----- */ Worker.prototype.using = Worker.prototype.set; Worker.prototype.saveAs = Worker.prototype.save; Worker.prototype.output = Worker.prototype.export; Worker.prototype.run = Worker.prototype.then; + + +/* ----- FINISHING ----- */ + +// Expose the Worker class. +export default Worker;