import * as React from 'react'; import type { TComboboxInputElement } from 'platejs'; import type { PlateElementProps } from 'platejs/react'; import { SlashPlugin } from '@platejs/slash-command/react'; import { ElementApi } from 'platejs'; import { PlateElement } from 'platejs/react'; import { getIntl } from '@plone/plate/components/editor/plugins/split-utils'; import { TITLE_BLOCK_TYPE } from '@plone/plate/components/editor/plugins/title'; import { resolveSlashMenuGroups, type SlashMenuConfig, } from '@plone/plate/components/editor/plugins/slash-menu'; import { InlineCombobox, InlineComboboxContent, InlineComboboxEmpty, InlineComboboxGroup, InlineComboboxGroupLabel, InlineComboboxInput, InlineComboboxItem, } from '@plone/plate/components/ui/inline-combobox'; import { DATE_ELEMENT_TYPE } from '../plugins/date-node'; import { DatePickerPanel } from '../plugins/date-picker-panel'; // Typing a single "/" already opens this combobox (see SlashPlugin's // triggerPreviousCharPattern). A second "/" therefore lands as the combobox's // own live search text rather than reaching the document as plain text, so // "//" is detected here by branching on that search value instead of // registering a separate two-character trigger. const isDateTrigger = (search: string) => search.startsWith('/'); // Some registered blocks (e.g. Volto core's title/description/image/...) // store their icon as the raw `{ attributes, content }` object produced by // the SVG loader rather than as a component, meant to be rendered through // Volto's own `Icon` helper. `resolveSlashMenuGroups`'s built-in "Blocks" // group renders `icon` directly as a JSX element, which crashes the whole // editor when that happens. Guard against it here instead of assuming every // registered block followed the convention this menu expects. const isRenderableIcon = (node: React.ReactNode) => React.isValidElement(node) && (typeof node.type === 'function' || typeof node.type === 'string'); export function WikiSlashInputElement( props: PlateElementProps, ) { const { editor, element } = props; const [search, setSearch] = React.useState(''); const menuConfig = ( editor.getOptions(SlashPlugin as any) as | { menu?: SlashMenuConfig } | undefined )?.menu; const intl = getIntl(editor); const translate = React.useMemo(() => { if (!intl?.formatMessage) { return (id: string) => id; } return (id: string) => intl.formatMessage({ defaultMessage: id, id }); }, [intl]); const hasTitleBlock = editor.children.some( (child) => ElementApi.isElement(child) && child.type === TITLE_BLOCK_TYPE, ); const groups = React.useMemo(() => { return resolveSlashMenuGroups(editor, menuConfig, { hasTitleBlock, translate, }); }, [editor, hasTitleBlock, menuConfig, translate]); const insertDate = (isoDate: string) => { editor.tf.withoutNormalizing(() => { const path = editor.api.findPath(element); if (!path) return; editor.tf.removeNodes({ at: path }); editor.tf.insertNodes( { children: [{ text: '' }], type: DATE_ELEMENT_TYPE, value: isoDate, }, { at: path, select: true }, ); }); editor.tf.move({ unit: 'offset' }); }; const showDatePicker = isDateTrigger(search); return ( ); }