From c7ce763f4e619985e30e85f4580f0065bfb7ded4 Mon Sep 17 00:00:00 2001 From: troyeguo <13820674+troyeguo@users.noreply.github.com> Date: Sun, 1 Mar 2026 14:58:52 +0800 Subject: [PATCH] feat: add duplicate bookmark check and error message for existing bookmarks --- main.js | 34 ++++++++++++------- src/assets/locales/zh-CN/translation.json | 1 + src/containers/pageWidget/component.tsx | 3 ++ .../panels/operationPanel/component.tsx | 15 ++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/main.js b/main.js index 4e9ee942..7d95108d 100644 --- a/main.js +++ b/main.js @@ -963,14 +963,18 @@ const createMainWin = () => { }); ipcMain.on("get-file-data", function (event) { if (fs.existsSync(path.join(dirPath, "log.json"))) { - const _data = JSON.parse( - fs.readFileSync(path.join(dirPath, "log.json"), "utf-8") || "{}" - ); - if (_data && _data.filePath) { - filePath = _data.filePath; - setTimeout(() => { - fs.writeFileSync(path.join(dirPath, "log.json"), "", "utf-8"); - }, 1000); + try { + const _data = JSON.parse( + fs.readFileSync(path.join(dirPath, "log.json"), "utf-8") || "{}" + ); + if (_data && _data.filePath) { + filePath = _data.filePath; + setTimeout(() => { + fs.writeFileSync(path.join(dirPath, "log.json"), "", "utf-8"); + }, 1000); + } + } catch (error) { + console.error("Error reading log.json:", error); } } @@ -979,11 +983,15 @@ const createMainWin = () => { }); ipcMain.on("check-file-data", function (event) { if (fs.existsSync(path.join(dirPath, "log.json"))) { - const _data = JSON.parse( - fs.readFileSync(path.join(dirPath, "log.json"), "utf-8") || "{}" - ); - if (_data && _data.filePath) { - filePath = _data.filePath; + try { + const _data = JSON.parse( + fs.readFileSync(path.join(dirPath, "log.json"), "utf-8") || "{}" + ); + if (_data && _data.filePath) { + filePath = _data.filePath; + } + } catch (error) { + console.error("Error reading log.json:", error); } } diff --git a/src/assets/locales/zh-CN/translation.json b/src/assets/locales/zh-CN/translation.json index e26f11f6..9e805152 100644 --- a/src/assets/locales/zh-CN/translation.json +++ b/src/assets/locales/zh-CN/translation.json @@ -28,6 +28,7 @@ "Auto open book in full screen": "图书窗口自动全屏", "Your quota will be reset in": "您的额度将于 {{ttl}} 后重置", "System font": "系统字体", + "Bookmark already exists": "书签已存在", "Access token expired, refetching token": "凭证过期,重新获取中", "Access token received, please continue": "获取凭证成功,请继续之前的操作", "Change log": "更新日志", diff --git a/src/containers/pageWidget/component.tsx b/src/containers/pageWidget/component.tsx index ac803142..1b0c7a5f 100644 --- a/src/containers/pageWidget/component.tsx +++ b/src/containers/pageWidget/component.tsx @@ -21,6 +21,9 @@ class Background extends React.Component { nextProps.htmlBook.rendition.on("page-changed", async () => { await this.handlePageNum(nextProps.htmlBook.rendition); }); + nextProps.htmlBook.rendition.on("rendered", async () => { + await this.handlePageNum(nextProps.htmlBook.rendition); + }); } if (nextProps.readerMode !== this.props.readerMode) { this.setState({ isSingle: nextProps.readerMode !== "double" }); diff --git a/src/containers/panels/operationPanel/component.tsx b/src/containers/panels/operationPanel/component.tsx index d4ba16c0..2f29f8d0 100644 --- a/src/containers/panels/operationPanel/component.tsx +++ b/src/containers/panels/operationPanel/component.tsx @@ -99,6 +99,21 @@ class OperationPanel extends React.Component< let text = bookLocation.text; let chapter = bookLocation.chapterTitle; let percentage = bookLocation.percentage; + let isDuplicate = false; + let bookmarks = await DatabaseService.getRecordsByBookKey( + this.props.currentBook.key, + "bookmarks" + ); + for (let i = 0; i < bookmarks.length; i++) { + if (bookmarks[i].percentage === bookLocation.percentage) { + isDuplicate = true; + break; + } + } + if (isDuplicate) { + toast.error(this.props.t("Bookmark already exists")); + return; + } let cfi = JSON.stringify(bookLocation); if (!text) {