From 48b8f132d621a26fa972774855d86d3fbe254b7e Mon Sep 17 00:00:00 2001 From: Lightling Date: Wed, 27 Mar 2024 18:27:07 -0400 Subject: [PATCH] begin themeing work - define themes - implement global stylesheets --- src/content-env.d.ts | 29 ++++++++++++++++++++++++++++- src/main.vue | 22 +++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/content-env.d.ts b/src/content-env.d.ts index 33008a5..add1203 100644 --- a/src/content-env.d.ts +++ b/src/content-env.d.ts @@ -102,13 +102,40 @@ declare module 'content/routes.js' { } | { children: HeaderEntry[] }) + + /** + * Defines what kind of theme a specified theme is + * for the purposes of determining a default theme + * based on the system/browser setting of the visitor + */ + type SiteThemeType = + | 'dark' + | 'light' + | 'dark_hc' + | 'light_hc' + + /** + * Defines a stylesheet the site uses for applying a theme + */ + type SiteTheme = { + displayName?: string + type: SiteThemeType + url: string + } + + /** + * Defines a list of themes the site uses + */ + type SiteThemeList = { [id: string]: SiteTheme } /** * Defines global values for the site. */ type SiteGlobals = { - header: HeaderEntry[], + header: HeaderEntry[] id: string + stylesheetUrls: string[] + themes: SiteThemeList warning: WarningModal } diff --git a/src/main.vue b/src/main.vue index ba1bb73..895c38e 100644 --- a/src/main.vue +++ b/src/main.vue @@ -40,17 +40,28 @@ const determineWarning = () => { : routeConfig.warning } +const injectStylesheet = (url: string, className: string) => { + const newElement = document.createElement('link') + newElement.setAttribute('rel', 'stylesheet') + newElement.setAttribute('href', url) + newElement.classList.add(className) + document.head.appendChild(newElement) +} + const determineStylesheets = (stylesheetUrls?: string[]) => { - const staleStylesheets = document.head.querySelectorAll('link[rel="stylesheet"]') + const staleStylesheets = document.head.querySelectorAll('link.page-stylesheet[rel="stylesheet"]') staleStylesheets.forEach(stylesheet => { document.head.removeChild(stylesheet) }) stylesheetUrls?.forEach(stylesheet => { - const newElement = document.createElement('link') - newElement.setAttribute('rel', 'stylesheet') - newElement.setAttribute('href', stylesheet) - document.head.appendChild(newElement) + injectStylesheet(stylesheet, 'page-stylesheet') + }) +} + +const determineGlobalStylesheets = () => { + globalConfig.stylesheetUrls.forEach(stylesheet => { + injectStylesheet(stylesheet, 'global-stylesheet') }) } @@ -77,6 +88,7 @@ const onAcknowledgedWarning = () => { onMounted(async () => { await refresh() + determineGlobalStylesheets() router.afterEach(async (to, from) => { await refresh() })