fix: when id not exist, still navigate to the tiddler

This commit is contained in:
linonetwo 2023-09-17 20:25:40 +08:00
parent cff0240ac8
commit fef444cb1c
5 changed files with 29 additions and 12 deletions

View File

@ -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
*/

View File

@ -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,

View File

@ -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);
}

View File

@ -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.

View File

@ -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.