2
0

first commit

This commit is contained in:
2024-08-09 00:39:27 +02:00
commit 79688abe2e
5698 changed files with 497838 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
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";
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
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 type { Dispatch, SetStateAction } from "react";
import { classNames } from "@calcom/lib";
import ExampleTheme from "./ExampleTheme";
import { VariableNode } from "./nodes/VariableNode";
import AddVariablesPlugin from "./plugins/AddVariablesPlugin";
import AutoLinkPlugin from "./plugins/AutoLinkPlugin";
import ToolbarPlugin from "./plugins/ToolbarPlugin";
import "./stylesEditor.css";
/*
Detault toolbar items:
- blockType
- bold
- italic
- link
*/
export type TextEditorProps = {
getText: () => string;
setText: (text: string) => void;
excludedToolbarItems?: string[];
variables?: string[];
height?: string;
placeholder?: string;
disableLists?: boolean;
updateTemplate?: boolean;
firstRender?: boolean;
setFirstRender?: Dispatch<SetStateAction<boolean>>;
editable?: boolean;
};
const editorConfig = {
theme: ExampleTheme,
onError(error: Error) {
throw error;
},
namespace: "",
nodes: [
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
CodeHighlightNode,
TableNode,
TableCellNode,
TableRowNode,
AutoLinkNode,
LinkNode,
VariableNode,
],
};
export const Editor = (props: TextEditorProps) => {
const editable = props.editable ?? true;
return (
<div className="editor rounded-md">
<LexicalComposer initialConfig={{ ...editorConfig, editable }}>
<div className="editor-container hover:border-emphasis focus-within:ring-brand-default rounded-md p-0 transition focus-within:ring-2">
<ToolbarPlugin
getText={props.getText}
setText={props.setText}
editable={editable}
excludedToolbarItems={props.excludedToolbarItems}
variables={props.variables}
updateTemplate={props.updateTemplate}
firstRender={props.firstRender}
setFirstRender={props.setFirstRender}
/>
<div
className={classNames("editor-inner scroll-bar", !editable && "!bg-subtle")}
style={{ height: props.height }}>
<RichTextPlugin
contentEditable={
<ContentEditable
readOnly={!editable}
style={{ height: props.height }}
className="editor-input"
/>
}
placeholder={
props?.placeholder ? (
<div className="text-muted -mt-11 p-3 text-sm">{props.placeholder}</div>
) : null
}
ErrorBoundary={LexicalErrorBoundary}
/>
<ListPlugin />
<LinkPlugin />
<AutoLinkPlugin />
{props?.variables ? <AddVariablesPlugin variables={props.variables} /> : null}
<HistoryPlugin />
<MarkdownShortcutPlugin
transformers={
props.disableLists
? TRANSFORMERS.filter((value, index) => {
if (index !== 3 && index !== 4) return value;
})
: TRANSFORMERS
}
/>
</div>
</div>
</LexicalComposer>
</div>
);
};

View File

@@ -0,0 +1,25 @@
const exampleTheme = {
placeholder: "editor-placeholder",
paragraph: "editor-paragraph",
heading: {
h1: "editor-heading-h1",
h2: "editor-heading-h2",
h6: "editor-heading-h6",
},
list: {
nested: {
listitem: "editor-nested-listitem",
},
ol: "editor-list-ol",
ul: "editor-list-ul",
listitem: "editor-listitem",
},
image: "editor-image",
link: "editor-link",
text: {
bold: "editor-text-bold",
italic: "editor-text-italic",
},
};
export default exampleTheme;

View File

@@ -0,0 +1,68 @@
import { Canvas, Meta, Story } from "@storybook/addon-docs";
import {
Examples,
Example,
Title,
VariantsTable,
CustomArgsTable,
VariantRow,
} from "@calcom/storybook/components";
import { Editor } from "./Editor";
<Meta title="UI/Editor" component={Editor} />
<Title title="Editor" suffix="Brief" subtitle="Version 1.0 — Last Update: 16 Aug 2023" />
## Definition
The `Editor` component is a versatile rich text editor that provides a customizable environment for creating and editing content with various formatting options and plugins.
## Structure
The `Editor` component offers a flexible rich text editing interface, complete with a configurable toolbar
<CustomArgsTable of={Editor} />
<Examples title="States">
<Example title="Default">
<Editor setText={() => {}} getText={() => "Text"} />
</Example>
<Example title="With placeholder">
<Editor setText={() => {}} getText={() => ""} placeholder="placeholder" />
</Example>
<Example title="With variables">
<Editor setText={() => {}} getText={() => "Text"} variables={["variable1", "variable2", "variable3"]} />
</Example>
</Examples>
<Title offset title="Editor" suffix="Variants" />
<Canvas>
<Story
name="Editor"
args={{
setText: () => {},
getText: () => "Text",
editable: true,
}}
argTypes={{
excludedToolbarItems: { control: { type: "check", options: ["blockType", "bold", "italic", "link"] } },
variables: { control: { type: "check", options: ["variable1", "variable2", "variable3"] } },
height: { control: { type: "text" } },
placeholder: { control: { type: "text" } },
disableLists: { control: { type: "boolean" } },
updateTemplate: { control: { type: "boolean" } },
firstRender: { control: { type: "boolean" } },
editable: { control: { type: "boolean" } },
}}>
{(props) => (
<VariantsTable titles={["Default"]} columnMinWidth={150}>
<VariantRow>
<Editor {...props} />
</VariantRow>
</VariantsTable>
)}
</Story>
</Canvas>

