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

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-01-15 20:10:54 +01:00
import BasicWidget from "./basic_widget.js";
import utils from "../services/utils.js";
2020-03-08 11:41:42 +01:00
import contextMenu from "../services/context_menu.js";
import treeService from "../services/tree.js";
2020-01-15 20:10:54 +01:00
const TPL = `
<div class="history-navigation">
<style>
.history-navigation {
margin: 0 15px 0 5px;
}
</style>
2020-02-29 14:32:26 +01:00
<a title="Go to previous note." data-trigger-command="backInNoteHistory" class="icon-action bx bx-left-arrow-circle"></a>
2020-01-15 20:10:54 +01:00
2020-02-29 14:32:26 +01:00
<a title="Go to next note." data-trigger-command="forwardInNoteHistory" class="icon-action bx bx-right-arrow-circle"></a>
2020-01-15 20:10:54 +01:00
</div>
`;
export default class HistoryNavigationWidget extends BasicWidget {
2020-01-22 20:48:56 +01:00
doRender() {
if (!utils.isElectron()) {
this.$widget = $("<div>");
return;
}
2020-03-08 11:41:42 +01:00
this.$widget = $(TPL);
2020-03-08 11:41:42 +01:00
const contextMenuHandler = e => {
e.preventDefault();
2020-03-08 11:41:42 +01:00
if (this.webContents.history.length < 2) {
return;
}
2020-03-08 11:41:42 +01:00
this.showContextMenu(e);
};
2020-03-08 11:41:42 +01:00
this.$backInHistory = this.$widget.find("[data-trigger-command='backInNoteHistory']");
this.$backInHistory.on('contextmenu', contextMenuHandler);
2020-03-08 11:41:42 +01:00
this.$forwardInHistory = this.$widget.find("[data-trigger-command='forwardInNoteHistory']");
this.$forwardInHistory.on('contextmenu', contextMenuHandler);
2020-03-08 17:17:18 +01:00
2021-11-16 22:43:08 +01:00
this.webContents = utils.dynamicRequire('@electron/remote').webContents;
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) {
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
}