import clsx from 'clsx'; import React, { useEffect, useRef } from 'react'; import { useNotebookStore } from '@/store/notebookStore'; import { TextSelection } from '@/utils/sel'; import { BookNote } from '@/types/book'; interface NoteEditorProps { onSave: (selction: TextSelection, note: string) => void; onEdit: (annotation: BookNote) => void; } const NoteEditor: React.FC = ({ onSave, onEdit }) => { const { notebookNewAnnotation, notebookEditAnnotation } = useNotebookStore(); const editorRef = useRef(null); const [note, setNote] = React.useState(''); useEffect(() => { if (editorRef.current) { editorRef.current.focus(); } }, []); useEffect(() => { if (notebookEditAnnotation) { setNote(notebookEditAnnotation.note); } }, [notebookEditAnnotation]); const adjustHeight = () => { if (editorRef.current) { editorRef.current.style.height = 'auto'; editorRef.current.style.height = `${editorRef.current.scrollHeight}px`; } }; const handleChange = (e: React.ChangeEvent) => { adjustHeight(); setNote(e.currentTarget.value); }; const handleSaveNote = () => { if (editorRef.current && notebookNewAnnotation) { onSave(notebookNewAnnotation, editorRef.current.value); } else if (editorRef.current && notebookEditAnnotation) { notebookEditAnnotation.note = editorRef.current.value; onEdit(notebookEditAnnotation); } }; return (
{notebookNewAnnotation?.text || notebookEditAnnotation?.text}
); }; export default NoteEditor;