2022-11-25 14:34:55 +00:00
|
|
|
import { CodeHighlightNode, CodeNode } from "@lexical/code";
|
|
|
|
import { AutoLinkNode, LinkNode } from "@lexical/link";
|
|
|
|
import { ListItemNode, ListNode } from "@lexical/list";
|
|
|
|
import { TRANSFORMERS } from "@lexical/markdown";
|
|
|
|
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
|
|
|
|
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
|
|
|
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
|
|
|
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
|
|
|
|
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
|
|
|
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
|
|
|
|
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
|
|
|
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
|
|
|
|
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
|
|
|
|
|
|
|
|
import ExampleTheme from "./ExampleTheme";
|
|
|
|
import AutoLinkPlugin from "./plugins/AutoLinkPlugin";
|
|
|
|
import ToolbarPlugin from "./plugins/ToolbarPlugin";
|
|
|
|
import "./stylesEditor.css";
|
|
|
|
|
2023-01-09 14:15:11 +00:00
|
|
|
/*
|
|
|
|
Detault toolbar items:
|
|
|
|
- blockType
|
|
|
|
- bold
|
|
|
|
- italic
|
|
|
|
- link
|
|
|
|
*/
|
2022-11-25 14:34:55 +00:00
|
|
|
export type TextEditorProps = {
|
2023-01-09 14:15:11 +00:00
|
|
|
getText: () => string;
|
|
|
|
setText: (text: string) => void;
|
|
|
|
excludedToolbarItems?: string[];
|
|
|
|
variables?: string[];
|
2023-01-25 01:08:10 +00:00
|
|
|
height?: string;
|
2023-03-02 19:55:25 +00:00
|
|
|
placeholder?: string;
|
2023-03-03 23:20:13 +00:00
|
|
|
disableLists?: boolean;
|
2022-11-25 14:34:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const editorConfig = {
|
|
|
|
theme: ExampleTheme,
|
|
|
|
onError(error: any) {
|
|
|
|
throw error;
|
|
|
|
},
|
|
|
|
namespace: "",
|
|
|
|
nodes: [
|
|
|
|
HeadingNode,
|
|
|
|
ListNode,
|
|
|
|
ListItemNode,
|
|
|
|
QuoteNode,
|
|
|
|
CodeNode,
|
|
|
|
CodeHighlightNode,
|
|
|
|
TableNode,
|
|
|
|
TableCellNode,
|
|
|
|
TableRowNode,
|
|
|
|
AutoLinkNode,
|
|
|
|
LinkNode,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-01-09 14:15:11 +00:00
|
|
|
export const Editor = (props: TextEditorProps) => {
|
2022-11-25 14:34:55 +00:00
|
|
|
return (
|
|
|
|
<div className="editor">
|
|
|
|
<LexicalComposer initialConfig={editorConfig}>
|
|
|
|
<div className="editor-container">
|
2023-01-09 14:15:11 +00:00
|
|
|
<ToolbarPlugin
|
|
|
|
getText={props.getText}
|
|
|
|
setText={props.setText}
|
|
|
|
excludedToolbarItems={props.excludedToolbarItems}
|
|
|
|
variables={props.variables}
|
|
|
|
/>
|
2023-01-25 01:08:10 +00:00
|
|
|
<div className="editor-inner" style={{ height: props.height }}>
|
|
|
|
<RichTextPlugin
|
|
|
|
contentEditable={<ContentEditable style={{ height: props.height }} className="editor-input" />}
|
2023-03-02 19:55:25 +00:00
|
|
|
placeholder={<div className="-mt-11 p-3 text-sm text-gray-300">{props.placeholder || ""}</div>}
|
2023-01-25 01:08:10 +00:00
|
|
|
/>
|
2022-11-25 14:34:55 +00:00
|
|
|
<AutoFocusPlugin />
|
|
|
|
<ListPlugin />
|
|
|
|
<LinkPlugin />
|
|
|
|
<AutoLinkPlugin />
|
2023-03-03 23:20:13 +00:00
|
|
|
<MarkdownShortcutPlugin
|
|
|
|
transformers={
|
|
|
|
props.disableLists
|
|
|
|
? TRANSFORMERS.filter((value, index) => {
|
|
|
|
if (index !== 3 && index !== 4) return value;
|
|
|
|
})
|
|
|
|
: TRANSFORMERS
|
|
|
|
}
|
|
|
|
/>
|
2022-11-25 14:34:55 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</LexicalComposer>
|
|
|
|
</div>
|
|
|
|
);
|
2023-01-09 14:15:11 +00:00
|
|
|
};
|