|
| 1 | +import { FC, useCallback, useMemo } from 'react' |
| 2 | +import CodeMirror from '@uiw/react-codemirror' |
| 3 | +import { loadLanguage } from '@uiw/codemirror-extensions-langs' |
| 4 | +import isFunction from 'lodash/isFunction' |
| 5 | +import { tags as t } from '@lezer/highlight' |
| 6 | +import { createTheme } from '@uiw/codemirror-themes' |
| 7 | +import { useTheme } from '@emotion/react' |
| 8 | + |
| 9 | +import { Props, defaultProps } from './CodeEditor.types' |
| 10 | +import * as styles from './CodeMirror.styles' |
| 11 | +import { getSizeInPx } from '../_utils/commonStyles' |
| 12 | +import { ThemeType } from '../_theme' |
| 13 | + |
| 14 | +const CodeEditor: FC<Props> = (props) => { |
| 15 | + const { height, onChange, value } = { ...defaultProps, ...props } |
| 16 | + const globalTheme: ThemeType = useTheme() |
| 17 | + |
| 18 | + const handleChange = useCallback( |
| 19 | + (v: string) => { |
| 20 | + isFunction(onChange) && onChange(v) |
| 21 | + }, |
| 22 | + [onChange] |
| 23 | + ) |
| 24 | + |
| 25 | + const extensions = useMemo(() => { |
| 26 | + const ext = [] |
| 27 | + |
| 28 | + if (isFunction(loadLanguage)) { |
| 29 | + ext.push(loadLanguage('markdown') as any) |
| 30 | + } |
| 31 | + |
| 32 | + return ext |
| 33 | + }, []) |
| 34 | + |
| 35 | + const theme = useMemo( |
| 36 | + () => |
| 37 | + createTheme({ |
| 38 | + theme: 'light', |
| 39 | + settings: { |
| 40 | + background: globalTheme?.colors?.CodeEditor.background, |
| 41 | + lineHighlight: globalTheme?.colors?.CodeEditor.lineHighlight, |
| 42 | + selection: globalTheme?.colors?.CodeEditor.selection, |
| 43 | + }, |
| 44 | + styles: [ |
| 45 | + { tag: t.comment, color: '#787b8099' }, |
| 46 | + { tag: t.variableName, color: '#0080ff' }, |
| 47 | + { tag: [t.string, t.special(t.brace)], color: '#5c6166' }, |
| 48 | + { tag: t.number, color: '#5c6166' }, |
| 49 | + { tag: t.bool, color: '#5c6166' }, |
| 50 | + { tag: t.null, color: '#5c6166' }, |
| 51 | + { tag: t.keyword, color: '#5c6166' }, |
| 52 | + { tag: t.operator, color: '#5c6166' }, |
| 53 | + { tag: t.className, color: '#5c6166' }, |
| 54 | + { tag: t.definition(t.typeName), color: '#5c6166' }, |
| 55 | + { tag: t.typeName, color: '#5c6166' }, |
| 56 | + { tag: t.angleBracket, color: '#5c6166' }, |
| 57 | + { tag: t.tagName, color: '#5c6166' }, |
| 58 | + { tag: t.attributeName, color: '#5c6166' }, |
| 59 | + ], |
| 60 | + }), |
| 61 | + [] |
| 62 | + ) |
| 63 | + |
| 64 | + return ( |
| 65 | + <CodeMirror |
| 66 | + basicSetup={{ |
| 67 | + foldGutter: false, |
| 68 | + drawSelection: true, |
| 69 | + }} |
| 70 | + css={styles.editor} |
| 71 | + extensions={extensions} |
| 72 | + height={getSizeInPx(height!)} |
| 73 | + onChange={(v) => handleChange(v)} |
| 74 | + theme={theme} |
| 75 | + value={value} |
| 76 | + /> |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +CodeEditor.displayName = 'CodeEditor' |
| 81 | +CodeEditor.defaultProps = defaultProps |
| 82 | + |
| 83 | +export default CodeEditor |
0 commit comments