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 { LexicalComposer } from "@lexical/react/LexicalComposer";
|
|
|
|
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
2023-05-12 16:28:14 +00:00
|
|
|
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
|
2023-05-02 14:29:52 +00:00
|
|
|
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
|
2022-11-25 14:34:55 +00:00
|
|
|
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";
|
2023-04-18 10:08:09 +00:00
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
2022-11-25 14:34:55 +00:00
|
|
|
|
2023-04-13 02:10:23 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
|
|
|
|
2022-11-25 14:34:55 +00:00
|
|
|
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;
|
2023-04-18 10:08:09 +00:00
|
|
|
updateTemplate?: boolean;
|
|
|
|
firstRender?: boolean;
|
|
|
|
setFirstRender?: Dispatch<SetStateAction<boolean>>;
|
2023-04-13 02:10:23 +00:00
|
|
|
editable?: boolean;
|
2022-11-25 14:34:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const editorConfig = {
|
|
|
|
theme: ExampleTheme,
|
2023-06-06 11:59:57 +00:00
|
|
|
onError(error: Error) {
|
2022-11-25 14:34:55 +00:00
|
|
|
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) => {
|
2023-04-13 02:10:23 +00:00
|
|
|
const editable = props.editable ?? true;
|
2022-11-25 14:34:55 +00:00
|
|
|
return (
|
2023-04-13 02:10:23 +00:00
|
|
|
<div className="editor rounded-md">
|
|
|
|
<LexicalComposer initialConfig={{ ...editorConfig, editable }}>
|
|
|
|
<div className="editor-container rounded-md p-0">
|
2023-01-09 14:15:11 +00:00
|
|
|
<ToolbarPlugin
|
|
|
|
getText={props.getText}
|
|
|
|
setText={props.setText}
|
2023-04-13 02:10:23 +00:00
|
|
|
editable={editable}
|
2023-01-09 14:15:11 +00:00
|
|
|
excludedToolbarItems={props.excludedToolbarItems}
|
|
|
|
variables={props.variables}
|
2023-04-18 10:08:09 +00:00
|
|
|
updateTemplate={props.updateTemplate}
|
|
|
|
firstRender={props.firstRender}
|
|
|
|
setFirstRender={props.setFirstRender}
|
2023-01-09 14:15:11 +00:00
|
|
|
/>
|
2023-04-13 02:10:23 +00:00
|
|
|
<div
|
2023-05-15 13:56:26 +00:00
|
|
|
className={classNames("editor-inner scroll-bar", !editable && "!bg-subtle")}
|
2023-04-13 02:10:23 +00:00
|
|
|
style={{ height: props.height }}>
|
2023-01-25 01:08:10 +00:00
|
|
|
<RichTextPlugin
|
2023-05-15 13:56:26 +00:00
|
|
|
contentEditable={
|
|
|
|
<ContentEditable
|
|
|
|
readOnly={!editable}
|
|
|
|
style={{ height: props.height }}
|
|
|
|
className="editor-input"
|
|
|
|
/>
|
|
|
|
}
|
2023-07-11 09:26:43 +00:00
|
|
|
placeholder={
|
|
|
|
props?.placeholder ? (
|
|
|
|
<div className="text-muted -mt-11 p-3 text-sm">{props.placeholder}</div>
|
|
|
|
) : null
|
|
|
|
}
|
2023-05-12 16:28:14 +00:00
|
|
|
ErrorBoundary={LexicalErrorBoundary}
|
2023-01-25 01:08:10 +00:00
|
|
|
/>
|
2022-11-25 14:34:55 +00:00
|
|
|
<ListPlugin />
|
|
|
|
<LinkPlugin />
|
|
|
|
<AutoLinkPlugin />
|
2023-05-02 14:29:52 +00:00
|
|
|
<HistoryPlugin />
|
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
|
|
|
};
|