Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 2x 21x 21x 1x 21x 16x 21x 16x 13x 21x 1x 20x 5x 3x 2x 2x 1x 20x 1x 1x 20x 1x | import { useState, useEffect, type MutableRefObject } from "react";
import { useEditor, EditorContent, type Editor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import Image from "@tiptap/extension-image";
import Link from "@tiptap/extension-link";
import Placeholder from "@tiptap/extension-placeholder";
import { cn } from "../../lib/utils";
import { EditorToolbar, type CopilotAction } from "./EditorToolbar";
import { ImageBubbleMenu } from "./ImageBubbleMenu";
import { ImageGalleryModal } from "./ImageGalleryModal";
import "./rich-text-editor.css";
const CustomImage = Image.extend({
addAttributes() {
return {
...this.parent?.(),
style: {
default: null,
parseHTML: (element: HTMLElement) => element.getAttribute("style"),
renderHTML: (attributes: Record<string, unknown>) => {
if (!attributes.style) return {};
return { style: attributes.style };
},
},
};
},
});
interface RichTextEditorProps {
content: string;
onChange: (html: string) => void;
placeholder?: string;
proId?: string;
blogId?: string;
editable?: boolean;
className?: string;
/** Called when user triggers an AI action from the toolbar */
onAIAction?: (action: CopilotAction, selection: string, from: number, to: number) => void;
/** Whether an AI action is currently loading */
aiLoading?: boolean;
/** Ref to access the TipTap editor instance from parent */
editorRef?: MutableRefObject<Editor | null>;
}
export function RichTextEditor({
content,
onChange,
placeholder = "Start writing your blog...",
proId,
blogId,
editable = true,
className,
onAIAction,
aiLoading,
editorRef,
}: RichTextEditorProps) {
const [showImageGallery, setShowImageGallery] = useState(false);
const editor = useEditor({
extensions: [
StarterKit.configure({
link: false,
}),
CustomImage.configure({
HTMLAttributes: {
class: "max-w-full h-auto rounded-lg cursor-pointer",
},
}),
Link.configure({
openOnClick: false,
HTMLAttributes: {
class: "text-primary-600 underline",
},
}),
Placeholder.configure({
placeholder,
}),
],
content,
editable,
editorProps: {
attributes: {
class: "tiptap-content focus:outline-none min-h-[400px] p-4",
},
},
onUpdate: ({ editor: e }) => {
onChange(e.getHTML());
},
});
// Expose editor instance to parent via effect (not during render)
useEffect(() => {
if (editorRef) editorRef.current = editor;
}, [editor, editorRef]);
// Disable editing while AI is loading to prevent stale cursor positions
useEffect(() => {
if (editor && editable) {
editor.setEditable(!aiLoading);
}
}, [editor, aiLoading, editable]);
if (!editor) {
return null;
}
const handleImageClick = () => {
if (proId && blogId) {
setShowImageGallery(true);
} else {
const url = window.prompt("Enter image URL:");
if (url) {
editor.chain().focus().setImage({ src: url }).run();
}
}
};
const handleInsertImage = (image: { url: string; alt: string }) => {
editor.chain().focus().setImage({ src: image.url, alt: image.alt }).run();
setShowImageGallery(false);
};
return (
<div className={cn("border border-border-default rounded-lg", className)}>
{editable && (
<EditorToolbar
editor={editor}
onImageClick={handleImageClick}
onAIAction={onAIAction}
aiLoading={aiLoading}
/>
)}
<EditorContent editor={editor} className="bg-background-elevated" />
{editable && <ImageBubbleMenu editor={editor} />}
{proId && blogId && (
<ImageGalleryModal
isOpen={showImageGallery}
proId={proId}
blogId={blogId}
onClose={() => setShowImageGallery(false)}
onInsertImage={handleInsertImage}
/>
)}
</div>
);
}
|