"use client"; import { Node, mergeAttributes } from "@tiptap/react"; import { ReactNodeViewRenderer } from "@tiptap/react"; import type { NodeViewProps } from "@tiptap/react"; import { Star } from "lucide-react"; import * as React from "react"; import { cn } from "@/lib/utils"; import { NodeViewWrapper } from "@tiptap/react"; export type DividerStyle = "solid" | "dashed" | "dotted" | "dots" | "star"; function lineClass(style: DividerStyle) { switch (style) { case "solid": return "h-px bg-border"; case "dashed": return "h-0 w-full border-t-2 border-dashed border-border"; case "dotted": return "h-0 w-full border-t-2 border-dotted border-border"; case "dots": return "hidden"; case "star": return "h-px w-full bg-border/70"; default: return "h-px bg-border"; } } function DividerView(props: NodeViewProps) { const { node, updateAttributes, selected } = props; const style = (node.attrs.style as DividerStyle) ?? "solid"; return (
{style === "dots" ? (
) : null} {style === "star" ? ( ) : style !== "dots" ? ( Divider ) : null} ); } export const Divider = Node.create({ name: "divider", group: "block", atom: true, draggable: true, selectable: true, addAttributes() { return { style: { default: "solid" as DividerStyle, parseHTML: (el) => (el.getAttribute("data-divider-style") as DividerStyle | null) ?? "solid", renderHTML: (attrs) => ({ "data-divider-style": attrs.style ?? "solid", }), }, }; }, parseHTML() { return [ { tag: 'div[data-type="divider"]', }, ]; }, renderHTML({ HTMLAttributes }) { return [ "div", mergeAttributes(HTMLAttributes, { "data-type": "divider", role: "separator", }), ]; }, addNodeView() { return ReactNodeViewRenderer(DividerView); }, addKeyboardShortcuts() { return { "Mod-Shift-Alt-d": () => this.editor.commands.insertContent({ type: this.name, attrs: { style: "solid" }, }), }; }, });