import * as React from 'react'; import type { TResolvedSuggestion } from '@platejs/suggestion'; import { acceptSuggestion, getSuggestionKey, keyId2SuggestionId, rejectSuggestion, } from '@platejs/suggestion'; import { CheckIcon, MinusIcon, PlusIcon, XIcon } from 'lucide-react'; import { type NodeEntry, type Path, type TElement, type TSuggestionElement, type TSuggestionText, ElementApi, KEYS, PathApi, TextApi, } from 'platejs'; import { useEditorPlugin } from 'platejs/react'; import { Button } from '@plone/plate/components/ui/button'; import { cn } from '@plone/plate/lib/utils'; import { usePlatePlugins } from '@plone/plate/components/editor/plate-plugins-context'; import { type TDiscussion } from '@plone/plate/components/editor/plugins/discussion-kit'; import { SuggestionPlugin, suggestionPlugin, } from '@plone/plate/components/editor/plugins/suggestion-kit'; import { type TComment, Comment, formatCommentDate, } from '@plone/plate/components/ui/comment'; // === START CUSTOMIZATION === // Customization of @plone/plate's components/ui/block-suggestion.tsx: // render the suggestion author with the shared PersonPill component // instead of the Avatar/AvatarFallback initial-letter avatar. import PersonPill from '@kitconcept/volto-plate/components/PersonPill/PersonPill'; // === END CUSTOMIZATION === export interface ResolvedSuggestion extends TResolvedSuggestion { comments: TComment[]; } const BLOCK_SUGGESTION = '__block__'; const TYPE_TEXT_MAP: Record string> = { [KEYS.audio]: () => 'Audio', [KEYS.blockquote]: () => 'Blockquote', [KEYS.callout]: () => 'Callout', [KEYS.codeBlock]: () => 'Code Block', [KEYS.column]: () => 'Column', [KEYS.file]: () => 'File', [KEYS.h1]: () => `Heading 1`, [KEYS.h2]: () => `Heading 2`, [KEYS.h3]: () => `Heading 3`, [KEYS.h4]: () => `Heading 4`, [KEYS.h5]: () => `Heading 5`, [KEYS.h6]: () => `Heading 6`, [KEYS.hr]: () => 'Horizontal Rule', [KEYS.img]: () => 'Image', [KEYS.mediaEmbed]: () => 'Media', [KEYS.p]: (node) => { if (node?.[KEYS.listType] === KEYS.listTodo) return 'Todo List'; if (node?.[KEYS.listType] === KEYS.ol) return 'Ordered List'; if (node?.[KEYS.listType] === KEYS.ul) return 'List'; return 'Paragraph'; }, [KEYS.table]: () => 'Table', [KEYS.toc]: () => 'Table of Contents', [KEYS.toggle]: () => 'Toggle', [KEYS.video]: () => 'Video', }; export function BlockSuggestion({ element }: { element: TSuggestionElement }) { const suggestionData = element.suggestion; if (suggestionData?.isLineBreak) return null; const isRemove = suggestionData?.type === 'remove'; return (
); } export function BlockSuggestionCard({ idx, isLast, suggestion, }: { idx: number; isLast: boolean; suggestion: ResolvedSuggestion; }) { const { api, editor } = useEditorPlugin(SuggestionPlugin); const { currentUserId, users } = usePlatePlugins(); const userInfo = users[suggestion.userId]; const canManageSuggestion = !!currentUserId; const accept = (suggestion: ResolvedSuggestion) => { api.suggestion.withoutSuggestions(() => { acceptSuggestion(editor, suggestion); }); }; const reject = (suggestion: ResolvedSuggestion) => { api.suggestion.withoutSuggestions(() => { rejectSuggestion(editor, suggestion); }); }; const suggestionText2Array = (text: string) => { if (text === BLOCK_SUGGESTION) return ['line breaks']; return text.split(BLOCK_SUGGESTION).filter(Boolean); }; const [editingId, setEditingId] = React.useState(null); return (
{/* === START CUSTOMIZATION === Original rendered: {userInfo?.name?.[0]}

{userInfo?.name}

{formatCommentDate(...)}
Replaced with PersonPill, which already renders name + subheading. */} {/* === END CUSTOMIZATION === */}
{suggestion.type === 'remove' && ( {suggestionText2Array(suggestion.text!).map((text, index) => (
Deletion

Suggested removing{' '} “{text}”

))}
)} {suggestion.type === 'insert' && ( {suggestionText2Array(suggestion.newText!).map( (text, index) => (
Insertion

Suggested adding{' '} “{text || 'line breaks'}”

), )}
)} {suggestion.type === 'replace' && (
{suggestionText2Array(suggestion.newText!).map( (text, index) => (
with: {text || 'line breaks'}
), )} {suggestionText2Array(suggestion.text!).map((text, index) => (
{index === 0 ? 'Replace:' : 'Delete:'} {text || 'line breaks'}
))}
)} {suggestion.type === 'update' && (
{Object.keys(suggestion.properties).map((key) => ( Un{key} ))} {Object.keys(suggestion.newProperties).map((key) => ( {key.charAt(0).toUpperCase() + key.slice(1)} ))} {suggestion.newText}
)}
{suggestion.comments.map((comment, index) => ( ))} {canManageSuggestion && (
)}
{!isLast &&
}
); } export const useResolveSuggestion = ( suggestionNodes: NodeEntry[], blockPath: Path, ) => { const { discussions } = usePlatePlugins(); const { api, editor, getOption, setOption } = useEditorPlugin(suggestionPlugin); suggestionNodes.forEach(([node]) => { const id = api.suggestion.nodeId(node); const map = getOption('uniquePathMap'); if (!id) return; const previousPath = map.get(id); // If there are no suggestion nodes in the corresponding path in the map, then update it. if (PathApi.isPath(previousPath)) { const nodes = api.suggestion.node({ id, at: previousPath, isText: true }); const parentNode = api.node(previousPath); let lineBreakId: string | null = null; if (parentNode && ElementApi.isElement(parentNode[0])) { lineBreakId = api.suggestion.nodeId(parentNode[0]) ?? null; } if (!nodes && lineBreakId !== id) { return setOption('uniquePathMap', new Map(map).set(id, blockPath)); } return; } setOption('uniquePathMap', new Map(map).set(id, blockPath)); }); const resolvedSuggestion: ResolvedSuggestion[] = React.useMemo(() => { const map = getOption('uniquePathMap'); if (suggestionNodes.length === 0) return []; const suggestionIds = new Set( suggestionNodes .flatMap(([node]) => { if (TextApi.isText(node)) { const dataList = api.suggestion.dataList(node); const includeUpdate = dataList.some( (data) => data.type === 'update', ); if (!includeUpdate) return api.suggestion.nodeId(node); return dataList .filter((data) => data.type === 'update') .map((d) => d.id); } if (ElementApi.isElement(node)) { return api.suggestion.nodeId(node); } // === START CUSTOMIZATION === return undefined; // === END CUSTOMIZATION === }) .filter(Boolean), ); const res: ResolvedSuggestion[] = []; suggestionIds.forEach((id) => { if (!id) return; const path = map.get(id); if (!path || !PathApi.isPath(path)) return; if (!PathApi.equals(path, blockPath)) return; const entries = [ ...editor.api.nodes({ at: [], mode: 'all', match: (n) => (n[KEYS.suggestion] && n[getSuggestionKey(id)]) || api.suggestion.nodeId(n as TElement) === id, }), ]; // move line break to the end entries.sort(([, path1], [, path2]) => { return PathApi.isChild(path1, path2) ? -1 : 1; }); let newText = ''; let text = ''; let properties: any = {}; let newProperties: any = {}; // overlapping suggestion entries.forEach(([node]) => { if (TextApi.isText(node)) { const dataList = api.suggestion.dataList(node); dataList.forEach((data) => { if (data.id !== id) return; switch (data.type) { case 'insert': { newText += node.text; break; } case 'remove': { text += node.text; break; } case 'update': { properties = { ...properties, ...data.properties, }; newProperties = { ...newProperties, ...data.newProperties, }; newText += node.text; break; } // No default } }); } else { const lineBreakData = api.suggestion.isBlockSuggestion(node) ? node.suggestion : undefined; if (lineBreakData?.id !== keyId2SuggestionId(id)) return; if (lineBreakData.type === 'insert') { newText += lineBreakData.isLineBreak ? BLOCK_SUGGESTION : BLOCK_SUGGESTION + TYPE_TEXT_MAP[node.type](node); } else if (lineBreakData.type === 'remove') { text += lineBreakData.isLineBreak ? BLOCK_SUGGESTION : BLOCK_SUGGESTION + TYPE_TEXT_MAP[node.type](node); } } }); if (entries.length === 0) return; const nodeData = api.suggestion.suggestionData(entries[0][0]); if (!nodeData) return; const comments = discussions.find((s: TDiscussion) => s.id === id)?.comments || []; const createdAt = new Date(nodeData.createdAt); const keyId = getSuggestionKey(id); if (nodeData.type === 'update') { return res.push({ comments, createdAt, keyId, newProperties, newText, properties, suggestionId: keyId2SuggestionId(id), type: 'update', userId: nodeData.userId, }); } if (newText.length > 0 && text.length > 0) { return res.push({ comments, createdAt, keyId, newText, suggestionId: keyId2SuggestionId(id), text, type: 'replace', userId: nodeData.userId, }); } if (newText.length > 0) { return res.push({ comments, createdAt, keyId, newText, suggestionId: keyId2SuggestionId(id), type: 'insert', userId: nodeData.userId, }); } if (text.length > 0) { return res.push({ comments, createdAt, keyId, suggestionId: keyId2SuggestionId(id), text, type: 'remove', userId: nodeData.userId, }); } }); return res; }, [ api.suggestion, blockPath, discussions, editor.api, getOption, suggestionNodes, ]); return resolvedSuggestion; }; export const isResolvedSuggestion = ( suggestion: ResolvedSuggestion | TDiscussion, ): suggestion is ResolvedSuggestion => { return 'suggestionId' in suggestion; };