View File

@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
</svg>

After

Width:  |  Height:  |  Size: 404 B

View File

@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-list-ol" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M5 11.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5z"/>
<path d="M1.713 11.865v-.474H2c.217 0 .363-.137.363-.317 0-.185-.158-.31-.361-.31-.223 0-.367.152-.373.31h-.59c.016-.467.373-.787.986-.787.588-.002.954.291.957.703a.595.595 0 0 1-.492.594v.033a.615.615 0 0 1 .569.631c.003.533-.502.8-1.051.8-.656 0-1-.37-1.008-.794h.582c.008.178.186.306.422.309.254 0 .424-.145.422-.35-.002-.195-.155-.348-.414-.348h-.3zm-.004-4.699h-.604v-.035c0-.408.295-.844.958-.844.583 0 .96.326.96.756 0 .389-.257.617-.476.848l-.537.572v.03h1.054V9H1.143v-.395l.957-.99c.138-.142.293-.304.293-.508 0-.18-.147-.32-.342-.32a.33.33 0 0 0-.342.338v.041zM2.564 5h-.635V2.924h-.031l-.598.42v-.567l.629-.443h.635V5z"/>
</svg>

After

Width:  |  Height:  |  Size: 984 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-list-ul" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M5 11.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zm-3 1a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm0 4a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm0 4a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
</svg>

After

Width:  |  Height:  |  Size: 448 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-fill" viewBox="0 0 16 16">
<path d="M12.854.146a.5.5 0 0 0-.707 0L10.5 1.793 14.207 5.5l1.647-1.646a.5.5 0 0 0 0-.708l-3-3zm.646 6.061L9.793 2.5 3.293 9H3.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.207l6.5-6.5zm-7.468 7.468A.5.5 0 0 1 6 13.5V13h-.5a.5.5 0 0 1-.5-.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.5-.5V10h-.5a.499.499 0 0 1-.175-.032l-.179.178a.5.5 0 0 0-.11.168l-2 5a.5.5 0 0 0 .65.65l5-2a.5.5 0 0 0 .168-.11l.178-.178z"/>
</svg>

After

Width:  |  Height:  |  Size: 590 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-paragraph" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm4-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5z"/>
</svg>

After

Width:  |  Height:  |  Size: 418 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-bold" viewBox="0 0 16 16">
<path d="M8.21 13c2.106 0 3.412-1.087 3.412-2.823 0-1.306-.984-2.283-2.324-2.386v-.055a2.176 2.176 0 0 0 1.852-2.14c0-1.51-1.162-2.46-3.014-2.46H3.843V13H8.21zM5.908 4.674h1.696c.963 0 1.517.451 1.517 1.244 0 .834-.629 1.32-1.73 1.32H5.908V4.673zm0 6.788V8.598h1.73c1.217 0 1.88.492 1.88 1.415 0 .943-.643 1.449-1.832 1.449H5.907z"/>
</svg>

After

Width:  |  Height:  |  Size: 471 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-h1" viewBox="0 0 16 16">
<path d="M8.637 13V3.669H7.379V7.62H2.758V3.67H1.5V13h1.258V8.728h4.62V13h1.259zm5.329 0V3.669h-1.244L10.5 5.316v1.265l2.16-1.565h.062V13h1.244z"/>
</svg>

After

Width:  |  Height:  |  Size: 283 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-h2" viewBox="0 0 16 16">
<path d="M7.638 13V3.669H6.38V7.62H1.759V3.67H.5V13h1.258V8.728h4.62V13h1.259zm3.022-6.733v-.048c0-.889.63-1.668 1.716-1.668.957 0 1.675.608 1.675 1.572 0 .855-.554 1.504-1.067 2.085l-3.513 3.999V13H15.5v-1.094h-4.245v-.075l2.481-2.844c.875-.998 1.586-1.784 1.586-2.953 0-1.463-1.155-2.556-2.919-2.556-1.941 0-2.966 1.326-2.966 2.74v.049h1.223z"/>
</svg>

