From 0d40b691e71a11937e2f6576e3d1082b1abcc9c6 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 27 Aug 2018 18:12:54 +0100 Subject: [PATCH] Make "close tiddler" button in STM go back to the previous tiddler in the history stack --- core/modules/widgets/navigator.js | 39 +++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 6ed2c6453..7595dfbca 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -127,6 +127,12 @@ NavigatorWidget.prototype.addToStory = function(title,fromTitle) { }); }; +NavigatorWidget.prototype.removeFromStory = function(title) { + var storyList = this.getStoryList(); + this.removeTitleFromStory(storyList,title); + this.saveStoryList(storyList); +}; + /* Add a new record to the top of the history stack title: a title string or an array of title strings @@ -152,11 +158,34 @@ NavigatorWidget.prototype.handleNavigateEvent = function(event) { // Close a specified tiddler NavigatorWidget.prototype.handleCloseTiddlerEvent = function(event) { - var title = event.param || event.tiddlerTitle, - storyList = this.getStoryList(); - // Look for tiddlers with this title to close - this.removeTitleFromStory(storyList,title); - this.saveStoryList(storyList); + var title = event.param || event.tiddlerTitle; + if(this.singleTiddlerMode) { + // Get the history stack and find the topmost occurance of the current tiddler + var history = this.wiki.getTiddlerDataCached(this.historyTitle,[]), + currPos = history.findIndex(function(historyRecord) { + return historyRecord.title === title; + }), + newTitle; + // Skip over any duplicates + while(currPos > 0 && history[currPos - 1].title === title) { + currPos--; + } + // Get the new title + if(currPos > 0) { + newTitle = history[currPos - 1].title; + } + // Navigate to the new title if we've got one + if(newTitle) { + this.addToStory(newTitle); + this.addToHistory(newTitle); + } else { + // If there's nothing to navigate back to then we really do close the last tiddler + this.removeFromStory(title); + } + } else { + // Look for tiddlers with this title to close + this.removeFromStory(title); + } return false; };