made simplier editor, added color coding

This commit is contained in:
Fedor Katurov 2023-04-25 20:01:46 +06:00
parent 601eda17de
commit c1533e9cb9
15 changed files with 287 additions and 43 deletions

View file

@ -0,0 +1,41 @@
import { ChangeEvent, FC, useCallback, useMemo } from "react";
import styles from "./styles.module.scss";
import { useTheme } from "~/modules/theme/context/ThemeContext";
interface ReactMarkdownEditorProps {
value: string;
onChange: (val: string) => void;
}
const ReactMarkdownEditor: FC<ReactMarkdownEditorProps> = ({
value,
onChange,
}) => {
const changeHandler = useCallback(
(event: ChangeEvent<HTMLTextAreaElement>) => {
onChange(event.target.value);
},
[onChange]
);
const { paddingHorizontal, paddingVertical } = useTheme();
const style = useMemo(
() => ({
padding: `${paddingVertical}px ${paddingHorizontal}px`,
}),
[paddingHorizontal, paddingVertical]
);
return (
<textarea
onChange={changeHandler}
className={styles.textarea}
style={style}
>
{value}
</textarea>
);
};
export { ReactMarkdownEditor };

View file

@ -0,0 +1,9 @@
.textarea {
width: 100%;
height: 100%;
padding: 0;
background: transparent;
color: inherit;
border: none;
box-sizing: border-box;
}

View file

@ -0,0 +1,20 @@
import { FC } from "react";
import ReactMarkdown from "react-markdown";
import { useContainerPaddings } from "~/modules/theme/hooks/useContainerPaddings";
import styles from "./styles.module.scss";
interface ReactMarkdownViewerProps {
value: string;
}
const ReactMarkdownViewer: FC<ReactMarkdownViewerProps> = ({ value }) => {
const style = useContainerPaddings();
return (
<div style={style} className={styles.editor}>
<ReactMarkdown>{value}</ReactMarkdown>
</div>
);
};
export { ReactMarkdownViewer };

View file

@ -0,0 +1,11 @@
.editor {
& > :first-child {
margin-top: 0 !important;
margin-left: 0 !important;
}
& > :last-child {
margin-bottom: 0 !important;
margin-right: 0 !important;
}
}

View file

@ -31,6 +31,7 @@ import {
GapCursorExtension,
} from "remirror/extensions";
import styles from "./styles.module.scss";
import { useContainerPaddings } from "~/modules/theme/hooks/useContainerPaddings";
interface RemirrorEditorProps {
locked: boolean;
@ -63,23 +64,28 @@ const RemirrorEditor: FC<RemirrorEditorProps> = ({
[onChange, setState]
);
const style = useContainerPaddings();
return (
<Remirror
placeholder="Start typing..."
manager={manager}
classNames={[styles.editor]}
editable={!locked}
onChange={onStateChange}
state={state}
>
<EditorComponent />
{!locked && (
<FloatingToolbar>
<FormattingButtonGroup />
<HeadingLevelButtonGroup />
</FloatingToolbar>
)}
</Remirror>
<div className={styles.wrapper} style={style}>
<Remirror
placeholder="Start typing..."
manager={manager}
classNames={[styles.editor]}
editable={!locked}
onChange={onStateChange}
state={state}
autoFocus
>
<EditorComponent />
{!locked && (
<FloatingToolbar>
<FormattingButtonGroup />
<HeadingLevelButtonGroup />
</FloatingToolbar>
)}
</Remirror>
</div>
);
};

View file

@ -1,6 +1,17 @@
.wrapper {
height: 100%;
box-sizing: border-box;
& > :global(.remirror-editor-wrapper) {
height: 100%;
box-sizing: border-box;
}
}
.editor {
outline: none;
height: 100%;
box-sizing: border-box;
& > :first-child {
margin-top: 0 !important;

View file

@ -1,7 +1,8 @@
import { FC } from "react";
import styles from "./styles.module.scss";
import { ReactMarkdownEditor } from "../../components/ReactMarkdownEditor";
import { ReactMarkdownViewer } from "../../components/ReactMarkdownViewer";
import { usePersistedValue } from "./hooks/usePersistedValue";
import { RemirrorEditor } from "../../components/RemirrorEditor";
import styles from "./styles.module.scss";
interface MarkdownEditorContainerProps {
id: string;
@ -16,7 +17,19 @@ export const MarkdownEditorContainer: FC<MarkdownEditorContainerProps> = ({
return (
<div className={styles.editor}>
<RemirrorEditor value={value} onChange={setValue} locked={locked} />
{/*
locked ? (
<ReactMarkdownViewer value={value} />
) : (
<ReactMarkdownEditor value={value} onChange={setValue} />
)
*/}
{locked ? (
<ReactMarkdownViewer value={value} />
) : (
<ReactMarkdownEditor value={value} onChange={setValue} />
// <RemirrorEditor value={value} onChange={setValue} locked={locked} />
)}
</div>
);
};

View file

@ -1,6 +1,5 @@
.editor {
height: 100%;
padding: 16px;
overflow: scroll;
box-sizing: border-box;
}