mirror of
https://github.com/TriliumNext/Trilium.git
synced 2025-12-11 05:45:26 -06:00
feat(tab_navigation): reflect state of history by disabling the buttons
This commit is contained in:
parent
5a668ede01
commit
4b2a4b8f7b
@ -1,14 +1,16 @@
|
|||||||
import "./TabHistoryNavigationButtons.css";
|
import "./TabHistoryNavigationButtons.css";
|
||||||
|
|
||||||
|
import { useEffect, useMemo, useState } from "preact/hooks";
|
||||||
|
|
||||||
import { t } from "../services/i18n";
|
import { t } from "../services/i18n";
|
||||||
import ActionButton from "./react/ActionButton";
|
|
||||||
import { useCallback, useMemo } from "preact/hooks";
|
|
||||||
import { handleHistoryContextMenu } from "./launch_bar/HistoryNavigation";
|
|
||||||
import { dynamicRequire } from "../services/utils";
|
import { dynamicRequire } from "../services/utils";
|
||||||
|
import { handleHistoryContextMenu } from "./launch_bar/HistoryNavigation";
|
||||||
|
import ActionButton from "./react/ActionButton";
|
||||||
|
|
||||||
export default function TabHistoryNavigationButtons() {
|
export default function TabHistoryNavigationButtons() {
|
||||||
const webContents = useMemo(() => dynamicRequire("@electron/remote").getCurrentWebContents(), []);
|
const webContents = useMemo(() => dynamicRequire("@electron/remote").getCurrentWebContents(), []);
|
||||||
const onContextMenu = handleHistoryContextMenu(webContents);
|
const onContextMenu = handleHistoryContextMenu(webContents);
|
||||||
|
const { canGoBack, canGoForward } = useBackForwardState(webContents);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="tab-history-navigation-buttons">
|
<div className="tab-history-navigation-buttons">
|
||||||
@ -17,13 +19,37 @@ export default function TabHistoryNavigationButtons() {
|
|||||||
text={t("tab_history_navigation_buttons.go-back")}
|
text={t("tab_history_navigation_buttons.go-back")}
|
||||||
triggerCommand="backInNoteHistory"
|
triggerCommand="backInNoteHistory"
|
||||||
onContextMenu={onContextMenu}
|
onContextMenu={onContextMenu}
|
||||||
|
disabled={!canGoBack}
|
||||||
/>
|
/>
|
||||||
<ActionButton
|
<ActionButton
|
||||||
icon="bx bx-right-arrow-alt"
|
icon="bx bx-right-arrow-alt"
|
||||||
text={t("tab_history_navigation_buttons.go-forward")}
|
text={t("tab_history_navigation_buttons.go-forward")}
|
||||||
triggerCommand="forwardInNoteHistory"
|
triggerCommand="forwardInNoteHistory"
|
||||||
onContextMenu={onContextMenu}
|
onContextMenu={onContextMenu}
|
||||||
|
disabled={!canGoForward}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function useBackForwardState(webContents: Electron.WebContents) {
|
||||||
|
const [ canGoBack, setCanGoBack ] = useState(webContents.navigationHistory.canGoBack());
|
||||||
|
const [ canGoForward, setCanGoForward ] = useState(webContents.navigationHistory.canGoForward());
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const updateNavigationState = () => {
|
||||||
|
setCanGoBack(webContents.navigationHistory.canGoBack());
|
||||||
|
setCanGoForward(webContents.navigationHistory.canGoForward());
|
||||||
|
};
|
||||||
|
|
||||||
|
webContents.on("did-navigate", updateNavigationState);
|
||||||
|
webContents.on("did-navigate-in-page", updateNavigationState);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
webContents.removeListener("did-navigate", updateNavigationState);
|
||||||
|
webContents.removeListener("did-navigate-in-page", updateNavigationState);
|
||||||
|
};
|
||||||
|
}, [ webContents ]);
|
||||||
|
|
||||||
|
return { canGoBack, canGoForward };
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user