"use client" import * as React from "react" import { createPortal } from "react-dom" import { EditorContent, useEditor, useEditorState } from "@tiptap/react" import { BubbleMenu } from "@tiptap/react/menus" import Placeholder from "@tiptap/extension-placeholder" import StarterKit from "@tiptap/starter-kit" import { cn } from "../../lib/utils" import { Button } from "./button" import { Input } from "./input" import { Kbd } from "./kbd" import { Popover, PopoverContent, PopoverTrigger, } from "./popover" import { Tip } from "./tip" import { useMergeTagSuggest } from "./merge-tag-suggest" import { MergeFieldMenu, MergeTagDetails, chipUnderSelection, createMergeTagExtension, htmlToTokens, tokensToHtml, type MergeField, type MergeFieldExample, } from "./merge-tag" type RichTextEditorProps = Omit< React.ComponentProps<"div">, "children" | "defaultValue" | "onChange" > & { value?: string defaultValue?: string onChange?: (html: string) => void name?: string placeholder?: string disabled?: boolean readOnly?: boolean /** * Merge fields offered in an "Insert merge field" toolbar menu and an `@` list. They draw as * badge chips and are stored as `{{key}}` in `value` / `onChange`. Memoize the array. */ mergeFields?: MergeField[] /** * Drop the box: no border, radius, fill or focus ring, and a plain toolbar. For editors that * fill their surface edge to edge, such as an email message. */ bare?: boolean /** Extra controls at the right end of the toolbar, e.g. an Attach files button. */ toolbarEnd?: React.ReactNode /** Rendered between the toolbar and the text, e.g. an email header block. */ contentTop?: React.ReactNode /** Rendered after the text, e.g. an email footer block. */ contentBottom?: React.ReactNode /** What a chip becomes for the person being previewed; shown in its details card. */ describeMergeField?: (key: string) => MergeFieldExample | null } type EditorCommand = { label: string shortcut?: string icon: string active: boolean run: () => void } const EDITOR_TOGGLE_ACTIVE_CLASS = "border border-transparent aria-pressed:border-brand aria-pressed:bg-brand-tint aria-pressed:text-foreground hc:aria-pressed:border-foreground hc:aria-pressed:bg-accent hc:aria-pressed:text-accent-foreground forced-colors:aria-pressed:border-[CanvasText] forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText]" function ToolbarButton({ command }: { command: EditorCommand }) { const button = (