2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
import { Extension } from '@codemirror/state';
|
|
|
|
import { tags } from '@lezer/highlight';
|
|
|
|
import { createTheme } from '@uiw/codemirror-themes';
|
|
|
|
import CodeMirror, { BasicSetupOptions, ReactCodeMirrorProps, ReactCodeMirrorRef } from '@uiw/react-codemirror';
|
|
|
|
import { EditorView } from 'codemirror';
|
|
|
|
import { RefObject, useCallback, useMemo, useRef, useState } from 'react';
|
|
|
|
|
|
|
|
import { useRSForm } from '../../context/RSFormContext';
|
|
|
|
import { useConceptTheme } from '../../context/ThemeContext';
|
2023-11-01 13:47:49 +03:00
|
|
|
import DlgEditReference from '../../dialogs/DlgEditReference';
|
2023-09-25 23:50:41 +03:00
|
|
|
import useResolveText from '../../hooks/useResolveText';
|
2023-09-29 15:33:32 +03:00
|
|
|
import { ReferenceType } from '../../models/language';
|
|
|
|
import { IConstituenta } from '../../models/rsform';
|
2023-09-28 16:31:10 +03:00
|
|
|
import { CodeMirrorWrapper } from '../../utils/codemirror';
|
2023-09-25 23:50:41 +03:00
|
|
|
import Label from '../Common/Label';
|
|
|
|
import Modal from '../Common/Modal';
|
|
|
|
import PrettyJson from '../Common/PrettyJSON';
|
2023-09-28 16:31:10 +03:00
|
|
|
import { NaturalLanguage, ReferenceTokens } from './parse';
|
2023-09-29 15:33:32 +03:00
|
|
|
import { RefEntity } from './parse/parser.terms';
|
2023-09-27 23:36:51 +03:00
|
|
|
import { refsHoverTooltip } from './tooltip';
|
2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
const editorSetup: BasicSetupOptions = {
|
|
|
|
highlightSpecialChars: false,
|
|
|
|
history: true,
|
|
|
|
drawSelection: false,
|
|
|
|
syntaxHighlighting: false,
|
|
|
|
defaultKeymap: true,
|
|
|
|
historyKeymap: true,
|
|
|
|
|
|
|
|
lineNumbers: false,
|
|
|
|
highlightActiveLineGutter: false,
|
|
|
|
foldGutter: false,
|
2023-09-28 17:04:06 +03:00
|
|
|
dropCursor: true,
|
2023-09-25 23:50:41 +03:00
|
|
|
allowMultipleSelections: false,
|
|
|
|
indentOnInput: false,
|
|
|
|
bracketMatching: false,
|
|
|
|
closeBrackets: false,
|
|
|
|
autocompletion: false,
|
|
|
|
rectangularSelection: false,
|
|
|
|
crosshairCursor: false,
|
|
|
|
highlightActiveLine: false,
|
|
|
|
highlightSelectionMatches: false,
|
|
|
|
closeBracketsKeymap: false,
|
|
|
|
searchKeymap: false,
|
|
|
|
foldKeymap: false,
|
|
|
|
completionKeymap: false,
|
|
|
|
lintKeymap: false
|
|
|
|
};
|
|
|
|
|
|
|
|
interface RefsInputInputProps
|
|
|
|
extends Pick<ReactCodeMirrorProps,
|
2023-11-06 02:20:16 +03:00
|
|
|
'id'| 'height' | 'value' | 'className' | 'onFocus' | 'onBlur' | 'placeholder'
|
2023-09-25 23:50:41 +03:00
|
|
|
> {
|
|
|
|
label?: string
|
|
|
|
innerref?: RefObject<ReactCodeMirrorRef> | undefined
|
|
|
|
onChange?: (newValue: string) => void
|
2023-09-29 15:33:32 +03:00
|
|
|
items?: IConstituenta[]
|
2023-11-06 02:20:16 +03:00
|
|
|
disabled?: boolean
|
2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
initialValue?: string
|
|
|
|
value?: string
|
|
|
|
resolved?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function RefsInput({
|
2023-11-06 02:20:16 +03:00
|
|
|
id, label, innerref, disabled, items,
|
2023-09-25 23:50:41 +03:00
|
|
|
initialValue, value, resolved,
|
2023-09-30 17:16:20 +03:00
|
|
|
onFocus, onBlur, onChange,
|
2023-09-25 23:50:41 +03:00
|
|
|
...props
|
|
|
|
}: RefsInputInputProps) {
|
|
|
|
const { darkMode, colors } = useConceptTheme();
|
|
|
|
const { schema } = useRSForm();
|
|
|
|
|
|
|
|
const { resolveText, refsData } = useResolveText({schema: schema});
|
|
|
|
|
|
|
|
const [showResolve, setShowResolve] = useState(false);
|
|
|
|
const [isFocused, setIsFocused] = useState(false);
|
|
|
|
|
2023-09-29 15:33:32 +03:00
|
|
|
const [showEditor, setShowEditor] = useState(false);
|
|
|
|
const [currentType, setCurrentType] = useState<ReferenceType>(ReferenceType.ENTITY);
|
|
|
|
const [refText, setRefText] = useState('');
|
|
|
|
const [hintText, setHintText] = useState('');
|
2023-09-30 17:16:20 +03:00
|
|
|
const [basePosition, setBasePosition] = useState(0);
|
|
|
|
const [mainRefs, setMainRefs] = useState<string[]>([]);
|
2023-09-29 15:33:32 +03:00
|
|
|
|
2023-09-25 23:50:41 +03:00
|
|
|
const internalRef = useRef<ReactCodeMirrorRef>(null);
|
|
|
|
const thisRef = useMemo(
|
|
|
|
() => {
|
|
|
|
return innerref ?? internalRef;
|
|
|
|
}, [internalRef, innerref]);
|
|
|
|
|
2023-11-06 02:20:16 +03:00
|
|
|
const cursor = useMemo(() => !disabled ? 'cursor-text': 'cursor-default', [disabled]);
|
2023-09-25 23:50:41 +03:00
|
|
|
const customTheme: Extension = useMemo(
|
|
|
|
() => createTheme({
|
|
|
|
theme: darkMode ? 'dark' : 'light',
|
|
|
|
settings: {
|
|
|
|
fontFamily: 'inherit',
|
2023-11-06 02:20:16 +03:00
|
|
|
background: !disabled ? colors.bgInput : colors.bgDefault,
|
2023-09-25 23:50:41 +03:00
|
|
|
foreground: colors.fgDefault,
|
|
|
|
selection: colors.bgHover
|
|
|
|
},
|
|
|
|
styles: [
|
2023-09-28 13:05:43 +03:00
|
|
|
{ tag: tags.name, color: colors.fgPurple, cursor: 'default' }, // EntityReference
|
|
|
|
{ tag: tags.literal, color: colors.fgTeal, cursor: 'default' }, // SyntacticReference
|
|
|
|
{ tag: tags.comment, color: colors.fgRed }, // Error
|
2023-09-25 23:50:41 +03:00
|
|
|
]
|
2023-11-06 02:20:16 +03:00
|
|
|
}), [disabled, colors, darkMode]);
|
2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
const editorExtensions = useMemo(
|
|
|
|
() => [
|
|
|
|
EditorView.lineWrapping,
|
|
|
|
NaturalLanguage,
|
2023-09-28 16:31:10 +03:00
|
|
|
refsHoverTooltip(schema?.items || [], colors)
|
2023-09-27 23:36:51 +03:00
|
|
|
], [schema?.items, colors]);
|
2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
function handleChange(newValue: string) {
|
|
|
|
if (onChange) onChange(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFocusIn(event: React.FocusEvent<HTMLDivElement>) {
|
|
|
|
setIsFocused(true);
|
|
|
|
if (onFocus) onFocus(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFocusOut(event: React.FocusEvent<HTMLDivElement>) {
|
|
|
|
setIsFocused(false);
|
|
|
|
if (onBlur) onBlur(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleInput = useCallback(
|
|
|
|
(event: React.KeyboardEvent<HTMLDivElement>) => {
|
2023-09-28 16:31:10 +03:00
|
|
|
if (!thisRef.current?.view) {
|
2023-09-25 23:50:41 +03:00
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.altKey) {
|
|
|
|
if (event.key === 'r' && value) {
|
|
|
|
event.preventDefault();
|
|
|
|
resolveText(value, () => {
|
|
|
|
setShowResolve(true);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 16:31:10 +03:00
|
|
|
if (event.ctrlKey && event.code === 'Space') {
|
|
|
|
const wrap = new CodeMirrorWrapper(thisRef.current as Required<ReactCodeMirrorRef>);
|
|
|
|
wrap.fixSelection(ReferenceTokens);
|
2023-09-29 15:33:32 +03:00
|
|
|
const nodes = wrap.getEnvelopingNodes(ReferenceTokens);
|
|
|
|
if (nodes.length !== 1) {
|
|
|
|
setCurrentType(ReferenceType.ENTITY);
|
|
|
|
setRefText('');
|
|
|
|
setHintText(wrap.getSelectionText());
|
|
|
|
} else {
|
|
|
|
setCurrentType(nodes[0].type.id === RefEntity ? ReferenceType.ENTITY : ReferenceType.SYNTACTIC);
|
|
|
|
setRefText(wrap.getSelectionText());
|
|
|
|
}
|
2023-09-30 17:16:20 +03:00
|
|
|
|
|
|
|
const selection = wrap.getSelection();
|
|
|
|
const mainNodes = wrap.getAllNodes([RefEntity]).filter(node => node.from >= selection.to || node.to <= selection.from);
|
|
|
|
setMainRefs(mainNodes.map(node => wrap.getText(node.from, node.to)));
|
|
|
|
setBasePosition(mainNodes.filter(node => node.to <= selection.from).length);
|
|
|
|
|
2023-09-29 15:33:32 +03:00
|
|
|
setShowEditor(true);
|
2023-09-28 16:31:10 +03:00
|
|
|
}
|
2023-09-25 23:50:41 +03:00
|
|
|
}, [thisRef, resolveText, value]);
|
|
|
|
|
2023-09-29 15:33:32 +03:00
|
|
|
const handleInputReference = useCallback(
|
|
|
|
(referenceText: string) => {
|
|
|
|
if (!thisRef.current?.view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thisRef.current.view.focus();
|
|
|
|
const wrap = new CodeMirrorWrapper(thisRef.current as Required<ReactCodeMirrorRef>);
|
|
|
|
wrap.replaceWith(referenceText);
|
|
|
|
}, [thisRef]);
|
|
|
|
|
2023-09-25 23:50:41 +03:00
|
|
|
return (
|
|
|
|
<>
|
2023-09-29 15:33:32 +03:00
|
|
|
{ showEditor &&
|
|
|
|
<DlgEditReference
|
|
|
|
hideWindow={() => setShowEditor(false)}
|
|
|
|
items={items ?? []}
|
2023-09-30 17:16:20 +03:00
|
|
|
initial={{
|
|
|
|
type: currentType,
|
|
|
|
refRaw: refText,
|
|
|
|
text: hintText,
|
|
|
|
basePosition: basePosition,
|
|
|
|
mainRefs: mainRefs
|
|
|
|
}}
|
2023-09-29 15:33:32 +03:00
|
|
|
onSave={handleInputReference}
|
|
|
|
/>
|
|
|
|
}
|
2023-09-25 23:50:41 +03:00
|
|
|
{ showResolve &&
|
|
|
|
<Modal
|
|
|
|
readonly
|
|
|
|
hideWindow={() => setShowResolve(false)}
|
|
|
|
>
|
|
|
|
<div className='max-h-[60vh] max-w-[80vw] overflow-auto'>
|
|
|
|
<PrettyJson data={refsData} />
|
|
|
|
</div>
|
|
|
|
</Modal>}
|
|
|
|
<div className={`flex flex-col w-full ${cursor}`}>
|
|
|
|
{label &&
|
|
|
|
<Label
|
|
|
|
text={label}
|
|
|
|
htmlFor={id}
|
|
|
|
className='mb-2'
|
|
|
|
/>}
|
|
|
|
<CodeMirror id={id}
|
|
|
|
ref={thisRef}
|
|
|
|
basicSetup={editorSetup}
|
|
|
|
theme={customTheme}
|
|
|
|
extensions={editorExtensions}
|
|
|
|
|
2023-09-29 15:33:32 +03:00
|
|
|
value={isFocused ? value : (value !== initialValue || showEditor ? value : resolved)}
|
2023-09-25 23:50:41 +03:00
|
|
|
|
|
|
|
indentWithTab={false}
|
|
|
|
onChange={handleChange}
|
2023-11-06 02:20:16 +03:00
|
|
|
editable={!disabled}
|
2023-09-25 23:50:41 +03:00
|
|
|
onKeyDown={handleInput}
|
|
|
|
onFocus={handleFocusIn}
|
|
|
|
onBlur={handleFocusOut}
|
2023-09-28 17:04:06 +03:00
|
|
|
spellCheck
|
|
|
|
// spellCheck= // TODO: figure out while automatic spellcheck doesnt work or implement with extension
|
2023-09-25 23:50:41 +03:00
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RefsInput;
|