Repository navigation
Expand file tree
/
Copy pathselection.js
More file actions
66 lines (50 loc) · 1.52 KB
/
Copy pathselection.js
File metadata and controls
66 lines (50 loc) · 1.52 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
'use strict';
const SELECTED_TEXT_KEY = 'tecleese-selected-text';
const selectionSelector = document.getElementById('textSelector');
let savedSelection = null;
/* -----------------------------------------------------------
Load saved selection
----------------------------------------------------------- */
try {
savedSelection = localStorage.getItem(SELECTED_TEXT_KEY);
} catch (error) {
console.warn('Could not restore selected text:', error);
}
/* -----------------------------------------------------------
Save selection
----------------------------------------------------------- */
selectionSelector.addEventListener('change', () => {
try {
localStorage.setItem(SELECTED_TEXT_KEY, selectionSelector.value);
} catch (error) {
console.warn('Could not save selected text:', error);
}
});
/* -----------------------------------------------------------
Restore selection when options are populated
----------------------------------------------------------- */
const selectionObserver = new MutationObserver(() => {
if (!savedSelection) {
return;
}
const option = Array.from(selectionSelector.options).find(
(option) => option.value === savedSelection
);
if (!option) {
return;
}
selectionSelector.value = savedSelection;
savedSelection = null;
/*
Tell worker.js to load the restored selection.
*/
selectionSelector.dispatchEvent(
new Event('change', {
bubbles: true,
})
);
selectionObserver.disconnect();
});
selectionObserver.observe(selectionSelector, {
childList: true,
});