| 1 | "use client"; |
| 2 | |
| 3 | import { getCommentCount } from "@platejs/comment"; |
| 4 | import { type TCommentText } from "platejs"; |
| 5 | import { |
| 6 | PlateLeaf, |
| 7 | useEditorPlugin, |
| 8 | useEditorReadOnly, |
| 9 | useEditorRef, |
| 10 | usePluginOption, |
| 11 | type PlateLeafProps, |
| 12 | } from "platejs/react"; |
| 13 | |
| 14 | import { commentPlugin } from "@/components/plate/plugins/comment-kit"; |
| 15 | import { suggestionPlugin } from "@/components/plate/plugins/suggestion-kit"; |
| 16 | import { cn } from "@/lib/utils"; |
| 17 | |
| 18 | export function CommentLeaf(props: PlateLeafProps<TCommentText>) { |
| 19 | const { children, leaf } = props; |
| 20 | |
| 21 | const editor = useEditorRef(); |
| 22 | const { api, setOption } = useEditorPlugin(commentPlugin); |
| 23 | const readOnly = useEditorReadOnly(); |
| 24 | const hoverId = usePluginOption(commentPlugin, "hoverId"); |
| 25 | const activeId = usePluginOption(commentPlugin, "activeId"); |
| 26 | |
| 27 | const isOverlapping = getCommentCount(leaf) > 1; |
| 28 | const currentId = api.comment.nodeId(leaf); |
| 29 | const isActive = activeId === currentId; |
| 30 | const isHover = hoverId === currentId; |
| 31 | |
| 32 | return ( |
| 33 | <PlateLeaf |
| 34 | {...props} |
| 35 | className={cn( |
| 36 | "rounded-[3px] bg-amber-200/30 text-inherit no-underline transition-colors duration-200", |
| 37 | !readOnly && "bg-amber-200/45", |
| 38 | !readOnly && (isHover || isActive) && "bg-amber-200/65", |
| 39 | isOverlapping && "outline outline-amber-300/50", |
| 40 | !readOnly && isOverlapping && "bg-amber-200/55 outline-amber-300/70", |
| 41 | (isHover || isActive) && |
| 42 | !readOnly && |
| 43 | isOverlapping && |
| 44 | "bg-amber-200/75 outline-amber-300", |
| 45 | !readOnly && currentId && "cursor-pointer", |
| 46 | )} |
| 47 | attributes={{ |
| 48 | ...props.attributes, |
| 49 | onClick: () => { |
| 50 | if (readOnly || !currentId) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | setOption("activeId", currentId); |
| 55 | setOption("commentingBlock", null); |
| 56 | editor.setOption(suggestionPlugin, "activeId", null); |
| 57 | }, |
| 58 | onMouseEnter: () => { |
| 59 | if (readOnly) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | setOption("hoverId", currentId ?? null); |
| 64 | }, |
| 65 | onMouseLeave: () => setOption("hoverId", null), |
| 66 | }} |
| 67 | > |
| 68 | {children} |
| 69 | </PlateLeaf> |
| 70 | ); |
| 71 | } |
| 72 |