implement back/forward for help view (#991)

This commit is contained in:
Mike Sawka 2024-10-08 16:54:41 -07:00 committed by GitHub
parent b8e03e9628
commit 5d6b85cf54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 22 deletions

View File

@ -4,33 +4,41 @@
import { getApi } from "@/app/store/global";
import { WebView, WebViewModel } from "@/app/view/webview/webview";
import { NodeModel } from "@/layout/index";
import { WebviewTag } from "electron";
import { atom } from "jotai";
import { createRef } from "react";
import "./helpview.less";
class HelpViewModel extends WebViewModel {
viewType: string;
blockId: string;
webviewRef: React.RefObject<WebviewTag>;
constructor(blockId: string, nodeModel: NodeModel) {
super(blockId, nodeModel);
this.getSettingsMenuItems = undefined;
this.viewText = atom([
{
elemtype: "iconbutton",
icon: "house",
click: this.handleHome.bind(this),
disabled: this.shouldDisabledHomeButton(),
},
]);
this.viewText = atom((get) => {
// force a dependency on meta.url so we re-render the buttons when the url changes
let url = get(this.blockAtom)?.meta?.url || get(this.homepageUrl);
return [
{
elemtype: "iconbutton",
icon: "chevron-left",
click: this.handleBack.bind(this),
disabled: this.shouldDisableBackButton(),
},
{
elemtype: "iconbutton",
icon: "chevron-right",
click: this.handleForward.bind(this),
disabled: this.shouldDisableForwardButton(),
},
{
elemtype: "iconbutton",
icon: "house",
click: this.handleHome.bind(this),
disabled: this.shouldDisableHomeButton(),
},
];
});
this.homepageUrl = atom(getApi().getDocsiteUrl());
this.viewType = "help";
this.blockId = blockId;
this.viewIcon = atom("circle-question");
this.viewName = atom("Help");
this.webviewRef = createRef<WebviewTag>();
}
}

View File

@ -81,19 +81,19 @@ export class WebViewModel implements ViewModel {
elemtype: "iconbutton",
icon: "chevron-left",
click: this.handleBack.bind(this),
disabled: this.shouldDisabledBackButton(),
disabled: this.shouldDisableBackButton(),
},
{
elemtype: "iconbutton",
icon: "chevron-right",
click: this.handleForward.bind(this),
disabled: this.shouldDisabledForwardButton(),
disabled: this.shouldDisableForwardButton(),
},
{
elemtype: "iconbutton",
icon: "house",
click: this.handleHome.bind(this),
disabled: this.shouldDisabledHomeButton(),
disabled: this.shouldDisableHomeButton(),
},
{
elemtype: "div",
@ -142,7 +142,7 @@ export class WebViewModel implements ViewModel {
* Whether the back button in the header should be disabled.
* @returns True if the WebView cannot go back or if the WebView call fails. False otherwise.
*/
shouldDisabledBackButton() {
shouldDisableBackButton() {
try {
return !this.webviewRef.current?.canGoBack();
} catch (_) {}
@ -153,7 +153,7 @@ export class WebViewModel implements ViewModel {
* Whether the forward button in the header should be disabled.
* @returns True if the WebView cannot go forward or if the WebView call fails. False otherwise.
*/
shouldDisabledForwardButton() {
shouldDisableForwardButton() {
try {
return !this.webviewRef.current?.canGoForward();
} catch (_) {}
@ -164,7 +164,7 @@ export class WebViewModel implements ViewModel {
* Whether the home button in the header should be disabled.
* @returns True if the current url is the pinned url or the pinned url is not set. False otherwise.
*/
shouldDisabledHomeButton() {
shouldDisableHomeButton() {
try {
const homepageUrl = globalStore.get(this.homepageUrl);
return !homepageUrl || this.getUrl() === homepageUrl;