From fef444cb1ce3e6ef62aeddfb53c62349c6343805 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Sun, 17 Sep 2023 20:25:40 +0800 Subject: [PATCH] fix: when id not exist, still navigate to the tiddler --- core/modules/widgets/blockid.js | 30 ++++++++++++++----- core/modules/widgets/link.js | 2 +- core/modules/widgets/navigator.js | 1 + .../Block Level Links in WikiText.tid | 4 +-- .../tiddlers/wikitext/Linking in WikiText.tid | 4 +-- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/core/modules/widgets/blockid.js b/core/modules/widgets/blockid.js index ea27b4d0a..aa04deee5 100644 --- a/core/modules/widgets/blockid.js +++ b/core/modules/widgets/blockid.js @@ -9,11 +9,18 @@ var Widget = require("$:/core/modules/widgets/widget.js").widget; var BlockIdWidget = function(parseTreeNode,options) { this.initialise(parseTreeNode,options); // only this widget knows target info (if the block is before this node or not), so we need to hook the focus event, and process it here, instead of in the root widget. + this.hookNavigationAddHistoryEvent = this.hookNavigationAddHistoryEvent.bind(this); this.hookNavigatedEvent = this.hookNavigatedEvent.bind(this); + $tw.hooks.addHook("th-navigating-add-history",this.hookNavigationAddHistoryEvent); $tw.hooks.addHook("th-navigated",this.hookNavigatedEvent); }; BlockIdWidget.prototype = new Widget(); +BlockIdWidget.prototype.removeChildDomNodes = function() { + $tw.hooks.removeHook("th-navigating-add-history",this.hookNavigationAddHistoryEvent); + $tw.hooks.removeHook("th-navigated",this.hookNavigatedEvent); +}; + BlockIdWidget.prototype.render = function(parent,nextSibling) { // Save the parent dom node this.parentDomNode = parent; @@ -33,10 +40,15 @@ BlockIdWidget.prototype.render = function(parent,nextSibling) { this.domNodes.push(this.idNode); }; +BlockIdWidget.prototype._isNavigateToHere = function(event) { + if(!event || !event.toBlockId) return false; + if(event.toBlockId !== this.id) return false; + if(this.tiddlerTitle && event.navigateTo !== this.tiddlerTitle) return false; + return true; +} + BlockIdWidget.prototype.hookNavigatedEvent = function(event) { - if(!event || !event.toBlockId) return event; - if(event.toBlockId !== this.id) return event; - if(this.tiddlerTitle && event.navigateTo !== this.tiddlerTitle) return event; + if(!this._isNavigateToHere(event)) return event; var baseElement = event.event && event.event.target ? event.event.target.ownerDocument : document; var element = this._getTargetElement(baseElement); if(element) { @@ -54,6 +66,14 @@ BlockIdWidget.prototype.hookNavigatedEvent = function(event) { return false; }; +BlockIdWidget.prototype.hookNavigationAddHistoryEvent = function(event) { + // DEBUG: console this._isNavigateToHere(event) + console.log(`this._isNavigateToHere(event)`, this._isNavigateToHere(event)); + if(!this._isNavigateToHere(event)) return event; + event.navigateSuppressNavigation = true; + return event; +}; + BlockIdWidget.prototype._getTargetElement = function(baseElement) { var selector = "span[data-block-id='"+this.id+"']"; if(this.tiddlerTitle) { @@ -85,10 +105,6 @@ BlockIdWidget.prototype._scrollToBlockAndHighlight = function(element) { }, 50); }; -BlockIdWidget.prototype.removeChildDomNodes = function() { - $tw.hooks.removeHook("th-navigated",this.hookNavigatedEvent); -}; - /* Compute the internal state of the widget */ diff --git a/core/modules/widgets/link.js b/core/modules/widgets/link.js index 372a45beb..f336f6463 100755 --- a/core/modules/widgets/link.js +++ b/core/modules/widgets/link.js @@ -161,7 +161,7 @@ LinkWidget.prototype.handleClickEvent = function(event) { navigateFromClientRight: bounds.right, navigateFromClientBottom: bounds.bottom, navigateFromClientHeight: bounds.height, - navigateSuppressNavigation: this.toBlockId || event.metaKey || event.ctrlKey || (event.button === 1), + navigateSuppressNavigation: event.metaKey || event.ctrlKey || (event.button === 1), metaKey: event.metaKey, ctrlKey: event.ctrlKey, altKey: event.altKey, diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 1ce51d302..84d69dd6b 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -150,6 +150,7 @@ NavigatorWidget.prototype.handleNavigateEvent = function(event) { event = $tw.hooks.invokeHook("th-navigating",event); if(event.navigateTo) { this.addToStory(event.navigateTo,event.navigateFromTitle); + event = $tw.hooks.invokeHook("th-navigating-add-history",event); if(!event.navigateSuppressNavigation) { this.addToHistory(event.navigateTo,event.navigateFromClientRect); } diff --git a/editions/tw5.com/tiddlers/wikitext/Block Level Links in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Block Level Links in WikiText.tid index 3e0b32964..6b0dfaf5b 100644 --- a/editions/tw5.com/tiddlers/wikitext/Block Level Links in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Block Level Links in WikiText.tid @@ -1,6 +1,6 @@ caption: Block Level Links created: 20230916061138153 -modified: 20230917121127276 +modified: 20230917122221226 tags: WikiText title: Block Level Links in WikiText type: text/vnd.tiddlywiki @@ -35,7 +35,7 @@ Some block, for example, code block, can't be suffixed by `^id`, but we can add ">> -! Link to the block ID ^linkto091607 +! Link to the block ID ^091607 Adding `^blockID` after the title in the link, will make this link highlight the block with that ID. diff --git a/editions/tw5.com/tiddlers/wikitext/Linking in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Linking in WikiText.tid index b7aa53fab..ae80c7088 100644 --- a/editions/tw5.com/tiddlers/wikitext/Linking in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Linking in WikiText.tid @@ -1,6 +1,6 @@ caption: Linking created: 20131205155230596 -modified: 20230916070722108 +modified: 20230917121659927 tags: WikiText title: Linking in WikiText type: text/vnd.tiddlywiki @@ -128,4 +128,4 @@ See [[Anchor Links using HTML]] for more information. You can link to a specific block within a tiddler using `^blockId` syntax. You will also get block level backlinks with this technique. Some examples are in [[BlockIdWidget^exampleid1]]. -See [[Block Level Links in WikiText^linkto091607]] for more information. +See [[Block Level Links in WikiText^091607]] for more information.