"use client"; import { useRef, useState } from "react"; import { EditorContent, useEditor, type Editor } from "@tiptap/react"; import StarterKit from "@tiptap/starter-kit"; import Image from "@tiptap/extension-image"; import { NodeSelection } from "@tiptap/pm/state"; import { AlignCenter, AlignLeft, AlignRight, ArrowDown, ArrowUp, Bold, Captions, Code, Heading2, Heading3, ImagePlus, Italic, Link2, List, ListOrdered, Loader2, Pilcrow, Quote, Redo2, Strikethrough, Trash2, Underline, Undo2, } from "lucide-react"; import { Button } from "@/components/ui/button"; /** The for an image node, whether the node parsed from an or a
. */ function imageElement(element: HTMLElement): HTMLElement { return element.tagName === "IMG" ? element : element.querySelector("img") ?? element; } /** * The image node adds alignment and a caption to the stock image extension. * Width is already a native attribute and the caption is emitted as the * figcaption text, so the toolbar needs no inline styles (which the server * sanitizer intentionally strips). */ const TrainingImage = Image.extend({ addAttributes() { return { src: { default: null, parseHTML: (element: HTMLElement) => imageElement(element).getAttribute("src"), }, alt: { default: null, parseHTML: (element: HTMLElement) => imageElement(element).getAttribute("alt"), }, title: { default: null, parseHTML: (element: HTMLElement) => imageElement(element).getAttribute("title"), }, width: { default: null, parseHTML: (element: HTMLElement) => imageElement(element).getAttribute("width"), }, align: { default: "center", parseHTML: (element: HTMLElement) => imageElement(element).getAttribute("data-align") ?? "center", renderHTML: (attributes: Record) => ({ "data-align": attributes.align ?? "center", }), }, caption: { default: null, parseHTML: (element: HTMLElement) => element.tagName === "FIGURE" ? element.querySelector("figcaption")?.textContent?.trim() || null : null, }, }; }, parseHTML() { return [ { tag: "figure[data-lesson-image]" }, { tag: 'img[src]:not([src^="data:"])' }, ]; }, renderHTML({ HTMLAttributes }) { const { caption: captionAttribute, ...attributes } = HTMLAttributes; const caption = typeof captionAttribute === "string" ? captionAttribute.trim() : ""; const align = attributes["data-align"] ?? "center"; if (!caption) { return ["figure", { "data-lesson-image": "true" }, ["img", attributes]]; } return [ "figure", { "data-lesson-image": "true" }, ["img", attributes], ["figcaption", { "data-align": align }, caption], ]; }, }); const IMAGE_WIDTHS = ["25%", "50%", "75%", "100%"] as const; function ToolButton({ label, active, disabled, onClick, children, }: { label: string; active?: boolean; disabled?: boolean; onClick: () => void; children: React.ReactNode; }) { return ( ); } function Separator() { return