boot-theme.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Theme bootstrap row for the browser's pre-plugin interval. Each index
  3. * render embeds the current durable built-in preference and content font size;
  4. * the browser resolves only `system`, then writes the same DOM fields
  5. * ui-layout's ThemePresenter owns after the client plugin tree activates.
  6. */
  7. import type { IndexInjection } from '@deepseek-ai/dsh-host-webserver'
  8. import { DEFAULT_FONT_SIZE, DEFAULT_PREFERENCE, type ThemePreference } from './theme-settings.ts'
  9. /** Build the inline script body for one schema-validated durable theme section. */
  10. function bootThemeScript(preference: ThemePreference, fontSize: number): string {
  11. return `(() => {
  12. const preference = ${JSON.stringify(preference)}
  13. const systemDark = preference === 'system'
  14. && typeof matchMedia !== 'undefined'
  15. && matchMedia('(prefers-color-scheme: dark)').matches
  16. const dark = preference === 'dark' || systemDark
  17. document.documentElement.style.colorScheme = dark ? 'dark' : 'light'
  18. document.body.toggleAttribute('data-ds-dark-theme', dark)
  19. document.body.style.setProperty('--dsh-content-font-size', ${JSON.stringify(`${fontSize}px`)})
  20. })()`
  21. }
  22. /**
  23. * The theme bootstrap as an injection row: an inline script immediately after
  24. * the opening body tag, before the shell mount and module script.
  25. * @param preference - Current Host-backed built-in preference.
  26. * @param fontSize - Current Host-backed content font size in px.
  27. * @returns the body script row.
  28. */
  29. export function bootThemeInjection(
  30. preference: ThemePreference = DEFAULT_PREFERENCE,
  31. fontSize: number = DEFAULT_FONT_SIZE,
  32. ): IndexInjection {
  33. return { kind: 'script', placement: 'body', text: bootThemeScript(preference, fontSize) }
  34. }