Files
edentext/src/lib/storage/autoText.ts
T
Steffen Becker 405b175e49 feat: AutoText keeps a block of text under a shortcut
LibreOffice's Tools ▸ AutoText, in the ribbon where Word keeps its Quick
Parts: store the selection under a name and a shortcut, insert it from the
library or by typing the shortcut and pressing F3 — LibreOffice's key for
it.

An entry is HTML parsed back through the schema, so it keeps its marks and
blocks; the library is app-level, as it is in both products, so no file
carries it.
2026-08-15 10:33:16 +02:00

39 lines
1.4 KiB
TypeScript

// LibreOffice's Tools ▸ AutoText / Word's Insert ▸ Quick Parts: named blocks of text
// kept beside the documents, not in one. Both products store them in a template of
// their own (`.bau`, a `.dotx`); here they live in localStorage, so they outlive the
// document that made them.
export type AutoTextEntry = {
name: string;
/** Typed in the text, then F3 — LibreOffice's shortcut, case-insensitive. */
shortcut: string;
/** The entry's content as HTML, parsed back through the schema on insert. */
html: string;
};
const KEY = 'edentext-autotext';
export function loadAutoText(): AutoTextEntry[] {
try {
const raw = localStorage.getItem(KEY);
const data = raw ? (JSON.parse(raw) as AutoTextEntry[]) : [];
return Array.isArray(data)
? data.filter((e) => e && typeof e.name === 'string' && typeof e.html === 'string')
.map((e) => ({ name: e.name, shortcut: String(e.shortcut ?? ''), html: e.html }))
: [];
} catch {
return [];
}
}
export function saveAutoText(list: AutoTextEntry[]): void {
if (list.length) localStorage.setItem(KEY, JSON.stringify(list));
else localStorage.removeItem(KEY);
}
/** The entry a typed shortcut names, matched as LibreOffice matches it: ignoring case. */
export function entryForShortcut(list: AutoTextEntry[], word: string): AutoTextEntry | null {
const key = word.trim().toLowerCase();
return key ? list.find((e) => e.shortcut.toLowerCase() === key) ?? null : null;
}