import clsx from 'clsx'; import React, { useState } from 'react'; import { MdAdd, MdDelete } from 'react-icons/md'; import { IoMdCloseCircleOutline } from 'react-icons/io'; import { useEnv } from '@/context/EnvContext'; import { useReaderStore } from '@/store/readerStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useSettingsStore } from '@/store/settingsStore'; import { useCustomFontStore } from '@/store/customFontStore'; import { useFileSelector } from '@/hooks/useFileSelector'; import { saveViewSettings } from '@/helpers/settings'; import { CustomFont, mountCustomFont } from '@/styles/fonts'; interface CustomFontsProps { bookKey: string; onBack: () => void; } type FontFamily = { name: string; fonts: CustomFont[]; }; const CustomFonts: React.FC = ({ bookKey, onBack }) => { const _ = useTranslation(); const { appService, envConfig } = useEnv(); const { settings } = useSettingsStore(); const { fonts: customFonts, addFont, loadFont, removeFont, getAvailableFonts, saveCustomFonts, } = useCustomFontStore(); const { getViewSettings } = useReaderStore(); const viewSettings = getViewSettings(bookKey) || settings.globalViewSettings; const [isDeleteMode, setIsDeleteMode] = useState(false); const { selectFiles } = useFileSelector(appService, _); const currentDefaultFont = viewSettings.defaultFont.toLowerCase() === 'serif' ? 'serif' : 'sans-serif'; const currentFontFamily = currentDefaultFont === 'serif' ? viewSettings.serifFont : viewSettings.sansSerifFont; const handleImportFont = () => { selectFiles({ type: 'fonts', multiple: true }).then(async (result) => { if (result.error || result.files.length === 0) return; for (const selectedFile of result.files) { const fontInfo = await appService?.importFont(selectedFile.path || selectedFile.file); if (!fontInfo) continue; const customFont = addFont(fontInfo.path, { name: fontInfo.name, family: fontInfo.family, style: fontInfo.style, weight: fontInfo.weight, variable: fontInfo.variable, }); console.log('Added custom font:', customFont); if (customFont && !customFont.error) { const loadedFont = await loadFont(envConfig, customFont.id); mountCustomFont(document, loadedFont); } } saveCustomFonts(envConfig); }); }; const handleDeleteFamily = (family: FontFamily) => { for (const font of family.fonts) { if (font) { if (removeFont(font.id)) { appService?.deleteFont(font); saveCustomFonts(envConfig); if (getAvailableFonts().length === 0) { setIsDeleteMode(false); } } } } }; const handleSelectFamily = (family: FontFamily) => { if (currentDefaultFont === 'serif') { saveViewSettings(envConfig, bookKey, 'serifFont', family.name); } else { saveViewSettings(envConfig, bookKey, 'sansSerifFont', family.name); } }; const toggleDeleteMode = () => { setIsDeleteMode(!isDeleteMode); }; const getAvailableFamilies = (fonts: CustomFont[]): FontFamily[] => { const familyMap = new Map(); for (const font of fonts) { const family = font.family || font.name; if (!familyMap.has(family)) { familyMap.set(family, []); } familyMap.get(family)!.push(font.id); } return Array.from(familyMap.entries()).map(([family, ids]) => ({ name: family, fonts: ids.map((id) => fonts.find((f) => f.id === id)!).filter((f): f is CustomFont => !!f), })); }; const availableFonts = customFonts .filter((font) => !font.deletedAt) .sort((a, b) => (b.downloadedAt || 0) - (a.downloadedAt || 0)); const availableFamilies = getAvailableFamilies(availableFonts); return (
  • {_('Custom Fonts')}
{availableFonts.length > 0 && ( )}
{availableFamilies.map((family) => (
handleSelectFamily(family) : undefined} title={family.fonts.map((f) => f.name).join('\n')} >
{family.name}
{isDeleteMode && ( )}
))}
{_('Tips')}:
  • {_('Supported font formats: .ttf, .otf, .woff, .woff2')}
  • {_('Custom fonts can be selected from the Font Face menu')}
); }; export default CustomFonts;