-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent-script.js
More file actions
44 lines (36 loc) · 1.13 KB
/
Copy pathcontent-script.js
File metadata and controls
44 lines (36 loc) · 1.13 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
import { get, set, remove } from "./storage";
const FOCUS_COLOR = "darkblue";
const FOCUS_STYLES = [
{ key: "border-color", value: `${FOCUS_COLOR}` },
{ key: "box-shadow", value: `0 0 10px ${FOCUS_COLOR}` }
];
function doShit(elem, pathname) {
const elemId = elem.id;
FOCUS_STYLES.forEach(({ key, value }) => {
elem.style.setProperty(key, value, "important");
});
const draft = elem.value;
if (!draft) {
elem.value = get(`drafts:${pathname}:${elemId}`);
}
elem.addEventListener("blur", function() {
FOCUS_STYLES.forEach(({ key, value }) => {
elem.style.setProperty(key, "");
});
});
elem.addEventListener("input", function(e) {
const draft = e.target.value;
if (!draft) return;
set(`drafts:${pathname}:${elemId}`, draft);
});
elem.closest("form").addEventListener("submit", function() {
remove(`drafts:${pathname}:${elemId}`);
});
}
function onFocusAny(e) {
if (e.target.tagName !== "TEXTAREA") return;
const pathname = location.pathname;
doShit(e.target, pathname);
}
// seems like the easiest certain way to cover all we want
document.addEventListener("focus", onFocusAny, true);