Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to EPUB improvements + bug fixes #670

Merged
merged 23 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21,384 changes: 3,062 additions & 18,322 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prepare": "husky install"
},
"dependencies": {
"@cd-z/react-native-epub-creator": "^2.0.0",
"@cd-z/react-native-epub-creator": "^2.0.4",
"@gorhom/bottom-sheet": "^4.4.5",
"@react-native-async-storage/async-storage": "^1.18.1",
"@react-native-community/clipboard": "^1.5.1",
Expand Down Expand Up @@ -89,10 +89,11 @@
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^7.32.0",
"eslint-plugin-react-native": "^3.11.0",
"eslint": "^8.0.0",
"eslint-plugin-react-native": "^4.0.0",
"husky": "^7.0.4",
"lint-staged": "^12.3.7",
"prettier": "^2.8.8",
"typescript": "^4.6.3"
},
"resolutions": {
Expand Down
4 changes: 4 additions & 0 deletions src/components/BottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
BottomSheetModalProps,
} from '@gorhom/bottom-sheet';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useTheme } from '@hooks/useTheme';
import { overlay } from 'react-native-paper';

interface BottomSheetProps extends Omit<BottomSheetModalProps, 'ref'> {
bottomSheetRef: Ref<BottomSheetModal> | null;
}

const BottomSheet: React.FC<BottomSheetProps> = props => {
const { bottom } = useSafeAreaInsets();
const theme = useTheme();

const renderBackdrop = useCallback(
(backdropProps: BottomSheetBackdropProps) => (
Expand All @@ -31,6 +34,7 @@ const BottomSheet: React.FC<BottomSheetProps> = props => {
backdropComponent={renderBackdrop}
handleComponent={null}
containerStyle={{ paddingBottom: bottom }}
backgroundStyle={{ backgroundColor: overlay(2, theme.surface) }}
{...props}
>
{props.children}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SwitchSetting: React.FC<SwitchProps> = ({
paddingHorizontal: 16,
flexDirection: 'row',
justifyContent: 'space-between',
paddingVertical: description ? 16 : 12,
paddingVertical: 12,
},
row: {
flexDirection: 'row',
Expand All @@ -39,7 +39,6 @@ const SwitchSetting: React.FC<SwitchProps> = ({
},
text: {
fontSize: 16,
flex: 1,
textAlignVertical: 'center',
},
switch: { marginLeft: 8 },
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useBoolean.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Dispatch, SetStateAction, useCallback, useState } from 'react';

interface ReturnType {
export interface booleanHookType {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export interface booleanHookType {
export interface BooleanHookType {

value: boolean;
setValue: Dispatch<SetStateAction<boolean>>;
setTrue: () => void;
setFalse: () => void;
toggle: () => void;
}

const useBoolean = (defaultValue?: boolean): ReturnType => {
const useBoolean = (defaultValue?: boolean): booleanHookType => {
const [value, setValue] = useState(!!defaultValue);

const setTrue = useCallback(() => setValue(true), []);
Expand Down
41 changes: 32 additions & 9 deletions src/screens/novel/components/EpubIconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ChapterItem, LibraryNovelInfo } from '@database/types';
import { showToast } from '@hooks/showToast';
import { getChapterFromDb } from '@database/queries/DownloadQueries';
import { useReaderSettings } from '@redux/hooks';
import { useSettings } from '@hooks/reduxHooks';

interface extendedNovelInfo extends LibraryNovelInfo {
chapters: ChapterItem[];
Expand All @@ -28,7 +29,15 @@ const EpubIconButton: React.FC<EpubIconButtonProps> = ({
}) => {
const chooseEpubLocationModal = useBoolean(false);
const readerSettings = useReaderSettings();
const epubStyle = `html {
const {
epubUseAppTheme = false,
epubUseCustomCSS = false,
epubUseCustomJS = false,
} = useSettings();

const epubStyle = `${
epubUseAppTheme
? `html {
scroll-behavior: smooth;
overflow-x: hidden;
padding-top: ${StatusBar.currentHeight};
Expand All @@ -43,6 +52,7 @@ const EpubIconButton: React.FC<EpubIconButtonProps> = ({
text-align: ${readerSettings.textAlign};
line-height: ${readerSettings.lineHeight};
font-family: "${readerSettings.fontFamily}";
background-color: "${readerSettings.theme}";
}
hr {
margin-top: 20px;
Expand All @@ -56,9 +66,25 @@ const EpubIconButton: React.FC<EpubIconButtonProps> = ({
width: auto;
height: auto;
max-width: 100%;
}`
: ''
}

${readerSettings.customCSS}`;
${
epubUseCustomCSS
? readerSettings.customCSS
.replace(RegExp(`\#sourceId-${novel.sourceId}\\s*\\{`, 'g'), 'body {')
.replace(RegExp(`\#sourceId-${novel.sourceId}[^\.\#A-Z]*`, 'gi'), '')
: ''
}`;

const epubJS = `let novelName = "${novel.novelName}";
let chapterName = "";
let sourceId =${novel.sourceId};
let chapterId ="";
let novelId =${novel.novelId};
let html = document.querySelector("chapter").innerHTML;
${readerSettings.customJS}
`;

const createEpub = async (uri: string) => {
var epub = new EpubBuilder(
Expand All @@ -70,7 +96,8 @@ const EpubIconButton: React.FC<EpubIconButtonProps> = ({
description: novel.novelSummary,
author: novel.author,
bookId: novel.novelId.toString(),
stylesheet: epubStyle,
stylesheet: epubStyle || undefined,
js: epubUseCustomJS ? epubJS : undefined,
},
uri,
);
Expand All @@ -85,21 +112,17 @@ const EpubIconButton: React.FC<EpubIconButtonProps> = ({
await epub.addChapter({
title: downloaded.chapterName?.trim() ?? 'Chapter ' + i,
fileName: 'Chapter' + i,
htmlBody: downloaded.chapterText,
htmlBody: `<chapter data-novel-id='${chapter.novelId}' data-chapter-id='${chapter.chapterId}'>${downloaded.chapterText}</chapter>`,
});
}
}
var epubFilePath = await epub.save();
showToast('Epub file saved at: ' + epubFilePath);
} catch (error) {
// remove the temp created folder
showToast('Cannot create because: ' + error);
console.error(error);

await epub.discardChanges();
}
};

return (
<>
<IconButton
Expand Down
Loading