|
| 1 | +import { get, set, remove } from "./storage"; |
| 2 | + |
| 3 | +const FOCUS_COLOR = "darkblue"; |
| 4 | +const FOCUS_STYLES = [ |
| 5 | + { key: "border-color", value: `${FOCUS_COLOR}` }, |
| 6 | + { key: "box-shadow", value: `0 0 10px ${FOCUS_COLOR}` } |
| 7 | +]; |
| 8 | + |
| 9 | +function doShit(elem, pathname) { |
| 10 | + const elemId = elem.id; |
| 11 | + |
| 12 | + FOCUS_STYLES.forEach(({ key, value }) => { |
| 13 | + elem.style.setProperty(key, value, "important"); |
| 14 | + elem.nextElementSibling.style.setProperty(key, value, "important") |
| 15 | + }); |
| 16 | + const draft = elem.value; |
| 17 | + if (!draft) { |
| 18 | + elem.value = get(`drafts:${pathname}:${elemId}`); |
| 19 | + } |
| 20 | + |
| 21 | + elem.addEventListener("blur", function() { |
| 22 | + FOCUS_STYLES.forEach(({ key, value }) => { |
| 23 | + elem.style.setProperty(key, ""); |
| 24 | + elem.nextElementSibling.style.setProperty(key, "") |
| 25 | + }) |
| 26 | + }); |
| 27 | + |
| 28 | + elem.addEventListener("input", function(e) { |
| 29 | + const draft = e.target.value; |
| 30 | + if (!draft) return; |
| 31 | + set(`drafts:${pathname}:${elemId}`, draft); |
| 32 | + }); |
| 33 | + |
| 34 | + elem.closest("form").addEventListener("submit", function() { |
| 35 | + remove(`drafts:${pathname}:${elemId}`); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +function onFocusAny(e) { |
| 40 | + if (e.target.tagName !== "TEXTAREA") return; |
| 41 | + const pathname = location.pathname; |
| 42 | + doShit(e.target, pathname); |
| 43 | +} |
| 44 | + |
| 45 | +// seems like the easiest certain way to cover all we want |
| 46 | +document.addEventListener("focus", onFocusAny, true); |
0 commit comments