"use client" /** * Merge tags — values filled in per recipient ("Recipient first name"). * * Stored as plain `{{key}}` text in subjects and bodies; drawn as badge chips inside editors. * The chip look lives here so a field reads the same whether it was picked from the menu, * typed out in full, or arrived with a template. */ import * as React from "react" import { Node, mergeAttributes } from "@tiptap/react" import { cn } from "../../lib/utils" import { Button } from "./button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "./dropdown-menu" import { Input } from "./input" import { Popover, PopoverAnchor, PopoverContent } from "./popover" import { Tip } from "./tip" export interface MergeField { key: string label: string /** What the field becomes and where its value comes from. Shown when the chip is clicked. */ description?: string /** Colour group 1–5 (the chip palette), so related fields share a colour. Default brand. */ tone?: 1 | 2 | 3 | 4 | 5 } const CHIP_BASE = "mx-0.5 inline-flex items-center whitespace-nowrap rounded-4xl border px-2 py-0.5 align-baseline text-xs font-medium leading-none text-foreground select-none" // Full class names, so Tailwind sees every one. const CHIP_TONE: Record = { 0: "border-brand/30 bg-brand-tint", 1: "border-chip-1/40 bg-chip-1/10", 2: "border-chip-2/40 bg-chip-2/10", 3: "border-chip-3/40 bg-chip-3/10", 4: "border-chip-4/40 bg-chip-4/10", 5: "border-chip-5/40 bg-chip-5/10", } /** Chip classes, shared by the editor node, menus and details so they cannot drift. */ export function mergeTagChipClass(tone?: MergeField["tone"]): string { return cn(CHIP_BASE, CHIP_TONE[tone ?? 0]) } /** @deprecated Use {@link mergeTagChipClass}. Default-tone chip classes. */ export const MERGE_TAG_CHIP_CLASS = mergeTagChipClass() const TOKEN = /\{\{\s*([\w.-]+)\s*\}\}/g const TOKEN_SPLIT = /\{\{\s*([\w.-]+)\s*\}\}/g const TAG_SPAN = /]*data-merge-tag="([^"]+)"[^>]*>[\s\S]*?<\/span>/g const escapeHtml = (text: string) => text.replace(/&/g, "&").replace(//g, ">") /** `{{key}}` → chip markup, for keys the editor knows. Unknown tokens stay as text. */ export function tokensToHtml(html: string, fields: MergeField[]): string { return html.replace(TOKEN, (match, key: string) => { const field = fields.find(f => f.key === key) return field ? `@${escapeHtml(field.label)}` : match }) } /** Chip markup → `{{key}}`. */ export function htmlToTokens(html: string): string { return html.replace(TAG_SPAN, (_match, key: string) => `{{${key}}}`) } export function createMergeTagExtension(fields: MergeField[]) { return Node.create({ name: "mergeTag", group: "inline", inline: true, atom: true, selectable: true, addAttributes() { return { key: { default: "", parseHTML: (element: HTMLElement) => element.getAttribute("data-merge-tag"), }, } }, parseHTML() { return [{ tag: "span[data-merge-tag]" }] }, renderHTML({ node, HTMLAttributes }) { const key = String(node.attrs.key) const field = fields.find(f => f.key === key) const label = field?.label ?? key return [ "span", mergeAttributes(HTMLAttributes, { "data-merge-tag": key, class: cn(mergeTagChipClass(field?.tone), "cursor-pointer"), contenteditable: "false", role: "button", "aria-haspopup": "dialog", "aria-label": `${label} merge field. Press Enter for details.`, }), `@${label}`, ] }, renderText({ node }) { return `{{${String(node.attrs.key)}}}` }, }) } export interface MergeFieldMenuProps { fields: MergeField[] onPick: (field: MergeField) => void disabled?: boolean /** Icon-only trigger for tight rows. */ iconOnly?: boolean /** Look of the labelled trigger. Default outline. */ variant?: "outline" | "ghost" label?: string className?: string } /** * "@ Insert merge field" menu: search box, then each field as the badge it becomes in the * text with a one-line description. Same list as the `@` typeahead. */ export function MergeFieldMenu({ fields, onPick, disabled, iconOnly = false, variant = "outline", label = "Insert merge field", className, }: MergeFieldMenuProps) { const [query, setQuery] = React.useState("") const q = query.trim().toLowerCase() const shown = fields.filter( f => !q || `${f.label} ${f.description ?? ""}`.toLowerCase().includes(q), ) const trigger = (