diff options
| author | hallgren <hallgren@chalmers.se> | 2012-06-19 13:59:19 +0000 |
|---|---|---|
| committer | hallgren <hallgren@chalmers.se> | 2012-06-19 13:59:19 +0000 |
| commit | 9bee6a90503b9fd28229726c691ab1475143d9db (patch) | |
| tree | 069ebb8cd5cb870713c7a3f093d15d8e40260512 /src | |
| parent | 3d5efdd2d26f5c411891fc1cf56e6a2ebbf91a66 (diff) | |
translator: import text from local files
It works in Chrome, Firefox and Opera, but not Safari.
(The browser must support the File, FileList and FileReader APIs.)
Diffstat (limited to 'src')
| -rw-r--r-- | src/www/translator/about.html | 9 | ||||
| -rw-r--r-- | src/www/translator/translator.js | 40 |
2 files changed, 36 insertions, 13 deletions
diff --git a/src/www/translator/about.html b/src/www/translator/about.html index 7fb8ee0e9..f1bf573ca 100644 --- a/src/www/translator/about.html +++ b/src/www/translator/about.html @@ -44,8 +44,11 @@ closed and reopened later. Documents can be saved locally or in the cloud. <h2>TODO</h2> <ul> - <li>Text can be imported/exported by copying and pasting, but other ways - could be added. + <li>Text can be imported by copying and pasting and from local files + in browsers that support it (Chrome, Firefox and Opera, but not Safari), + but other ways could be added. + <li>Text can be exported by copying and pasting, but other ways could + be added. <li>GF's lexer/unlexer is used to allow for more natural looking text, but the unlexer does the wrong thing if the first word of a sentence is supposed to be capitalized, e.g. "I am ready." and "Spanish wine is good." @@ -61,7 +64,7 @@ closed and reopened later. Documents can be saved locally or in the cloud. <hr> <div class=modtime><small> -<!-- hhmts start --> Last modified: Wed Jun 13 23:28:06 CEST 2012 <!-- hhmts end --> +<!-- hhmts start --> Last modified: Tue Jun 19 15:54:33 CEST 2012 <!-- hhmts end --> </small></div> <address> <a href="http://www.cse.chalmers.se/~hallgren/">TH</a> diff --git a/src/www/translator/translator.js b/src/www/translator/translator.js index 86f8ff4f5..2c2cdab9d 100644 --- a/src/www/translator/translator.js +++ b/src/www/translator/translator.js @@ -397,16 +397,30 @@ Translator.prototype.import=function(el) { t.redraw() } function done() { - var text=inp.value - var ls=text.split("\n") - var segs= punct.firstChild.checked - ? split_punct(text,punctchars.value) - : paras.firstChild.checked - ? join_paragraphs(ls) - : ls - for(var i in segs) - t.document.segments.push(new_segment(segs[i])) - restore() + function add_segments(text) { + var ls=text.split("\n") + var segs= punct.firstChild.checked + ? split_punct(text,punctchars.value) + : paras.firstChild.checked + ? join_paragraphs(ls) + : ls + for(var i in segs) + t.document.segments.push(new_segment(segs[i])) + } + function import_text(text2) { + var text=inp.value + add_segments(text2) + add_segments(text) + restore() + } + function import_file(ev) { import_text(ev.target.result) } + if(files) { + var file=files.files[0] + var r=new FileReader() + r.onload=import_file + r.readAsText(file) + } + else import_text("") return false } var inp=node("textarea",{name:"it",value:"",rows:"10"}) @@ -424,6 +438,12 @@ Translator.prototype.import=function(el) { wrap("dl",[dt([punct,punctchars]),dt(lines),dt(paras)]), submit(), button("Cancel",restore)]) + if(window.File && window.FileList && window.FileReader) { + // Allow import from local files, if the browers supports it. + // See http://www.html5rocks.com/en/tutorials/file/dndfiles/ + var files=node("input",{name:"files","type":"file"}) + e.insertBefore(files,inp) + } t.view.appendChild(e) e.onsubmit=done inp.focus(); |
