From a5c2f8558e3f9b64b7a48181d3b9df7ec83bf373 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Sun, 17 Sep 2023 20:03:15 +0800 Subject: [PATCH] feat: allow different tiddler have same block id in the text, and only jump to the one with a same tiddler title. --- core/modules/widgets/blockid.js | 23 ++++++++++++------- .../tiddlers/widgets/BlockIdWidget.tid | 17 +++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/modules/widgets/blockid.js b/core/modules/widgets/blockid.js index 916793ba5..42f096f4a 100644 --- a/core/modules/widgets/blockid.js +++ b/core/modules/widgets/blockid.js @@ -22,20 +22,22 @@ BlockIdWidget.prototype.render = function(parent,nextSibling) { // Execute our logic this.execute(); // Create an invisible DOM element with data that can be accessed from JS or CSS - this.spanDomNode = this.document.createElement("span"); - this.spanDomNode.id = this.id; - this.spanDomNode.setAttribute("data-block-id",this.id); + this.idNode = this.document.createElement("span"); + this.idNode.id = this.id; + this.idNode.setAttribute("data-block-id",this.id); + this.idNode.setAttribute("data-block-title",this.tiddlerTitle); if(this.before) { - this.spanDomNode.setAttribute("data-before","true"); + this.idNode.setAttribute("data-before","true"); } - this.spanDomNode.className = "tc-block-id"; - parent.insertBefore(this.spanDomNode,nextSibling); - this.domNodes.push(this.spanDomNode); + this.idNode.className = "tc-block-id"; + parent.insertBefore(this.idNode,nextSibling); + this.domNodes.push(this.idNode); }; 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; var baseElement = event.event && event.event.target ? event.event.target.ownerDocument : document; var element = this._getTargetElement(baseElement); if(element) { @@ -55,7 +57,11 @@ BlockIdWidget.prototype.hookNavigatedEvent = function(event) { BlockIdWidget.prototype._getTargetElement = function(baseElement) { var selector = "#"+this.id; - // re-query the dom node, because `this.spanDomNode.parentNode` might already be removed from document + if(this.tiddlerTitle) { + // allow different tiddler have same block id in the text, and only jump to the one with a same tiddler title. + selector += "[data-block-title='"+this.tiddlerTitle+"']"; + } + // re-query the dom node, because `this.idNode.parentNode` might already be removed from document var element = $tw.utils.querySelectorSafe(selector,baseElement); if(!element || !element.parentNode) return; // the actual block is always at the parent level @@ -90,6 +96,7 @@ Compute the internal state of the widget BlockIdWidget.prototype.execute = function() { // Get the id from the parse tree node or manually assigned attributes this.id = this.getAttribute("id"); + this.tiddlerTitle = this.getVariable("currentTiddler"); this.previousSibling = this.getAttribute("previousSibling") === "yes"; // Make the child widgets this.makeChildWidgets(); diff --git a/editions/tw5.com/tiddlers/widgets/BlockIdWidget.tid b/editions/tw5.com/tiddlers/widgets/BlockIdWidget.tid index ab8a5e230..47d0cb177 100644 --- a/editions/tw5.com/tiddlers/widgets/BlockIdWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/BlockIdWidget.tid @@ -1,6 +1,6 @@ caption: block id created: 20230916061829840 -modified: 20230916062346854 +modified: 20230917120133519 tags: Widgets title: BlockIdWidget type: text/vnd.tiddlywiki @@ -21,19 +21,14 @@ See [[Block Level Links in WikiText^block091606]] for WikiText syntax of block I ! Example -< +< -[[exampleid1|BlockIdWidget^exampleid1]] +[[Link to BlockLevelLinksID1|BlockIdWidget^BlockLevelLinksID1]] """>> -<<$blockid id="exampleid2" previousSibling="yes"/>

+ID is here:<$blockid id="BlockLevelLinksID2" previousSibling="yes"/> -[[exampleid2|BlockIdWidget^exampleid2]] +[[Link to BlockLevelLinksID2|BlockIdWidget^BlockLevelLinksID2]] """>> - -< - -[[exampleid1|BlockIdWidget^exampleid1]] -""">> \ No newline at end of file