Skip to content

Commit c61fe82

Browse files
committed
feat: book toc progress save logic
1 parent 8c72890 commit c61fe82

6 files changed

Lines changed: 35 additions & 43 deletions

File tree

epub_browser/assets/book.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,13 @@ body {
395395
background: var(--border-color);
396396
}
397397

398-
.chapter-list a:target {
398+
/* 除了 hashtag 锚点,还支持 active class */
399+
.chapter-list a:target, .chapter-list a.active {
399400
background: linear-gradient(to right, var(--primary), var(--success));
400401
color: #e9ecef;
401402
}
402403

403-
.chapter-list a:target .chapter-page {
404+
.chapter-list a:target .chapter-page, .chapter-list a.active .chapter-page {
404405
color: #e9ecef;
405406
}
406407

epub_browser/assets/book.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,27 @@ function initScript() {
155155
});
156156
}
157157

158-
// 书籍目录锚点删除
159-
const anchor = window.location.hash;
158+
// 恢复书籍阅读的章节进度
159+
let currentChapter = "";
160160
if (!isKindleMode()) {
161-
if (anchor === '' || !anchor.startsWith('#chapter_')) {
162-
localStorage.removeItem(book_hash); // 此时 lastPart 就是 book_hash
163-
}
161+
currentChapter = localStorage.getItem(book_hash) || "";
164162
} else {
165-
if (anchor === '' || !anchor.startsWith('#chapter_')) {
166-
deleteCookie(book_hash); // 此时 lastPart 就是 book_hash
163+
currentChapter = getCookie(book_hash) || "";
164+
}
165+
if (currentChapter !== "") {
166+
// 激活当前章节
167+
const chapterElement = document.getElementById(currentChapter);
168+
if (chapterElement) {
169+
chapterElement.classList.add('active');
170+
// 滚动到当前章节(兼容性写法)
171+
let tocContainer = document.querySelector('.chapter-list');
172+
if (isKindleMode()) {
173+
// kindle mode 的滚动元素是不同的
174+
tocContainer = document.documentElement
175+
}
176+
if (tocContainer) {
177+
tocContainer.scrollTop = chapterElement.offsetTop - tocContainer.offsetTop - 50;
178+
}
167179
}
168180
}
169181

epub_browser/assets/chapter.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,22 +1654,17 @@ function initScript() {
16541654
wrapAllElements('table', 'div');
16551655
wrapAllElements('img', 'div');
16561656

1657-
// 书籍目录锚点更新
1658-
const lastPart = pathParts[pathParts.length - 1];
1659-
var anchor = '';
1660-
if (lastPart.startsWith('chapter_') && lastPart.endsWith('.html')) {
1661-
anchor = "#" + lastPart.replace('.html', '');
1662-
}
1663-
if (anchor !== '') {
1664-
let bookHomes = document.querySelectorAll('.a-book-home');
1665-
bookHomes.forEach(item => {
1666-
item.href += anchor;
1667-
});
1657+
// 书籍章节进度保存
1658+
let lastPart = pathParts[pathParts.length - 1];
1659+
if (lastPart.startsWith('chapter_')) {
1660+
if (window.location.hash !== '') {
1661+
lastPart += window.location.hash;
1662+
}
16681663
if (!isKindleMode()) {
1669-
localStorage.setItem(book_hash, anchor);
1664+
localStorage.setItem(book_hash, lastPart);
16701665
} else {
1671-
setCookie(book_hash, anchor);
1672-
}
1666+
setCookie(book_hash, lastPart);
1667+
}
16731668
}
16741669

16751670
// 主题切换功能

epub_browser/assets/library.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,6 @@ function initScript() {
119119
});
120120
}
121121

122-
// 书籍目录锚点
123-
const allBookLinks = document.querySelectorAll('.book-card .book-link');
124-
allBookLinks.forEach(item => {
125-
let pathParts = item.href.split('/');
126-
pathParts = pathParts.filter(item => item !== "");
127-
let book_hash = pathParts[pathParts.length - 2]; // 最后一个是 index.html
128-
if (!isKindleMode()) {
129-
let book_anchor = localStorage.getItem(book_hash) || '';
130-
item.href += book_anchor;
131-
} else {
132-
let book_anchor = getCookie(book_hash) || '';
133-
item.href += book_anchor;
134-
}
135-
});
136-
137122
// 主题切换
138123
const themeToggle = document.getElementById('themeToggle');
139124
const themeIcon = themeToggle.querySelector('i');

epub_browser/processor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,16 @@ def create_index_page(self):
494494

495495
if chapter_index is not None:
496496
if chapter_anchor is not None:
497-
# 只加一个正向链接的 anchor 定位,反链接中的 id 不加 anchor 防止章节中锚点乱搞而回来时无法锚定
498-
index_html += f' <li class="{level_class}"><a href="/book/{self.book_hash}/chapter_{chapter_index}.html#{chapter_anchor}" id="chapter_{chapter_index}"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
497+
index_html += f' <li class="{level_class}"><a class="chapter-link" href="/book/{self.book_hash}/chapter_{chapter_index}.html#{chapter_anchor}" id="chapter_{chapter_index}.html#{chapter_anchor}"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
499498
else:
500-
index_html += f' <li class="{level_class}"><a href="/book/{self.book_hash}/chapter_{chapter_index}.html" id="chapter_{chapter_index}"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
499+
index_html += f' <li class="{level_class}"><a class="chapter-link" href="/book/{self.book_hash}/chapter_{chapter_index}.html" id="chapter_{chapter_index}.html"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
501500
toc_item['new_file_name'] = f'chapter_{chapter_index}.html'
502501
else:
503502
print(f"Chapter index not found for toc item: {toc_item['title']} (src: {toc_src})")
504503
else:
505504
# 回退到简单章节列表
506505
for i, chapter in enumerate(self.chapters):
507-
index_html += f' <li><a href="/book/{self.book_hash}/chapter_{i}.html">{chapter["title"]}</a></li>\n'
506+
index_html += f' <li><a class="chapter-link" href="/book/{self.book_hash}/chapter_{i}.html" id="chapter_{i}.html">{chapter["title"]}</a></li>\n'
508507

509508
index_html += f""" </ul>
510509
</div>

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="epub-browser", # 在PyPI上显示的项目名称
9-
version="1.7.16", # 初始版本号
9+
version="1.8.0", # 初始版本号
1010
author="dfface", # 作者名
1111
author_email="dfface@sina.com", # 作者邮箱
1212
keywords="epub reader html export browser convert calibre-web calibre kindle web server local",

0 commit comments

Comments
 (0)