-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
362 lines (348 loc) · 18.7 KB
/
demo.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
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
<!DOCTYPE html>
<html lang="en" translate="no">
<head>
<meta charset="utf-8">
<title>Text Selection Demo</title>
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<meta name="google" content="notranslate">
<style>
:root {
-webkit-text-size-adjust: 100%;
--page-bg: hsl(0, 0%, 100%);
--text-color: hsl(0, 0%, 0%);
--gray-90: hsl(0, 0%, 90%);
--gray-50: hsl(0, 0%, 50%);
}
@media (prefers-color-scheme: dark) { :root {
--page-bg: hsl(0, 0%, 17%);
--text-color: hsl(0, 0%, 95%);
--gray-90: hsl(0, 0%, 30%);
--gray-50: hsl(0, 0%, 70%);
}}
body {
background-color: var(--page-bg);
color: var(--text-color);
line-height: 1.4;
}
img { width: 120px; height: 120px; float: right; }
a { color: var(--gray-50); }
button { cursor: pointer; }
#annotation-menu {
font-family: "Helvetica", sans-serif;
font-size: 0.8em;
.buttons-area {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
gap: 0.6em;
overflow: scroll;
& > * {
background-color: var(--page-bg);
border: 0; border-radius: 0.4em;
padding: 0.6em 0.4em;
font-size: inherit;
white-space: nowrap;
}
input[type="text"] { width: 2em; padding: 0.1em 0.3em; border: 1px solid var(--gray-50); }
button[name="hh-color"] { color: var(--page-bg); }
button[name="hh-color"][value="red"] { background-color: hsl(352, 99%, 65%); }
button[name="hh-color"][value="orange"] { background-color: hsl(31, 99%, 58%); }
button[name="hh-color"][value="yellow"] { background-color: hsl(50, 98%, 61%); }
button[name="hh-color"][value="green"] { background-color: hsl(75, 70%, 49%); }
button[name="hh-color"][value="blue"] { background-color: hsl(182, 86%, 47%); }
button[name="hh-style"], button[name="hh-wrapper"] { color: inherit; border: 2px solid var(--gray-50); }
button[name="hh-remove"] { background-color: var(--gray-50); color: var(--page-bg); }
.highlight-placeholder {
background-color: var(--gray-90);
padding: 0 0.3em;
}
}
}
.footnote-marker {
display: inline-block;
vertical-align: super;
font-size: smaller;
line-height: 0.5em;
&::before {
content: attr(data-marker) / attr(data-accessibility-label);
}
}
#disambiguation-panel {
color: inherit;
background-color: inherit;
width: 400px; max-width: 100%;
flex-direction: column;
gap: 0.5em;
box-sizing: border-box;
&[open] { display: flex; }
#disambiguation-buttons {
display: flex;
flex-direction: column;
& button {
display: inline-block;
border: none;
background-color: inherit;
color: inherit;
font-family: inherit;
font-size: inherit;
text-align: inherit;
margin-bottom: 1em;
& * {
display: inline;
}
}
}
&::backdrop {
background-color: hsla(0, 0%, 0%, 0.5);
}
}
</style>
</head>
<body>
<main>
<h1 id="title1">Highlight Helper Demo</h1>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/CMYK_color_model.svg/180px-CMYK_color_model.svg.png">
<p id="p1"><b>Highlight Helper</b> enables highlighting or underlining text in an HTML page. Highlights are drawn via <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/SVG">SVG shapes</a>, the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API">Custom Highlight API</a>, or inserted HTML spans. Highlights can be drawn across multiple paragraphs or <b>inline <i>style</i></b> elements—they can even overlap!</p>
<p id="p2">Select text, then choose a color and style. Tap away when you’re done. Tap to activate it again for editing. Secondary elements like images (right) and Japanese ruby/furigana (below) are highlighted around.</p>
<p id="p3">わたしニーファイは<ruby><rb>善</rb><rt>よ</rt></ruby>い<ruby><rb>両</rb><rt>りょう</rt></ruby><ruby><rb>親</rb><rt>しん</rt></ruby>から<ruby><rb>生</rb><rt>う</rt></ruby>まれたので,<ruby><rb>父</rb><rt>ちち</rt></ruby>が<ruby><rb>学</rb><rt>まな</rt></ruby>んだすべてのことの<ruby><rb>中</rb><rt>なか</rt></ruby>から<ruby><rb>幾</rb><rt>いく</rt></ruby>らかの<ruby><rb>教</rb><rt>おし</rt></ruby>えを<ruby><rb>受</rb><rt>う</rt></ruby>けた。</p>
<p id="p4" style="direction: rtl;">فِي الْبَدْءِ خَلَقَ اللهُ السَّمَاوَاتِ وَالأَرْضَ. وَكَانَتِ الأَرْضُ خَرِبَةً وَخَالِيَةً، وَعَلَى وَجْهِ الْغَمْرِ ظُلْمَةٌ، وَرُوحُ اللهِ يَرِفُّ عَلَى وَجْهِ الْمِيَاهِ. وَقَالَ اللهُ: «لِيَكُنْ نُورٌ»، فَكَانَ نُورٌ.</p>
</main>
<hr>
<aside id="annotation-menu">
<div class="buttons-area">
<button name="hh-color" value="red">Red</button>
<button name="hh-color" value="orange">Orange</button>
<button name="hh-color" value="yellow">Yellow</button>
<button name="hh-color" value="green">Green</button>
<button name="hh-color" value="blue">Blue</button>
<button name="hh-style" value="fill" checked> Fill</button>
<button name="hh-style" value="single-underline"> Underline</button>
<button name="hh-style" value="double-underline"> Double underline</button>
<button name="hh-style" value="colored-text"> Colored text</button>
<button name="hh-style" value="redacted"> Redacted</button>
<button name="hh-remove">Remove</button>
<div></div>
<!--button name="hh-wrapper" value="none" title="No wrapper"><span class="highlight-placeholder">…</span></button>
<button name="hh-wrapper" value="footnote" title="Footnote"><span class="footnote-marker" data-marker="a" data-accessibility-label="Footnote"></span><span class="highlight-placeholder">…</span></button-->
</div>
<p>
<label><input type="checkbox" id="hh-remember-style" checked> Remember most recent color and style</label><br>
<label><input type="checkbox" id="hh-snap-to-word"> Snap highlights to the nearest word</label><br>
</p>
<p>
Pointer mode:<br>
<label><input type="radio" name="hh-pointer-mode" value="auto" checked> Auto (based on input device)</label><br>
<label><input type="radio" name="hh-pointer-mode" value="simple"> Simple</label><br>
<label><input type="radio" name="hh-pointer-mode" value="live"> Live</label><br>
</p>
<p>
Drawing mode:<br>
<label><input type="radio" name="hh-drawing-mode" value="svg" checked> SVG shapes</label><br>
<label><input type="radio" name="hh-drawing-mode" value="highlight-api"> Custom Highlight API</label><br>
<label><input type="radio" name="hh-drawing-mode" value="inserted-spans"> Inserted spans (read-only)</label><br>
</p>
<b>Active highlight:</b>
<div id="snippet-container">
<div id="snippet">N/A</div>
</div>
<p id="highlight-info"></p>
</aside>
<dialog id="disambiguation-panel">
<h1>Select One</h1>
<div id="disambiguation-buttons"></div>
<button id="disambiguation-cancel">Cancel</button>
</dialog>
<!-- Link to the Highlight Helper JavaScript file -->
<script src="highlight-helper.js"></script>
<script>
// Example highlights
let highlights = [
{
highlightId: 'hh-1729991847207',
color: 'green', style: 'fill',
startParagraphId: 'p1', startParagraphOffset: 25,
endParagraphId: 'p1', endParagraphOffset: 57,
},
{
highlightId: 'hh-1729991847300',
color: 'blue', style: 'fill',
startParagraphId: 'p2', startParagraphOffset: 27,
endParagraphId: 'p2', endParagraphOffset: 42,
},
{
highlightId: 'hh-1729991847468',
color: 'red', style: 'colored-text', wrapper: 'footnote',
wrapperVariables: { marker: 'a', accessibilityLabel: 'Footnote 1a', },
readOnly: true,
startParagraphId: 'p2', startParagraphOffset: 119,
endParagraphId: 'p2', endParagraphOffset: 127,
},
{
highlightId: 'hh-1729991847301',
color: 'orange', style: 'fill',
startParagraphId: 'p3', startParagraphOffset: 56,
endParagraphId: 'p3', endParagraphOffset: 65,
},
{
highlightId: 'hh-1735379721523',
color: 'red', style: 'fill',
startParagraphId: 'p1', startParagraphOffset: 238,
endParagraphId: 'p1', endParagraphOffset: 251,
},
{
highlightId: 'hh-1735379727958',
color: 'yellow', style: 'fill',
startParagraphId: 'p1', startParagraphOffset: 247,
endParagraphId: 'p1', endParagraphOffset: 260,
},
{
highlightId: 'hh-1735479727958',
color: 'yellow', style: 'fill',
startParagraphId: 'p4', startParagraphOffset: 191,
endParagraphId: 'p4', endParagraphOffset: 207,
},
];
// Initialize a Highlighter instance, then load highlights
const highlighter = new Highlighter({
containerSelector: 'main',
paragraphSelector: 'main [id]',
wrappers: {
'footnote': { start: '<span class="footnote-marker" data-marker="{marker}" data-accessibility-label="{accessibilityLabel}"></span>', end: '', },
},
});
highlighter.loadHighlights(highlights);
// Call Highlight Helper methods when color, style, wrapper, or remove buttons on this demo page are tapped
const colorButtons = document.getElementsByName('hh-color');
for (const colorButton of colorButtons) {
colorButton.addEventListener('click', () => { highlighter.createOrUpdateHighlight({ color: colorButton.value }); });
}
const styleButtons = document.getElementsByName('hh-style');
for (const styleButton of styleButtons) {
styleButton.addEventListener('click', () => { highlighter.createOrUpdateHighlight({ style: styleButton.value }); });
}
const wrapperButtons = document.getElementsByName('hh-wrapper');
for (const wrapperButton of wrapperButtons) {
wrapperButton.addEventListener('click', () => { highlighter.createOrUpdateHighlight({ wrapper: wrapperButton.value }); });
}
const removeButton = document.getElementsByName('hh-remove')[0];
removeButton.addEventListener('click', () => { highlighter.removeHighlights([highlighter.getActiveHighlightId()]); });
document.addEventListener('keydown', respondToKeyPress);
function respondToKeyPress(event) {
if (event.key === 'Backspace') highlighter.removeHighlights([highlighter.getActiveHighlightId()]);
}
// Call Highlight Helper methods when options are changed
const rememberStyleCheckbox = document.getElementById('hh-remember-style');
const snapToWordCheckbox = document.getElementById('hh-snap-to-word');
const pointerModeRadioButtons = document.getElementsByName('hh-pointer-mode');
const drawingModeRadioButtons = document.getElementsByName('hh-drawing-mode');
rememberStyleCheckbox.addEventListener('change', (event) => { highlighter.setOption('rememberStyle', event.currentTarget.checked); });
snapToWordCheckbox.addEventListener('change', (event) => { highlighter.setOption('snapToWord', event.currentTarget.checked); });
for (const pointerModeRadioButton of pointerModeRadioButtons) {
pointerModeRadioButton.addEventListener('change', (event) => {
let pointerMode = document.querySelector('[name="hh-pointer-mode"]:checked').value;
highlighter.setOption('pointerMode', pointerMode);
});
}
for (const drawingModeRadioButton of drawingModeRadioButtons) {
drawingModeRadioButton.addEventListener('change', (event) => {
let drawingMode = document.querySelector('[name="hh-drawing-mode"]:checked').value;
if (drawingMode === 'highlight-api' && !CSS.highlights) {
alert('Your browser doesn’t support the Custom Highlight API.');
} else {
highlighter.setOption('drawingMode', drawingMode);
}
});
}
const container = document.querySelector(highlighter.getOptions().containerSelector);
const snippetDiv = document.getElementById('snippet');
const infoDiv = document.getElementById('highlight-info');
// Respond to highlight update and highlight activate event
container.addEventListener('hh:highlightupdate', updateHighlightInfo);
container.addEventListener('hh:highlightactivate', updateHighlightInfo);
function updateHighlightInfo(event) {
clearHighlightInfo();
infoDiv.innerHTML = `
<b>Highlight:</b> ${event.detail.highlight.highlightId}<br>
<b>Color and style:</b> ${event.detail.highlight.color}, ${event.detail.highlight.style}<br>
<b>Wrapper:</b> ${event.detail.highlight.wrapper}, ${JSON.stringify(event.detail.highlight.wrapperVariables)}<br>
<b>Read-only:</b> ${event.detail.highlight.readOnly}<br>
<b>Start:</b> #${event.detail.highlight.startParagraphId}, offset ${event.detail.highlight.startParagraphOffset}<br>
<b>End:</b> #${event.detail.highlight.endParagraphId}, offset ${event.detail.highlight.endParagraphOffset}
`;
const annotatedParagraphs = container.querySelectorAll(`#${event.detail.highlight.rangeParagraphIds.join(', #')}`);
for (const annotatedParagraph of annotatedParagraphs) {
const snippetParagraph = annotatedParagraph.cloneNode(true);
snippetParagraph.id = `${snippetParagraph.id}_${event.detail.highlight.highlightId}`;
snippetDiv.append(snippetParagraph);
}
const snippetHighlights = [
{
highlightId: event.detail.highlight.highlightId + '_snippet',
readOnly: true,
color: event.detail.highlight.color, style: event.detail.highlight.style,
startParagraphId: `${event.detail.highlight.startParagraphId}_${event.detail.highlight.highlightId}`, startParagraphOffset: event.detail.highlight.startParagraphOffset,
endParagraphId: `${event.detail.highlight.endParagraphId}_${event.detail.highlight.highlightId}`, endParagraphOffset: event.detail.highlight.endParagraphOffset,
},
];
const snippetHighlighter = new Highlighter({
containerSelector: '#snippet-container',
paragraphSelector: '#snippet [id]',
});
snippetHighlighter.loadHighlights(snippetHighlights);
}
// Respond to highlight deactivate event
container.addEventListener('hh:highlightdeactivate', clearHighlightInfo);
function clearHighlightInfo(event) {
snippetDiv.innerHTML = '';
infoDiv.innerHTML = 'N/A';
}
// Respond to ambiguous action events (when a user taps on overlapping highlights, or an overlapping link and highlight)
const disambiguationPanel = document.getElementById('disambiguation-panel');
const disambiguationButtonsDiv = document.getElementById('disambiguation-buttons');
const disambiguationCancelButton = document.getElementById('disambiguation-cancel');
disambiguationCancelButton.addEventListener('click', (event) => disambiguationPanel.close());
container.addEventListener('hh:ambiguousaction', handleAmbiguousAction);
function handleAmbiguousAction(event) {
disambiguationButtonsDiv.innerHTML = '';
for (const highlight of event.detail.highlights) {
let buttonStyle = highlighter.getOptions().styles[highlight.style]['css']
let buttonHtml = `
<button data-color="${highlight.color}" onclick="disambiguationPanel.close();highlighter.activateHighlight('${highlight.highlightId}');">
<span style="${buttonStyle}">${highlight.rangeHtml}</span>
</button>
`;
disambiguationButtonsDiv.insertAdjacentHTML('beforeend', buttonHtml);
}
for (const hyperlink of event.detail.hyperlinks) {
let buttonStyle = 'color: var(--gray-50); text-decoration: underline;'
let buttonHtml = `
<button onclick="disambiguationPanel.close();highlighter.activateHyperlink(${hyperlink.position});">
<span style="${buttonStyle}">${hyperlink.text}</span>
</button>
`;
disambiguationButtonsDiv.insertAdjacentHTML('beforeend', buttonHtml);
}
// TODO: Waiting 10 milliseconds before showing the disambiguation panel is a workaround for an issue in Android Chrome where a click event fires right after the pointerup event that triggered hh:ambiguousaction. If the click event happens to be at the same position as a button in the panel, the button will receive the click, dismissing the panel before the user sees it.
setTimeout(function() { disambiguationPanel.showModal(); }, 10);
}
// Log events for debugging
container.addEventListener('hh:highlightcreate', console.info)
container.addEventListener('hh:highlightupdate', console.info)
container.addEventListener('hh:highlightremove', console.info)
container.addEventListener('hh:highlightsload', console.info)
container.addEventListener('hh:highlightactivate', console.info)
container.addEventListener('hh:highlightdeactivate', console.info)
container.addEventListener('hh:selectionupdate', console.info)
container.addEventListener('hh:ambiguousaction', console.info)
container.addEventListener('hh:tap', console.info)
// snippetDiv.addEventListener('hh:highlightcreate', console.warn)
// snippetDiv.addEventListener('hh:highlightupdate', console.warn)
// snippetDiv.addEventListener('hh:highlightremove', console.warn)
// snippetDiv.addEventListener('hh:highlightsload', console.warn)
// snippetDiv.addEventListener('hh:highlightactivate', console.warn)
// snippetDiv.addEventListener('hh:highlightdeactivate', console.warn)
// snippetDiv.addEventListener('hh:selectionupdate', console.warn)
// snippetDiv.addEventListener('hh:ambiguousaction', console.warn)
// snippetDiv.addEventListener('hh:tap', console.warn)
</script>
</body>
</html>