From dfe1b93fe526777d3a98b54adedf243e76f1d906 Mon Sep 17 00:00:00 2001 From: yami Date: Sat, 5 Feb 2022 04:16:58 +0530 Subject: [PATCH] big fixed and ability to pin side list --- package.json | 2 +- src/App.tsx | 113 +++++++++++++++++++++++------- src/Components/Reader.tsx | 28 +++++++- src/Components/ReaderSettings.tsx | 1 + src/Components/ReaderSideList.tsx | 74 +++++++++++++++++-- src/MainImports.ts | 19 +++++ src/styles/index.scss | 38 ++++++++-- 7 files changed, 238 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 9f0f499..6200972 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mangareader", "productName": "Manga Reader", - "version": "2.2.0", + "version": "2.2.1", "description": "App to read manga on desktop", "main": "./.webpack/main/index.js", "author": { diff --git a/src/App.tsx b/src/App.tsx index 7210e3d..d2c7442 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,6 +4,7 @@ import Main from "./Components/Main"; import TopBar from "./Components/TopBar"; import useTheme from "./hooks/useTheme"; import checkforupdate from "./checkforupdate"; +import { settingValidatorData } from "./MainImports"; const userDataURL = window.electron.app.getPath("userData"); const settingsPath = window.path.join(userDataURL, "settings.json"); const themesPath = window.path.join(userDataURL, "themes.json"); @@ -13,8 +14,8 @@ const historyPath = window.path.join(userDataURL, "history.json"); const historyDataInit: ListItem[] = []; const themesMain: { name: string; main: string }[] = []; -const makeSettingsJson = () => { - const settingsData: appsettings = { +const makeSettingsJson = (locations?: string[]) => { + const settingsDataNew: appsettings = { theme: "theme2", bookmarksPath, historyPath, @@ -27,9 +28,18 @@ const makeSettingsJson = () => { readerTypeSelected: 0, pagesPerRowSelected: 0, gapBetweenRows: true, + sideListWidth: 450, }, }; - window.fs.writeFileSync(settingsPath, JSON.stringify(settingsData)); + if (locations) { + const settingsDataSaved: appsettings = JSON.parse(window.fs.readFileSync(settingsPath, "utf-8")); + locations.forEach((e) => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + settingsDataSaved[e] = settingsDataNew[e]; + }); + window.fs.writeFileSync(settingsPath, JSON.stringify(settingsDataSaved)); + } else window.fs.writeFileSync(settingsPath, JSON.stringify(settingsDataNew)); }; if (!window.fs.existsSync(settingsPath)) { @@ -43,29 +53,84 @@ try { console.error(err); makeSettingsJson(); } -function isSettingsValid(): boolean { +//! this function have a lot of @ts-ignore +function isSettingsValid(): { isValid: boolean; location: string[] } { const settings: appsettings = JSON.parse(window.fs.readFileSync(settingsPath, "utf-8")); - return ( - typeof settings.theme === "string" && - typeof settings.bookmarksPath === "string" && - typeof settings.historyPath === "string" && - typeof settings.baseDir === "string" && - typeof settings.historyLimit === "number" && - settings.locationListSortType === ("normal" || "inverse") && - typeof settings.readerSettings === "object" && - typeof settings.readerSettings.readerWidth === "number" && - typeof settings.readerSettings.variableImageSize === "boolean" && - typeof settings.readerSettings.gapBetweenRows === "boolean" && - (settings.readerSettings.readerTypeSelected === 0 || settings.readerSettings.readerTypeSelected === 1) && - (settings.readerSettings.pagesPerRowSelected === 0 || - settings.readerSettings.pagesPerRowSelected === 1 || - settings.readerSettings.pagesPerRowSelected === 2) - ); + const output: { isValid: boolean; location: string[] } = { + isValid: true, + location: [], + }; + for (const key in settingValidatorData) { + if (!Object.prototype.hasOwnProperty.call(settings, key)) { + output.isValid = false; + output.location.push(key); + continue; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (typeof settingValidatorData[key] === "string" && typeof settings[key] !== settingValidatorData[key]) { + output.isValid = false; + output.location.push(key); + continue; + } + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (settingValidatorData[key] instanceof Array) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (!settingValidatorData[key].includes(settings[key])) { + output.isValid = false; + output.location.push(key); + } + continue; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (settingValidatorData[key] instanceof Object) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + for (const key2 in settingValidatorData[key]) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (!Object.prototype.hasOwnProperty.call(settings[key], key2)) { + output.isValid = false; + output.location.push(key); + continue; + } + if ( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + typeof settingValidatorData[key][key2] === "string" && + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + typeof settings[key][key2] !== settingValidatorData[key][key2] + ) { + output.isValid = false; + output.location.push(key); + continue; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (settingValidatorData[key][key2] instanceof Array) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (!settingValidatorData[key][key2].includes(settings[key][key2])) { + output.isValid = false; + output.location.push(key); + } + continue; + } + } + } + } + return output; } -if (!isSettingsValid()) { - window.dialog.customError({ message: `Settings in ${settingsPath} are not invalid. Re-writing settings.` }); - console.warn(`Settings in ${settingsPath} are not invalid. Re-writing settings.`); - makeSettingsJson(); +if (!isSettingsValid().isValid) { + window.dialog.customError({ message: `Some Settings in ${settingsPath} invalid. Re-writing some settings.` }); + console.warn(`Some Settings in ${settingsPath} invalid. Re-writing some settings.`); + console.log(isSettingsValid()); + makeSettingsJson(isSettingsValid().location); } const settings: appsettings = JSON.parse(window.fs.readFileSync(settingsPath, "utf-8")); diff --git a/src/Components/Reader.tsx b/src/Components/Reader.tsx index dbeca3e..85463ba 100644 --- a/src/Components/Reader.tsx +++ b/src/Components/Reader.tsx @@ -7,6 +7,7 @@ import ReaderSettings from "./ReaderSettings"; const Reader = () => { const { appSettings, + setAppSettings, isReaderOpen, setReaderOpen, pageNumberInputRef, @@ -35,6 +36,8 @@ const Reader = () => { const [imageRowCount, setImageRowCount] = useState(0); const [imagesLength, setImagesLength] = useState(0); const [imagesLoaded, setImagesLoaded] = useState(0); + const [isSideListPinned, setSideListPinned] = useState(false); + const [sideListWidth, setSideListWidth] = useState(appSettings.readerSettings.sideListWidth || 450); const [isBookmarked, setBookmarked] = useState(false); const [scrollPosPercent, setScrollPosPercent] = useState(0); const readerSettingExtender = useRef(null); @@ -149,6 +152,8 @@ const Reader = () => { }); }, []); const makeScrollPos = () => { + if (isSideListPinned && imgContRef.current) + return setScrollPosPercent(imgContRef.current.scrollTop / imgContRef.current.scrollHeight); if (readerRef.current) setScrollPosPercent(readerRef.current.scrollTop / readerRef.current.scrollHeight); }; const changePageNumber = () => { @@ -349,13 +354,23 @@ const Reader = () => { }, [imagesLoaded]); useLayoutEffect(() => { readerRef.current?.scrollTo(0, scrollPosPercent * readerRef.current.scrollHeight); - }, [appSettings.readerSettings.readerWidth]); + imgContRef.current?.scrollTo(0, scrollPosPercent * imgContRef.current.scrollHeight); + }, [appSettings.readerSettings.readerWidth, isSideListPinned]); useEffect(() => { if (linkInReader && linkInReader !== "") { if (mangaInReader && mangaInReader.link === linkInReader) return; checkForImgsAndLoad(linkInReader); } }, [linkInReader]); + useLayoutEffect(() => { + if (isSideListPinned) { + readerRef.current?.scrollTo(0, scrollPosPercent * readerRef.current.scrollHeight); + } + setAppSettings((init) => { + init.readerSettings.sideListWidth = sideListWidth; + return { ...init }; + }); + }, [sideListWidth]); useLayoutEffect(() => { changePageNumber(); }, [currentImageRow]); @@ -363,7 +378,12 @@ const Reader = () => {
{ changePageNumber(); }} @@ -382,6 +402,10 @@ const Reader = () => { addToBookmarkRef={addToBookmarkRef} isBookmarked={isBookmarked} setBookmarked={setBookmarked} + isSideListPinned={isSideListPinned} + setSideListPinned={setSideListPinned} + setSideListWidth={setSideListWidth} + makeScrollPos={makeScrollPos} />