Merge pull request #12936 from overleaf/td-history-diff-viewer-fixed-font

History migration: Use fixed font, text size, line height and theme in document diff viewer

GitOrigin-RevId: 9f86be609e0fe72ca41429bae9db03e047952c08
This commit is contained in:
Tim Down 2023-05-04 10:57:24 +01:00 committed by Copybot
parent a3333fe3f1
commit 95646a1bd5
2 changed files with 33 additions and 86 deletions

View file

@ -10,8 +10,7 @@ import {
import { EditorView, lineNumbers } from '@codemirror/view' import { EditorView, lineNumbers } from '@codemirror/view'
import { indentationMarkers } from '@replit/codemirror-indentation-markers' import { indentationMarkers } from '@replit/codemirror-indentation-markers'
import { highlights, setHighlightsEffect } from '../../extensions/highlights' import { highlights, setHighlightsEffect } from '../../extensions/highlights'
import useScopeValue from '../../../../shared/hooks/use-scope-value' import { theme } from '../../extensions/theme'
import { theme, Options } from '../../extensions/theme'
import { indentUnit } from '@codemirror/language' import { indentUnit } from '@codemirror/language'
import { Highlight } from '../../services/types/doc' import { Highlight } from '../../services/types/doc'
import useIsMounted from '../../../../shared/hooks/use-is-mounted' import useIsMounted from '../../../../shared/hooks/use-is-mounted'
@ -24,11 +23,7 @@ import Icon from '../../../../shared/components/icon'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { inlineBackground } from '../../../source-editor/extensions/inline-background' import { inlineBackground } from '../../../source-editor/extensions/inline-background'
type FontFamily = 'monaco' | 'lucida' function extensions(): Extension[] {
type LineHeight = 'compact' | 'normal' | 'wide'
type OverallTheme = '' | 'light-'
function extensions(themeOptions: Options): Extension[] {
return [ return [
EditorView.editable.of(false), EditorView.editable.of(false),
lineNumbers(), lineNumbers(),
@ -37,7 +32,7 @@ function extensions(themeOptions: Options): Extension[] {
indentationMarkers({ hideFirstIndent: true, highlightActiveBlock: false }), indentationMarkers({ hideFirstIndent: true, highlightActiveBlock: false }),
highlights(), highlights(),
highlightLocations(), highlightLocations(),
theme(themeOptions), theme(),
inlineBackground(false), inlineBackground(false),
] ]
} }
@ -49,22 +44,13 @@ function DocumentDiffViewer({
doc: string doc: string
highlights: Highlight[] highlights: Highlight[]
}) { }) {
const [fontFamily] = useScopeValue<FontFamily>('settings.fontFamily')
const [fontSize] = useScopeValue<number>('settings.fontSize')
const [lineHeight] = useScopeValue<LineHeight>('settings.lineHeight')
const [overallTheme] = useScopeValue<OverallTheme>('settings.overallTheme')
const isMounted = useIsMounted() const isMounted = useIsMounted()
const { t } = useTranslation() const { t } = useTranslation()
const [state, setState] = useState(() => { const [state, setState] = useState(() => {
return EditorState.create({ return EditorState.create({
doc, doc,
extensions: extensions({ extensions: extensions(),
fontFamily,
fontSize,
lineHeight,
overallTheme,
}),
}) })
}) })

View file

@ -1,50 +1,13 @@
import { EditorView } from '@codemirror/view' import { EditorView } from '@codemirror/view'
import { Compartment } from '@codemirror/state'
export type FontFamily = 'monaco' | 'lucida' const fontFamily = 'Monaco, Menlo, Ubuntu Mono, Consolas, monospace'
export type LineHeight = 'compact' | 'normal' | 'wide'
export type OverallTheme = '' | 'light-'
export type Options = { export const theme = () =>
fontSize: number
fontFamily: FontFamily
lineHeight: LineHeight
overallTheme: OverallTheme
}
const optionsThemeConf = new Compartment()
export const theme = (options: Options) => [
optionsThemeConf.of(createThemeFromOptions(options)),
]
export const lineHeights: Record<LineHeight, number> = {
compact: 1.33,
normal: 1.6,
wide: 2,
}
const fontFamilies: Record<FontFamily, string[]> = {
monaco: ['Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'monospace'],
lucida: ['Lucida Console', 'Source Code Pro', 'monospace'],
}
const createThemeFromOptions = ({
fontSize = 12,
fontFamily = 'monaco',
lineHeight = 'normal',
overallTheme = '',
}: Options) => {
// Theme styles that depend on settings
return [
EditorView.editorAttributes.of({
class: overallTheme === '' ? 'overall-theme-dark' : 'overall-theme-light',
}),
EditorView.theme({ EditorView.theme({
'&.cm-editor': { '&.cm-editor': {
'--font-size': `${fontSize}px`, '--font-size': '12px',
'--source-font-family': fontFamilies[fontFamily]?.join(', '), '--source-font-family': fontFamily,
'--line-height': lineHeights[lineHeight], '--line-height': 1.6,
}, },
'.cm-content': { '.cm-content': {
fontSize: 'var(--font-size)', fontSize: 'var(--font-size)',
@ -58,14 +21,12 @@ const createThemeFromOptions = ({
}, },
'.cm-tooltip': { '.cm-tooltip': {
// Set variables for tooltips, which are outside the editor // Set variables for tooltips, which are outside the editor
'--font-size': `${fontSize}px`, '--font-size': '12px',
'--source-font-family': fontFamilies[fontFamily]?.join(', '), '--source-font-family': fontFamily,
// NOTE: fontFamily is not set here, as most tooltips use the UI font // NOTE: fontFamily is not set here, as most tooltips use the UI font
fontSize: 'var(--font-size)', fontSize: 'var(--font-size)',
}, },
'.cm-lineNumbers': { '.cm-lineNumbers': {
fontFamily: 'var(--source-font-family)', fontFamily: 'var(--source-font-family)',
}, },
}), })
]
}