Notes/src/public/app/widgets/buttons/history_navigation.js

100 lines
3.0 KiB
JavaScript
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";
export default class HistoryNavigationButton extends ButtonFromNoteWidget {
constructor(launcherNote, command) {
super();
this.title(() => launcherNote.title)
.icon(() => launcherNote.getIcon())
.command(() => command)
.titlePlacement("right")
.buttonNoteIdProvider(() => launcherNote.noteId)
.onContextMenu(e => this.showContextMenu(e))
.class("launcher-button");
}
2022-11-25 15:29:57 +01:00
isEnabled() {
return super.isEnabled() && utils.isElectron();
2020-01-15 20:10:54 +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()) {
return;
}
2022-11-25 15:29:57 +01: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) {
2022-11-25 15:29:57 +01:00
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
return;
}
2022-11-25 15:29:57 +01:00
if (this.webContents.history.length < 2) {
return;
}
2020-03-08 11:41:42 +01:00
let items = [];
const activeIndex = this.webContents.getActiveIndex();
2020-03-08 17:17:18 +01:00
for (const idx in this.webContents.history) {
const url = this.webContents.history[idx];
2020-03-08 11:41:42 +01:00
const [_, notePathWithTab] = url.split('#');
2021-05-22 12:26:45 +02:00
const [notePath, ntxId] = notePathWithTab.split('-');
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,
2022-05-31 22:45:57 +02:00
uiIcon: idx == activeIndex ? "bx bx-radio-circle-marked" : // compare with type coercion!
(idx < activeIndex ? "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,
2020-03-08 17:17:18 +01:00
selectMenuItemHandler: ({idx}) => this.webContents.goToIndex(idx)
2020-03-08 11:41:42 +01:00
});
}
refresh() {
if (!utils.isElectron()) {
return;
}
// disabling this because in electron 9 there's 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());
2020-03-08 11:41:42 +01:00
}
activeNoteChangedEvent() {
this.refresh();
}
2020-07-03 22:27:45 +02:00
}