import * as React from 'react'; import type { TElement } from 'platejs'; import type { PlateElementProps } from 'platejs/react'; import { CalendarIcon } from 'lucide-react'; import { PlateElement, useFocused, useSelected } from 'platejs/react'; import { cn } from '@plone/plate/lib/utils'; import { getIntl } from '@plone/plate/components/editor/plugins/split-utils'; import { Popover, PopoverAnchor, PopoverContent, } from '@plone/plate/components/ui/popover'; import { DatePickerPanel } from './date-picker-panel'; export const DATE_ELEMENT_TYPE = 'date'; export type TDateElement = TElement & { type: typeof DATE_ELEMENT_TYPE; value: string; }; export const formatDatePillLabel = (value: string, locale?: string) => { const date = new Date(`${value}T00:00:00`); if (Number.isNaN(date.getTime())) return value; return new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric', }).format(date); }; export function DateElement(props: PlateElementProps) { const { editor, element } = props; const [open, setOpen] = React.useState(false); const selected = useSelected(); const focused = useFocused(); const intl = getIntl(editor); const locale = intl?.locale; const translate = React.useCallback( (id: string) => intl?.formatMessage ? intl.formatMessage({ defaultMessage: id, id }) : id, [intl], ); return ( { const path = editor.api.findPath(element); if (path) { editor.tf.setNodes({ value: isoDate }, { at: path }); } setOpen(false); }} translate={translate} value={element.value} /> {props.children} ); }