import clsx from 'clsx'; import dayjs from 'dayjs'; import React, { useRef, useState } from 'react'; import { marked } from 'marked'; import { useEnv } from '@/context/EnvContext'; import { BookNote, HighlightColor } from '@/types/book'; import { useSettingsStore } from '@/store/settingsStore'; import { useReaderStore } from '@/store/readerStore'; import { useNotebookStore } from '@/store/notebookStore'; import { useBookDataStore } from '@/store/bookDataStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useResponsiveSize } from '@/hooks/useResponsiveSize'; import { eventDispatcher } from '@/utils/event'; import { NOTE_PREFIX } from '@/types/view'; import useScrollToItem from '../../hooks/useScrollToItem'; import TextButton from '@/components/TextButton'; import TextEditor, { TextEditorRef } from '@/components/TextEditor'; interface BooknoteItemProps { bookKey: string; item: BookNote; onClick?: () => void; } const BooknoteItem: React.FC = ({ bookKey, item, onClick }) => { const _ = useTranslation(); const { envConfig } = useEnv(); const { settings } = useSettingsStore(); const { getConfig, saveConfig, updateBooknotes } = useBookDataStore(); const { getProgress, getView, getViewsById } = useReaderStore(); const { setNotebookEditAnnotation, setNotebookVisible } = useNotebookStore(); const globalReadSettings = settings.globalReadSettings; const customColors = globalReadSettings.customHighlightColors; const { text, cfi, note } = item; const editorRef = useRef(null); const [editorDraft, setEditorDraft] = useState(text || ''); const [inlineEditMode, setInlineEditMode] = useState(false); const separatorWidth = useResponsiveSize(3); const progress = getProgress(bookKey); const { isCurrent, viewRef } = useScrollToItem(cfi, progress); const handleClickItem = (event: React.MouseEvent | React.KeyboardEvent) => { event.preventDefault(); eventDispatcher.dispatch('navigate', { bookKey, cfi }); onClick?.(); 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; booknotes.forEach((item) => { if (item.id === note.id) { item.deletedAt = Date.now(); const views = getViewsById(bookKey.split('-')[0]!); views.forEach((view) => view?.addAnnotation({ ...item, value: `${NOTE_PREFIX}${item.cfi}` }, true), ); } }); const updatedConfig = updateBooknotes(bookKey, booknotes); if (updatedConfig) { saveConfig(envConfig, bookKey, updatedConfig, settings); } }; const editNote = (note: BookNote) => { setNotebookVisible(true); setNotebookEditAnnotation(note); }; const editBookmark = () => { setEditorDraft(text || ''); setInlineEditMode(true); }; const handleSaveBookmark = () => { setInlineEditMode(false); const config = getConfig(bookKey); if (!config || !editorDraft) return; const { booknotes: annotations = [] } = config; const existingIndex = annotations.findIndex((annotation) => item.id === annotation.id); if (existingIndex === -1) return; annotations[existingIndex]!.updatedAt = Date.now(); annotations[existingIndex]!.text = editorDraft; const updatedConfig = updateBooknotes(bookKey, annotations); if (updatedConfig) { saveConfig(envConfig, bookKey, updatedConfig, settings); } }; if (inlineEditMode) { return (
setInlineEditMode(false)} spellCheck={false} />
setInlineEditMode(false)}>{_('Cancel')} {_('Save')}
); } return (
  • { if (e.key === 'Enter' || e.key === ' ') { handleClickItem(e); } else { e.stopPropagation(); } }} >
    {item.note && (
    )}
    {item.note && (
    )}
    {text || ''}
    {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
    e.stopPropagation()} >
    {dayjs(item.createdAt).fromNow()}
    {(item.note || item.type === 'bookmark') && ( {_('Edit')} )} {_('Delete')}
  • ); }; export default BooknoteItem;