import * as React from 'react'; import type { PlateElementProps, RenderNodeWrapper } from 'platejs/react'; import { getDraftCommentKey } from '@platejs/comment'; import { CommentPlugin } from '@platejs/comment/react'; import { getTransientSuggestionKey } from '@platejs/suggestion'; import { MessageSquareTextIcon, MessagesSquareIcon, XIcon } from 'lucide-react'; import { type AnyPluginConfig, type NodeEntry, type Path, type TCommentText, type TElement, type TSuggestionText, PathApi, TextApi, } from 'platejs'; import { useEditorPlugin, useEditorRef, usePluginOption } from 'platejs/react'; // === START CUSTOMIZATION === // Customization of @plone/plate's components/ui/block-discussion.tsx: // add "Accept all" / "Reject all" bulk actions below the suggestion list, // shown when the group has more than one open suggestion. import { acceptSuggestion, rejectSuggestion } from '@platejs/suggestion'; import { CheckIcon } from 'lucide-react'; import { FormattedMessage, useIntl } from 'react-intl'; import { messages } from '../../../../../index'; // === END CUSTOMIZATION === import { Button } from '@plone/plate/components/ui/button'; import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger, } from '@plone/plate/components/ui/popover'; import { usePlatePlugins } from '@plone/plate/components/editor/plate-plugins-context'; import { commentPlugin } from '@plone/plate/components/editor/plugins/comment-kit'; import { type TDiscussion } from '@plone/plate/components/editor/plugins/discussion-kit'; import { SuggestionPlugin, suggestionPlugin, } from '@plone/plate/components/editor/plugins/suggestion-kit'; import { BlockSuggestionCard, isResolvedSuggestion, useResolveSuggestion, } from '@plone/plate/components/ui/block-suggestion'; import { type CommentCreateFormHandle, Comment, CommentCreateForm, } from '@plone/plate/components/ui/comment'; import { cn } from '@plone/plate/lib/utils'; export const discussionPopoverContentClassName = ` max-h-[min(70dvh,calc(-24px+var(--radix-popper-available-height)))] w-[min(320px,calc(100vw-24px))] min-w-[300px] overflow-y-auto rounded-[14px] border border-border bg-popover p-0 shadow-[0_10px_30px_rgba(15,23,42,0.14)] data-[state=closed]:opacity-0 `; export const discussionTriggerClassName = ` mt-1 flex h-auto min-w-0 items-center gap-1.5 rounded-md border-0 bg-transparent px-1.5 py-0.5 text-muted-foreground shadow-none hover:bg-muted hover:text-muted-foreground data-[active=true]:bg-muted `; type DiscussionTriggerKind = 'comments' | 'mixed' | 'suggestions'; const DISCUSSION_POPOVER_COLLISION_PADDING = 12; const getDiscussionPopoverCollisionPadding = () => { if (typeof window === 'undefined' || typeof document === 'undefined') { return DISCUSSION_POPOVER_COLLISION_PADDING; } const toolbarRect = document .getElementById('toolbar') ?.getBoundingClientRect(); const sidebarRect = document .querySelector('.sidebar-container:not(.collapsed)') ?.getBoundingClientRect(); const contentAreaRect = document .querySelector('.content-area') ?.getBoundingClientRect(); return { top: DISCUSSION_POPOVER_COLLISION_PADDING, bottom: DISCUSSION_POPOVER_COLLISION_PADDING, left: Math.max( DISCUSSION_POPOVER_COLLISION_PADDING, (toolbarRect?.right ?? 0) + DISCUSSION_POPOVER_COLLISION_PADDING, (contentAreaRect?.left ?? 0) + DISCUSSION_POPOVER_COLLISION_PADDING, ), right: Math.max( DISCUSSION_POPOVER_COLLISION_PADDING, sidebarRect ? window.innerWidth - sidebarRect.left + DISCUSSION_POPOVER_COLLISION_PADDING : DISCUSSION_POPOVER_COLLISION_PADDING, contentAreaRect ? window.innerWidth - contentAreaRect.right + DISCUSSION_POPOVER_COLLISION_PADDING : DISCUSSION_POPOVER_COLLISION_PADDING, ), }; }; const hasMeaningfulRect = (element?: HTMLElement | null) => { const rect = element?.getBoundingClientRect(); return Boolean(rect && rect.width > 0 && rect.height > 0); }; function useDiscussionPopoverCollisionPadding() { const [padding, setPadding] = React.useState( getDiscussionPopoverCollisionPadding, ); React.useEffect(() => { if (typeof window === 'undefined' || typeof document === 'undefined') { return undefined; } const updatePadding = () => { setPadding(getDiscussionPopoverCollisionPadding()); }; updatePadding(); window.addEventListener('resize', updatePadding); const sidebar = document.querySelector('.sidebar-container'); const toolbar = document.getElementById('toolbar'); const contentArea = document.querySelector('.content-area'); const resizeObserver = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => { updatePadding(); }); [sidebar, toolbar, contentArea].forEach((element) => { if (resizeObserver && element && hasMeaningfulRect(element)) { resizeObserver.observe(element); } }); const mutationObserver = new MutationObserver(() => { updatePadding(); }); [sidebar, toolbar, contentArea].forEach((element) => { if (element) { mutationObserver.observe(element, { attributes: true, attributeFilter: ['class', 'style'], }); } }); return () => { window.removeEventListener('resize', updatePadding); resizeObserver?.disconnect(); mutationObserver.disconnect(); }; }, []); return padding; } export const DiscussionTriggerButton = React.forwardRef< HTMLButtonElement, React.ComponentProps & { active: boolean; count: number; kind: DiscussionTriggerKind; } >(({ active, className, count, kind, ...props }, ref) => ( )); DiscussionTriggerButton.displayName = 'DiscussionTriggerButton'; export function DiscussionPopover({ anchorAsChild = true, anchorElement, children, content, contentProps, onOpenChange, open, trigger, triggerAsPopoverTrigger = true, wrapperProps, }: React.PropsWithChildren<{ anchorElement: HTMLElement | null; content: React.ReactNode; onOpenChange: (open: boolean) => void; open: boolean; anchorAsChild?: boolean; contentProps?: Omit, 'children'>; trigger?: React.ReactNode; triggerAsPopoverTrigger?: boolean; wrapperProps?: React.ComponentProps<'div'>; }>) { const { className: wrapperClassName, ...restWrapperProps } = wrapperProps ?? {}; const { className: contentClassName, ...restContentProps } = contentProps ?? {}; const collisionPadding = useDiscussionPopoverCollisionPadding(); return (
{children}
{anchorElement && ( )} event.preventDefault()} onOpenAutoFocus={(event) => event.preventDefault()} align="center" side="bottom" {...restContentProps} > {content} {trigger && (
{triggerAsPopoverTrigger ? ( {trigger} ) : ( trigger )}
)}
); } export function DiscussionPopoverHeader({ count, onClose, title, }: { count: number; onClose: () => void; title: string; }) { return (
{title} ({count})
); } export const BlockDiscussion: RenderNodeWrapper = (props) => { const { editor, element } = props; const commentsApi = editor.getApi(CommentPlugin).comment; const blockPath = editor.api.findPath(element); // avoid duplicate in table or column if (!blockPath || blockPath.length > 1) return; const draftCommentNode = commentsApi.node({ at: blockPath, isDraft: true }); const commentNodes = [...commentsApi.nodes({ at: blockPath })]; const suggestionNodes = [ ...editor.getApi(SuggestionPlugin).suggestion.nodes({ at: blockPath }), ].filter(([node]) => !node[getTransientSuggestionKey()]); if ( commentNodes.length === 0 && suggestionNodes.length === 0 && !draftCommentNode ) { return; } // eslint-disable-next-line react/display-name return (props) => ( ); }; const BlockCommentContent = ({ blockPath, children, commentNodes, draftCommentNode, suggestionNodes, }: PlateElementProps & { blockPath: Path; commentNodes: NodeEntry[]; draftCommentNode: NodeEntry | undefined; suggestionNodes: NodeEntry[]; }) => { const editor = useEditorRef(); // === START CUSTOMIZATION === // Customization of @plone/plate's components/ui/block-discussion.tsx: // add "Accept all" / "Reject all" bulk actions below the suggestion list // when there is more than one open suggestion in the group. `suggestionApi` // is needed to bulk-apply the suggestions, and `canManageSuggestion` mirrors // the same gate used for the per-suggestion buttons in block-suggestion.tsx. const intl = useIntl(); const { api: suggestionApi } = useEditorPlugin(SuggestionPlugin); const { currentUserId } = usePlatePlugins(); const canManageSuggestion = !!currentUserId; // === END CUSTOMIZATION === const resolvedSuggestions = useResolveSuggestion(suggestionNodes, blockPath); const resolvedDiscussions = useResolvedDiscussion(commentNodes, blockPath); const suggestionsCount = resolvedSuggestions.length; const discussionsCount = resolvedDiscussions.length; const totalCount = suggestionsCount + discussionsCount; const popoverTitle = suggestionsCount > 0 && discussionsCount === 0 ? 'Suggestions' : discussionsCount > 0 && suggestionsCount === 0 ? 'Comments' : 'Comments & Suggestions'; const activeSuggestionId = usePluginOption(suggestionPlugin, 'activeId'); const activeSuggestion = activeSuggestionId && resolvedSuggestions.find((s) => s.suggestionId === activeSuggestionId); const commentingBlock = usePluginOption(commentPlugin, 'commentingBlock'); const activeCommentId = usePluginOption(commentPlugin, 'activeId'); const isCommenting = activeCommentId === getDraftCommentKey(); const activeDiscussion = activeCommentId && resolvedDiscussions.find((d) => d.id === activeCommentId); const noneActive = !activeSuggestion && !activeDiscussion; const popoverHeaderCount = noneActive ? totalCount : activeDiscussion ? activeDiscussion.comments.length : 1; const triggerKind: DiscussionTriggerKind = suggestionsCount > 0 && discussionsCount === 0 ? 'suggestions' : suggestionsCount === 0 && discussionsCount > 0 ? 'comments' : 'mixed'; const sortedMergedData = [ ...resolvedDiscussions, ...resolvedSuggestions, ].sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()); const selected = resolvedDiscussions.some((d) => d.id === activeCommentId) || resolvedSuggestions.some((s) => s.suggestionId === activeSuggestionId); const [_open, setOpen] = React.useState(selected); // in some cases, we may comment the multiple blocks const commentingCurrent = !!commentingBlock && PathApi.equals(blockPath, commentingBlock); const open = _open || selected || (isCommenting && !!draftCommentNode && commentingCurrent); const closePopover = React.useCallback(() => { if (isCommenting && draftCommentNode) { editor.tf.unsetNodes(getDraftCommentKey(), { at: [], mode: 'lowest', match: (n) => n[getDraftCommentKey()], }); } setOpen(false); }, [draftCommentNode, editor.tf, isCommenting]); // === START CUSTOMIZATION === const acceptAllSuggestions = () => { suggestionApi.suggestion.withoutSuggestions(() => { resolvedSuggestions.forEach((s) => acceptSuggestion(editor, s)); }); }; const rejectAllSuggestions = () => { suggestionApi.suggestion.withoutSuggestions(() => { resolvedSuggestions.forEach((s) => rejectSuggestion(editor, s)); }); }; // === END CUSTOMIZATION === const anchorElement = React.useMemo(() => { let activeNode: NodeEntry | undefined; if (activeSuggestion) { activeNode = suggestionNodes.find( ([node]) => TextApi.isText(node) && editor.getApi(SuggestionPlugin).suggestion.nodeId(node) === activeSuggestion.suggestionId, ); } if (activeCommentId) { if (activeCommentId === getDraftCommentKey()) { activeNode = draftCommentNode; } else { activeNode = commentNodes.find( ([node]) => editor.getApi(commentPlugin).comment.nodeId(node) === activeCommentId, ); } } if (!activeNode) return null; return editor.api.toDOMNode(activeNode[0])!; // eslint-disable-next-line react-hooks/exhaustive-deps }, [ open, activeSuggestion, activeCommentId, editor.api, suggestionNodes, draftCommentNode, commentNodes, ]); if (suggestionsCount + resolvedDiscussions.length === 0 && !draftCommentNode) return
{children}
; const popoverContent = isCommenting ? ( ) : ( {noneActive ? ( sortedMergedData.map((item, index) => isResolvedSuggestion(item) ? ( ) : ( ), ) ) : ( {activeSuggestion && ( )} {activeDiscussion && ( )} )} {/* === START CUSTOMIZATION === Bulk "Accept all" / "Reject all" footer, shown only when viewing the full list and there is more than one open suggestion to act on. */} {noneActive && suggestionsCount > 1 && canManageSuggestion && (
)} {/* === END CUSTOMIZATION === */} ); return ( { if (!_open_) { closePopover(); return; } setOpen(_open_); }} open={open} trigger={ totalCount > 0 ? ( ) : null } > {children} ); }; function BlockComment({ discussion, isLast, }: { discussion: TDiscussion; isLast: boolean; }) { const [editingId, setEditingId] = React.useState(null); const createFormRef = React.useRef(null); return (
{discussion.comments.map((comment, index) => ( createFormRef.current?.focus() : undefined } setEditingId={setEditingId} showDocumentContent /> ))}
{!isLast &&
} ); } const useResolvedDiscussion = ( commentNodes: NodeEntry[], blockPath: Path, ) => { const { api, getOption, setOption } = useEditorPlugin(commentPlugin); const { discussions } = usePlatePlugins(); commentNodes.forEach(([node]) => { const id = api.comment.nodeId(node); const map = getOption('uniquePathMap'); if (!id) return; const previousPath = map.get(id); // If there are no comment nodes in the corresponding path in the map, then update it. if (PathApi.isPath(previousPath)) { const nodes = api.comment.node({ id, at: previousPath }); if (!nodes) { setOption('uniquePathMap', new Map(map).set(id, blockPath)); return; } return; } // TODO: fix throw error setOption('uniquePathMap', new Map(map).set(id, blockPath)); }); const commentsIds = new Set( commentNodes.map(([node]) => api.comment.nodeId(node)).filter(Boolean), ); const resolvedDiscussions = discussions .map((d: TDiscussion) => ({ ...d, createdAt: new Date(d.createdAt), })) .filter((item: TDiscussion) => { /** If comment cross blocks just show it in the first block */ const commentsPathMap = getOption('uniquePathMap'); const firstBlockPath = commentsPathMap.get(item.id); if (!firstBlockPath) return false; if (!PathApi.equals(firstBlockPath, blockPath)) return false; return ( api.comment.has({ id: item.id }) && commentsIds.has(item.id) && !item.isResolved ); }); return resolvedDiscussions; };