mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-05 14:14:39 +00:00
(fix:widget: Keep a back-button entry while any panel is open
This commit is contained in:
1 parent
2d1e940fe1
commit
d57bb00215
1 file changed
+25
-7
@@ -6,15 +6,33 @@ const HISTORY_MARKER = 'dgptOverlay';
|
||||
const layers: Array<() => void> = [];
|
||||
let entryPushed = false;
|
||||
let releaseTimer: ReturnType<typeof setTimeout> | 0 = 0;
|
||||
let releasing = false;
|
||||
|
||||
const ours = () =>
|
||||
(window.history.state as Record<string, unknown> | null)?.[HISTORY_MARKER] ===
|
||||
true;
|
||||
|
||||
const arm = () => {
|
||||
if (entryPushed) return;
|
||||
entryPushed = true;
|
||||
if (!ours()) window.history.pushState({ [HISTORY_MARKER]: true }, '');
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
};
|
||||
|
||||
const handlePopState = () => {
|
||||
if (releasing) {
|
||||
releasing = false;
|
||||
entryPushed = ours();
|
||||
if (layers.length > 0) arm();
|
||||
else if (!entryPushed)
|
||||
window.removeEventListener('popstate', handlePopState);
|
||||
return;
|
||||
}
|
||||
// A host entry stacked above ours pops first, leaving ours in place.
|
||||
entryPushed = ours();
|
||||
const remaining = layers.length > 1;
|
||||
layers[layers.length - 1]?.();
|
||||
if (remaining) arm();
|
||||
};
|
||||
|
||||
const register = (dismiss: () => void) => {
|
||||
@@ -23,10 +41,7 @@ const register = (dismiss: () => void) => {
|
||||
clearTimeout(releaseTimer);
|
||||
releaseTimer = 0;
|
||||
}
|
||||
if (entryPushed) return;
|
||||
entryPushed = true;
|
||||
window.history.pushState({ [HISTORY_MARKER]: true }, '');
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
arm();
|
||||
};
|
||||
|
||||
const unregister = (dismiss: () => void) => {
|
||||
@@ -38,11 +53,14 @@ const unregister = (dismiss: () => void) => {
|
||||
releaseTimer = setTimeout(() => {
|
||||
releaseTimer = 0;
|
||||
if (layers.length > 0) return;
|
||||
window.removeEventListener('popstate', handlePopState);
|
||||
if (!entryPushed) return;
|
||||
entryPushed = false;
|
||||
// Closing by button or overlay would otherwise leave the entry behind.
|
||||
if (ours()) window.history.back();
|
||||
if (ours()) {
|
||||
releasing = true;
|
||||
window.history.back();
|
||||
return;
|
||||
}
|
||||
window.removeEventListener('popstate', handlePopState);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user