summaryrefslogtreecommitdiff
path: root/Blog/Components/Pages/Note.razor.js
blob: 3d47d239c62abddad808aed6a0be830426d0bdeb (plain)
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
import { getById, debounce, writeError, resetLog } from "/common.module.js";
import lzString from "/lz-string.module.js";

let input = undefined;
export function onLoad() {
    input = getById("input");
    input.addEventListener("input", debounce(() => {
        if (input.value === '') {
            window.location.hash = ''
        }
        else {
            window.location.hash = '#' + lzString.compressToEncodedURIComponent(input.value);
        }
        resetLog();
    }, 10))
    
    window.addEventListener('hashchange', loadState);
    loadState();
}

export function onUpdate() {
    loadState();
}

function loadState() {
    if (window.location.hash !== '') {
        input.value = lzString.decompressFromEncodedURIComponent(window.location.hash.substring(1));
        if (input.value === '') {
            //Hash but no content?
            writeError("Failed to load note from url.")
        }
    }
}