'use client';
import * as React from 'react';
import type { DropdownMenuProps } from '@radix-ui/react-dropdown-menu';
import type { TElement } from 'platejs';
import { DropdownMenuItemIndicator } from '@radix-ui/react-dropdown-menu';
import {
CheckIcon,
ChevronRightIcon,
FileCodeIcon,
Heading1Icon,
Heading2Icon,
Heading3Icon,
Heading4Icon,
Heading5Icon,
Heading6Icon,
InfoIcon,
ListIcon,
ListOrderedIcon,
PilcrowIcon,
QuoteIcon,
SquareIcon,
} from 'lucide-react';
import { KEYS } from 'platejs';
import { useEditorRef, useSelectionFragmentProp } from 'platejs/react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import {
getBlockType,
setBlockType,
} from '@/components/editor/transforms';
import { ToolbarButton, ToolbarMenuGroup } from './toolbar';
export const turnIntoItems = [
{
icon: ,
keywords: ['paragraph'],
label: 'Text',
value: KEYS.p,
},
{
icon: ,
keywords: ['title', 'h1'],
label: 'Heading 1',
value: 'h1',
},
{
icon: ,
keywords: ['subtitle', 'h2'],
label: 'Heading 2',
value: 'h2',
},
{
icon: ,
keywords: ['subtitle', 'h3'],
label: 'Heading 3',
value: 'h3',
},
{
icon: ,
keywords: ['subtitle', 'h4'],
label: 'Heading 4',
value: 'h4',
},
{
icon: ,
keywords: ['subtitle', 'h5'],
label: 'Heading 5',
value: 'h5',
},
{
icon: ,
keywords: ['subtitle', 'h6'],
label: 'Heading 6',
value: 'h6',
},
{
icon: ,
keywords: ['unordered', 'ul', '-'],
label: 'Bulleted list',
value: KEYS.ul,
},
{
icon: ,
keywords: ['ordered', 'ol', '1'],
label: 'Numbered list',
value: KEYS.ol,
},
{
icon: ,
keywords: ['checklist', 'task', 'checkbox', '[]'],
label: 'To-do list',
value: KEYS.listTodo,
},
{
icon: ,
keywords: ['```'],
label: 'Code',
value: KEYS.codeBlock,
},
{
icon: ,
keywords: ['citation', 'blockquote', '>'],
label: 'Quote',
value: KEYS.blockquote,
},
{
icon: ,
keywords: ['callout', 'note', 'info', 'warning', 'tip'],
label: 'Callout',
value: KEYS.callout,
},
{
icon: ,
keywords: ['toggle', 'collapsible', 'expand'],
label: 'Toggle',
value: KEYS.toggle,
},
];
export function TurnIntoToolbarButton(props: DropdownMenuProps) {
const editor = useEditorRef();
const [open, setOpen] = React.useState(false);
const value = useSelectionFragmentProp({
defaultValue: KEYS.p,
getProp: (node) => getBlockType(node as TElement),
});
const selectedItem = React.useMemo(
() =>
turnIntoItems.find((item) => item.value === (value ?? KEYS.p)) ??
turnIntoItems[0],
[value]
);
return (
{selectedItem.label}
{
e.preventDefault();
editor.tf.focus();
}}
align="start"
>
{
setBlockType(editor, type);
}}
label="Turn into"
>
{turnIntoItems.map(({ icon, label, value: itemValue }) => (
{icon}
{label}
))}
);
}