1
0
forked from GitHub/gf-core

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 7a7beabe03
commit 2a55b77d4b
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> <h2>TODO</h2>
<ul> <ul>
<li>Text can be imported/exported by copying and pasting, but other ways <li>Text can be imported by copying and pasting and from local files
could be added. 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 <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 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." 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> <hr>
<div class=modtime><small> <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> </small></div>
<address> <address>
<a href="http://www.cse.chalmers.se/~hallgren/">TH</a> <a href="http://www.cse.chalmers.se/~hallgren/">TH</a>

View File

@@ -397,16 +397,30 @@ Translator.prototype.import=function(el) {
t.redraw() t.redraw()
} }
function done() { function done() {
var text=inp.value function add_segments(text) {
var ls=text.split("\n") var ls=text.split("\n")
var segs= punct.firstChild.checked var segs= punct.firstChild.checked
? split_punct(text,punctchars.value) ? split_punct(text,punctchars.value)
: paras.firstChild.checked : paras.firstChild.checked
? join_paragraphs(ls) ? join_paragraphs(ls)
: ls : ls
for(var i in segs) for(var i in segs)
t.document.segments.push(new_segment(segs[i])) t.document.segments.push(new_segment(segs[i]))
restore() }
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 return false
} }
var inp=node("textarea",{name:"it",value:"",rows:"10"}) 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)]), wrap("dl",[dt([punct,punctchars]),dt(lines),dt(paras)]),
submit(), button("Cancel",restore)]) 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) t.view.appendChild(e)
e.onsubmit=done e.onsubmit=done
inp.focus(); inp.focus();