translator: you can now import text by copying and pasting

This commit is contained in:
hallgren
2012-05-25 16:48:03 +00:00
parent bc5363de1b
commit 33165aa8e3
3 changed files with 87 additions and 7 deletions

View File

@@ -22,7 +22,8 @@
</dl>
<td>Edit
<dl>
<dt onclick="translator.import(this)">Add a segment...
<dt onclick="translator.import(this)">Import text...
<dt onclick="translator.add_segment(this)">Add a segment...
<dt onclick="translator.remove(this)">Remove the last segment
</dl>
<td>View
@@ -68,7 +69,7 @@
</div>
<hr>
<div class=modtime><small>HMTL
<!-- hhmts start --> Last modified: Wed May 16 16:15:20 CEST 2012 <!-- hhmts end -->
<!-- hhmts start --> Last modified: Fri May 25 16:08:41 CEST 2012 <!-- hhmts end -->
</small></div>
<a href="about.html">About</a>

View File

@@ -1,6 +1,8 @@
body { margin: 5px; }
h1 { float: right; margin: 0; font-size: 150%; }
h2 { font-size: 120%; }
h3 { font-size: 100%; }
div.pagehead { font-family: sans-serif;
background-color: #ccc;
}
@@ -26,7 +28,7 @@ div.document {
padding: 0.6ex;
}
div.document h2 { color: #009; }
div.document h2, div.document h3 { color: #009; }
table.segments { margin-left: auto; margin-right: auto; }
tr.segment:hover { background: #ffc; }
@@ -46,9 +48,11 @@ td.options > div > dl {
white-space: nowrap;
}
td.target input[name=it] {
td.source input[name=it], td.target input[name=it], textarea {
width: 100%; font-family: inherit; font-size: inherit;
}
div.document form { width: 100%; }
span.arrow { color: blue; }
span.error { color: red; }
span.error { color: red; }

View File

@@ -211,8 +211,8 @@ Translator.prototype.close=function(el) {
hide_menu(el);
this.browse();
}
Translator.prototype.import=function(el) {
/*
Translator.prototype.add_segment1=function(el) {
hide_menu(el);
var t=this
function imp() {
@@ -224,6 +224,70 @@ Translator.prototype.import=function(el) {
}
setTimeout(imp,100)
}
*/
Translator.prototype.add_segment=function(el) {
hide_menu(el);
var t=this
function imp() {
function restore() {
t.redraw()
}
function done() {
var text=inp.value
if(text) t.document.segments.push(new_segment(text))
restore()
return false
}
var inp=node("input",{name:"it",value:""})
var e=wrap("form",[inp, submit(), button("Cancel",restore)])
var source=wrap_class("td","source",e)
var edit=wrap_class("tr","segment",source)
var ss=t.drawing.segments
var n=ss.length
if(n>0) insertAfter(edit,ss[n-1])
else t.view.appendChild(wrap_class("table","segments",edit))
e.onsubmit=done
inp.focus();
}
setTimeout(imp,100)
}
Translator.prototype.import=function(el) {
hide_menu(el);
var t=this
function imp() {
function restore() {
t.redraw()
}
function done2() {
var text=inp.value
var ls=text.split("\n")
var segs= paras.firstChild.checked ? join_paragraphs(ls) : ls
for(var i in segs)
t.document.segments.push(new_segment(segs[i]))
restore()
return false
}
var inp=node("textarea",{name:"it",value:"",rows:"10"})
var lines=radiobutton("separator","lines",
"Segments are separated by line breaks",null,true)
var paras=radiobutton("separator","paras",
"Segments are separate by blank lines",null,false)
var e=node("form",{onsubmit:done2},
[wrap("h3",text("Import text")),
inp,
wrap("dl",map(dt,[lines,paras])),
submit(), button("Cancel",restore)])
t.view.appendChild(e)
e.onsubmit=done2
inp.focus();
}
setTimeout(imp,100)
}
Translator.prototype.remove=function(el) {
hide_menu(el);
var t=this
@@ -419,6 +483,17 @@ function mapix(f,xs) {
return ys;
}
// Convert array of lines to array of paragraphs
function join_paragraphs(lines) {
var paras=[]
var current="";
for(var i in lines)
if(lines[i]=="") paras.push(current),current=""
else current+=" "+lines[i]
if(current) paras.push(current)
return paras
}
/* --- DOM Support ---------------------------------------------------------- */
function a(url,linked) { return node("a",{href:url},linked); }