File size: 7,432 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 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<SettingsPanelPanelProp> = ({ 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<string | null>(null);
const [draftUIStylesheet, setDraftUIStylesheet] = useState(viewSettings.userUIStylesheet);
const [draftUIStylesheetSaved, setDraftUIStylesheetSaved] = useState(true);
const [uiError, setUIError] = useState<string | null>(null);
const [inputFocusInAndroid, setInputFocusInAndroid] = useState(false);
const contentTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const uiTextareaRef = useRef<HTMLTextAreaElement | null>(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<HTMLTextAreaElement>, 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<HTMLTextAreaElement>) => {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
};
const handleInputFocus = (textareaRef: React.RefObject<HTMLTextAreaElement | null>) => {
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<HTMLTextAreaElement | null>,
settingId?: string,
) => (
<div className='w-full' data-setting-id={settingId}>
<h2 className='mb-2 font-medium' aria-label={_(title)}>
{_(title)}
</h2>
<div
className={`card border-base-200 bg-base-100 border shadow ${error ? 'border-red-500' : ''}`}
>
<div className='relative p-1'>
<textarea
ref={textareaRef}
className={clsx(
'textarea textarea-ghost h-48 w-full border-0 p-3 text-base !outline-none sm:text-sm',
'placeholder:text-base-content/70',
)}
placeholder={_(placeholder)}
spellCheck='false'
value={value}
onFocus={() => handleInputFocus(textareaRef)}
onBlur={handleInputBlur}
onInput={handleInput}
onKeyDown={handleInput}
onKeyUp={handleInput}
onChange={(e) => handleStylesheetChange(e, type)}
/>
<button
className={clsx(
'btn btn-ghost bg-base-200 absolute bottom-2 right-4 h-8 min-h-8 px-4 py-2',
saved ? 'hidden' : '',
error ? 'btn-disabled' : '',
)}
onClick={() => applyStyles(type)}
disabled={!!error}
>
{_('Apply')}
</button>
</div>
</div>
{error && <p className='mt-1 text-sm text-red-500'>{error}</p>}
</div>
);
return (
<div
className={clsx(
'my-4 w-full space-y-6',
inputFocusInAndroid && 'h-[50%] overflow-y-auto pb-[200px]',
)}
>
{renderCSSEditor(
'book',
_('Custom Content CSS'),
_('Enter CSS for book content styling...'),
draftContentStylesheet,
contentError,
draftContentStylesheetSaved,
contentTextareaRef,
'settings.custom.contentCss',
)}
{renderCSSEditor(
'reader',
_('Custom Reader UI CSS'),
_('Enter CSS for reader interface styling...'),
draftUIStylesheet,
uiError,
draftUIStylesheetSaved,
uiTextareaRef,
'settings.custom.readerUiCss',
)}
</div>
);
};
export default MiscPanel;
|