theme.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { EditorView } from '@codemirror/view'
  2. import { Compartment, TransactionSpec } from '@codemirror/state'
  3. import { FontFamily, LineHeight, userStyles } from '@/shared/utils/styles'
  4. import { ThemeCache } from '@/features/source-editor/utils/theme-cache'
  5. export type Options = {
  6. fontSize: number
  7. fontFamily: FontFamily
  8. lineHeight: LineHeight
  9. }
  10. const optionsThemeConf = new Compartment()
  11. export const theme = (options: Options) => [
  12. baseTheme,
  13. optionsThemeConf.of(createThemeFromOptions(options)),
  14. ]
  15. const tooltipThemeCache = new ThemeCache()
  16. const createThemeFromOptions = ({
  17. fontSize = 12,
  18. fontFamily = 'monaco',
  19. lineHeight = 'normal',
  20. }: Options) => {
  21. // Theme styles that depend on settings
  22. const styles = userStyles({ fontSize, fontFamily, lineHeight })
  23. return [
  24. EditorView.editorAttributes.of({
  25. style: Object.entries({
  26. '--font-size': styles.fontSize,
  27. '--source-font-family': styles.fontFamily,
  28. '--line-height': styles.lineHeight,
  29. })
  30. .map(([key, value]) => `${key}: ${value}`)
  31. .join(';'),
  32. }),
  33. tooltipThemeCache.get({
  34. '.cm-tooltip': {
  35. '--font-size': styles.fontSize,
  36. '--source-font-family': styles.fontFamily,
  37. },
  38. }),
  39. ]
  40. }
  41. const baseTheme = EditorView.theme({
  42. '.cm-content': {
  43. fontSize: 'var(--font-size)',
  44. fontFamily: 'var(--source-font-family)',
  45. lineHeight: 'var(--line-height)',
  46. color: '#000',
  47. },
  48. '.cm-gutters': {
  49. fontSize: 'var(--font-size)',
  50. lineHeight: 'var(--line-height)',
  51. },
  52. '.cm-lineNumbers': {
  53. fontFamily: 'var(--source-font-family)',
  54. },
  55. '.cm-tooltip': {
  56. // NOTE: fontFamily is not set here, as most tooltips use the UI font
  57. fontSize: 'var(--font-size)',
  58. },
  59. })
  60. export const setOptionsTheme = (options: Options): TransactionSpec => {
  61. return {
  62. effects: optionsThemeConf.reconfigure(createThemeFromOptions(options)),
  63. }
  64. }