| 1 |
/** |
| 2 |
* Helpers to update the page title. |
| 3 |
*/ |
| 4 |
|
| 5 |
import { derived, writable } from "svelte/store"; |
| 6 |
|
| 7 |
import { getScriptTagValue } from "../lib/dom"; |
| 8 |
import { string } from "../lib/validation"; |
| 9 |
|
| 10 |
/** The page title, can either be the title itself or `account:{name}` */ |
| 11 |
export const raw_page_title = writable(""); |
| 12 |
|
| 13 |
/** Whether any changes to the Beancount files has been detected. */ |
| 14 |
export const has_changes = writable(false); |
| 15 |
|
| 16 |
interface PageTitle { |
| 17 |
title: string; |
| 18 |
type: "plain" | "account"; |
| 19 |
} |
| 20 |
|
| 21 |
export const page_title = derived( |
| 22 |
raw_page_title, |
| 23 |
($raw_page_title): PageTitle => { |
| 24 |
if ($raw_page_title.startsWith("account:")) { |
| 25 |
return { |
| 26 |
title: $raw_page_title.slice("account:".length), |
| 27 |
type: "account", |
| 28 |
}; |
| 29 |
} |
| 30 |
return { title: $raw_page_title, type: "plain" }; |
| 31 |
}, |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Update page (next to our icon) and the html document `<title>`. |
| 36 |
*/ |
| 37 |
export function updatePageTitle(): void { |
| 38 |
const v = getScriptTagValue("#page-title", string); |
| 39 |
if (v.is_ok) { |
| 40 |
raw_page_title.set(v.value); |
| 41 |
} |
| 42 |
} |