2022-12-05 05:42:21 +00:00
|
|
|
import {
|
|
|
|
EditorState,
|
|
|
|
$getRoot,
|
|
|
|
$getSelection,
|
|
|
|
SerializedEditorState,
|
|
|
|
LexicalEditor,
|
|
|
|
} from "lexical";
|
2022-11-29 14:19:39 +00:00
|
|
|
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
|
|
|
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
|
|
|
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
|
|
|
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
|
|
|
|
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
|
|
|
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
|
|
|
|
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
|
|
|
|
import { TRANSFORMERS, CHECK_LIST } from "@lexical/markdown";
|
|
|
|
import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
|
|
|
|
import { $generateHtmlFromNodes } from "@lexical/html";
|
|
|
|
import { CheckListPlugin } from "@lexical/react/LexicalCheckListPlugin";
|
|
|
|
|
|
|
|
// custom plugins
|
|
|
|
import { CodeHighlightPlugin } from "./plugins/code-highlight";
|
|
|
|
import { LexicalToolbar } from "./toolbar";
|
|
|
|
// config
|
|
|
|
import { initialConfig } from "./config";
|
|
|
|
// helpers
|
|
|
|
import { getValidatedValue } from "./helpers/editor";
|
2022-11-30 15:58:45 +00:00
|
|
|
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
|
2022-11-29 14:19:39 +00:00
|
|
|
|
|
|
|
export interface RichTextEditorProps {
|
2022-12-05 05:42:21 +00:00
|
|
|
onChange: (state: SerializedEditorState) => void;
|
2022-11-29 14:19:39 +00:00
|
|
|
id: string;
|
|
|
|
value: string;
|
2022-12-02 14:12:58 +00:00
|
|
|
placeholder?: string;
|
2022-11-29 14:19:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 14:12:58 +00:00
|
|
|
const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
|
|
onChange,
|
|
|
|
id,
|
|
|
|
value,
|
|
|
|
placeholder = "Enter some text...",
|
|
|
|
}) => {
|
2022-12-05 05:42:21 +00:00
|
|
|
const handleChange = (editorState: EditorState) => {
|
|
|
|
editorState.read(() => {
|
|
|
|
let editorData = editorState.toJSON();
|
|
|
|
if (onChange) onChange(editorData);
|
2022-11-29 14:19:39 +00:00
|
|
|
});
|
2022-12-05 05:42:21 +00:00
|
|
|
};
|
2022-12-06 14:38:28 +00:00
|
|
|
|
2022-12-05 05:42:21 +00:00
|
|
|
// function handleChange(state: EditorState, editor: LexicalEditor) {
|
|
|
|
// state.read(() => {
|
|
|
|
// onChange(state.toJSON());
|
|
|
|
// });
|
|
|
|
// }
|
2022-11-29 14:19:39 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<LexicalComposer
|
|
|
|
initialConfig={{
|
|
|
|
...initialConfig,
|
|
|
|
namespace: id || "Lexical Editor",
|
|
|
|
editorState: getValidatedValue(value),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="border border-[#e2e2e2] rounded-md">
|
|
|
|
<LexicalToolbar />
|
|
|
|
<div className="relative">
|
|
|
|
<RichTextPlugin
|
|
|
|
contentEditable={
|
|
|
|
<ContentEditable className='className="h-[450px] outline-none py-[15px] px-2.5 resize-none overflow-hidden text-ellipsis' />
|
|
|
|
}
|
2022-11-30 15:58:45 +00:00
|
|
|
ErrorBoundary={LexicalErrorBoundary}
|
2022-11-29 14:19:39 +00:00
|
|
|
placeholder={
|
2022-12-02 14:12:58 +00:00
|
|
|
<div className="absolute top-4 left-3 pointer-events-none select-none text-gray-400">
|
|
|
|
{placeholder}
|
2022-11-29 14:19:39 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<OnChangePlugin onChange={handleChange} />
|
|
|
|
<HistoryPlugin />
|
|
|
|
<CodeHighlightPlugin />
|
|
|
|
<ListPlugin />
|
|
|
|
<LinkPlugin />
|
|
|
|
<CheckListPlugin />
|
|
|
|
<MarkdownShortcutPlugin transformers={TRANSFORMERS} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</LexicalComposer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RichTextEditor;
|