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

101 lines
2.9 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";
import keyboardActionService from "../services/keyboard_actions.js";
2020-03-08 11:41:42 +01:00
import contextMenu from "../services/context_menu.js";
import treeService from "../services/tree.js";
import appContext from "../services/app_context.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() {
2020-02-05 22:46:20 +01:00
if (utils.isElectron()) {
this.$widget = $(TPL);
2020-03-08 11:41:42 +01:00
2020-03-08 17:17:18 +01:00
const contextMenuHandler = e => {
2020-03-08 11:41:42 +01:00
e.preventDefault();
if (this.webContents.history.length < 2) {
return;
}
this.showContextMenu(e);
2020-03-08 17:17:18 +01:00
};
2020-03-08 11:41:42 +01:00
2020-03-08 17:17:18 +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']");
2020-03-08 17:17:18 +01:00
this.$forwardInHistory.on('contextmenu', contextMenuHandler);
2020-03-08 11:41:42 +01:00
const electron = require('electron');
this.webContents = electron.remote.getCurrentWindow().webContents;
2020-03-08 17:17:18 +01:00
// without this the history is preserved across frontend reloads
2020-03-08 11:41:42 +01:00
this.webContents.clearHistory();
this.refresh();
2020-02-05 22:46:20 +01:00
}
else {
this.$widget = $("<div>");
2020-01-15 20:10:54 +01:00
}
return this.$widget;
}
2020-03-08 11:41:42 +01:00
async showContextMenu(e) {
let items = [];
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('#');
const [notePath, tabId] = notePathWithTab.split('-');
const title = await treeService.getNotePathTitle(notePath);
items.push({
title,
2020-03-08 17:17:18 +01:00
idx,
2020-03-08 11:41:42 +01:00
uiIcon: "empty"
});
}
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;
}
this.$backInHistory.toggleClass('disabled', !this.webContents.canGoBack());
this.$forwardInHistory.toggleClass('disabled', !this.webContents.canGoForward());
}
activeNoteChangedEvent() {
this.refresh();
}
2020-01-15 20:10:54 +01:00
}