index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 { settingsNamespace } from '@deepseek-ai/dsh-settings'
  5. import { bootThemeInjection } from './boot-theme.ts'
  6. import {
  7. DEFAULT_PREFERENCE, THEME_SETTINGS_NAMESPACE, ThemeSettingsSchema,
  8. type ThemePreference, type ThemeSettings,
  9. } from './theme-settings.ts'
  10. export {
  11. DEFAULT_PREFERENCE, THEME_PREFERENCE_FIELD, THEME_PREFERENCES, THEME_SETTINGS_NAMESPACE,
  12. type ThemePreference, type ThemeSettings,
  13. } from './theme-settings.ts'
  14. const THEME_NAMESPACE = settingsNamespace(THEME_SETTINGS_NAMESPACE)
  15. /** Read the registered preference or use the schema default without a settings provider. */
  16. function readPreference(ctx: Context): ThemePreference {
  17. const settings = ctx.get('settings')
  18. if (settings === undefined) return DEFAULT_PREFERENCE
  19. const section = settings.get(THEME_NAMESPACE) as ThemeSettings | undefined
  20. if (section === undefined) return DEFAULT_PREFERENCE
  21. return section.preference
  22. }
  23. /**
  24. * Register the durable theme section when the optional settings service is
  25. * composed, and answer every index injection collection with the current
  26. * theme bootstrap row.
  27. * @param ctx - Host context that may acquire the settings service.
  28. */
  29. export function apply(ctx: Context): void {
  30. ctx.inject(['settings'], (settingsCtx) => {
  31. settingsCtx.settings.register(THEME_NAMESPACE, ThemeSettingsSchema)
  32. })
  33. ctx.on('webserver/index-inject', (table) => {
  34. table.push(bootThemeInjection(readPreference(ctx)))
  35. })
  36. }