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";
|
2025-02-28 15:45:26 +01:00
|
|
|
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 {
|
2025-02-28 15:45:26 +01:00
|
|
|
private webContents?: WebContents;
|
|
|
|
|
|
|
|
|
|
constructor(launcherNote: FNote, command: string) {
|
2022-12-23 20:17:04 +01:00
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.title(() => launcherNote.title)
|
|
|
|
|
.icon(() => launcherNote.getIcon())
|
2025-02-28 15:45:26 +01:00
|
|
|
.command(() => command as CommandNames)
|
2022-12-23 20:17:04 +01:00
|
|
|
.titlePlacement("right")
|
|
|
|
|
.buttonNoteIdProvider(() => launcherNote.noteId)
|
2025-02-28 15:45:26 +01:00
|
|
|
.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
|
|
|
|
2025-01-04 22:43:51 +02: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
|
|
|
|
2025-01-04 22:43:51 +02:00
|
|
|
// without this, the history is preserved across frontend reloads
|
2025-02-28 15:45:26 +01:00
|
|
|
this.webContents?.clearHistory();
|
2020-08-28 23:49:24 +02:00
|
|
|
|
2025-01-04 22:43:51 +02:00
|
|
|
this.refresh();
|
|
|
|
|
}
|
2020-01-15 20:10:54 +01:00
|
|
|
}
|
2020-03-08 11:41:42 +01:00
|
|
|
|
2025-02-28 15:45:26 +01:00
|
|
|
async showContextMenu(e: JQuery.ContextMenuEvent) {
|
2022-11-25 15:29:57 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
2025-02-28 15:45:26 +01:00
|
|
|
if (!this.webContents || this.webContents.history.length < 2) {
|
2022-11-25 15:29:57 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 15:45:26 +01:00
|
|
|
let items: ContextMenuItem[] = [];
|
2020-03-08 11:41:42 +01:00
|
|
|
|
2020-03-17 21:15:57 +01:00
|
|
|
const activeIndex = this.webContents.getActiveIndex();
|
2025-02-28 15:45:26 +01:00
|
|
|
const history = this.webContents.history;
|
2020-03-17 21:15:57 +01:00
|
|
|
|
2025-02-28 15:45:26 +01:00
|
|
|
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:
|
2025-02-28 15:45:26 +01:00
|
|
|
parseInt(idx) === activeIndex
|
2025-01-09 18:07:02 +02:00
|
|
|
? "bx bx-radio-circle-marked" // compare with type coercion!
|
2025-02-28 15:45:26 +01:00
|
|
|
: 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,
|
2025-02-28 15:45:26 +01:00
|
|
|
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
|
|
|
}
|