Former-commit-id: de1e844903a2fa54910d9c48371af849985c445e
This commit is contained in:
troyeguo
2021-10-18 20:16:00 +08:00
parent 8bd2d03b5c
commit 875ced6db4
13 changed files with 70 additions and 41 deletions

View File

@@ -25,7 +25,7 @@ import animationSiri from "../../assets/lotties/siri.json";
import _ from "underscore";
import BackgroundWidget from "../../components/backgroundWidget";
import toast from "react-hot-toast";
let isFirst = true;
declare var window: any;
const siriOptions = {
loop: true,
@@ -44,6 +44,7 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
this.state = {
key: "",
isLoading: true,
isFirst: true,
scale: OtherUtil.getReaderConfig("scale") || 1,
chapterCount: RecordLocation.getScrollHeight(key).count || 0,
};
@@ -86,6 +87,7 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
document.title = book.name + " - Koodo Reader";
});
});
this.props.handleRenderFunc(this.handleRenderHtml);
window.frames[0].document.addEventListener("click", (event) => {
@@ -176,8 +178,8 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
let htmlParser = new HtmlParser(
new DOMParser().parseFromString(docStr, "text/html")
);
this.props.handleHtmlBook({
key: this.state.key,
doc: htmlParser.getAnchoredDoc(),
chapters: htmlParser.getContentList(),
subitems: [],
@@ -187,28 +189,37 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
htmlParser.getContentList()
) || [],
});
this.handleRenderHtml();
};
handleRenderHtml = () => {
handleRenderHtml = (id: string = "") => {
window.frames[0].document.body.innerHTML = "";
id &&
this.setState({
chapterCount:
_.findIndex(this.props.htmlBook.chapters, {
id: id,
}) + 1,
});
window.frames[0].document.body.innerHTML = this.props.htmlBook.chapterDoc[
3 * this.state.chapterCount
].concat(
this.props.htmlBook.chapterDoc[3 * this.state.chapterCount + 1] || "",
this.props.htmlBook.chapterDoc[3 * this.state.chapterCount + 2] || ""
);
id
? _.findIndex(this.props.htmlBook.chapters, {
id,
}) + 1
: this.state.chapterCount
];
// this.props.handleCurrentChapter("");
this.setState({ isLoading: false });
styleUtil.addHtmlCss();
this.handleIframeHeight();
setTimeout(() => {
if (isFirst) {
if (this.state.isFirst) {
document
.getElementsByClassName("ebook-viewer")[0]
.scrollTo(0, RecordLocation.getScrollHeight(this.state.key).scroll);
isFirst = true;
this.setState({ isFirst: false });
} else {
document.getElementsByClassName("ebook-viewer")[0].scrollTo(0, 0);
}
@@ -248,11 +259,9 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
element.scrollHeight - element.scrollTop - element.clientHeight
) < 10
) {
console.log("scrolled");
if (
this.state.chapterCount >=
this.props.htmlBook.chapterDoc.length / 3 - 1
this.state.chapterCount ===
this.props.htmlBook.chapterDoc.length - 1
) {
return;
}
@@ -293,8 +302,11 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
handleTxt = (result: ArrayBuffer) => {
let text = iconv.decode(
Buffer.from(result),
chardet.detect(Buffer.from(result)) as string
this.props.currentBook.size / 1024 / 1024 > 1
? "utf8"
: (chardet.detect(Buffer.from(result)) as string)
);
this.handleRest(txtToHtml(text));
};
handleMD = (result: ArrayBuffer) => {
@@ -308,7 +320,9 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
handleRtf = (result: ArrayBuffer) => {
let text = iconv.decode(
Buffer.from(result),
chardet.detect(Buffer.from(result)) as string
this.props.currentBook.size / 1024 / 1024 > 1
? "utf8"
: (chardet.detect(Buffer.from(result)) as string)
);
rtfToHTML.fromString(text, (err: any, html: any) => {
this.handleRest(html);
@@ -322,7 +336,9 @@ class Viewer extends React.Component<ViewerProps, ViewerState> {
handleFb2 = (result: ArrayBuffer) => {
let fb2Str = iconv.decode(
Buffer.from(result),
chardet.detect(Buffer.from(result)) as string
this.props.currentBook.size / 1024 / 1024 > 1
? "utf8"
: (chardet.detect(Buffer.from(result)) as string)
);
let bookObj = xmlBookToObj(Buffer.from(result));
bookObj += xmlBookTagFilter(fb2Str);

View File

@@ -17,5 +17,6 @@ export interface ViewerState {
key: string;
scale: string;
isLoading: boolean;
isFirst: boolean;
chapterCount: number;
}

View File

@@ -2,6 +2,8 @@ import React from "react";
import "./contentList.css";
import { ContentListProps, ContentListState } from "./interface";
import OtherUtil from "../../../utils/otherUtil";
import RecordLocation from "../../../utils/readUtils/recordLocation";
import _ from "underscore";
class ContentList extends React.Component<ContentListProps, ContentListState> {
constructor(props: ContentListProps) {
super(props);
@@ -9,6 +11,7 @@ class ContentList extends React.Component<ContentListProps, ContentListState> {
chapters: [],
isCollapsed: true,
currentIndex: -1,
currentChapter: "",
isExpandContent: OtherUtil.getReaderConfig("isExpandContent") === "yes",
};
this.handleJump = this.handleJump.bind(this);
@@ -39,10 +42,18 @@ class ContentList extends React.Component<ContentListProps, ContentListState> {
this.props.currentEpub.rendition.display(href);
} else {
let id = href.substr(1);
var top = window.frames[0].document.getElementById(id)?.offsetTop;
if (!top) return;
document.getElementsByClassName("ebook-viewer")[0].scrollTo(0, top);
RecordLocation.recordScrollHeight(
this.props.htmlBook.key,
document.body.clientWidth,
document.body.clientHeight,
0,
1,
_.findIndex(this.state.chapters, { id })
);
this.props.renderFunc(id);
// var top = window.frames[0].document.getElementById(id)?.offsetTop;
// if (!top) return;
// document.getElementsByClassName("ebook-viewer")[0].scrollTo(0, top);
}
}
UNSAFE_componentWillReceiveProps(nextProps: ContentListProps) {

View File

@@ -6,6 +6,7 @@ const mapStateToProps = (state: stateType) => {
currentEpub: state.book.currentEpub,
chapters: state.reader.chapters,
htmlBook: state.reader.htmlBook,
renderFunc: state.book.renderFunc,
};
};
const actionCreator = {};

View File

@@ -3,10 +3,12 @@ export interface ContentListProps {
currentEpub: any;
chapters: any;
htmlBook: HtmlBookModel;
renderFunc: (id: string) => void;
}
export interface ContentListState {
chapters: any;
currentIndex: number;
currentChapter: string;
isCollapsed: boolean;
isExpandContent: boolean;
}

View File

@@ -22,7 +22,7 @@ class ProgressPanel extends React.Component<
super(props);
this.state = {
displayPercentage: this.props.percentage ? this.props.percentage : 0,
currentChapter: "",
currentPage: 0,
totalPage: 0,
currentChapterIndex: 0,
@@ -44,15 +44,6 @@ class ProgressPanel extends React.Component<
totalPage: currentLocation.start.displayed.total,
currentChapterIndex: currentLocation.start.index,
});
let chapterHref = currentLocation.start.href;
let chapter = "Unknown Chapter";
let currentChapter = this.props.flattenChapters.filter(
(item: any) => item.href.split("#")[0] === chapterHref
)[0];
if (currentChapter) {
chapter = currentChapter.label.trim(" ");
}
this.setState({ currentChapter: chapter });
}
if (nextProps.currentBook.key) {
this.props.handleFetchPercentage(this.props.currentBook);

View File

@@ -11,7 +11,6 @@ export interface ProgressPanelProps {
}
export interface ProgressPanelState {
displayPercentage: number;
currentChapter: string;
currentPage: number;
totalPage: number;
currentChapterIndex: number;

View File

@@ -1,15 +1,17 @@
class HtmlBook {
key:string;
doc:HTMLElement;
chapters:{label:string,id:string,href:string}[];
subitems:any;
chapterDoc:string[];
constructor(
key:string,
doc:HTMLElement,
chapters:{label:string,id:string,href:string}[],
subitems:any,
chapterDoc:string[],
) {
this.key=key,
this.doc=doc,
this.chapters=chapters,
this.subitems=subitems,

View File

@@ -72,7 +72,6 @@ export function flatChapter(chapters: any) {
export function handleFetchChapters(epub: any) {
return (dispatch: (arg0: { type: string; payload: any }) => void) => {
console.log(epub);
epub.loaded.navigation
.then((chapters: any) => {
dispatch(handleChapters(chapters.toc));

View File

@@ -78,6 +78,7 @@ export type stateType = {
chapters: any[];
flattenChapters: any;
noteKey: string;
currentChapter: string;
originalText: string;
htmlBook: HtmlBookModel;
};

View File

@@ -6,6 +6,7 @@ const initState = {
chapters: null,
currentChapter: "",
flattenChapters: null,
color: parseInt(OtherUtil.getReaderConfig("highlightIndex"))
? parseInt(OtherUtil.getReaderConfig("highlightIndex"))
: OtherUtil.getReaderConfig("isDisplayDark") === "yes"
@@ -31,6 +32,11 @@ export function reader(
...state,
notes: action.payload,
};
case "HANDLE_CURRENT_CHAPTER":
return {
...state,
currentChapter: action.payload,
};
case "HANDLE_ORIGINAL_TEXT":
return {
...state,
@@ -46,6 +52,7 @@ export function reader(
...state,
color: action.payload,
};
case "HANDLE_NOTE_KEY":
return {
...state,

View File

@@ -125,7 +125,6 @@ class BookUtil {
}
}
static getBookUrl(book: BookModel) {
console.log(book);
let ref =
book.description === "readonly" || book.description === "pdf"
? book.format.toLowerCase()

View File

@@ -43,19 +43,19 @@ class HtmlParser {
});
}
getChapter(bookStr: string, contentList: any) {
// console.log(bookStr, contentList);
if (contentList.length === 0) return [bookStr];
let chapterDoc: string[] = [];
let chapterStr = "";
console.log(contentList);
for (let i = 0; i < contentList.length; i++) {
if (!bookStr) return;
chapterStr = bookStr.split(contentList[i].id)[0];
bookStr =
'<h1 id="' + contentList[i].id + bookStr.split(contentList[i].id)[1];
chapterStr.substring(chapterStr.lastIndexOf("<")) +
contentList[i].id +
bookStr.split(contentList[i].id)[1];
chapterDoc.push(chapterStr.substring(0, chapterStr.length - 8));
chapterDoc.push(chapterStr.substring(0, chapterStr.lastIndexOf("<")));
if (i === contentList.length - 1) {
chapterDoc.push(bookStr);
}