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.)
This commit is contained in:
hallgren
2012-06-19 13:59:19 +00:00
parent 3d5efdd2d2
commit 9bee6a9050
2 changed files with 36 additions and 13 deletions

View File

@@ -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>

View File

@@ -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();