import clsx from 'clsx'; import React, { useEffect, useRef, useState } from 'react'; import { useEnv } from '@/context/EnvContext'; import { useReaderStore } from '@/store/readerStore'; import { useSettingsStore } from '@/store/settingsStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useResetViewSettings } from '@/hooks/useResetSettings'; import { SettingsPanelPanelProp } from './SettingsDialog'; import { saveViewSettings } from '@/helpers/settings'; import { validateCSS, formatCSS } from '@/utils/css'; import { getStyles } from '@/utils/style'; type CSSType = 'book' | 'reader'; const MiscPanel: React.FC = ({ bookKey, onRegisterReset }) => { const _ = useTranslation(); const { appService, envConfig } = useEnv(); const { settings, isSettingsGlobal, setSettings } = useSettingsStore(); const { getView, getViewSettings, setViewSettings } = useReaderStore(); const viewSettings = getViewSettings(bookKey) || settings.globalViewSettings; const [draftContentStylesheet, setDraftContentStylesheet] = useState(viewSettings.userStylesheet); const [draftContentStylesheetSaved, setDraftContentStylesheetSaved] = useState(true); const [contentError, setContentError] = useState(null); const [draftUIStylesheet, setDraftUIStylesheet] = useState(viewSettings.userUIStylesheet); const [draftUIStylesheetSaved, setDraftUIStylesheetSaved] = useState(true); const [uiError, setUIError] = useState(null); const [inputFocusInAndroid, setInputFocusInAndroid] = useState(false); const contentTextareaRef = useRef(null); const uiTextareaRef = useRef(null); const resetToDefaults = useResetViewSettings(); const handleReset = () => { resetToDefaults({ userStylesheet: setDraftContentStylesheet, userUIStylesheet: setDraftUIStylesheet, }); applyStyles('book', true); applyStyles('reader', true); }; useEffect(() => { onRegisterReset(handleReset); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleValidateCSS = (cssInput: string): { isValid: boolean; error?: string } => { if (!cssInput.trim()) return { isValid: true }; try { const { isValid, error } = validateCSS(cssInput); if (!isValid) { return { isValid: false, error: error || 'Invalid CSS' }; } return { isValid: true }; } catch (err: unknown) { if (err instanceof Error) { return { isValid: false, error: err.message }; } return { isValid: false, error: 'Invalid CSS: Please check your input.' }; } }; const handleStylesheetChange = (e: React.ChangeEvent, type: CSSType) => { const cssInput = e.target.value; if (type === 'book') { setDraftContentStylesheet(cssInput); setDraftContentStylesheetSaved(false); const { isValid, error } = handleValidateCSS(cssInput); setContentError(isValid ? null : error || 'Invalid CSS'); } else { setDraftUIStylesheet(cssInput); setDraftUIStylesheetSaved(false); const { isValid, error } = handleValidateCSS(cssInput); setUIError(isValid ? null : error || 'Invalid CSS'); } }; const applyStyles = (type: CSSType, clear = false) => { const cssInput = type === 'book' ? draftContentStylesheet : draftUIStylesheet; const formattedCSS = formatCSS(clear ? '' : cssInput); if (type === 'book') { setDraftContentStylesheet(formattedCSS); setDraftContentStylesheetSaved(true); viewSettings.userStylesheet = formattedCSS; if (isSettingsGlobal) { settings.globalViewSettings.userStylesheet = formattedCSS; setSettings(settings); } } else { setDraftUIStylesheet(formattedCSS); setDraftUIStylesheetSaved(true); viewSettings.userUIStylesheet = formattedCSS; if (isSettingsGlobal) { settings.globalViewSettings.userUIStylesheet = formattedCSS; setSettings(settings); } } setViewSettings(bookKey, { ...viewSettings }); getView(bookKey)?.renderer.setStyles?.(getStyles(viewSettings)); saveViewSettings( envConfig, bookKey, type === 'book' ? 'userStylesheet' : 'userUIStylesheet', formattedCSS, false, false, ); }; const handleInput = (e: React.FormEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); }; const handleInputFocus = (textareaRef: React.RefObject) => { if (appService?.isAndroidApp) { setInputFocusInAndroid(true); } setTimeout(() => { textareaRef.current?.scrollIntoView({ behavior: 'instant', block: 'center', }); }, 300); }; const handleInputBlur = () => { if (appService?.isAndroidApp) { setTimeout(() => { setInputFocusInAndroid(false); }, 100); } }; const renderCSSEditor = ( type: CSSType, title: string, placeholder: string, value: string, error: string | null, saved: boolean, textareaRef: React.RefObject, settingId?: string, ) => (

{_(title)}