// Copyright (c) 2026 Probo Inc . // Use of this source code is governed by the ISC license // that can be found in the LICENSE file. import { CodeBlockIcon, ListBulletsIcon, ListNumbersIcon, QuotesIcon, TextHFourIcon, TextHOneIcon, TextHThreeIcon, TextHTwoIcon, TextTIcon } from "@phosphor-icons/react"; import { Fragment, type Node as PmNode } from "@tiptap/pm/model"; import { TextSelection } from "@tiptap/pm/state"; import { type Editor } from "@tiptap/react"; import { getBlockNode, isBlockNodeType } from "../_lib/getBlockNode"; import { MenuButton } from "../MenuButton"; import type { OptionsMenuFloating } from "./OptionsMenu"; import { optionsMenuVariants } from "./variants"; const { menu } = optionsMenuVariants(); type TargetKind = "textblock" | "wrapper"; function collectTextBlocks(node: PmNode): PmNode[] { const result: PmNode[] = []; node.forEach((child) => { if (child.isTextblock) { result.push(child); } else { result.push(...collectTextBlocks(child)); } }); return result; } type DecomposeResult = { targetContent: Fragment | null; remaining: PmNode[]; }; function decomposeWrapper(node: PmNode): DecomposeResult { const firstChild = node.firstChild; if (!firstChild) { return { targetContent: null, remaining: [] }; } let targetContent: Fragment | null = null; const promotedSiblings: PmNode[] = []; if (firstChild.isTextblock) { targetContent = firstChild.content; } else { firstChild.forEach((child) => { if (targetContent === null && child.isTextblock) { targetContent = child.content; } else { promotedSiblings.push(child); } }); } const remainingChildren: PmNode[] = []; node.forEach((_child, _offset, index) => { if (index > 0) remainingChildren.push(_child); }); const remaining: PmNode[] = [...promotedSiblings]; if (remainingChildren.length > 0) { remaining.push(node.copy(Fragment.from(remainingChildren))); } return { targetContent, remaining }; } type OptionsMenuContentProps = { editor: Editor; hoveredBlock: HTMLElement | null; setMenuOpen: React.Dispatch>; setDropdownEl: OptionsMenuFloating["setDropdownEl"]; menuRefs: OptionsMenuFloating["menuRefs"]; menuStyles: OptionsMenuFloating["menuStyles"]; getFloatingProps: OptionsMenuFloating["getFloatingProps"]; }; export function OptionsMenuContent({ editor, hoveredBlock, setMenuOpen, setDropdownEl, menuRefs, menuStyles, getFloatingProps, }: OptionsMenuContentProps) { const handleAction = ( applyCommand: (chain: ReturnType) => ReturnType, targetKind: TargetKind, ) => { if (!hoveredBlock) { setMenuOpen(false); return; } const data = getBlockNode(editor, hoveredBlock); if (!data) { setMenuOpen(false); return; } try { const { node, pos } = data; const schema = editor.state.schema; if (node.isTextblock) { const $near = editor.state.doc.resolve(pos + 1); const textPos = TextSelection.near($near).from; applyCommand( editor.chain() .focus() .setTextSelection(textPos), ).run(); } else if (targetKind === "textblock") { const { targetContent, remaining } = decomposeWrapper(node); if (!targetContent) { setMenuOpen(false); return; } const target = schema.nodes.paragraph.create(null, targetContent); const replacements = [target, ...remaining]; editor.chain() .focus() .command(({ tr }) => { tr.replaceWith( pos, pos + node.nodeSize, Fragment.from(replacements), ); return true; }) .run(); const $near = editor.state.doc.resolve(pos + 1); const textPos = TextSelection.near($near).from; applyCommand( editor.chain() .focus() .setTextSelection(textPos), ).run(); } else { const textBlocks = collectTextBlocks(node); if (textBlocks.length === 0) { setMenuOpen(false); return; } const paragraphs = textBlocks.map(tb => schema.nodes.paragraph.create(null, tb.content), ); editor.chain() .focus() .command(({ tr }) => { tr.replaceWith( pos, pos + node.nodeSize, Fragment.from(paragraphs), ); return true; }) .run(); let totalSize = 0; for (const p of paragraphs) { totalSize += p.nodeSize; } const from = pos + 1; const to = pos + totalSize - 1; applyCommand( editor.chain() .focus() .setTextSelection({ from, to }), ).run(); } } catch { // Block may no longer be in the document } setMenuOpen(false); }; return (
{ setDropdownEl(node); menuRefs.setFloating(node); }} style={menuStyles} {...getFloatingProps()} onMouseDown={e => e.preventDefault()} className={menu()} >
Turn into
handleAction(chain => chain.setParagraph(), "textblock")} > Text handleAction(chain => chain.toggleHeading({ level: 1 }), "textblock")} > Heading 1 handleAction(chain => chain.toggleHeading({ level: 2 }), "textblock")} > Heading 2 handleAction(chain => chain.toggleHeading({ level: 3 }), "textblock")} > Heading 3 handleAction(chain => chain.toggleHeading({ level: 4 }), "textblock")} > Heading 4 handleAction(chain => chain.toggleBulletList(), "wrapper")} > Bullet List handleAction(chain => chain.toggleOrderedList(), "wrapper")} > Ordered List handleAction(chain => chain.toggleCodeBlock(), "textblock")} > Code Block handleAction(chain => chain.toggleBlockquote(), "wrapper")} > Blockquote
); }