summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-28 12:38:02 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-28 12:38:02 +0100
commite07b39afe54d744597c32f71fc3191ba1449ab90 (patch)
tree41f8b74cdb24ca4b281e75c8d4d99c302f8d5f02
parent6ed031ebcf38481123ef8be5a7c9c9881cfcd26b (diff)
Allow browser to read separately from HTML generation
-rw-r--r--lib/browser.js27
-rw-r--r--lib/index.js6
2 files changed, 25 insertions, 8 deletions
diff --git a/lib/browser.js b/lib/browser.js
index 2a9c4e1..5a8d70a 100644
--- a/lib/browser.js
+++ b/lib/browser.js
@@ -2,11 +2,12 @@ var q = require("q");
var mammoth = require("./");
-exports.Converter = mammoth.Converter;
-exports.fileInput = fileInput;
+module.exports = Object.create(mammoth);
-function fileInput(inputElement, callback) {
- var converter = new mammoth.Converter(mammoth.standardOptions);
+module.exports.fileInput = fileInput;
+module.exports.readFileInputOnChange = readFileInputOnChange;
+
+function readFileInputOnChange(inputElement, callback) {
function handleFileSelect(event) {
var file = event.target.files[0];
@@ -15,7 +16,7 @@ function fileInput(inputElement, callback) {
reader.onload = function(loadEvent) {
var arrayBuffer = loadEvent.target.result;
try {
- converter.convertToHtml({file: openFile(arrayBuffer)})
+ mammoth.read({file: openFile(arrayBuffer)})
.then(callback, function(error) {
callback({error: error});
})
@@ -30,6 +31,22 @@ function fileInput(inputElement, callback) {
inputElement.addEventListener('change', handleFileSelect, false);
}
+function fileInput(inputElement, callback) {
+ readFileInputOnChange(inputElement, function(documentResult) {
+ if (documentResult.error) {
+ callback(documentResult);
+ } else {
+ var options = mammoth.standardOptions;
+ mammoth.convertDocumentToHtml(documentResult, options)
+ .then(callback, function(error) {
+ callback({error: error});
+ })
+ .done();
+ }
+
+ });
+}
+
function openFile(arrayBuffer) {
var zipFile = new JSZip(arrayBuffer);
diff --git a/lib/index.js b/lib/index.js
index 5300322..a4b687a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -4,7 +4,7 @@ var htmlPaths = require("./html-paths");
exports.Converter = Converter;
exports.read = read;
-exports.convertToHtml = convertToHtml;
+exports.convertDocumentToHtml = convertDocumentToHtml;
exports.standardOptions = {
paragraphStyleMap: {
"Heading1": htmlPaths.topLevelElement("h1"),
@@ -22,7 +22,7 @@ Converter.prototype.convertToHtml = function(inputOptions) {
var options = this._options;
return read(inputOptions)
.then(function(documentResult) {
- return convertToHtml(documentResult, options);
+ return convertDocumentToHtml(documentResult, options);
});
}
@@ -30,7 +30,7 @@ function read(inputOptions) {
return docxReader.read(inputOptions);
}
-function convertToHtml(documentResult, options) {
+function convertDocumentToHtml(documentResult, options) {
var documentConverter = new DocumentConverter(options);
return documentResult.flatMapThen(function(document) {
return documentConverter.convertToHtml(document);