-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVGTranslate.html
270 lines (252 loc) · 10.9 KB
/
SVGTranslate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<html>
<head><title>SVG Translate - JS Prototype</title></head>
<body>
<p><a href="https://commons.wikimedia.org/wiki/Category:SVG_with_multiple_translations_with_contributions_from_Aeroid">Some Examples</a> | <a href="https://www.diffchecker.com/">diff</a></p>
<textarea name="SVGOUT" cols="120" rows="25"><?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" font-family="DejaVu Sans,sans-serif" font-stretch="condensed" font-weight="bold">
<rect x="10%" y="10%" width="80%" height="80%" stroke-width="4px" fill="lightgray" stroke="green"/>
<!-- switch -->
<g>
<text x="50%" y="50%" text-anchor="middle" dy=".3em">Translate me!</text>
</g>
<!-- /switch -->
<!--
This is just very simple example. Hit the checkbox below ... Play around and use more complex examples.
-->
</svg>
</textarea></br>
<input name="EDITABLE" type="checkbox" onChange="edit(this.checked)"/> Editable</br>
<div id="EDIT"></div>
<script>
//
// SVG switch/systemLanguage allows for translation
// text/tspan can make this a text-only translation process
// this is js-tooling to expose these SVG data structures for easy access
// it allows text-only changes per element and language
// and adding new languages based on prepared defaults
//
// simplest precondition:
// text element within a switch element
//
// this will enable to replicate the text in-place for a new language
//
// more complex scenarios supported are
// - multiple switch elements
// - g / text / tspan combinations
//
const parser = new DOMParser();
var svgdom = "";
var strings = {};
var nstr = 1;
var langs = [];
var editors = [];
var defaultLang = -1;
function setSVGcode(code) {
// unparse XML into textarea
if (code.startsWith("<"+"?"))
document.getElementsByName("SVGOUT")[0].value = code;
else
document.getElementsByName("SVGOUT")[0].value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + code;
}
function getSVGcode() {
// get SVG code from textarea
return document.getElementsByName("SVGOUT")[0].value;
}
function addLanguage(lang) {
// register a new language or return index for existing language
if (typeof langs[lang] === "undefined") {
var l = numOfLanguages();
langs[lang] = l;
editors[l] = [];
}
//console.log("lang: "+langs[lang]);
return langs[lang];
}
function numOfLanguages() {
// return number of registered languages
return Object.keys(langs).length;
}
function languageByIndex(i) {
// return language label by index
return Object.keys(langs)[i];
}
function edit(makeEditable) {
var ed = document.getElementById("EDIT");
if (makeEditable) {
// turn on editing by parsing the SVG code, annotate the DOM for structured editing
strings = {};
nstr = 1;
langs = {};
editors = [];
defaultLang = [];
// parse SVG
svgdom = parser.parseFromString(getSVGcode(), "image/svg+xml");
const errorNode = svgdom.querySelector('parsererror');
if (errorNode) {
console.log("SVG parsing from textarea failed");
} else {
// find text to be edited and group them by language
// TO DO: WILL NOT work for single children for multiple languages (e.g. "fr, it, de")
var switches = svgdom.getElementsByTagName('switch');
for (var sw = 0; sw < switches.length ; sw++) {
for (var i = 0; i < switches[sw].childElementCount; i++) {
if (getSystemLanguageStrings(switches[sw].children[i], false) === "") {
defaultLang.push(newString(switches[sw].children[i],false));
}
}
}
// show the SVG code annotated with editor IDs
setSVGcode(svgdom.documentElement.outerHTML);
}
if (numOfLanguages() < 1) {
alert("Sorry, are there really <switch> elements in the SVG, that you want to edit?");
document.getElementsByName("EDITABLE")[0].checked = false;
}
// create the editor from the languages
var edtab = document.createElement("table");
ed.appendChild(edtab);
for (var row = numOfLanguages()-1; row >= 0 ; row--) {
edtab.appendChild(addEditLine(row));
}
} else {
// finalize editing by removing annotated IDs and create SVG code
if (ed.childNodes.length > 0) {
ed.removeChild(ed.childNodes[0]);
for (var dl = 0; dl < defaultLang.length; dl++) {
removeId(defaultLang[dl]);
}
for (var row = numOfLanguages()-1; row >= 0 ; row--) {
for (var col = 0; col < editors[row].length; col++) {
//console.log("editors["+ row +"]["+ col +"]:"+ editors[row][col]);
removeId(editors[row][col]);
}
}
setSVGcode(svgdom.documentElement.outerHTML);
}
}
}
function removeId(id) {
// for an element, remove the editor ID and put back original IDs if existing
var sid = svgdom.getElementById(id);
if ( typeof sid !== 'undefined' && sid !== null ) {
if ( typeof sid.attributes["OLDid"] !== 'undefined' ) {
sid.setAttribute("id", sid.attributes["OLDid"].value);
sid.removeAttribute("OLDid");
} else {
sid.removeAttribute("id");
}
}
}
function newString(strElem, replace) {
// for an element, create a editor ID and save the original ID for restoration later within DOM
// replacement of IDs needed for clones from defaultLang
var label = "Aaa-" + (nstr++) + "-zzZ";
//console.log(label);
strings[label] = strElem.value;
if (! replace)
if (typeof strElem.attributes["id"] !== 'undefined')
strElem.setAttribute("OLDid", strElem.attributes["id"].value);
strElem.setAttribute("id", label);
return label;
}
function addEditor(li, textElem, replace) {
// add an ediotr ID to a language (by index)
// replacement of IDs needed for clones from defaultLang
//console.log("li: "+li+" textElem: "+textElem);
editors[li].push(newString(textElem, replace));
}
function getSystemLanguageStrings(elem, replace) {
// for each immediate element (elem) underneath a switch element, find all text/tspan elements and register them for editing
// assigned to the language. This child (elem) could be a <g> or a <text>. It could come from the parse or a newly added
// clone from default (which might need removal of existing IDs -> replace)
var lang = "";
if (typeof elem.attributes["systemLanguage"] !== 'undefined')
lang = elem.attributes["systemLanguage"].value;
var li = addLanguage(lang);
var txts = [];
if ( elem.tagName === "text" )
txts[0] = elem;
else
txts = elem.getElementsByTagName('text');
if (typeof txts !== 'undefined' && txts.length > 0) {
for (var t = 0; t < txts.length; t++) {
var spans = txts[t].getElementsByTagName('tspan');
if (typeof spans !== 'undefined' && spans.length > 0) {
for (var s = 0; s < spans.length; s++) {
addEditor(li, spans[s], replace);
}
} else {
addEditor(li, txts[t], replace);
}
}
} else {
console.log("(no text)");
}
return lang;
}
function update(label, value) {
// update the DOM after edit by the editor ID
//console.log(label, value);
svgdom.getElementById(label).textContent = value;
setSVGcode(svgdom.documentElement.outerHTML);
}
function addEditLine(row) {
// add all editors for a single language
//
//console.log("addEditLine: "+row);
var tr = document.createElement("tr");
var th = document.createElement("th");
tr.appendChild(th);
if (languageByIndex(row) != "")
th.textContent=languageByIndex(row);
else {
// the default language line (without lang code) will host the button to clone a new language from
tr.setAttribute("id","defaultLang");
var inp = document.createElement("input");
th.appendChild(inp);
inp.setAttribute("Type", "button");
inp.setAttribute("Value", "+");
inp.setAttribute("onClick", "addLang(prompt('Whats the two-letter code of the new language you want to add?'))");
}
for (var col = 0; col < editors[row].length; col++) {
//console.log("editors["+ row +"]["+ col +"]:"+ editors[row][col]);
var td = document.createElement("td");
tr.appendChild(td);
var inp = document.createElement("input");
td.appendChild(inp);
inp.setAttribute("Type", "text");
inp.setAttribute("Name", editors[row][col]);
inp.setAttribute("value", svgdom.getElementById(editors[row][col]).textContent);
inp.setAttribute("onChange", "update(this.name, this.value)");
}
return tr;
}
function removeAllIds(elem) {
// recursivly remove all IDs from a cloned element
for ( var c = 0; c < elem.children.length; c++)
removeAllIds(elem.children[c]);
elem.removeAttribute("id");
elem.removeAttribute("OLDid");
//console.log(elem.outerHTML);
}
function addLang(lang) {
// clone from default language and add editors
// this could be a simple <g> element to be cloned or going through all <switch>es and clone each default
for (var dl = 0; dl < defaultLang.length; dl++) {
var def = svgdom.getElementById(defaultLang[dl]);
var defClone = def.cloneNode(true);
defClone.setAttribute("systemLanguage",lang);
removeAllIds(defClone);
def.before(defClone);
getSystemLanguageStrings(defClone,true);
}
var el = addEditLine(numOfLanguages()-1);
var defEdit = document.getElementById("defaultLang");
defEdit.after(el);
setSVGcode(svgdom.documentElement.outerHTML);
}
document.getElementsByName("EDITABLE")[0].checked = false;
</script>
</body>
</html>