After

Width:  |  Height:  |  Size: 483 B

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-italic" viewBox="0 0 16 16">
<path d="M7.991 11.674 9.53 4.455c.123-.595.246-.71 1.347-.807l.11-.52H7.211l-.11.52c1.06.096 1.128.212 1.005.807L6.57 11.674c-.123.595-.246.71-1.346.806l-.11.52h3.774l.11-.52c-1.06-.095-1.129-.211-1.006-.806z"/>
</svg>

After

Width:  |  Height:  |  Size: 352 B

View File

@@ -0,0 +1,2 @@
export { Editor } from "./Editor";
export { AddVariablesDropdown } from "./plugins/AddVariablesDropdown";

View File

@@ -0,0 +1,53 @@
import type { EditorConfig, LexicalNode, NodeKey, SerializedTextNode } from "lexical";
import { $applyNodeReplacement, TextNode } from "lexical";
export class VariableNode extends TextNode {
static getType(): string {
return "variable";
}
static clone(node: VariableNode): VariableNode {
return new VariableNode(node.__text, node.__key);
}
constructor(text: string, key?: NodeKey) {
super(text, key);
}
createDOM(config: EditorConfig): HTMLElement {
const dom = super.createDOM(config);
dom.className = "bg-info";
dom.setAttribute("data-lexical-variable", "true");
return dom;
}
exportJSON(): SerializedTextNode {
return {
...super.exportJSON(),
type: "variable",
version: 1,
};
}
isTextEntity(): true {
return true;
}
canInsertTextBefore(): boolean {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
}
export function $createVariableNode(text = ""): VariableNode {
const node = new VariableNode(text);
node.setMode("segmented").toggleDirectionless();
return $applyNodeReplacement(node);
}
export function $isVariableNode(node: LexicalNode | null | undefined): node is VariableNode {
return node instanceof VariableNode;
}

View File

@@ -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>
);
};

View File

@@ -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
}
/>
);
}

View File

@@ -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} />;
}

View 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(/&lt;/g, "<").replace(/&gt;/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>
);
}

View File

