-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotator.html
More file actions
427 lines (346 loc) · 11.4 KB
/
annotator.html
File metadata and controls
427 lines (346 loc) · 11.4 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Annotator Demo</title>
<style>
/*#annotator {position: relative;}
#annotator #controls {display: absolute; top: 0%; bottom: 90%; height: 10%; left: 0; right: 0; width: 100%;}
#annotator #message {display: absolute; bottom: 0%; height: 5%; top: 95%; left: 0; right: 0; width: 100%;}
#annotator #text-container {display: absolute; top: 10%; bottom: 5%; height: 85%; left: 0; right: 0; width: 100%;}
#annotator #text-container #annotations {position: absolute; top: 0; bottom: 0; height: 100%; left: 0; right: 70%; width: 30%;}
#annotator #text-container #text {position: absolute; top: 0; bottom: 0; height: 100%; left: 30%; right: 0; width: 70%; overflow: scroll; }
*/
</style>
</head>
<body>
<div id="annotator" role="region" aria-label="Annotator">
<h1>Annotator Demo</h1>
<h2>Instructions</h2>
<p><a href="https://github.com/RichCaloggero/annotator/">See README for instructions.</a></p>
<div id="controls">
<div role="region" aria-label="Project">
<h2 id="projectName"></h2>
<label>Project: <input type="text" id="project"></label>
<button id="restore" accesskey="r">restore project</button>
<label>Load Project: <select id="projects"></select></label>
<button id="save" accesskey="s">Save Project</button>
<button id="delete" accesskey="d">Delete Project</button>
</div><div>
<label>Note: <input type="text" id="note" accesskey="n"></label>
<button id="addNote">Add Note</button>
</div><div>
<label>Enable Annotator <input type="checkbox" checked id="enableAnnotator" accesskey="a"></label>
<label>Enable Text Modification<input type="checkbox" id="enableTextModification" accesskey="e"></label>
</div>
</div><!-- controls -->
<hr>
<div id="text-container">
<ul id="annotations"></ul>
<div contenteditable="true" id="text" role="textbox" tabindex="0" aria-multiline="true" accesskey="t"
style="white-space: pre-wrap;">
</div><!-- .text -->
</div><!-- #text-container -->
<hr>
<div id="message" aria-live="polite"></div>
</div><!-- .annotator -->
<script>
"use strict";
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
const annotator = $(".annotator");
const text = $("#text");
const controls = $(".controls");
const enableAnnotator = $("#enableAnnotator");
const enableTextModification = $("#enableTextModification");
const save = $("#save");
const restore = $("#restore");
const annotations = $("#annotations");
const annotationMap = new WeakMap();
const projects = $("#projects");
/// variables
let start = null;
let project = {};
let annotation = null;
/// listen for events
$("#text").addEventListener("keydown", filterKeys);
$("#text").addEventListener("keyup", setAnnotationMode);
$("#annotations").addEventListener("click", e => annotation = displayAnnotation(annotationMap.get(e.target.closest(".annotation"))));
$("#addNote").addEventListener("click", () => {
addNote(annotation, $("#note").value)
displayAnnotation(annotation, annotationMap.get(annotation));
});
$("#restore").addEventListener("click", e => project = restoreProject($("#projects").value));
$("#save").addEventListener("click", () => saveProject(project));
$("#enableAnnotator").addEventListener ("click", (e) => {
text.contentEditable = e.target.checked? "true" : "false";
if (!e.target.checked&& enableTextModification.checked) enableTextModification.checked = false;
compensateForNVDAAccessKeyBug (e.target);
});
$("#enableTextModification").addEventListener ("click", (e) => {
if (e.target.checked&& !enableAnnotator.checked) enableAnnotator.checked = true;
compensateForNVDAAccessKeyBug (e.target);
});
/// init
createProjectList(localStorage, $("#projects"));
// create a project
project = createProject();
/// load default text
fetch("default.txt")
.then(response => response.text())
.then(_text => {
if (!$("#text") || !text.textContent) {
message ("No default text.");
$("#enableTextModification").checked = true;
text.innerHTML = project.text = "";
return;
} // if
project.text = displayText(_text, $("#text"));
message("default text loaded.");
});
function compensateForNVDAAccessKeyBug (element, delay = 50) {
element.blur();
setTimeout(() => element.focus(), delay);
} // compensateForNVDAAccessKeyBug
/// keyboard handlers
function filterKeys (e) {
if (enableTextModification.checked) return true;
const key = e.key;
switch (key) {
case "*": case "ArrowUp": case "ArrowDown": case "ArrowLeft": case "ArrowRight":
case "Home": case "End": case "Tab":
return true;
default: e.preventDefault();
return false;
} // switch
} // filterKeys
function setAnnotationMode (e) {
if (!text.isContentEditable) return false;
const key = e.key;
switch (key) {
case "*":
if (!start) {
start = copySelection(getSelection());
message ("Move to end of text to be annotated and press '*' again.");
} else {
processAnnotation(start, copySelection(getSelection()));
start = null;
} // if
return true;
default: return false;
} // switch
function processAnnotation (start, end) {
const s = getSelection();
const r = removeMarkers(start, end);
s.removeAllRanges();
s.addRange(r);
const newAnnotation = addAnnotation(createAnnotation(s.toString(), r), project.annotations);
if (newAnnotation) {
annotation = newAnnotation;
createAnnotationListItem(annotation, $("#annotations").appendChild(document.createElement("li")));
} else {
message("Cannot create annotation.");
} // if
function removeMarkers (_start, _end) {
const start = _start.anchorNode;
const startOffset = _start.anchorOffset-1;
const end = _end.anchorNode;
const endOffset = _end.anchorOffset-1;
//message(`removeMarkers: ${startOffset}, ${start.textContent[startOffset]}, ${endOffset}, ${end.textContent[endOffset]}`);
const t2 = removeCharacter(end.textContent, endOffset);
end.textContent = t2;
const t1 = removeCharacter(start.textContent, startOffset);
start.textContent = t1;
const r = new Range();
r.setStart(start, startOffset);
r.setEnd(end, endOffset-1); // one less because of merker removal
return r;
} // removeMarkers
} // processAnnotation
} // setAnnotationMode
/// handling selections and ranges
function removeCharacter (s, offset) {
//console.log ("removing: ", offset, s);
if (offset === 0) s = s.slice(1);
else if (offset === s.length-1) s = s.slice(0, -1);
else s = s.slice(0, offset) + s.slice(offset+1);
return s;
} // removeCharacter
/// annotation list
function addAnnotation (annotation, list) {
list.push(annotation);
return annotation;
}// addAnnotation
function removeAnnotation (annotation, list) {
return list.filter(a => a !== annotation);
} // removeAnnotation
function createAnnotation (title, range) {
return {
title: title,
range: copyRange(range),
notes: []
}; // annotation
} // createAnnotation
function addNote (annotation, text) {
if (!annotation) {
message("No current annotation.");
return "";
} // if
annotation.notes.push(text);
createAnnotationListItem (annotation, annotationMap.get(annotation));
return text;
} // addNote
function displayText (text, element) {
element.innerHTML = "";
element.appendChild(document.createTextNode(text));
return text;
} // displayText
function displayAnnotation (annotation) {
const r = annotation.range;
console.log("displayAnnotation: ", r);
const range = new Range();
range.setStart(r.startContainer, r.startOffset);
range.setEnd(r.endContainer, r.endOffset);
const s = getSelection();
s.removeAllRanges();
s.addRange(range);
$("#text").focus();
return annotation;
} // displayAnnotation
/// projects
function createProject (name) {
return {
name: name,
annotations: [],
};
} // createProject
function findProject (name) {
return localStorage[name] || null;
} // findProject
function createProjectList (projects, element) {
element.innerHTML = '<option>--Select Project--</option>';
Object.keys(projects).forEach(key => {
const option = document.createElement ("option");
option.textContent = key;
element.add(option);
});
} // createProjectList
function saveProject (project) {
updateProject(project);
try {
if (!project.name) {
$("#project").focus();
throw new Error("Project has no name; type a name and try save again.");
} // if
const p = Object.assign ({}, project);
p.annotations = p.annotations.map(x => {
x.range = cloneRange(x.range);
return x;
});
const json = JSON.stringify(p);
localStorage[p.name] = json;
} catch (e) {
message(e);
console.log(e);
} // catch
return project;
} // saveProject
function restoreProject (name) {
message (`restoring ${name}:`);
try {
const p = JSON.parse(localStorage.getItem(name));
p.annotations.forEach(x => x.range = restoreRange(x.range, $("#text").firstChild));
displayText(p.text, $("#text"));
message ("Done.");
createAnnotationList(p.annotations);
return p;
} catch (e) {
message(e);
console.log(e);
return null;
} // catch
} // restoreProject
function createAnnotationList (list) {
const annotationList = $("#annotations");
annotationList.innerHTML = "";
list.forEach(annotation => {
annotationList.appendChild(
createAnnotationListItem (annotation, document.createElement("li"))
); // appendChild
}); // forEach
return annotationList;
} // createAnnotationList
function createAnnotationListItem (annotation, element) {
if (! element) return null;
element.innerHTML = "";
element.classList.add("annotation");
element.innerHTML = `<h3 class="title"><button class="display">${annotation.title}</button></h3>
<ul class="notes">
${annotation.notes.map(note => `<li class="note">${note}</li>`).join("\n")}
</ul>`;
annotationMap.set(element, annotation);
annotationMap.set(annotation, element);
return element;
} // createAnnotationListItem
function updateProject (project) {
project.name = $("#project").value;
project.text = $("#text").textContent;
} // updateProject
/// utilities
function cloneRange (r) {
if (r.startContainer !== r.endContainer) {
message ("Cannot clone / save a range whose start and end nodes are not the same!");
return {};
} // if
return {
startOffset: r.startOffset,
endOffset: r.endOffset
};
} // cloneRange
function restoreRange (r, node) {
console.log ("restoreRange: ", r);
return {
startContainer: node,
endContainer: node,
startOffset: Number(r.startOffset),
endOffset: Number(r.endOffset)
};
} // restoreRange
function copySelection (selection) {
const copy = {
anchorNode: selection.anchorNode,
anchorOffset: selection.anchorOffset,
focusNode: selection.focusNode,
focusOffset: selection.focusOffset
};
//console.log ("copy: ", copy);
return copy;
} // copySelection
function copyRange (r) {
return {
startContainer: r.startContainer,
startOffset: r.startOffset,
endContainer: r.endContainer,
endOffset: r.endOffset,
};
} // copyRange
function probeEvent (type, element = document.body) {
element.addEventListener(e => message(
`${element} just heard event ${e.type}, target ${e.target.toString()}`
)); // listener
} // probeEvent
function message (text) {
$("#message").textContent = text;
} // message
/// debugging
function dim (element) {
const s = getComputedStyle(element);
console.log(`${s.width}, ${s.height}, ${s.top}, ${s.left}`);
} // dim
function pos (element) {
const s = getComputedStyle(element);
console.log(`${s.top}, ${s.bottom}, ${s.left}, ${s.right}`);
} // pos
</script>
</body>
</html>