66 lines
3.0 KiB
TypeScript
66 lines
3.0 KiB
TypeScript
/* The window's own habits.
|
|
|
|
These are the two things the wiki does differently once it is a desktop app
|
|
rather than a page of the web — see utils/desktop.ts for the asking, and
|
|
src-tauri/ for the shell. The web build takes the first line and returns:
|
|
nothing here changes how a browser sees the wiki.
|
|
|
|
Runs after the auth plugin (plugins come in name order), which matters: by
|
|
now a signed-in person's account has already had its say about the interface
|
|
language, and an account outranks this device. */
|
|
|
|
import { isDesktop, leadsOutward, openExternal, storeGet, storeSet } from '~/utils/desktop'
|
|
|
|
/** Where a window keeps the interface language it was last shown. Also the
|
|
* name of the cookie the web build writes it to — the same choice, remembered
|
|
* the way this kind of host can be sure of remembering it. */
|
|
const LOCALE_KEY = 'mathew-locale'
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
if (!import.meta.client || !isDesktop()) return
|
|
|
|
/* Links. The wiki's own addresses are relative and the router turns them as
|
|
it always does; an absolute one is about something outside this window —
|
|
and a window has one page, no tab strip, no back button a reader would
|
|
trust after the wiki had been replaced by some other site's document. So
|
|
those go out to the system's browser instead, and the window keeps
|
|
showing the wiki it was asked to show.
|
|
|
|
Captured on the way in, so it also catches the links drawn from article
|
|
Markdown — the rendered text belongs to the article, which has no reason
|
|
to know it is not in a browser. */
|
|
document.addEventListener(
|
|
'click',
|
|
(event) => {
|
|
if (event.defaultPrevented || event.button !== 0) return
|
|
// a modified click is the reader asking for the browser's own gesture
|
|
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
|
|
const target = event.target as Element | null
|
|
const href = target?.closest?.('a')?.getAttribute('href')
|
|
if (!href || !leadsOutward(href)) return
|
|
event.preventDefault()
|
|
void openExternal(href)
|
|
},
|
|
true,
|
|
)
|
|
|
|
/* The interface language, kept where a window can be certain of keeping it.
|
|
On the web the choice rides on the `mathew-locale` cookie, which the server
|
|
reads so the first draw already speaks it. A window draws nothing on a
|
|
server, and its own address — `tauri://localhost` — is one a webview is
|
|
not everywhere obliged to keep cookies for, so the choice is mirrored into
|
|
localStorage: a lost cookie costs a language switch at the next start, and
|
|
that is all.
|
|
|
|
An account's own choice outranks the mirror, as it outranks the cookie —
|
|
hence the guard, the auth plugin having run first. Whatever the UI ends up
|
|
speaking is then written back, so the mirror always says what this window
|
|
last showed. */
|
|
const { locale, choose } = useAppLocale()
|
|
const { user } = useAuth()
|
|
const remembered = storeGet(LOCALE_KEY)
|
|
if (remembered && !user.value?.locale) void choose(remembered)
|
|
|
|
watch(locale, (code) => storeSet(LOCALE_KEY, code))
|
|
})
|