Skip to content

Commit 6a02b9a

Browse files
committed
fix: #41 Fixed the bug that the beforeunload event was triggered incorrectly
1 parent f4e8272 commit 6a02b9a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,42 @@ const vaultWindows = new Set(),
9797
else showWindows();
9898
};
9999

100+
// 全局变量,用于标记是否通过点击链接触发导航
101+
let isLinkNavigation = false;
102+
100103
const onWindowClose = (event) => event.preventDefault(),
101104
onWindowUnload = (event) => {
105+
if (isLinkNavigation) {
106+
// 重置标记以避免影响后续事件
107+
isLinkNavigation = false;
108+
return;
109+
}
110+
102111
log(LOG_WINDOW_CLOSE);
103112
getCurrentWindow().hide();
104113
event.stopImmediatePropagation();
105114
// setting return value manually is more reliable than
106115
// via `return false` according to electron
107116
event.returnValue = false;
108117
},
118+
onAnchorClick = (event) => {
119+
const target = event.target.closest('a');
120+
if (target && target.getAttribute('target') !== '_blank') {
121+
// 如果是 <a> 元素且没有 target="_blank",标记为链接导航
122+
isLinkNavigation = true;
123+
}
124+
},
109125
interceptWindowClose = () => {
110126
// intercept in renderer
127+
document.addEventListener('click', onAnchorClick);
111128
window.addEventListener("beforeunload", onWindowUnload, true);
112129
// intercept in main: is asynchronously executed when registered
113130
// from renderer, so won't prevent close by itself, but counteracts
114131
// the 3-second delayed window force close in obsidian.asar/main.js
115132
getCurrentWindow().on("close", onWindowClose);
116133
},
117134
allowWindowClose = () => {
135+
document.removeEventListener('click', onAnchorClick);
118136
getCurrentWindow().removeListener("close", onWindowClose);
119137
window.removeEventListener("beforeunload", onWindowUnload, true);
120138
};

0 commit comments

Comments
 (0)