Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, shallowRef } from 'vue'
3 :
4 : /**
5 : * Prefixes for the browser tab title.
6 : * A source clears its prefix by setting an empty value (`''`, `null` or `undefined`).
7 : * Prefixes show in the order each source set one; a source that clears
8 : * and sets again moves to the end.
9 : */
10 2355 : export const useBrowserTabTitleStore = defineStore('browserTabTitle', () => {
11 2355 : const prefixes = shallowRef<ReadonlyMap<string, string>>(new Map())
12 :
13 2355 : const prefix = computed(() =>
14 : [...prefixes.value.values()].map((text) => `[${text}] `).join('')
15 : )
16 :
17 2356 : function setPrefix(source: string, text: string | null | undefined): void {
18 2356 : const current = prefixes.value.get(source)
19 2356 : if (!text ? current === undefined : current === text) return
20 0 : const next = new Map(prefixes.value)
21 0 : if (!text) next.delete(source)
22 0 : else next.set(source, text)
23 0 : prefixes.value = next
24 : }
25 :
26 2355 : return { prefix, setPrefix }
27 : })
|