import clsx from 'clsx'; import React, { useEffect, useRef, useState } from 'react'; import * as CFI from 'foliate-js/epubcfi.js'; import { useEnv } from '@/context/EnvContext'; import { BookNote } from '@/types/book'; import { useReaderStore } from '@/store/readerStore'; interface BooknoteItemProps { bookKey: string; item: BookNote; editable?: boolean; } const BooknoteItem: React.FC = ({ bookKey, item, editable = false }) => { const { envConfig } = useEnv(); const { settings, getConfig, saveConfig, getProgress, getView } = useReaderStore(); const { updateBooknotes, setNotebookEditAnnotation, setNotebookVisible } = useReaderStore(); const [isCurrent, setIsCurrent] = useState(false); const viewRef = useRef(null); const { text, cfi, note } = item; const progress = getProgress(bookKey); useEffect(() => { if (!progress) return; const { location } = progress; const start = CFI.collapse(location); const end = CFI.collapse(location, true); const isCurrent = CFI.compare(cfi, start) >= 0 && CFI.compare(cfi, end) <= 0; setIsCurrent(isCurrent); if (isCurrent && viewRef.current) { const rect = viewRef.current.getBoundingClientRect(); const isVisible = rect.top >= 0 && rect.bottom <= window.innerHeight; if (!isVisible) { (viewRef.current as HTMLElement).scrollIntoView({ behavior: 'smooth', block: 'center' }); } (viewRef.current as HTMLElement).setAttribute('aria-current', 'page'); } }, [progress]); const handleClickItem = (event: React.MouseEvent) => { event.preventDefault(); getView(bookKey)?.goTo(cfi); if (note) { setNotebookVisible(true); } }; const deleteNote = (note: BookNote) => { if (!bookKey) return; const config = getConfig(bookKey); if (!config) return; const { booknotes = [] } = config; const updatedNotes = booknotes.filter((item) => item.id !== note.id); const updatedConfig = updateBooknotes(bookKey, updatedNotes); if (updatedConfig) { saveConfig(envConfig, bookKey, updatedConfig, settings); } }; const editNote = (note: BookNote) => { setNotebookEditAnnotation(note); }; return (
  • {item.note && {item.note}}
    {item.note && (
    )}
    {text || ''}
    {editable && (
    e.stopPropagation()} >
    )}
  • ); }; export default BooknoteItem;