2025-07-21 08:12:11 +00:00
|
|
|
// components/custom-editor.js
|
2025-01-04 15:25:07 +00:00
|
|
|
|
2025-07-21 09:53:10 +00:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from "react";
|
2025-07-21 08:12:11 +00:00
|
|
|
import { CKEditor } from "@ckeditor/ckeditor5-react";
|
|
|
|
|
import Editor from "ckeditor5-custom-build";
|
2025-01-04 15:25:07 +00:00
|
|
|
|
|
|
|
|
function CustomEditor(props) {
|
2025-07-04 19:42:42 +00:00
|
|
|
const editorRef = useRef(null);
|
|
|
|
|
const [isEditorReady, setIsEditorReady] = useState(false);
|
|
|
|
|
const [currentContent, setCurrentContent] = useState(props.initialData || "");
|
|
|
|
|
|
|
|
|
|
// Handle editor initialization
|
|
|
|
|
const handleInit = useCallback((evt, editor) => {
|
|
|
|
|
editorRef.current = editor;
|
|
|
|
|
setIsEditorReady(true);
|
|
|
|
|
|
|
|
|
|
// Set initial content immediately when editor is ready
|
2025-07-21 08:28:41 +00:00
|
|
|
if (currentContent) {
|
|
|
|
|
editor.setContent(currentContent);
|
2025-07-04 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simple onChange handler
|
|
|
|
|
editor.on('change', () => {
|
|
|
|
|
const content = editor.getContent();
|
|
|
|
|
setCurrentContent(content);
|
|
|
|
|
if (props.onChange) {
|
|
|
|
|
props.onChange(content);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-21 08:28:41 +00:00
|
|
|
}, [currentContent, props.onChange]);
|
2025-07-04 19:42:42 +00:00
|
|
|
|
2025-07-21 08:28:41 +00:00
|
|
|
// Watch for changes in initialData prop
|
2025-07-04 19:42:42 +00:00
|
|
|
useEffect(() => {
|
2025-07-21 08:28:41 +00:00
|
|
|
if (props.initialData !== currentContent) {
|
2025-07-04 19:42:42 +00:00
|
|
|
setCurrentContent(props.initialData || "");
|
|
|
|
|
|
|
|
|
|
// Update editor content if editor is ready
|
|
|
|
|
if (editorRef.current && isEditorReady) {
|
|
|
|
|
editorRef.current.setContent(props.initialData || "");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-21 08:28:41 +00:00
|
|
|
}, [props.initialData, currentContent, isEditorReady]);
|
2025-07-04 19:42:42 +00:00
|
|
|
|
2025-07-21 08:28:41 +00:00
|
|
|
// Handle initial data when editor becomes ready
|
2025-07-04 19:42:42 +00:00
|
|
|
useEffect(() => {
|
2025-07-21 08:28:41 +00:00
|
|
|
if (isEditorReady && currentContent && editorRef.current) {
|
|
|
|
|
editorRef.current.setContent(currentContent);
|
2025-07-04 19:42:42 +00:00
|
|
|
}
|
2025-07-21 08:28:41 +00:00
|
|
|
}, [isEditorReady, currentContent]);
|
2025-07-04 19:42:42 +00:00
|
|
|
|
2025-01-04 15:25:07 +00:00
|
|
|
return (
|
2025-07-21 08:12:11 +00:00
|
|
|
<CKEditor
|
|
|
|
|
editor={Editor}
|
|
|
|
|
data={props.initialData}
|
|
|
|
|
onChange={(event, editor) => {
|
|
|
|
|
const data = editor.getData();
|
|
|
|
|
console.log({ event, editor, data });
|
|
|
|
|
props.onChange(data);
|
|
|
|
|
}}
|
|
|
|
|
config={{
|
|
|
|
|
toolbar: [
|
|
|
|
|
"heading",
|
|
|
|
|
"fontsize",
|
|
|
|
|
"bold",
|
|
|
|
|
"italic",
|
|
|
|
|
"link",
|
|
|
|
|
"numberedList",
|
|
|
|
|
"bulletedList",
|
|
|
|
|
"undo",
|
|
|
|
|
"redo",
|
|
|
|
|
"alignment",
|
|
|
|
|
"outdent",
|
|
|
|
|
"indent",
|
|
|
|
|
"blockQuote",
|
|
|
|
|
"insertTable",
|
|
|
|
|
"codeBlock",
|
|
|
|
|
"sourceEditing",
|
2025-07-04 19:42:42 +00:00
|
|
|
],
|
2025-01-05 00:12:17 +00:00
|
|
|
}}
|
2025-01-04 15:25:07 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CustomEditor;
|