diff options
| -rw-r--r-- | browser-demo/demo.js | 8 | ||||
| -rw-r--r-- | browser-demo/index.html | 1 | ||||
| -rw-r--r-- | lib/browser.js | 61 | ||||
| -rw-r--r-- | makefile | 2 |
4 files changed, 41 insertions, 31 deletions
diff --git a/browser-demo/demo.js b/browser-demo/demo.js new file mode 100644 index 0000000..079fbfc --- /dev/null +++ b/browser-demo/demo.js @@ -0,0 +1,8 @@ +(function() { + mammoth.fileInput( + document.getElementById("document"), + function(result) { + document.getElementById("output").innerHTML = result.html; + } + ); +})(); diff --git a/browser-demo/index.html b/browser-demo/index.html index 6f5d360..6056c19 100644 --- a/browser-demo/index.html +++ b/browser-demo/index.html @@ -18,6 +18,7 @@ </div> <script src="bundle.js"></script> + <script src="demo.js"></script> </body> </html> 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); @@ -24,4 +24,4 @@ browser-demo/bundle.js: cat static/jszip/jszip-inflate.js >> $@ cat static/jszip/jszip-load.js >> $@ node_modules/.bin/node-license-sniffer . --recurse --body --js-comment >> $@ - node_modules/.bin/browserify lib/browser.js >> $@ + node_modules/.bin/browserify lib/browser.js --standalone mammoth >> $@ |
