index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /** Host registration for the browser theme preference and pre-plugin palette. */
  2. import type { Context } from '@deepseek-ai/cordis'
  3. import type {} from '@deepseek-ai/dsh-host-webserver'
  4. import type {} from '@deepseek-ai/dsh-settings'
  5. import { bootThemeInjection } from './boot-theme.ts'
  6. import {
  7. DEFAULT_FONT_SIZE, DEFAULT_PREFERENCE, THEME_SETTINGS_NAMESPACE, ThemeSettingsSchema,
  8. type ThemePreference, type ThemeSettings,
  9. } from './theme-settings.ts'
  10. export {
  11. DEFAULT_FONT_SIZE, DEFAULT_PREFERENCE, FONT_SIZE_FIELD, FONT_SIZE_MAX, FONT_SIZE_MIN,
  12. THEME_PREFERENCE_FIELD, THEME_PREFERENCES, THEME_SETTINGS_NAMESPACE,
  13. type ThemePreference, type ThemeSettings,
  14. } from './theme-settings.ts'
  15. const THEME_NAMESPACE = THEME_SETTINGS_NAMESPACE
  16. /** Read the registered theme section or the schema defaults without a settings provider. */
  17. function readSection(ctx: Context): { preference: ThemePreference; fontSize: number } {
  18. const fallback = { preference: DEFAULT_PREFERENCE, fontSize: DEFAULT_FONT_SIZE }
  19. const settings = ctx.get('settings')
  20. if (settings === undefined) return fallback
  21. const section = settings.get(THEME_NAMESPACE) as ThemeSettings | undefined
  22. if (section === undefined) return fallback
  23. return section
  24. }
  25. /**
  26. * Register the durable theme section when the optional settings service is
  27. * composed, and answer every index injection collection with the current
  28. * theme bootstrap row.
  29. * @param ctx - Host context that may acquire the settings service.
  30. */
  31. export function apply(ctx: Context): void {
  32. ctx.inject(['settings'], (settingsCtx) => {
  33. settingsCtx.settings.register(THEME_NAMESPACE, ThemeSettingsSchema)
  34. })
  35. ctx.on('webserver/index-inject', (table) => {
  36. const section = readSection(ctx)
  37. table.push(bootThemeInjection(section.preference, section.fontSize))
  38. })
  39. }