2023-08-12 01:03:06 +03:00
|
|
|
import { bracketMatching } from '@codemirror/language';
|
2023-08-10 18:35:49 +03:00
|
|
|
import { Extension } from '@codemirror/state';
|
|
|
|
import { createTheme } from '@uiw/codemirror-themes';
|
2023-08-12 01:03:06 +03:00
|
|
|
import CodeMirror, { BasicSetupOptions, ReactCodeMirrorProps, ReactCodeMirrorRef } from '@uiw/react-codemirror';
|
2023-08-10 18:35:49 +03:00
|
|
|
import { EditorView } from 'codemirror';
|
2023-08-12 01:03:06 +03:00
|
|
|
import { Ref, useMemo } from 'react';
|
2023-08-10 18:35:49 +03:00
|
|
|
|
2023-08-12 20:52:11 +03:00
|
|
|
import { useConceptTheme } from '../../context/ThemeContext';
|
2023-08-10 18:35:49 +03:00
|
|
|
|
|
|
|
const editorSetup: BasicSetupOptions = {
|
|
|
|
highlightSpecialChars: true,
|
|
|
|
history: true,
|
|
|
|
drawSelection: true,
|
|
|
|
syntaxHighlighting: true,
|
|
|
|
defaultKeymap: true,
|
|
|
|
historyKeymap: true,
|
|
|
|
|
|
|
|
lineNumbers: false,
|
|
|
|
highlightActiveLineGutter: false,
|
|
|
|
foldGutter: false,
|
|
|
|
dropCursor: false,
|
|
|
|
allowMultipleSelections: false,
|
|
|
|
indentOnInput: false,
|
2023-08-12 01:03:06 +03:00
|
|
|
bracketMatching: false,
|
2023-08-10 18:35:49 +03:00
|
|
|
closeBrackets: false,
|
|
|
|
autocompletion: false,
|
|
|
|
rectangularSelection: false,
|
|
|
|
crosshairCursor: false,
|
|
|
|
highlightActiveLine: false,
|
|
|
|
highlightSelectionMatches: false,
|
|
|
|
closeBracketsKeymap: false,
|
|
|
|
searchKeymap: false,
|
|
|
|
foldKeymap: false,
|
|
|
|
completionKeymap: false,
|
|
|
|
lintKeymap: false
|
|
|
|
};
|
|
|
|
|
2023-08-11 10:54:53 +03:00
|
|
|
const editorExtensions = [
|
2023-08-12 01:03:06 +03:00
|
|
|
EditorView.lineWrapping,
|
|
|
|
bracketMatching()
|
2023-08-11 10:54:53 +03:00
|
|
|
];
|
|
|
|
|
2023-08-12 01:03:06 +03:00
|
|
|
interface RSInputProps
|
|
|
|
extends Omit<ReactCodeMirrorProps, 'onChange'> {
|
|
|
|
innerref?: Ref<ReactCodeMirrorRef> | undefined
|
2023-08-11 10:54:53 +03:00
|
|
|
onChange: (newValue: string) => void
|
2023-08-12 01:03:06 +03:00
|
|
|
onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => void
|
2023-08-11 10:54:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function RSInput({
|
2023-08-12 01:03:06 +03:00
|
|
|
innerref, onChange, editable,
|
2023-08-11 10:54:53 +03:00
|
|
|
height='10rem',
|
|
|
|
...props
|
|
|
|
}: RSInputProps) {
|
2023-08-10 18:35:49 +03:00
|
|
|
const { darkMode } = useConceptTheme();
|
|
|
|
|
2023-08-12 01:03:06 +03:00
|
|
|
const cursor = useMemo(() => editable ? 'cursor-text': 'cursor-default', [editable]);
|
|
|
|
const lightTheme: Extension = useMemo(
|
|
|
|
() => createTheme({
|
|
|
|
theme: 'light',
|
|
|
|
settings: {
|
|
|
|
fontFamily: 'inherit',
|
|
|
|
background: editable ? '#ffffff' : '#f0f2f7',
|
|
|
|
foreground: '#000000',
|
|
|
|
selection: '#036dd626',
|
|
|
|
selectionMatch: '#036dd626',
|
|
|
|
caret: '#5d00ff',
|
|
|
|
},
|
|
|
|
styles: [
|
|
|
|
// { tag: t.comment, color: '#787b8099' },
|
|
|
|
// { tag: t.variableName, color: '#0080ff' },
|
|
|
|
// { tag: [t.string, t.special(t.brace)], color: '#5c6166' },
|
|
|
|
// { tag: t.definition(t.typeName), color: '#5c6166' },
|
|
|
|
]
|
|
|
|
}), [editable]);
|
|
|
|
|
|
|
|
const darkTheme: Extension = useMemo(
|
|
|
|
() => createTheme({
|
|
|
|
theme: 'dark',
|
|
|
|
settings: {
|
|
|
|
fontFamily: 'inherit',
|
|
|
|
background: editable ? '#070b12' : '#374151',
|
|
|
|
foreground: '#e4e4e7',
|
|
|
|
selection: '#ffae00b0',
|
|
|
|
selectionMatch: '#ffae00b0',
|
|
|
|
caret: '#ffaa00'
|
|
|
|
},
|
|
|
|
styles: [
|
|
|
|
// { tag: t.comment, color: '#787b8099' },
|
|
|
|
// { tag: t.variableName, color: '#0080ff' },
|
|
|
|
// { tag: [t.string, t.special(t.brace)], color: '#5c6166' },
|
|
|
|
// { tag: t.definition(t.typeName), color: '#5c6166' },
|
|
|
|
]
|
|
|
|
}), [editable]);
|
|
|
|
|
2023-08-10 18:35:49 +03:00
|
|
|
return (
|
2023-08-12 01:03:06 +03:00
|
|
|
<div className={`w-full h-[${height}] ${cursor}`}>
|
2023-08-10 18:35:49 +03:00
|
|
|
<CodeMirror
|
2023-08-12 01:03:06 +03:00
|
|
|
ref={innerref}
|
2023-08-10 18:35:49 +03:00
|
|
|
basicSetup={editorSetup}
|
2023-08-11 10:54:53 +03:00
|
|
|
extensions={editorExtensions}
|
|
|
|
height={height}
|
2023-08-10 18:35:49 +03:00
|
|
|
indentWithTab={false}
|
|
|
|
theme={darkMode ? darkTheme : lightTheme}
|
2023-08-12 01:03:06 +03:00
|
|
|
onChange={value => onChange(value)}
|
|
|
|
editable={editable}
|
2023-08-11 10:54:53 +03:00
|
|
|
{...props}
|
2023-08-10 18:35:49 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RSInput;
|