Files
Trilium/apps/client/src/widgets/buttons/history_navigation.ts

107 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-12-23 20:17:04 +01:00
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;
}
2022-12-23 20:17:04 +01:00
export default class HistoryNavigationButton extends ButtonFromNoteWidget {
private webContents?: WebContents;
constructor(launcherNote: FNote, command: string) {
2022-12-23 20:17:04 +01:00
super();
this.title(() => launcherNote.title)
.icon(() => launcherNote.getIcon())
.command(() => command as CommandNames)
2022-12-23 20:17:04 +01:00
.titlePlacement("right")
.buttonNoteIdProvider(() => launcherNote.noteId)
.onContextMenu((e) => { if (e) this.showContextMenu(e); })
2022-12-23 20:17:04 +01:00
.class("launcher-button");
}
2022-11-25 15:29:57 +01:00
2020-01-22 20:48:56 +01:00
doRender() {
2022-11-25 15:29:57 +01:00
super.doRender();
2020-03-08 11:41:42 +01:00
if (utils.isElectron()) {
2025-01-09 18:07:02 +02:00
this.webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
2020-03-08 11:41:42 +01:00
// without this, the history is preserved across frontend reloads
this.webContents?.clearHistory();
this.refresh();
}
2020-01-15 20:10:54 +01:00
}
2020-03-08 11:41:42 +01:00
async showContextMenu(e: JQuery.ContextMenuEvent) {
2022-11-25 15:29:57 +01:00
e.preventDefault();
if (!this.webContents || this.webContents.history.length < 2) {
2022-11-25 15:29:57 +01:00
return;
}
let items: ContextMenuItem[] = [];
2020-03-08 11:41:42 +01:00
const activeIndex = this.webContents.getActiveIndex();
const history = this.webContents.history;
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];
2020-03-08 11:41:42 +01:00
const title = await treeService.getNotePathTitle(notePath);
items.push({
title,
2020-03-08 17:17:18 +01:00
idx,
2025-01-09 18:07:02 +02:00
uiIcon:
parseInt(idx) === activeIndex
2025-01-09 18:07:02 +02:00
? "bx bx-radio-circle-marked" // compare with type coercion!
: parseInt(idx) < activeIndex
2025-01-09 18:07:02 +02:00
? "bx bx-left-arrow-alt"
: "bx bx-right-arrow-alt"
2020-03-08 11:41:42 +01:00
});
}
items.reverse();
if (items.length > 20) {
2020-03-08 17:17:18 +01:00
items = items.slice(0, 50);
2020-03-08 11:41:42 +01:00
}
contextMenu.show({
x: e.pageX,
y: e.pageY,
items,
selectMenuItemHandler: (item: any) => {
if (item && item.idx && this.webContents) {
this.webContents.goToIndex(item.idx);
}
}
2020-03-08 11:41:42 +01:00
});
}
activeNoteChangedEvent() {
this.refresh();
}
2020-07-03 22:27:45 +02:00
}