@@ -0,0 +1,398 @@
.editor li {
padding-left: 1.28571429em;
text-indent: -1.28571429em;
}
.editor ul {
list-style: disc inside;
}
.editor ol {
list-style: decimal inside;
}
.editor-container {
border-radius: 6px;
position: relative;
line-height: 20px;
font-weight: 400;
text-align: left;
border-color: var(--cal-border);
border-width: 1px;
padding: 1px;
}
.editor-inner {
background: var(--cal-bg);
position: relative;
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
overflow: scroll;
resize: vertical;
height: auto;
min-height: 40px;
}
.editor-input {
height: auto;
font-size: 14px;
position: relative;
tab-size: 1;
outline: 0;
padding: 10px 10px;
outline: none;
}
.editor-text-bold {
font-weight: bold;
}
.editor-text-italic {
font-style: italic;
}
// h6 is only used for emoji links
.editor-heading-h6 {
}
h6 a {
font-size: 20px !important;
text-decoration: none !important;
}
.editor-link {
color: rgb(33, 111, 219);
text-decoration: underline;
}
.editor-tokenFunction {
color: #dd4a68;
}
.editor-paragraph {
margin: 0;
margin-top:14px;
position: relative;
color: var(--cal-text);
}
.editor-paragraph:last-child {
margin-bottom: 0;
}
.editor-heading-h1 {
font-size: 25px;
font-weight: 400;
margin-bottom: 20px;
font-weight: bold;
}
.editor-heading-h2 {
font-size: 20px;
font-weight: bold;
margin-bottom: 20px;
}
.editor-list-ul {
margin-bottom: 12px;
}
.editor-list-ol {
margin-bottom: 12px;
}
.editor-listitem {
margin: 0px 32px;
}
.editor-nested-listitem {
list-style-type: none;
}
pre::-webkit-scrollbar {
background: transparent;
width: 10px;
}
pre::-webkit-scrollbar-thumb {
background: #999;
}
.toolbar {
display: flex;
margin-bottom: 1px;
background: var(--cal-bg);
padding: 2px;
border-top-left-radius: 6px;
border-top-right-radius: 6px;
vertical-align: middle;
}
.toolbar button.toolbar-item {
display: flex;
border: 0;
padding: 8px;
cursor: pointer;
justify-self: center;
}
.toolbar button.toolbar-item.spaced {
margin-right: 2px;
}
.icon.paragraph {
background-image: url(images/icons/text-paragraph.svg);
}
.toolbar button.toolbar-item.active {
background-color: var(--cal-bg-inverted);
color: var(--cal-text-inverted);
}
.toolbar button.toolbar-item.active i {
opacity: 1;
}
.toolbar button.toolbar-item:not(.active):not([disabled]) {
background-color: var(--cal-bg);
color: var(--cal-text);
}
.toolbar button.toolbar-item:not(.active):hover {
color: var(--cal-text-default);
background-color: var(--cal-bg-inverted);
}
.toolbar select.toolbar-item {
border: 0;
display: flex;
background: none;
border-radius: 10px;
padding: 8px;
vertical-align: middle;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: 70px;
font-size: 14px;
color: #777;
text-overflow: ellipsis;
}
.toolbar .toolbar-item .text {
line-height: 20px;
width: 200px;
vertical-align: middle;
font-size: 14px;
text-overflow: ellipsis;
width: 70px;
overflow: hidden;
height: 20px;
text-align: left;
}
.toolbar .toolbar-item .icon {
display: flex;
color: var(--cal-text);
}
#block-controls button:hover {
background-color: #efefef;
}
#block-controls button:focus-visible {
border-color: blue;
}
.dropdown {
z-index: 5;
display: block;
position: absolute;
box-shadow: 0 12px 28px 0 rgba(0, 0, 0, 0.2), 0 2px 4px 0 rgba(0, 0, 0, 0.1),
inset 0 0 0 1px rgba(255, 255, 255, 0.5);
border-radius: 8px;
min-width: 100px;
min-height: 40px;
background-color: #fff;
}
.dropdown .item {
margin: 0 8px 0 8px;
padding: 8px;
color: #050505;
cursor: pointer;
line-height: 16px;
font-size: 15px;
display: flex;
align-content: center;
flex-direction: row;
flex-shrink: 0;
justify-content: space-between;
background-color: #fff;
border-radius: 8px;
border: 0;
min-width: 268px;
}
.dropdown .item .active {
display: flex;
width: 20px;
height: 20px;
background-size: contain;
}
.dropdown .item:first-child {
margin-top: 8px;
}
.dropdown .item:last-child {
margin-bottom: 8px;
}
.dropdown .item:hover {
background-color: #eee;
}
.dropdown .item .text {
display: flex;
line-height: 20px;
flex-grow: 1;
width: 200px;
}
.dropdown .item .icon {
display: flex;
width: 20px;
height: 20px;
user-select: none;
margin-right: 12px;
line-height: 16px;
background-size: contain;
}
.link-editor {
position: absolute;
z-index: 100;
top: -10000px;
left: -10000px;
margin-top: -6px;
max-width: 300px;
width: 100%;
opacity: 0;
background-color: #fff;
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3);
border-radius: 8px;
transition: opacity 0.5s;
}
.link-editor .link-input {
display: block;
width: calc(100% - 24px);
box-sizing: border-box;
margin: 8px 12px;
padding: 8px 12px;
border-radius: 15px;
background-color: #eee;
font-size: 15px;
color: rgb(5, 5, 5);
border: 0;
outline: 0;
position: relative;
font-family: inherit;
}
.link-editor div.link-edit {
background-image: url(images/icons/pencil-fill.svg);
background-size: 16px;
background-position: center;
background-repeat: no-repeat;
width: 35px;
vertical-align: -0.25em;
position: absolute;
right: 0;
top: 0;
bottom: 0;
cursor: pointer;
}
.link-editor .link-input a {
color: rgb(33, 111, 219);
text-decoration: none;
display: block;
white-space: nowrap;
overflow: hidden;
margin-right: 30px;
text-overflow: ellipsis;
}
.link-editor .link-input a:hover {
text-decoration: underline;
}
.link-editor .button {
width: 20px;
height: 20px;
display: inline-block;
padding: 6px;
border-radius: 8px;
cursor: pointer;
margin: 0 2px;
}
.link-editor .button.hovered {
width: 20px;
height: 20px;
display: inline-block;
background-color: #eee;
}
.link-editor .button i,
.actions i {
background-size: contain;
display: inline-block;
height: 20px;
width: 20px;
vertical-align: -0.25em;
}
i,
.icon {
fill: currentColor;
}
.icon.paragraph {
background-image: url(images/icons/text-paragraph.svg);
}
.icon.large-heading,
.icon.h1 {
background-image: url(images/icons/type-h1.svg);
}
.icon.small-heading,
.icon.h2 {
background-image: url(images/icons/type-h2.svg);
}
.icon.bullet-list,
.icon.ul {
background-image: url(images/icons/list-ul.svg);
}
.icon.numbered-list,
.icon.ol {
background-image: url(images/icons/list-ol.svg);
}
i.bold {
background-image: url(images/icons/type-bold.svg);
}
i.italic {
background-image: url(images/icons/type-italic.svg);
}
i.link {
background-image: url(images/icons/link.svg);
}