Files
overleaf-cep/services/web/frontend/js/features/history/extensions/theme.ts
Mathias Jakobsen 63a92b53f1 Merge pull request #28017 from overleaf/revert-28016-revert-27953-mj-editor-color-scheme
Reinstate "[web] Specify CSS color-scheme based on editor theme"

GitOrigin-RevId: a6ac8e5a352c3c795725290584a2edfa236e2baa
2025-08-22 08:05:31 +00:00

76 lines
1.9 KiB
TypeScript

import { EditorView } from '@codemirror/view'
import { Compartment, TransactionSpec } from '@codemirror/state'
import { FontFamily, LineHeight, userStyles } from '@/shared/utils/styles'
import { ThemeCache } from '@/features/source-editor/utils/theme-cache'
export type Options = {
fontSize: number
fontFamily: FontFamily
lineHeight: LineHeight
}
const optionsThemeConf = new Compartment()
export const theme = (options: Options) => [
baseTheme,
optionsThemeConf.of(createThemeFromOptions(options)),
]
const tooltipThemeCache = new ThemeCache()
const createThemeFromOptions = ({
fontSize = 12,
fontFamily = 'monaco',
lineHeight = 'normal',
}: Options) => {
// Theme styles that depend on settings
const styles = userStyles({ fontSize, fontFamily, lineHeight })
return [
EditorView.editorAttributes.of({
style: Object.entries({
'--font-size': styles.fontSize,
'--source-font-family': styles.fontFamily,
'--line-height': styles.lineHeight,
})
.map(([key, value]) => `${key}: ${value}`)
.join(';'),
}),
tooltipThemeCache.get({
'.cm-tooltip': {
'--font-size': styles.fontSize,
'--source-font-family': styles.fontFamily,
},
}),
]
}
const baseTheme = EditorView.theme({
'&.cm-editor.cm-editor': {
colorScheme: 'light',
},
'.cm-content': {
fontSize: 'var(--font-size)',
fontFamily: 'var(--source-font-family)',
lineHeight: 'var(--line-height)',
color: '#000',
},
'.cm-gutters': {
fontSize: 'var(--font-size)',
lineHeight: 'var(--line-height)',
},
'.cm-lineNumbers': {
fontFamily: 'var(--source-font-family)',
},
'.cm-tooltip': {
// NOTE: fontFamily is not set here, as most tooltips use the UI font
fontSize: 'var(--font-size)',
},
})
export const setOptionsTheme = (options: Options): TransactionSpec => {
return {
effects: optionsThemeConf.reconfigure(createThemeFromOptions(options)),
}
}