first commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
import { Icon } from "../../..";
|
||||
import { Dropdown, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "../../form/dropdown";
|
||||
|
||||
interface IAddVariablesDropdown {
|
||||
addVariable: (variable: string) => void;
|
||||
isTextEditor?: boolean;
|
||||
variables: string[];
|
||||
}
|
||||
|
||||
export const AddVariablesDropdown = (props: IAddVariablesDropdown) => {
|
||||
const { t } = useLocale();
|
||||
|
||||
return (
|
||||
<Dropdown>
|
||||
<DropdownMenuTrigger className="focus:bg-muted pt-[6px]">
|
||||
<div className="items-center ">
|
||||
{props.isTextEditor ? (
|
||||
<>
|
||||
<div className="hidden sm:flex">
|
||||
{t("add_variable")}
|
||||
<Icon name="chevron-down" className="ml-1 mt-[2px] h-4 w-4" />
|
||||
</div>
|
||||
<div className="block sm:hidden">+</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex">
|
||||
{t("add_variable")}
|
||||
<Icon name="chevron-down" className="ml-1 mt-[2px] h-4 w-4" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<div className="pb-1 pt-4">
|
||||
<div className="text-subtle mb-2 px-4 text-left text-xs">
|
||||
{t("add_dynamic_variables").toLocaleUpperCase()}
|
||||
</div>
|
||||
<div className="h-64 overflow-scroll md:h-80">
|
||||
{props.variables.map((variable) => (
|
||||
<DropdownMenuItem key={variable} className="hover:ring-0">
|
||||
<button
|
||||
key={variable}
|
||||
type="button"
|
||||
className="w-full px-4 py-2"
|
||||
onClick={() => props.addVariable(t(`${variable}_variable`))}>
|
||||
<div className="sm:grid sm:grid-cols-2">
|
||||
<div className="mr-3 text-left md:col-span-1">
|
||||
{`{${t(`${variable}_variable`).toUpperCase().replace(/ /g, "_")}}`}
|
||||
</div>
|
||||
<div className="text-default hidden text-left sm:col-span-1 sm:flex">
|
||||
{t(`${variable}_info`)}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,159 @@
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
import {
|
||||
LexicalTypeaheadMenuPlugin,
|
||||
TypeaheadOption,
|
||||
useBasicTypeaheadTriggerMatch,
|
||||
} from "@lexical/react/LexicalTypeaheadMenuPlugin";
|
||||
import type { LexicalEditor } from "lexical";
|
||||
import { TextNode } from "lexical";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
import { VariableNode, $createVariableNode } from "../nodes/VariableNode";
|
||||
|
||||
function $findAndTransformVariable(node: TextNode): null | TextNode {
|
||||
const text = node.getTextContent();
|
||||
|
||||
const regex = /\{[^}]+\}/g; // Regular expression to match {VARIABLE_NAME <whatever>}
|
||||
let match;
|
||||
|
||||
// Iterate over the text content
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
regex.lastIndex = i; // Set the regex search position to the current index
|
||||
match = regex.exec(text);
|
||||
|
||||
if (match !== null) {
|
||||
const matchedText = match[0]; // The entire matched text {VARIABLE_NAME <whatever here>}
|
||||
const matchIndex = match.index; // Start index of the matched text
|
||||
|
||||
// Ensure that we move the loop index past the current match
|
||||
i = matchIndex + matchedText.length - 1;
|
||||
|
||||
let targetNode;
|
||||
if (matchIndex === 0) {
|
||||
[targetNode] = node.splitText(matchIndex + matchedText.length);
|
||||
} else {
|
||||
[, targetNode] = node.splitText(matchIndex, matchIndex + matchedText.length);
|
||||
}
|
||||
const variableNode = $createVariableNode(matchedText);
|
||||
targetNode.replace(variableNode);
|
||||
return variableNode;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function useVariablesTransform(editor: LexicalEditor): void {
|
||||
useEffect(() => {
|
||||
if (!editor.hasNodes([VariableNode])) {
|
||||
console.error("VariableNode is not registered in the editor");
|
||||
return;
|
||||
}
|
||||
return editor.registerNodeTransform(TextNode, (node) => {
|
||||
let targetNode: TextNode | null = node;
|
||||
|
||||
while (targetNode !== null) {
|
||||
if (!targetNode.isSimpleText()) {
|
||||
return;
|
||||
}
|
||||
targetNode = $findAndTransformVariable(targetNode);
|
||||
}
|
||||
});
|
||||
}, [editor]);
|
||||
}
|
||||
|
||||
class VariableTypeaheadOption extends TypeaheadOption {
|
||||
name: string;
|
||||
constructor(name: string) {
|
||||
super(name);
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
interface AddVariablesPluginProps {
|
||||
variables: string[];
|
||||
}
|
||||
|
||||
export default function AddVariablesPlugin({ variables }: AddVariablesPluginProps): JSX.Element | null {
|
||||
const { t } = useLocale();
|
||||
const [editor] = useLexicalComposerContext();
|
||||
useVariablesTransform(editor);
|
||||
|
||||
const [queryString, setQueryString] = useState<string | null>(null);
|
||||
|
||||
const checkForTriggerMatch = useBasicTypeaheadTriggerMatch("{", {
|
||||
minLength: 0,
|
||||
});
|
||||
|
||||
const options = useMemo(() => {
|
||||
return variables
|
||||
.filter((variable) => variable.toLowerCase().includes(queryString?.toLowerCase() ?? ""))
|
||||
.map((result) => new VariableTypeaheadOption(result));
|
||||
}, [variables, queryString]);
|
||||
|
||||
const onSelectOption = useCallback(
|
||||
(selectedOption: VariableTypeaheadOption, nodeToReplace: TextNode | null, closeMenu: () => void) => {
|
||||
editor.update(() => {
|
||||
const variableNode = $createVariableNode(
|
||||
`{${t(`${selectedOption.name}_variable`).toUpperCase().replace(/ /g, "_")}}`
|
||||
);
|
||||
if (nodeToReplace) {
|
||||
nodeToReplace.replace(variableNode);
|
||||
}
|
||||
variableNode.select();
|
||||
closeMenu();
|
||||
});
|
||||
},
|
||||
[editor, t]
|
||||
);
|
||||
|
||||
return (
|
||||
<LexicalTypeaheadMenuPlugin<VariableTypeaheadOption>
|
||||
onQueryChange={setQueryString}
|
||||
onSelectOption={onSelectOption}
|
||||
triggerFn={checkForTriggerMatch}
|
||||
options={options}
|
||||
menuRenderFn={(anchorElementRef, { selectedIndex, selectOptionAndCleanUp, setHighlightedIndex }) =>
|
||||
anchorElementRef.current && options.length
|
||||
? createPortal(
|
||||
<div
|
||||
className={classNames(
|
||||
"shadow-dropdown bg-default border-subtle mt-5 w-64 overflow-hidden rounded-md border",
|
||||
"typeahead-popover" // class required by Lexical
|
||||
)}>
|
||||
<ul className="max-h-64 list-none overflow-y-scroll md:max-h-80">
|
||||
{options.map((option, index) => (
|
||||
<li
|
||||
id={`typeahead-item-${index}`}
|
||||
key={option.key}
|
||||
aria-selected={selectedIndex === index}
|
||||
tabIndex={-1}
|
||||
className="focus:ring-brand-800 hover:bg-subtle hover:text-emphasis text-default aria-selected:bg-subtle aria-selected:text-emphasis cursor-pointer px-4 py-2 text-sm outline-none ring-inset first-of-type:rounded-t-[inherit] last-of-type:rounded-b-[inherit] focus:outline-none focus:ring-1"
|
||||
ref={option.setRefElement}
|
||||
role="option"
|
||||
onClick={() => {
|
||||
setHighlightedIndex(index);
|
||||
selectOptionAndCleanUp(option);
|
||||
}}
|
||||
onMouseEnter={() => {
|
||||
setHighlightedIndex(index);
|
||||
}}>
|
||||
<p className="text-sm font-semibold">
|
||||
{`{${t(`${option.name}_variable`).toUpperCase().replace(/ /g, "_")}}`}
|
||||
</p>
|
||||
<span className="text-default text-sm">{t(`${option.name}_info`)}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>,
|
||||
anchorElementRef.current
|
||||
)
|
||||
: null
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { AutoLinkPlugin } from "@lexical/react/LexicalAutoLinkPlugin";
|
||||
|
||||
const URL_MATCHER =
|
||||
/((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
|
||||
|
||||
const EMAIL_MATCHER =
|
||||
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
|
||||
|
||||
const MATCHERS = [
|
||||
(text: string) => {
|
||||
const match = URL_MATCHER.exec(text);
|
||||
return (
|
||||
match && {
|
||||
index: match.index,
|
||||
length: match[0].length,
|
||||
text: match[0],
|
||||
url: match[0],
|
||||
}
|
||||
);
|
||||
},
|
||||
(text: string) => {
|
||||
const match = EMAIL_MATCHER.exec(text);
|
||||
return (
|
||||
match && {
|
||||
index: match.index,
|
||||
length: match[0].length,
|
||||
text: match[0],
|
||||
url: `mailto:${match[0]}`,
|
||||
}
|
||||
);
|
||||
},
|
||||
];
|
||||
|
||||
export default function PlaygroundAutoLinkPlugin() {
|
||||
return <AutoLinkPlugin matchers={MATCHERS} />;
|
||||
}
|
||||
519
calcom/packages/ui/components/editor/plugins/ToolbarPlugin.tsx
Normal file
519
calcom/packages/ui/components/editor/plugins/ToolbarPlugin.tsx
Normal file
@@ -0,0 +1,519 @@
|
||||
import { $generateHtmlFromNodes, $generateNodesFromDOM } from "@lexical/html";
|
||||
import { $isLinkNode, TOGGLE_LINK_COMMAND } from "@lexical/link";
|
||||
import {
|
||||
$isListNode,
|
||||
INSERT_ORDERED_LIST_COMMAND,
|
||||
INSERT_UNORDERED_LIST_COMMAND,
|
||||
ListNode,
|
||||
REMOVE_LIST_COMMAND,
|
||||
} from "@lexical/list";
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
import { $createHeadingNode, $isHeadingNode } from "@lexical/rich-text";
|
||||
import { $isAtNodeEnd, $wrapNodes } from "@lexical/selection";
|
||||
import { $getNearestNodeOfType, mergeRegister } from "@lexical/utils";
|
||||
import classNames from "classnames";
|
||||
import type { EditorState, GridSelection, LexicalEditor, NodeSelection, RangeSelection } from "lexical";
|
||||
import {
|
||||
$createParagraphNode,
|
||||
$getRoot,
|
||||
$getSelection,
|
||||
$insertNodes,
|
||||
$isRangeSelection,
|
||||
FORMAT_TEXT_COMMAND,
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
} from "lexical";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
|
||||
import { Icon } from "../../..";
|
||||
import { Button } from "../../button";
|
||||
import { Dropdown, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "../../form/dropdown";
|
||||
import type { TextEditorProps } from "../Editor";
|
||||
import { AddVariablesDropdown } from "./AddVariablesDropdown";
|
||||
|
||||
const LowPriority = 1;
|
||||
|
||||
const supportedBlockTypes = new Set(["paragraph", "h1", "h2", "ul", "ol"]);
|
||||
|
||||
interface BlockType {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
const blockTypeToBlockName: BlockType = {
|
||||
paragraph: "Normal",
|
||||
ol: "Numbered List",
|
||||
ul: "Bulleted List",
|
||||
h1: "Large Heading",
|
||||
h2: "Small Heading",
|
||||
};
|
||||
|
||||
function positionEditorElement(editor: HTMLInputElement, rect: DOMRect | null) {
|
||||
if (rect === null) {
|
||||
editor.style.opacity = "0";
|
||||
editor.style.top = "-1000px";
|
||||
editor.style.left = "-1000px";
|
||||
} else {
|
||||
editor.style.opacity = "1";
|
||||
editor.style.top = `${rect.top + rect.height + window.pageYOffset + 10}px`;
|
||||
editor.style.left = `${rect.left + window.pageXOffset - editor.offsetWidth / 2 + rect.width / 2}px`;
|
||||
}
|
||||
}
|
||||
|
||||
function FloatingLinkEditor({ editor }: { editor: LexicalEditor }) {
|
||||
const editorRef = useRef<HTMLInputElement>(null);
|
||||
const mouseDownRef = useRef(false);
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const [linkUrl, setLinkUrl] = useState("");
|
||||
const [isEditMode, setEditMode] = useState(true);
|
||||
const [lastSelection, setLastSelection] = useState<RangeSelection | NodeSelection | GridSelection | null>(
|
||||
null
|
||||
);
|
||||
|
||||
const updateLinkEditor = useCallback(() => {
|
||||
const selection = $getSelection();
|
||||
if ($isRangeSelection(selection)) {
|
||||
const node = getSelectedNode(selection);
|
||||
const parent = node.getParent();
|
||||
if ($isLinkNode(parent)) {
|
||||
setLinkUrl(parent.getURL());
|
||||
} else if ($isLinkNode(node)) {
|
||||
setLinkUrl(node.getURL());
|
||||
} else {
|
||||
setLinkUrl("");
|
||||
}
|
||||
}
|
||||
const editorElem = editorRef.current;
|
||||
const nativeSelection = window.getSelection();
|
||||
const activeElement = document.activeElement;
|
||||
|
||||
if (editorElem === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rootElement = editor.getRootElement();
|
||||
if (
|
||||
selection !== null &&
|
||||
!nativeSelection?.isCollapsed &&
|
||||
rootElement !== null &&
|
||||
rootElement.contains(nativeSelection?.anchorNode || null)
|
||||
) {
|
||||
const domRange = nativeSelection?.getRangeAt(0);
|
||||
let rect: DOMRect | undefined;
|
||||
if (nativeSelection?.anchorNode === rootElement) {
|
||||
let inner: Element = rootElement;
|
||||
while (inner.firstElementChild != null) {
|
||||
inner = inner.firstElementChild;
|
||||
}
|
||||
rect = inner.getBoundingClientRect();
|
||||
} else {
|
||||
rect = domRange?.getBoundingClientRect();
|
||||
}
|
||||
if (!mouseDownRef.current) {
|
||||
positionEditorElement(editorElem, rect || null);
|
||||
}
|
||||
|
||||
setLastSelection(selection);
|
||||
} else if (!activeElement || activeElement.className !== "link-input") {
|
||||
positionEditorElement(editorElem, null);
|
||||
setLastSelection(null);
|
||||
setEditMode(false);
|
||||
setLinkUrl("");
|
||||
}
|
||||
|
||||
return true;
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
return mergeRegister(
|
||||
editor.registerUpdateListener(({ editorState }: { editorState: EditorState }) => {
|
||||
editorState.read(() => {
|
||||
updateLinkEditor();
|
||||
});
|
||||
}),
|
||||
|
||||
editor.registerCommand(
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
() => {
|
||||
updateLinkEditor();
|
||||
return true;
|
||||
},
|
||||
LowPriority
|
||||
)
|
||||
);
|
||||
}, [editor, updateLinkEditor]);
|
||||
|
||||
useEffect(() => {
|
||||
editor.getEditorState().read(() => {
|
||||
updateLinkEditor();
|
||||
});
|
||||
}, [editor, updateLinkEditor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isEditMode && inputRef.current) {
|
||||
inputRef.current.focus();
|
||||
}
|
||||
}, [isEditMode]);
|
||||
|
||||
return (
|
||||
<div ref={editorRef} className="link-editor">
|
||||
{isEditMode ? (
|
||||
<input
|
||||
ref={inputRef}
|
||||
className="link-input"
|
||||
value={linkUrl}
|
||||
onChange={(event) => {
|
||||
setLinkUrl(event.target.value);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
if (lastSelection !== null) {
|
||||
if (linkUrl !== "") {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl);
|
||||
}
|
||||
setEditMode(false);
|
||||
}
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
setEditMode(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<div className="link-input">
|
||||
<a href={linkUrl} target="_blank" rel="noopener noreferrer">
|
||||
{linkUrl}
|
||||
</a>
|
||||
<div
|
||||
className="link-edit"
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={() => {
|
||||
setEditMode(true);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function getSelectedNode(selection: RangeSelection) {
|
||||
const anchor = selection.anchor;
|
||||
const focus = selection.focus;
|
||||
const anchorNode = selection.anchor.getNode();
|
||||
const focusNode = selection.focus.getNode();
|
||||
if (anchorNode === focusNode) {
|
||||
return anchorNode;
|
||||
}
|
||||
const isBackward = selection.isBackward();
|
||||
if (isBackward) {
|
||||
return $isAtNodeEnd(focus) ? anchorNode : focusNode;
|
||||
} else {
|
||||
return $isAtNodeEnd(anchor) ? focusNode : anchorNode;
|
||||
}
|
||||
}
|
||||
|
||||
export default function ToolbarPlugin(props: TextEditorProps) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const toolbarRef = useRef(null);
|
||||
const [blockType, setBlockType] = useState("paragraph");
|
||||
const [isLink, setIsLink] = useState(false);
|
||||
const [isBold, setIsBold] = useState(false);
|
||||
const [isItalic, setIsItalic] = useState(false);
|
||||
|
||||
const formatParagraph = () => {
|
||||
if (blockType !== "paragraph") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createParagraphNode());
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const formatLargeHeading = () => {
|
||||
if (blockType !== "h1") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createHeadingNode("h1"));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const formatSmallHeading = () => {
|
||||
if (blockType !== "h2") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createHeadingNode("h2"));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const formatBulletList = () => {
|
||||
if (blockType !== "ul") {
|
||||
editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined);
|
||||
} else {
|
||||
editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined);
|
||||
}
|
||||
};
|
||||
|
||||
const formatNumberedList = () => {
|
||||
if (blockType !== "ol") {
|
||||
editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined);
|
||||
} else {
|
||||
editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined);
|
||||
}
|
||||
};
|
||||
|
||||
const format = (newBlockType: string) => {
|
||||
switch (newBlockType) {
|
||||
case "paragraph":
|
||||
formatParagraph();
|
||||
break;
|
||||
case "ul":
|
||||
formatBulletList();
|
||||
break;
|
||||
case "ol":
|
||||
formatNumberedList();
|
||||
break;
|
||||
case "h1":
|
||||
formatLargeHeading();
|
||||
break;
|
||||
case "h2":
|
||||
formatSmallHeading();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
const updateToolbar = useCallback(() => {
|
||||
const selection = $getSelection();
|
||||
if ($isRangeSelection(selection)) {
|
||||
const anchorNode = selection.anchor.getNode();
|
||||
const element = anchorNode.getKey() === "root" ? anchorNode : anchorNode.getTopLevelElementOrThrow();
|
||||
const elementKey = element.getKey();
|
||||
const elementDOM = editor.getElementByKey(elementKey);
|
||||
if (elementDOM !== null) {
|
||||
if ($isListNode(element)) {
|
||||
const parentList = $getNearestNodeOfType(anchorNode, ListNode);
|
||||
const type = parentList ? parentList.getTag() : element.getTag();
|
||||
setBlockType(type);
|
||||
} else {
|
||||
const type = $isHeadingNode(element) ? element.getTag() : element.getType();
|
||||
setBlockType(type);
|
||||
}
|
||||
}
|
||||
setIsBold(selection.hasFormat("bold"));
|
||||
setIsItalic(selection.hasFormat("italic"));
|
||||
|
||||
const node = getSelectedNode(selection);
|
||||
const parent = node.getParent();
|
||||
if ($isLinkNode(parent) || $isLinkNode(node)) {
|
||||
setIsLink(true);
|
||||
} else {
|
||||
setIsLink(false);
|
||||
}
|
||||
}
|
||||
}, [editor]);
|
||||
|
||||
const addVariable = (variable: string) => {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
if ($isRangeSelection(selection)) {
|
||||
editor.update(() => {
|
||||
const formatedVariable = `{${variable.toUpperCase().replace(/ /g, "_")}}`;
|
||||
selection?.insertRawText(formatedVariable);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.firstRender) {
|
||||
editor.update(() => {
|
||||
const root = $getRoot();
|
||||
if (root) {
|
||||
editor.update(() => {
|
||||
const parser = new DOMParser();
|
||||
// Create a new TextNode
|
||||
const dom = parser.parseFromString(props.getText(), "text/html");
|
||||
|
||||
const nodes = $generateNodesFromDOM(editor, dom);
|
||||
const paragraph = $createParagraphNode();
|
||||
root.clear().append(paragraph);
|
||||
paragraph.select();
|
||||
$insertNodes(nodes);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [props.updateTemplate]);
|
||||
|
||||
useEffect(() => {
|
||||
if (props.setFirstRender) {
|
||||
props.setFirstRender(false);
|
||||
editor.update(() => {
|
||||
const parser = new DOMParser();
|
||||
const dom = parser.parseFromString(props.getText(), "text/html");
|
||||
|
||||
const nodes = $generateNodesFromDOM(editor, dom);
|
||||
|
||||
$getRoot().select();
|
||||
try {
|
||||
$insertNodes(nodes);
|
||||
} catch (e: unknown) {
|
||||
// resolves: "topLevelElement is root node at RangeSelection.insertNodes"
|
||||
// @see https://stackoverflow.com/questions/73094258/setting-editor-from-html
|
||||
const paragraphNode = $createParagraphNode();
|
||||
nodes.forEach((n) => paragraphNode.append(n));
|
||||
$getRoot().append(paragraphNode);
|
||||
}
|
||||
|
||||
editor.registerUpdateListener(({ editorState, prevEditorState }) => {
|
||||
editorState.read(() => {
|
||||
const textInHtml = $generateHtmlFromNodes(editor).replace(/</g, "<").replace(/>/g, ">");
|
||||
props.setText(
|
||||
textInHtml.replace(
|
||||
/<p\s+class="editor-paragraph"[^>]*>\s*<br>\s*<\/p>/g,
|
||||
"<p class='editor-paragraph'></p>"
|
||||
)
|
||||
);
|
||||
});
|
||||
if (!prevEditorState._selection) editor.blur();
|
||||
});
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
return mergeRegister(
|
||||
editor.registerUpdateListener(({ editorState }) => {
|
||||
editorState.read(() => {
|
||||
updateToolbar();
|
||||
});
|
||||
}),
|
||||
editor.registerCommand(
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
(_payload, _newEditor) => {
|
||||
updateToolbar();
|
||||
return false;
|
||||
},
|
||||
LowPriority
|
||||
)
|
||||
);
|
||||
}, [editor, updateToolbar]);
|
||||
|
||||
const insertLink = useCallback(() => {
|
||||
if (!isLink) {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, "https://");
|
||||
} else {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
||||
}
|
||||
}, [editor, isLink]);
|
||||
|
||||
if (!props.editable) return <></>;
|
||||
return (
|
||||
<div className="toolbar flex" ref={toolbarRef}>
|
||||
<>
|
||||
{!props.excludedToolbarItems?.includes("blockType") && supportedBlockTypes.has(blockType) && (
|
||||
<>
|
||||
<Dropdown>
|
||||
<DropdownMenuTrigger className="text-subtle">
|
||||
<>
|
||||
<span className={`icon${blockType}`} />
|
||||
<span className="text text-default hidden sm:flex">
|
||||
{blockTypeToBlockName[blockType as keyof BlockType]}
|
||||
</span>
|
||||
<Icon name="chevron-down" className="text-default ml-2 h-4 w-4" />
|
||||
</>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start">
|
||||
{Object.keys(blockTypeToBlockName).map((key) => {
|
||||
return (
|
||||
<DropdownMenuItem key={key} className="outline-none hover:ring-0 focus:ring-0">
|
||||
<Button
|
||||
color="minimal"
|
||||
type="button"
|
||||
onClick={() => format(key)}
|
||||
className={classNames(
|
||||
"w-full rounded-none focus:ring-0",
|
||||
blockType === key ? "bg-subtle w-full" : ""
|
||||
)}>
|
||||
<>
|
||||
<span className={`icon block-type ${key}`} />
|
||||
<span>{blockTypeToBlockName[key]}</span>
|
||||
</>
|
||||
</Button>
|
||||
</DropdownMenuItem>
|
||||
);
|
||||
})}
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
</>
|
||||
)}
|
||||
|
||||
<>
|
||||
{!props.excludedToolbarItems?.includes("bold") && (
|
||||
<Button
|
||||
color="minimal"
|
||||
variant="icon"
|
||||
type="button"
|
||||
StartIcon="bold"
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "bold");
|
||||
}}
|
||||
className={isBold ? "bg-subtle" : ""}
|
||||
/>
|
||||
)}
|
||||
{!props.excludedToolbarItems?.includes("italic") && (
|
||||
<Button
|
||||
color="minimal"
|
||||
variant="icon"
|
||||
type="button"
|
||||
StartIcon="italic"
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "italic");
|
||||
}}
|
||||
className={isItalic ? "bg-subtle" : ""}
|
||||
/>
|
||||
)}
|
||||
{!props.excludedToolbarItems?.includes("link") && (
|
||||
<>
|
||||
<Button
|
||||
color="minimal"
|
||||
variant="icon"
|
||||
type="button"
|
||||
StartIcon="link"
|
||||
onClick={insertLink}
|
||||
className={isLink ? "bg-subtle" : ""}
|
||||
/>
|
||||
{isLink && createPortal(<FloatingLinkEditor editor={editor} />, document.body)}{" "}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
{props.variables && (
|
||||
<div className="ml-auto">
|
||||
<AddVariablesDropdown
|
||||
addVariable={addVariable}
|
||||
isTextEditor={true}
|
||||
variables={props.variables || []}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user