From a3d9b04d96f3860490b2f416b6ca38ee941cbf22 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:45:26 +0100 Subject: [PATCH] port history navigation, remove redundance --- ...ry_navigation.js => history_navigation.ts} | 75 ++++++++++--------- 1 file changed, 41 insertions(+), 34 deletions(-) rename src/public/app/widgets/buttons/{history_navigation.js => history_navigation.ts} (52%) diff --git a/src/public/app/widgets/buttons/history_navigation.js b/src/public/app/widgets/buttons/history_navigation.ts similarity index 52% rename from src/public/app/widgets/buttons/history_navigation.js rename to src/public/app/widgets/buttons/history_navigation.ts index 4fdaec363..9fa54c60a 100644 --- a/src/public/app/widgets/buttons/history_navigation.js +++ b/src/public/app/widgets/buttons/history_navigation.ts @@ -2,17 +2,36 @@ import utils from "../../services/utils.js"; import contextMenu from "../../menus/context_menu.js"; import treeService from "../../services/tree.js"; import ButtonFromNoteWidget from "./button_from_note.js"; +import type FNote from "../../entities/fnote.js"; +import type { CommandNames } from "../../components/app_context.js"; + +interface WebContents { + history: string[]; + getActiveIndex(): number; + clearHistory(): void; + canGoBack(): boolean; + canGoForward(): boolean; + goToIndex(index: string): void; +} + +interface ContextMenuItem { + title: string; + idx: string; + uiIcon: string; +} export default class HistoryNavigationButton extends ButtonFromNoteWidget { - constructor(launcherNote, command) { + private webContents?: WebContents; + + constructor(launcherNote: FNote, command: string) { super(); this.title(() => launcherNote.title) .icon(() => launcherNote.getIcon()) - .command(() => command) + .command(() => command as CommandNames) .titlePlacement("right") .buttonNoteIdProvider(() => launcherNote.noteId) - .onContextMenu((e) => this.showContextMenu(e)) + .onContextMenu((e) => { if (e) this.showContextMenu(e); }) .class("launcher-button"); } @@ -23,35 +42,31 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { this.webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); // without this, the history is preserved across frontend reloads - this.webContents.clearHistory(); + this.webContents?.clearHistory(); this.refresh(); } } - async showContextMenu(e) { + async showContextMenu(e: JQuery.ContextMenuEvent) { e.preventDefault(); - // API is broken and will be replaced: https://github.com/electron/electron/issues/33899 - // until then no context menu - if (true) { - // avoid warning in dev console + if (!this.webContents || this.webContents.history.length < 2) { return; } - if (this.webContents.history.length < 2) { - return; - } - - let items = []; + let items: ContextMenuItem[] = []; const activeIndex = this.webContents.getActiveIndex(); + const history = this.webContents.history; - for (const idx in this.webContents.history) { - const url = this.webContents.history[idx]; - const [_, notePathWithTab] = url.split("#"); - // broken: use linkService.parseNavigationStateFromUrl(); - const [notePath, ntxId] = notePathWithTab.split("-"); + for (const idx in history) { + const url = history[idx]; + const parts = url.split("#"); + if (parts.length < 2) continue; + + const notePathWithTab = parts[1]; + const notePath = notePathWithTab.split("-")[0]; const title = await treeService.getNotePathTitle(notePath); @@ -59,9 +74,9 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { title, idx, uiIcon: - idx == activeIndex + parseInt(idx) === activeIndex ? "bx bx-radio-circle-marked" // compare with type coercion! - : idx < activeIndex + : parseInt(idx) < activeIndex ? "bx bx-left-arrow-alt" : "bx bx-right-arrow-alt" }); @@ -77,22 +92,14 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { x: e.pageX, y: e.pageY, items, - selectMenuItemHandler: ({ idx }) => this.webContents.goToIndex(idx) + selectMenuItemHandler: (item: any) => { + if (item && item.idx && this.webContents) { + this.webContents.goToIndex(item.idx); + } + } }); } - refresh() { - if (!utils.isElectron()) { - return; - } - - // disabling this because in electron 9 there's a weird performance problem which makes these webContents calls - // block UI thread for > 1 second on specific notes (book notes displaying underlying render notes with scripts) - - // this.$backInHistory.toggleClass('disabled', !this.webContents.canGoBack()); - // this.$forwardInHistory.toggleClass('disabled', !this.webContents.canGoForward()); - } - activeNoteChangedEvent() { this.refresh(); }