diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-18 19:52:05 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-18 19:52:05 +0100 |
| commit | 285e6f01314880f782cedb2770c26c5cf0c0b490 (patch) | |
| tree | 491b66bc6c68b9da61c6703afd0914edeadf7627 /lib | |
| parent | aaafc2f7cb97617536fdb54b3f8d41ba94e9cc36 (diff) | |
Split out demo-specific code from lib/browser.js
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/browser.js | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/lib/browser.js b/lib/browser.js index 04d73ee..f2d2f81 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -1,47 +1,48 @@ var q = require("q"); var mammoth = require("./"); -var converter = new mammoth.Converter(mammoth.standardOptions); -function handleFileSelect(event) { - var files = event.target.files; - for (var i = 0; i < files.length; i++) { - var file = files[i]; +exports.Converter = mammoth.Converter; +exports.fileInput = fileInput; + +function fileInput(inputElement, callback) { + var converter = new mammoth.Converter(mammoth.standardOptions); + function handleFileSelect(event) { + var file = event.target.files[0]; + var reader = new FileReader(); reader.onload = function(loadEvent) { var arrayBuffer = loadEvent.target.result; converter.convertToHtml({file: openFile(arrayBuffer)}) - .then(function(result) { - document.getElementById("output").innerHTML = result.html; - }) - .done(); + .then(callback, function(error) { + callback({error: error}); + }); }; - reader.readAsArrayBuffer(file); } - - function openFile(arrayBuffer) { - var zipFile = new JSZip(arrayBuffer); - function exists(name) { - return zipFile.file(name) !== null; - } + inputElement.addEventListener('change', handleFileSelect, false); +} + - function read(name, encoding) { - var array = zipFile.file(name).asUint8Array(); - var buffer = new Buffer(array); - if (encoding) { - return q.when(buffer.toString(encoding)); - } else { - return q.when(buffer); - } +function openFile(arrayBuffer) { + var zipFile = new JSZip(arrayBuffer); + function exists(name) { + return zipFile.file(name) !== null; + } + + function read(name, encoding) { + var array = zipFile.file(name).asUint8Array(); + var buffer = new Buffer(array); + if (encoding) { + return q.when(buffer.toString(encoding)); + } else { + return q.when(buffer); } - return { - exists: exists, - read: read - }; } + return { + exists: exists, + read: read + }; } - -document.getElementById("document").addEventListener('change', handleFileSelect, false); |
