Codemirror fixes

This commit is contained in:
IRBorisov 2023-08-11 10:54:53 +03:00
parent c59900cd6d
commit ceeac08e81
3 changed files with 45 additions and 28 deletions

View File

@ -18,6 +18,13 @@
@apply bg-gray-100 dark:bg-gray-600 dark:text-zinc-200 hover:bg-gray-50 hover:text-gray-700 dark:hover:text-white dark:hover:bg-gray-500 @apply bg-gray-100 dark:bg-gray-600 dark:text-zinc-200 hover:bg-gray-50 hover:text-gray-700 dark:hover:text-white dark:hover:bg-gray-500
} }
.cm-editor {
@apply border shadow rounded clr-border px-2
}
.cm-editor.cm-focused {
@apply border shadow rounded outline-2 outline
}
@layer components { @layer components {
h1 { h1 {
@apply text-lg font-bold text-center @apply text-lg font-bold text-center

View File

@ -1,5 +1,5 @@
import { ReactCodeMirrorRef } from '@uiw/react-codemirror';
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { toast } from 'react-toastify';
import Button from '../../components/Common/Button'; import Button from '../../components/Common/Button';
import Label from '../../components/Common/Label'; import Label from '../../components/Common/Label';
@ -38,9 +38,11 @@ function EditorRSExpression({
}: EditorRSExpressionProps) { }: EditorRSExpressionProps) {
const { user } = useAuth(); const { user } = useAuth();
const { schema } = useRSForm(); const { schema } = useRSForm();
const [isModified, setIsModified] = useState(false); const [isModified, setIsModified] = useState(false);
const { parseData, checkExpression, resetParse, loading } = useCheckExpression({ schema }); const { parseData, checkExpression, resetParse, loading } = useCheckExpression({ schema });
const expressionCtrl = useRef<HTMLTextAreaElement>(null); const expressionCtrl = useRef<HTMLTextAreaElement>(null);
const rsInput = useRef<ReactCodeMirrorRef>(null);
useLayoutEffect(() => { useLayoutEffect(() => {
setIsModified(false); setIsModified(false);
@ -51,8 +53,8 @@ function EditorRSExpression({
toggleEditMode() toggleEditMode()
} }
function handleChange(event: React.ChangeEvent<HTMLTextAreaElement>) { function handleChange(newvalue: string) {
onChange(event.target.value); onChange(newvalue);
setIsModified(true); setIsModified(true);
} }
@ -91,7 +93,6 @@ function EditorRSExpression({
const handleEdit = useCallback((id: TokenID, key?: string) => { const handleEdit = useCallback((id: TokenID, key?: string) => {
if (!expressionCtrl.current) { if (!expressionCtrl.current) {
toast.error('Нет доступа к полю редактирования формального выражения');
return; return;
} }
const text = new TextWrapper(expressionCtrl.current); const text = new TextWrapper(expressionCtrl.current);
@ -112,6 +113,7 @@ function EditorRSExpression({
return; return;
} }
const text = new TextWrapper(expressionCtrl.current); const text = new TextWrapper(expressionCtrl.current);
// rsInput.current?.state?.selection
if (event.shiftKey && event.key === '*' && !event.altKey) { if (event.shiftKey && event.key === '*' && !event.altKey) {
text.insertToken(TokenID.DECART); text.insertToken(TokenID.DECART);
} else if (event.altKey) { } else if (event.altKey) {
@ -232,16 +234,16 @@ function EditorRSExpression({
rows={6} rows={6}
placeholder={placeholder} placeholder={placeholder}
value={value} value={value}
onChange={handleChange} onChange={event => handleChange(event.target.value)}
onFocus={handleFocusIn} onFocus={handleFocusIn}
onKeyDown={handleInput} onKeyDown={handleInput}
disabled={disabled} disabled={disabled}
spellCheck={false} spellCheck={false}
/> />
{ user?.is_staff && <RSInput { user?.is_staff &&
<RSInput ref={rsInput}
value={value} value={value}
setValue={setValue} onChange={handleChange}
onChange={onChange}
placeholder={placeholder} placeholder={placeholder}
disabled={disabled} disabled={disabled}
/> } /> }

View File

@ -1,7 +1,8 @@
import { Extension } from '@codemirror/state'; import { Extension } from '@codemirror/state';
import { createTheme } from '@uiw/codemirror-themes'; import { createTheme } from '@uiw/codemirror-themes';
import CodeMirror, { BasicSetupOptions } from '@uiw/react-codemirror'; import CodeMirror, { BasicSetupOptions, ReactCodeMirrorRef } from '@uiw/react-codemirror';
import { EditorView } from 'codemirror'; import { EditorView } from 'codemirror';
import { Ref } from 'react';
import { useConceptTheme } from '../../../context/ThemeContext'; import { useConceptTheme } from '../../../context/ThemeContext';
@ -9,7 +10,7 @@ import { useConceptTheme } from '../../../context/ThemeContext';
const lightTheme: Extension = createTheme({ const lightTheme: Extension = createTheme({
theme: 'light', theme: 'light',
settings: { settings: {
fontFamily: 'Roboto', fontFamily: 'inherit',
background: '#ffffff', background: '#ffffff',
foreground: '#000000', foreground: '#000000',
selection: '#036dd626' selection: '#036dd626'
@ -26,7 +27,7 @@ const lightTheme: Extension = createTheme({
const darkTheme: Extension = createTheme({ const darkTheme: Extension = createTheme({
theme: 'dark', theme: 'dark',
settings: { settings: {
fontFamily: 'Roboto', fontFamily: 'inherit',
background: '#000000', background: '#000000',
foreground: '#ffffff', foreground: '#ffffff',
selection: '#036dd626' selection: '#036dd626'
@ -39,14 +40,6 @@ const darkTheme: Extension = createTheme({
] ]
}); });
interface RSInputProps {
disabled?: boolean
placeholder?: string
value: string
onChange: (newValue: string) => void
setValue: (expression: string) => void
}
const editorSetup: BasicSetupOptions = { const editorSetup: BasicSetupOptions = {
highlightSpecialChars: true, highlightSpecialChars: true,
history: true, history: true,
@ -75,22 +68,37 @@ const editorSetup: BasicSetupOptions = {
lintKeymap: false lintKeymap: false
}; };
function RSInput({ value, disabled, placeholder, setValue }: RSInputProps) { const editorExtensions = [
EditorView.lineWrapping
];
interface RSInputProps {
ref?: Ref<ReactCodeMirrorRef>
value?: string
disabled?: boolean
height?: string
placeholder?: string
onChange: (newValue: string) => void
}
function RSInput({
disabled, onChange,
height='10rem',
...props
}: RSInputProps) {
const { darkMode } = useConceptTheme(); const { darkMode } = useConceptTheme();
return ( return (
<div className='w-full h-[10rem] border text-lg'> <div className={`w-full h-[${height}]`}>
<CodeMirror <CodeMirror
value={value}
basicSetup={editorSetup} basicSetup={editorSetup}
extensions={[EditorView.lineWrapping]} extensions={editorExtensions}
editable={!disabled} editable={!disabled}
placeholder={placeholder} height={height}
height='10rem'
indentWithTab={false} indentWithTab={false}
theme={darkMode ? darkTheme : lightTheme} theme={darkMode ? darkTheme : lightTheme}
onChange={(value) => setValue(value)} onChange={(value) => onChange(value)}
{...props}
/> />
</div> </div>
); );