| 1 | "use client"; |
| 2 | |
| 3 | import { NodeApi } from "platejs"; |
| 4 | import { |
| 5 | type PlateElementProps, |
| 6 | PlateElement, |
| 7 | useEditorSelector, |
| 8 | useReadOnly, |
| 9 | } from "platejs/react"; |
| 10 | |
| 11 | import { NotesTemplateActions } from "@/components/notebook/notes/NotesTemplateActions"; |
| 12 | import { cn } from "@/lib/utils"; |
| 13 | |
| 14 | export function NoteParagraphElement(props: PlateElementProps) { |
| 15 | const readOnly = useReadOnly(); |
| 16 | const isEditorEmpty = useEditorSelector((editor) => editor.api.isEmpty(), []); |
| 17 | const isCurrentParagraphEmpty = |
| 18 | NodeApi.string(props.element).trim().length === 0; |
| 19 | const isFirstTopLevelParagraph = |
| 20 | props.path.length === 1 && props.path[0] === 0; |
| 21 | const showPlaceholder = |
| 22 | !readOnly && |
| 23 | isEditorEmpty && |
| 24 | isCurrentParagraphEmpty && |
| 25 | isFirstTopLevelParagraph; |
| 26 | |
| 27 | return ( |
| 28 | <PlateElement |
| 29 | {...props} |
| 30 | className={cn("relative m-0 px-0 py-1", showPlaceholder && "min-h-8")} |
| 31 | > |
| 32 | {showPlaceholder && ( |
| 33 | <div className="pointer-events-none absolute inset-y-0 left-0 flex max-w-full items-center"> |
| 34 | <NotesTemplateActions /> |
| 35 | </div> |
| 36 | )} |
| 37 | {props.children} |
| 38 | </PlateElement> |
| 39 | ); |
| 40 | } |
| 41 |