-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
441931f
commit 9566cd2
Showing
5 changed files
with
194 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {Extension} from '@codemirror/state' | ||
import {EditorView} from '@codemirror/view' | ||
import {rgba, useRootTheme} from '@sanity/ui' | ||
import {useMemo} from 'react' | ||
|
||
export function useThemeExtension(): Extension { | ||
const themeCtx = useRootTheme() | ||
|
||
return useMemo(() => { | ||
const dark = {color: themeCtx.theme.color.dark[themeCtx.tone]} | ||
const light = {color: themeCtx.theme.color.light[themeCtx.tone]} | ||
|
||
return EditorView.baseTheme({ | ||
'&.cm-editor': { | ||
height: '100%', | ||
}, | ||
'&.cm-editor.cm-focused': { | ||
outline: 'none', | ||
}, | ||
|
||
// Matching brackets | ||
'&.cm-editor.cm-focused .cm-matchingBracket': { | ||
backgroundColor: 'transparent', | ||
}, | ||
'&.cm-editor.cm-focused .cm-nonmatchingBracket': { | ||
backgroundColor: 'transparent', | ||
}, | ||
'&dark.cm-editor.cm-focused .cm-matchingBracket': { | ||
outline: `1px solid ${dark.color.base.border}`, | ||
}, | ||
'&dark.cm-editor.cm-focused .cm-nonmatchingBracket': { | ||
outline: `1px solid ${dark.color.base.border}`, | ||
}, | ||
'&light.cm-editor.cm-focused .cm-matchingBracket': { | ||
outline: `1px solid ${light.color.base.border}`, | ||
}, | ||
'&light.cm-editor.cm-focused .cm-nonmatchingBracket': { | ||
outline: `1px solid ${light.color.base.border}`, | ||
}, | ||
|
||
// Size and padding of gutter | ||
'& .cm-lineNumbers .cm-gutterElement': { | ||
minWidth: `32px !important`, | ||
padding: `0 8px !important`, | ||
}, | ||
'& .cm-gutter.cm-foldGutter': { | ||
width: `0px !important`, | ||
}, | ||
|
||
// Color of gutter | ||
'&dark .cm-gutters': { | ||
color: `${rgba(dark.color.card.enabled.code.fg, 0.5)} !important`, | ||
borderRight: `1px solid ${rgba(dark.color.base.border, 0.5)}`, | ||
}, | ||
'&light .cm-gutters': { | ||
color: `${rgba(light.color.card.enabled.code.fg, 0.5)} !important`, | ||
borderRight: `1px solid ${rgba(light.color.base.border, 0.5)}`, | ||
}, | ||
}) | ||
}, [themeCtx]) | ||
} |
58 changes: 35 additions & 23 deletions
58
src/codemirror/useCodeMirrorTheme.ts → ...demirror/extensions/useCodeMirrorTheme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Extension} from '@codemirror/state' | ||
import {EditorView} from '@codemirror/view' | ||
import {rem, useTheme} from '@sanity/ui' | ||
import {useMemo} from 'react' | ||
|
||
export function useFontSizeExtension(props: {fontSize: number}): Extension { | ||
const {fontSize: fontSizeProp} = props | ||
const theme = useTheme() | ||
|
||
return useMemo(() => { | ||
const {code: codeFont} = theme.sanity.fonts | ||
const {fontSize, lineHeight} = codeFont.sizes[fontSizeProp] || codeFont.sizes[2] | ||
|
||
return EditorView.baseTheme({ | ||
'&': { | ||
fontSize: rem(fontSize), | ||
}, | ||
|
||
'& .cm-scroller': { | ||
lineHeight: `${lineHeight / fontSize} !important`, | ||
}, | ||
}) | ||
}, [fontSizeProp, theme]) | ||
} |