ConceptPortal-public/rsconcept/frontend/src/components/RSInput/index.tsx

164 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-08-14 23:02:41 +03:00
2023-08-10 18:35:49 +03:00
import { Extension } from '@codemirror/state';
2023-08-13 00:57:31 +03:00
import { tags as t } from '@lezer/highlight';
2023-08-10 18:35:49 +03:00
import { createTheme } from '@uiw/codemirror-themes';
import CodeMirror, { BasicSetupOptions, ReactCodeMirrorProps, ReactCodeMirrorRef } from '@uiw/react-codemirror';
2023-08-10 18:35:49 +03:00
import { EditorView } from 'codemirror';
import { RefObject, useCallback, useMemo, useRef } from 'react';
2023-08-10 18:35:49 +03:00
2023-08-15 00:41:09 +03:00
import { useRSForm } from '../../context/RSFormContext';
2023-08-12 20:52:11 +03:00
import { useConceptTheme } from '../../context/ThemeContext';
import { TokenID } from '../../utils/enums';
import Label from '../Common/Label';
2023-08-14 23:02:41 +03:00
import { ccBracketMatching } from './bracketMatching';
2023-08-13 00:57:31 +03:00
import { RSLanguage } from './rslang';
import { getSymbolSubstitute,TextWrapper } from './textEditing';
2023-08-31 17:25:42 +03:00
import { rshoverTooltip as rsHoverTooltip } from './tooltip';
2023-08-10 18:35:49 +03:00
const editorSetup: BasicSetupOptions = {
2023-08-14 23:02:41 +03:00
highlightSpecialChars: false,
2023-08-10 18:35:49 +03:00
history: true,
2023-08-14 23:02:41 +03:00
drawSelection: false,
syntaxHighlighting: false,
2023-08-10 18:35:49 +03:00
defaultKeymap: true,
historyKeymap: true,
lineNumbers: false,
highlightActiveLineGutter: false,
foldGutter: false,
dropCursor: false,
allowMultipleSelections: false,
indentOnInput: false,
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
};
interface RSInputProps
extends Omit<ReactCodeMirrorProps, 'onChange'| 'onKeyDown'> {
label?: string
innerref?: RefObject<ReactCodeMirrorRef> | undefined
onChange?: (newValue: string) => void
2023-08-11 10:54:53 +03:00
}
function RSInput({
id, label, innerref, onChange, editable,
2023-08-11 10:54:53 +03:00
...props
}: RSInputProps) {
2023-08-27 15:39:49 +03:00
const { darkMode, colors } = useConceptTheme();
2023-08-15 00:41:09 +03:00
const { schema } = useRSForm();
2023-08-10 18:35:49 +03:00
const internalRef = useRef<ReactCodeMirrorRef>(null);
const thisRef = useMemo(
() => {
return innerref ?? internalRef;
2023-08-23 22:57:25 +03:00
}, [internalRef, innerref]);
const cursor = useMemo(() => editable ? 'cursor-text': 'cursor-default', [editable]);
const lightTheme: Extension = useMemo(
() => createTheme({
theme: 'light',
settings: {
fontFamily: 'inherit',
background: editable ? colors.bgInput : colors.bgDisabled,
foreground: colors.fgDefault,
selection: colors.bgHover
},
styles: [
2023-08-15 00:41:09 +03:00
{ tag: t.name, class: 'text-[#b266ff] cursor-default' }, // GlobalID
2023-08-13 00:57:31 +03:00
{ tag: t.variableName, class: 'text-[#24821a]' }, // LocalID
{ tag: t.propertyName, class: '' }, // Radical
{ tag: t.keyword, class: 'text-[#001aff]' }, // keywords
2023-08-14 18:40:19 +03:00
{ tag: t.literal, class: 'text-[#001aff]' }, // literals
2023-08-13 00:57:31 +03:00
{ tag: t.controlKeyword, class: 'font-semibold'}, // R | I | D
{ tag: t.unit, class: 'text-[0.75rem]' }, // indicies
]
2023-08-27 15:39:49 +03:00
}), [editable, colors]);
const darkTheme: Extension = useMemo(
() => createTheme({
theme: 'dark',
settings: {
fontFamily: 'inherit',
background: editable ? colors.bgInput : colors.bgDisabled,
foreground: colors.fgDefault,
selection: colors.bgHover
},
styles: [
2023-08-15 00:41:09 +03:00
{ tag: t.name, class: 'text-[#dfbfff] cursor-default' }, // GlobalID
2023-08-13 00:57:31 +03:00
{ tag: t.variableName, class: 'text-[#69bf60]' }, // LocalID
{ tag: t.propertyName, class: '' }, // Radical
{ tag: t.keyword, class: 'text-[#808dff]' }, // keywords
2023-08-14 18:40:19 +03:00
{ tag: t.literal, class: 'text-[#808dff]' }, // literals
2023-08-13 00:57:31 +03:00
{ tag: t.controlKeyword, class: 'font-semibold'}, // R | I | D
{ tag: t.unit, class: 'text-[0.75rem]' }, // indicies
]
2023-08-27 15:39:49 +03:00
}), [editable, colors]);
2023-08-14 23:02:41 +03:00
const editorExtensions = useMemo(
() => [
EditorView.lineWrapping,
RSLanguage,
ccBracketMatching(darkMode),
2023-08-31 17:25:42 +03:00
rsHoverTooltip(schema?.items || []),
2023-08-15 00:41:09 +03:00
], [darkMode, schema?.items]);
2023-08-14 23:02:41 +03:00
const handleInput = useCallback(
(event: React.KeyboardEvent<HTMLDivElement>) => {
if (!thisRef.current) {
return;
}
const text = new TextWrapper(thisRef.current as Required<ReactCodeMirrorRef>);
if (event.shiftKey && event.key === '*' && !event.altKey) {
text.insertToken(TokenID.DECART);
} else if (event.altKey) {
if (!text.processAltKey(event.key)) {
return;
}
} else if (!event.ctrlKey) {
const newSymbol = getSymbolSubstitute(event.key);
if (!newSymbol) {
return;
}
text.replaceWith(newSymbol);
} else {
return;
}
event.preventDefault();
}, [thisRef]);
2023-08-10 18:35:49 +03:00
return (
<div className={`flex flex-col w-full ${cursor}`}>
{label &&
<Label
text={label}
required={false}
htmlFor={id}
className='mb-2'
/>}
<CodeMirror id={id}
ref={thisRef}
2023-08-10 18:35:49 +03:00
basicSetup={editorSetup}
2023-08-14 23:02:41 +03:00
theme={darkMode ? darkTheme : lightTheme}
2023-08-11 10:54:53 +03:00
extensions={editorExtensions}
2023-08-10 18:35:49 +03:00
indentWithTab={false}
onChange={onChange}
editable={editable}
onKeyDown={handleInput}
2023-08-11 10:54:53 +03:00
{...props}
2023-08-10 18:35:49 +03:00
/>
</div>
);
}
export default RSInput;