srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/customLibrary.js
blob: e97ae7c986546a0b741749c946f221890e8a1594 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const STORAGE_KEY = 'typerpunk:library';
// A document is stored whole so it can be re-chunked later - switching
// markdown between stripped and verbatim needs the original, not the chunks.
// Ten is enough for the notes someone is actually working through, and keeps
// localStorage well clear of its quota.
const MAX_DOCUMENTS = 10;
const MAX_RAW_CHARS = 400_000;

// Imported documents, kept between visits.
//
// Custom text used to live only in memory: import a set of notes, reload the
// page, and it was gone. That is fine for pasting a paragraph to race, and
// useless for working through a file over several sittings - which is what
// people actually do with their own notes.
//
// Everything here stays on the device. Notes are not uploaded anywhere.

function load() {
    try {
        const raw = JSON.parse(localStorage.getItem(STORAGE_KEY));
        return Array.isArray(raw) ? raw : [];
    } catch {
        return [];
    }
}

function save(docs) {
    try {
        localStorage.setItem(STORAGE_KEY, JSON.stringify(docs));
        return true;
    } catch {
        // Quota, or storage disabled. The document still works for this
        // session; it simply will not be there next time.
        return false;
    }
}

export function listDocuments() {
    return load().sort((a, b) => (b.lastOpened || 0) - (a.lastOpened || 0));
}

export function getDocument(id) {
    return load().find(d => d.id === id) || null;
}

/// Stores a document, replacing any earlier import of the same name so
/// re-importing an edited file updates it in place rather than accumulating
/// copies. Returns the stored record.
export function saveDocument({ name, raw, chunkCount }) {
    const docs = load().filter(d => d.name !== name);
    const existing = load().find(d => d.name === name);
    const doc = {
        id: existing?.id || `doc-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
        name,
        raw: raw.slice(0, MAX_RAW_CHARS),
        chunkCount,
        // Re-importing an edited file keeps your place only if it still makes
        // sense: past the end of the new version, start again.
        position: Math.min(existing?.position || 0, Math.max(0, chunkCount - 1)),
        addedAt: existing?.addedAt || Date.now(),
        lastOpened: Date.now(),
    };
    docs.unshift(doc);
    save(docs.slice(0, MAX_DOCUMENTS));
    return doc;
}

/// Records how far through a document you have typed.
export function setPosition(id, position) {
    const docs = load();
    const doc = docs.find(d => d.id === id);
    if (!doc) return;
    doc.position = Math.max(0, position);
    doc.lastOpened = Date.now();
    save(docs);
}

export function removeDocument(id) {
    save(load().filter(d => d.id !== id));
}

/// How far through a document you are, as a percentage.
export function progressOf(doc) {
    if (!doc || !doc.chunkCount) return 0;
    return Math.min(100, Math.round((doc.position / doc.chunkCount) * 100));
}