Blog/Components/Pages/Note.razor.js 775 B · 26 lines · raw · history

1 import { getById, debounce, writeError, resetLog } from "/common.module.js";
2 import lzString from "/lz-string.module.js";
3
4 const input = getById("input");
5 input.addEventListener("input", debounce(() => {
6 if (input.value === '') {
7 window.location.hash = ''
8 }
9 else {
10 window.location.hash = '#' + lzString.compressToEncodedURIComponent(input.value);
11 }
12 resetLog();
13 }, 10))
14
15 window.addEventListener('hashchange', loadState);
16 loadState();
17
18 function loadState() {
19 if (window.location.hash !== '') {
20 input.value = lzString.decompressFromEncodedURIComponent(window.location.hash.substring(1));
21 if (input.value === '') {
22 //Hash but no content?
23 writeError("Failed to load note from url.")
24 }
25 }
26 }