-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathedit_item.js
More file actions
175 lines (156 loc) · 4.92 KB
/
edit_item.js
File metadata and controls
175 lines (156 loc) · 4.92 KB
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
document.querySelectorAll('commentaries > .edit').forEach( x => {
x.addEventListener("click", event => {
var c = document.querySelector('commentaries');
var commentary = document.createElement("commentary");
commentary.setAttribute("author", new Date().toLocaleDateString());
var entry = document.createElement("commentary-entry");
commentary.appendChild(entry);
entry.setAttribute("contenteditable", "true");
x.after(commentary);
entry.focus();
});
});
/*
* Enable showing the reading editor.
*/
document.querySelectorAll('readings > .edit').forEach( x => {
x.addEventListener("click", event => {
var r = document.querySelector('reading-spec');
r.style.display = (r.style.display == "none") ? "block" : "none";
var r = document.querySelector('.save');
r.style.display = (r.style.display == "none") ? "block" : "none";
});
});
const readingSpecBox = document.querySelector("reading-spec");
const transcribedReadingAsciiElement = document.querySelector("transcribed-reading-ascii > reading-text");
const transcribedReadingUnicodeElement = document.querySelector("transcribed-reading-unicode > reading-text");
const parsedReadingAsciiElement = document.querySelector("parsed-reading-ascii > reading-text");
const parsedReadingUnicodeElement = document.querySelector("parsed-reading-unicode > reading-text");
/*
Update the readings with the current state of the reading editor.
*/
readingSpecBox.oninput = (event) => {
var spec = readingSpecBox.innerText.split('\n').filter(x => x.trim() != "");
var text = createTranscribedAsciiFromSpec(spec);
transcribedReadingAsciiElement.innerHTML = text;
var text = createTranscribedAsciiFromSpec(spec_to_unicode(spec));
transcribedReadingUnicodeElement.innerHTML = text;
var text = createParsedAsciiFromSpec(spec);
parsedReadingAsciiElement.innerHTML = text;
var text = createParsedAsciiFromSpec(spec_to_unicode(spec));
parsedReadingUnicodeElement.innerHTML = text;
enableHighlighting();
}
/*
* Downloading the updated page.
*/
addDownloadElement();
function removeDownloadElement() {
document.querySelectorAll('.save').forEach( x => {
x.remove();
});
}
function addDownloadElement() {
const wrapper = document.querySelector("itemwrapper");
var saveButton = document.createElement("div");
saveButton.className = "save";
saveButton.innerText = "Download"
saveButton.style.display = "none"
saveButton.addEventListener("click", download);
wrapper.appendChild(saveButton);
}
function removeLinks() {
document.querySelectorAll('.link').forEach( x => {
x.remove();
});
}
function getFileName() {
let path = window.location.pathname;
let fileName = path.substring(path.lastIndexOf('/') + 1);
return fileName
}
function download() {
readingSpecBox.style.display = "none";
removeLinks();
removeDownloadElement();
removeHighlightElements(); // So that they don't clutter up the saved doc.
let htmlText = "<!DOCTYPE html>\n" + document.documentElement.outerHTML;
let blob = new Blob([htmlText], {type: "text/plain;charset=utf-8"});
saveAs(blob, getFileName());
addHighlightElements(); // Add the highlights back again.
addDownloadElement();
addLinks(); // Add the links back.
}
/*
* Convert ascii rendering to unicode.
*/
function spec_to_unicode(spec) {
return spec.map((s) => {
if (s[0] == "#") return s;
[r,l,w,syl,status] = s.split(' ');
return [r,l,w,ascii_to_ideogram.get(syl),status].join(' ');
});
}
function isNumeric(num){
return !isNaN(num)
}
function isASCII(str) {
return /^[\x00-\x7F]*$/.test(str);
}
function createTranscribedAsciiFromSpec(spec) {
output = "\n <line><word number=\"0\">";
pr = null;
pw = null;
spec.filter((x) => x[0] != '#').forEach( s => {
[r,l,w,syl,status] = s.split(' ');
if (!pr) {
pr = r;
pw = w;
}
if (w != pw) output += "</word>";
if (r != pr) {
pr = r;
output += "</line>\n <line>";
if (w == pw) output += `<word number="${w}">`;
}
if (w != pw) {
pw = w;
output += `<word number="${w}">`;
}
if (isNumeric(syl) && isASCII(syl)) {
output += `<number>${syl}</number>`;
} else {
output += `<ideogram>${syl}</ideogram>`;
}
});
output += "</word></line>";
return output;
}
function createParsedAsciiFromSpec(spec) {
output = "\n <line><word number=\"0\">";
pl = null;
pw = null;
spec.filter((x) => x[0] != '#').forEach( s => {
[r,l,w,syl,status] = s.split(' ');
if (!pr) {
pl = l;
pw = w;
}
if (w != pw) output += "</word>";
if (l != pl) {
pl = l;
output += "</line>\n <line>";
}
if (w != pw) {
pw = w;
output += `<word number="${w}">`;
}
if (isNumeric(syl) && isASCII(syl)) {
output += `<number>${syl}</number>`;
} else {
output += `<ideogram>${syl}</ideogram>`;
}
});
output += "</word></line>";
return output;
}