From fc46d6c9ca86c2954f6bbd37f8ddb5c0cd95f4c8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 28 Apr 2013 12:46:24 +0100 Subject: Expose callback-style convertDocumentToHtml in browser module --- lib/browser.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/browser.js b/lib/browser.js index 5a8d70a..67597fe 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -2,10 +2,14 @@ var q = require("q"); var mammoth = require("./"); -module.exports = Object.create(mammoth); +exports.fileInput = fileInput; +exports.readFileInputOnChange = readFileInputOnChange; -module.exports.fileInput = fileInput; -module.exports.readFileInputOnChange = readFileInputOnChange; +exports.convertDocumentToHtml = function() { + var args = Array.slice(arguments, 0, arguments.length - 1); + var callback = arguments[arguments.length - 1]; + satisfyCallback(mammoth.convertDocumentToHtml.apply(this, arguments), callback); +}; function readFileInputOnChange(inputElement, callback) { function handleFileSelect(event) { @@ -16,11 +20,8 @@ function readFileInputOnChange(inputElement, callback) { reader.onload = function(loadEvent) { var arrayBuffer = loadEvent.target.result; try { - mammoth.read({file: openFile(arrayBuffer)}) - .then(callback, function(error) { - callback({error: error}); - }) - .done(); + var file = openFile(arrayBuffer); + satisfyCallback(mammoth.read({file: file}), callback); } catch (error) { callback({error: error}); } @@ -37,11 +38,10 @@ function fileInput(inputElement, callback) { callback(documentResult); } else { var options = mammoth.standardOptions; - mammoth.convertDocumentToHtml(documentResult, options) - .then(callback, function(error) { - callback({error: error}); - }) - .done(); + satisfyCallback( + mammoth.convertDocumentToHtml(documentResult, options), + callback + ); } }); @@ -68,3 +68,10 @@ function openFile(arrayBuffer) { read: read }; } + + +function satisfyCallback(promise, callback) { + promise.then(callback, function(error) { + callback({error: error}); + }).done(); +} -- cgit v1.2.3