better back/forward navigation

This commit is contained in:
zadam 2020-03-08 17:17:18 +01:00
parent 5f4d963580
commit 493d088d80
4 changed files with 23 additions and 23 deletions

View File

@ -180,6 +180,10 @@ if (utils.isElectron()) {
}); });
} }
if (items.length === 0) {
return;
}
contextMenu.show({ contextMenu.show({
x: params.x, x: params.x,
y: params.y, y: params.y,

View File

@ -138,17 +138,11 @@ export default class Entrypoints extends Component {
} }
backInNoteHistoryCommand() { backInNoteHistoryCommand() {
const electron = require('electron'); window.history.back();
const {webContents} = electron.remote.getCurrentWindow();
webContents.goBack();
} }
forwardInNoteHistoryCommand() {console.log("forward"); forwardInNoteHistoryCommand() {
const electron = require('electron'); window.history.forward();
const {webContents} = electron.remote.getCurrentWindow();
webContents.goForward();
} }
async searchForResultsCommand({searchText}) { async searchForResultsCommand({searchText}) {

View File

@ -104,8 +104,8 @@ export default class TabManager extends Component {
setCurrentNotePathToHash() { setCurrentNotePathToHash() {
const activeTabContext = this.getActiveTabContext(); const activeTabContext = this.getActiveTabContext();
if (activeTabContext if (window.history.length === 0 // first history entry
&& activeTabContext.notePath !== treeService.getHashValueFromAddress()) { || (activeTabContext && activeTabContext.notePath !== treeService.getHashValueFromAddress()[0])) {
const url = '#' + (activeTabContext.notePath || "") + "-" + activeTabContext.tabId; const url = '#' + (activeTabContext.notePath || "") + "-" + activeTabContext.tabId;
// using pushState instead of directly modifying document.location because it does not trigger hashchange // using pushState instead of directly modifying document.location because it does not trigger hashchange
@ -117,9 +117,9 @@ export default class TabManager extends Component {
// it helps navigating in history if note title is included in the title // it helps navigating in history if note title is included in the title
document.title += " - " + activeTabContext.note.title; document.title += " - " + activeTabContext.note.title;
} }
this.triggerEvent('activeNoteChanged');
} }
this.triggerEvent('activeNoteChanged'); // trigger this even in on popstate event
} }
/** @return {TabContext[]} */ /** @return {TabContext[]} */

View File

@ -24,8 +24,7 @@ export default class HistoryNavigationWidget extends BasicWidget {
if (utils.isElectron()) { if (utils.isElectron()) {
this.$widget = $(TPL); this.$widget = $(TPL);
this.$backInHistory = this.$widget.find("[data-trigger-command='backInNoteHistory']"); const contextMenuHandler = e => {
this.$backInHistory.on('contextmenu', e => {
e.preventDefault(); e.preventDefault();
if (this.webContents.history.length < 2) { if (this.webContents.history.length < 2) {
@ -33,13 +32,18 @@ export default class HistoryNavigationWidget extends BasicWidget {
} }
this.showContextMenu(e); this.showContextMenu(e);
}); };
this.$backInHistory = this.$widget.find("[data-trigger-command='backInNoteHistory']");
this.$backInHistory.on('contextmenu', contextMenuHandler);
this.$forwardInHistory = this.$widget.find("[data-trigger-command='forwardInNoteHistory']"); this.$forwardInHistory = this.$widget.find("[data-trigger-command='forwardInNoteHistory']");
this.$forwardInHistory.on('contextmenu', contextMenuHandler);
const electron = require('electron'); const electron = require('electron');
this.webContents = electron.remote.getCurrentWindow().webContents; this.webContents = electron.remote.getCurrentWindow().webContents;
// without this the history is preserved across frontend reloads
this.webContents.clearHistory(); this.webContents.clearHistory();
this.refresh(); this.refresh();
@ -54,7 +58,8 @@ export default class HistoryNavigationWidget extends BasicWidget {
async showContextMenu(e) { async showContextMenu(e) {
let items = []; let items = [];
for (const url of this.webContents.history) { for (const idx in this.webContents.history) {
const url = this.webContents.history[idx];
const [_, notePathWithTab] = url.split('#'); const [_, notePathWithTab] = url.split('#');
const [notePath, tabId] = notePathWithTab.split('-'); const [notePath, tabId] = notePathWithTab.split('-');
@ -62,25 +67,22 @@ export default class HistoryNavigationWidget extends BasicWidget {
items.push({ items.push({
title, title,
notePath, idx,
tabId,
uiIcon: "empty" uiIcon: "empty"
}); });
} }
items.reverse(); items.reverse();
items = items.slice(1); // remove the current note
if (items.length > 20) { if (items.length > 20) {
items = items.slice(0, 20); items = items.slice(0, 50);
} }
contextMenu.show({ contextMenu.show({
x: e.pageX, x: e.pageX,
y: e.pageY, y: e.pageY,
items, items,
selectMenuItemHandler: ({notePath, tabId}) => appContext.tabManager.switchToTab(tabId, notePath) selectMenuItemHandler: ({idx}) => this.webContents.goToIndex(idx)
}); });
} }