|
@@ -1,30 +1,14 @@
|
|
|
-import { EditorView, ViewUpdate } from '@codemirror/view';
|
|
|
+import { EditorView } from '@codemirror/view';
|
|
|
import { FC, useRef, useMemo } from 'react';
|
|
|
import { useTheme } from '@mui/material';
|
|
|
import { json, jsonParseLinter } from '@codemirror/lang-json';
|
|
|
import { linter } from '@codemirror/lint';
|
|
|
+import { githubLight } from '@ddietr/codemirror-themes/github-light';
|
|
|
+import { githubDark } from '@ddietr/codemirror-themes/github-dark';
|
|
|
+import { Compartment } from '@codemirror/state';
|
|
|
|
|
|
import { useCodeMirror } from './hooks/use-codemirror';
|
|
|
-import { jsonFoldGutter } from './language/extensions/fold';
|
|
|
-
|
|
|
import { getCMStyle, getStyles } from './style';
|
|
|
-import {
|
|
|
- HighlightStyle,
|
|
|
- StreamLanguage,
|
|
|
- syntaxHighlighting,
|
|
|
-} from '@codemirror/language';
|
|
|
-import { json as jsonMode } from '@codemirror/legacy-modes/mode/javascript';
|
|
|
-import { tags } from '@lezer/highlight';
|
|
|
-
|
|
|
-const getJsonHighlightStyle = (isDarkMode: boolean) =>
|
|
|
- HighlightStyle.define([
|
|
|
- { tag: tags.string, color: isDarkMode ? '#9CDCFE' : '#085bd7' },
|
|
|
- { tag: tags.number, color: isDarkMode ? '#50fa7b' : '#0c7e5e' },
|
|
|
- { tag: tags.bool, color: '#a00' },
|
|
|
- { tag: tags.null, color: '#a00' },
|
|
|
- { tag: tags.propertyName, color: '#a0a' },
|
|
|
- { tag: tags.punctuation, color: '#555' },
|
|
|
- ]);
|
|
|
|
|
|
type Props = {
|
|
|
value: string;
|
|
@@ -35,6 +19,7 @@ type Props = {
|
|
|
export const JSONEditor: FC<Props> = props => {
|
|
|
const { value, editable = true, onChange } = props;
|
|
|
const theme = useTheme();
|
|
|
+ const themeCompartment = new Compartment();
|
|
|
const container = useRef<HTMLDivElement>(null);
|
|
|
const classes = getStyles();
|
|
|
|
|
@@ -45,10 +30,8 @@ export const JSONEditor: FC<Props> = props => {
|
|
|
EditorView.lineWrapping,
|
|
|
EditorView.editable.of(editable),
|
|
|
EditorView.theme(getCMStyle(theme)),
|
|
|
- StreamLanguage.define(jsonMode as any),
|
|
|
- syntaxHighlighting(getJsonHighlightStyle(isDarkMode)),
|
|
|
+ themeCompartment.of(isDarkMode ? githubDark : githubLight),
|
|
|
json(),
|
|
|
- jsonFoldGutter(),
|
|
|
linter(jsonParseLinter()),
|
|
|
];
|
|
|
}, [theme.palette.mode, isDarkMode, editable]);
|