fix(electron): history navigation context menu not working

This commit is contained in:
Elian Doran 2025-08-31 19:39:54 +03:00
parent 59a01b816c
commit 9d6bb306e7
No known key found for this signature in database

View file

@ -1,16 +1,11 @@
import utils from "../../services/utils.js"; import utils from "../../services/utils.js";
import contextMenu from "../../menus/context_menu.js"; import contextMenu, { MenuCommandItem } from "../../menus/context_menu.js";
import treeService from "../../services/tree.js"; import treeService from "../../services/tree.js";
import ButtonFromNoteWidget from "./button_from_note.js"; import ButtonFromNoteWidget from "./button_from_note.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import type { CommandNames } from "../../components/app_context.js"; import type { CommandNames } from "../../components/app_context.js";
import type { WebContents } from "electron"; import type { WebContents } from "electron";
import link from "../../services/link.js";
interface ContextMenuItem {
title: string;
idx: string;
uiIcon: string;
}
export default class HistoryNavigationButton extends ButtonFromNoteWidget { export default class HistoryNavigationButton extends ButtonFromNoteWidget {
private webContents?: WebContents; private webContents?: WebContents;
@ -47,24 +42,20 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
return; return;
} }
let items: ContextMenuItem[] = []; let items: MenuCommandItem<string>[] = [];
const history = this.webContents.navigationHistory; const history = this.webContents.navigationHistory.getAllEntries();
const activeIndex = history.getActiveIndex(); const activeIndex = this.webContents.navigationHistory.getActiveIndex();
for (const idx in history) { for (const idx in history) {
const url = history[idx]; const { notePath } = link.parseNavigationStateFromUrl(history[idx].url);
const parts = url.split("#"); if (!notePath) continue;
if (parts.length < 2) continue;
const notePathWithTab = parts[1];
const notePath = notePathWithTab.split("-")[0];
const title = await treeService.getNotePathTitle(notePath); const title = await treeService.getNotePathTitle(notePath);
items.push({ items.push({
title, title,
idx, command: idx,
uiIcon: uiIcon:
parseInt(idx) === activeIndex parseInt(idx) === activeIndex
? "bx bx-radio-circle-marked" // compare with type coercion! ? "bx bx-radio-circle-marked" // compare with type coercion!
@ -84,9 +75,10 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
x: e.pageX, x: e.pageX,
y: e.pageY, y: e.pageY,
items, items,
selectMenuItemHandler: (item: any) => { selectMenuItemHandler: (item: MenuCommandItem<string>) => {
if (item && item.idx && this.webContents) { if (item && item.command && this.webContents) {
this.webContents.goToIndex(item.idx); const idx = parseInt(item.command, 10);
this.webContents.navigationHistory.goToIndex(idx);
} }
} }
}); });