-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: allow link to section mark #7744
Open
linonetwo
wants to merge
32
commits into
TiddlyWiki:master
Choose a base branch
from
linonetwo:feat/section-mark
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
5b6f5b2
feat: parse and show ^id
linonetwo 4c407c2
refactor: use blockid for shorter name
linonetwo 18236b5
feat: allow add id for code block
linonetwo d5e9d2a
feat: allow wiki pretty link to have id
linonetwo b956e72
fix: properly match blockId and pass it to ast
linonetwo 3d8ade3
feat: redirect tm-focus-selector event to check parent or sibling
linonetwo 6b61243
fix: param maybe null
linonetwo d8bdd09
fix: ensure hightlight is visible
linonetwo 0863916
fix: wait until animation finish and dom show
linonetwo e6445b7
docs: why add hook
linonetwo db83401
docs: about usage
linonetwo 7200f73
refactor: use th-navigated to simplify the code
linonetwo c0b6b79
fix: element not exist
linonetwo 3bcd822
fix: scroll too slow if tiddler already appear
linonetwo 07130c2
fix: code style and types
linonetwo a5c2f85
feat: allow different tiddler have same block id in the text, and onl…
linonetwo cff0240
feat: allow using any char in id
linonetwo fef444c
fix: when id not exist, still navigate to the tiddler
linonetwo 0d18b25
Update blockid.js
linonetwo dfa0600
docs: about why history change will reflect on storyview
linonetwo 436343c
refactor: use history mechanism for block level navigation
linonetwo ebf84b5
docs: about HistoryMechanism in dev doc
linonetwo 507d004
feat: adapt for other story views
linonetwo 48d2eff
fix: no need for setTimeout
linonetwo 13f6bd8
refactor: remove unusned hook
linonetwo 13d0c5c
docs: about toAnchor added in 5.3.2
linonetwo 6d16c98
Merge remote-tracking branch 'upstream/master' into feat/section-mark
linonetwo 9c2c6d7
fix: link end position should add ^id 's length
linonetwo 7e9cadf
Revert "fix: link end position should add ^id 's length"
linonetwo 92ca17a
fix: correct anchor start end
linonetwo cc12af5
docs: fix BlockIdWidget
linonetwo cb2d4bb
Merge branch 'master' into feat/section-mark
linonetwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*\ | ||
title: $:/core/modules/parsers/wikiparser/rules/blockidentifier.js | ||
type: application/javascript | ||
module-type: wikirule | ||
|
||
Use hash as a tag for paragraph, we call it block identifier. | ||
|
||
1. Hash won't change, it can be written by hand or be generated, and it is a ` \^\S+$` string after line: `text ^cb9d485` or `text ^1`, so it can be human readable (while without space), here are the parse rule for this. | ||
2. When creating widgets for rendering, omit this hash, so it's invisible in view mode. But this widget will create an anchor to jump to. | ||
|
||
\*/ | ||
exports.name = "blockid"; | ||
exports.types = {inline: true}; | ||
|
||
/* | ||
Instantiate parse rule | ||
*/ | ||
exports.init = function(parser) { | ||
this.parser = parser; | ||
// Regexp to match the block identifier | ||
// 1. located on the end of the line, with a space before it, means it's the id of the current block. | ||
// 2. located at start of the line, no space, means it's the id of the previous block. Because some block can't have id suffix, otherwise id break the block mode parser like codeblock. | ||
this.matchRegExp = /[ ]\^(\S+)$|^\^(\S+)$/mg; | ||
}; | ||
|
||
/* | ||
Parse the most recent match | ||
*/ | ||
exports.parse = function() { | ||
// Move past the match | ||
this.parser.pos = this.matchRegExp.lastIndex; | ||
// will be one of following case, another will be undefined | ||
var blockId = this.match[1]; | ||
var blockBeforeId = this.match[2]; | ||
// Parse tree nodes to return | ||
return [{ | ||
type: "blockid", | ||
attributes: { | ||
id: {type: "string", value: blockId || blockBeforeId}, | ||
// `yes` means the block is before this node, in parent node's children list. | ||
// empty means the block is this node's direct parent node. | ||
previousSibling: {type: "string", value: Boolean(blockBeforeId) ? "yes" : ""}, | ||
}, | ||
children: [] | ||
}]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/*\ | ||
title: $:/core/modules/widgets/blockid.js | ||
type: application/javascript | ||
module-type: widget | ||
|
||
An invisible element with block id metadata. | ||
\*/ | ||
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; | ||
// Compute our attributes | ||
this.computeAttributes(); | ||
// Execute our logic | ||
this.execute(); | ||
// Create an invisible DOM element with data that can be accessed from JS or CSS | ||
this.idNode = this.document.createElement("span"); | ||
this.idNode.setAttribute("data-block-id",this.id); | ||
this.idNode.setAttribute("data-block-title",this.tiddlerTitle); | ||
if(this.before) { | ||
this.idNode.setAttribute("data-before","true"); | ||
} | ||
this.idNode.className = "tc-block-id"; | ||
parent.insertBefore(this.idNode,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(!this._isNavigateToHere(event)) return event; | ||
var baseElement = event.event && event.event.target ? event.event.target.ownerDocument : document; | ||
var element = this._getTargetElement(baseElement); | ||
if(element) { | ||
// if tiddler is already in the story view, just move to it. | ||
this._scrollToBlockAndHighlight(element); | ||
} else { | ||
var self = this; | ||
// Here we still need to wait for extra time after `duration`, so tiddler dom is actually added to the story view. | ||
var duration = $tw.utils.getAnimationDuration() + 50; | ||
setTimeout(function() { | ||
element = self._getTargetElement(baseElement); | ||
self._scrollToBlockAndHighlight(element); | ||
}, duration); | ||
} | ||
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) { | ||
// 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 | ||
element = element.parentNode; | ||
// need to check if the block is before this node | ||
if(this.previousSibling && element.previousSibling) { | ||
element = element.previousSibling; | ||
} | ||
return element; | ||
}; | ||
|
||
BlockIdWidget.prototype._scrollToBlockAndHighlight = function(element) { | ||
if(!element) return; | ||
// toggle class to trigger highlight animation | ||
$tw.utils.removeClass(element,"tc-focus-highlight"); | ||
// We enable the `navigateSuppressNavigation` in LinkWidget when sending `tm-navigate`, otherwise `tm-navigate` will force move to the title | ||
element.scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest" }); | ||
element.focus({ focusVisible: true }); | ||
// Using setTimeout to ensure the removal takes effect before adding the class again. | ||
setTimeout(function() { | ||
$tw.utils.addClass(element,"tc-focus-highlight"); | ||
}, 50); | ||
}; | ||
|
||
/* | ||
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(); | ||
}; | ||
|
||
/* | ||
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering | ||
*/ | ||
BlockIdWidget.prototype.refresh = function(changedTiddlers) { | ||
var changedAttributes = this.computeAttributes(); | ||
if(($tw.utils.count(changedAttributes) > 0)) { | ||
this.refreshSelf(); | ||
return true; | ||
} else { | ||
return this.refreshChildren(changedTiddlers); | ||
} | ||
}; | ||
|
||
exports.blockid = BlockIdWidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
tags: HookMechanism | ||
title: Hook: th-navigated | ||
type: text/vnd.tiddlywiki | ||
|
||
This hook allows plugins to do things after navigation takes effect. | ||
|
||
Hook function parameters are same as [[Hook: th-navigating]]: | ||
|
||
Return value: | ||
|
||
* possibly modified event object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
caption: block id | ||
created: 20230916061829840 | ||
modified: 20230917121007649 | ||
tags: Widgets | ||
title: BlockIdWidget | ||
type: text/vnd.tiddlywiki | ||
|
||
! Introduction | ||
|
||
The block id widget make an anchor that can be focused and jump to. | ||
|
||
! Content and Attributes | ||
|
||
The content of the `<$blockid>` widget is ignored. | ||
|
||
|!Attribute |!Description | | ||
|id |The unique id for the block | | ||
|previousSibling |`yes` means the block is before this node, in parent node's children list, else it means the block is this node's direct parent node. | | ||
|
||
See [[Block Level Links in WikiText^🤗→AddingIDforblock]] for WikiText syntax of block ID. | ||
|
||
! Example | ||
|
||
<<wikitext-example-without-html """The block id widget is invisible, and is usually located at the end of the line. ID is here:<$blockid id="BlockLevelLinksID1"/> | ||
|
||
[[Link to BlockLevelLinksID1|BlockIdWidget^BlockLevelLinksID1]] | ||
""">> | ||
|
||
<<wikitext-example """You can refer to the block that is a line before the block id widget. Make sure block id widget itself is in a block (paragraph). | ||
|
||
ID is here:<$blockid id="BlockLevelLinksID2" previousSibling="yes"/> | ||
|
||
[[Link to BlockLevelLinksID2|BlockIdWidget^BlockLevelLinksID2]] | ||
""">> |
46 changes: 46 additions & 0 deletions
46
editions/tw5.com/tiddlers/wikitext/Block Level Links in WikiText.tid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
caption: Block Level Links | ||
created: 20230916061138153 | ||
modified: 20230917122221226 | ||
tags: WikiText | ||
title: Block Level Links in WikiText | ||
type: text/vnd.tiddlywiki | ||
|
||
! Adding ID for block ^🤗→AddingIDforblock | ||
|
||
The basic syntax for block id is: | ||
|
||
<<wikitext-example src:"There is a block id that is invisible, but you can find it using developer tool's inspect element feature. ^BlockLevelLinksID1">> | ||
|
||
# Don't forget the space between the end of the line and the `^`. | ||
# And there is no space between `^` and the id. | ||
# ID can contain any char other than `^` and space ` `. | ||
|
||
And this block id widget will be rendered as an invisible element: | ||
|
||
```html | ||
<span class="tc-block-id" data-block-id="BlockLevelLinksID1" data-block-title="Block Level Links in WikiText"></span> | ||
``` | ||
|
||
!! Adding id to previous block | ||
|
||
Some block, for example, code block, can't be suffixed by `^id`, but we can add the id in the next line, with no space prefix to it. | ||
|
||
<<wikitext-example src:"```css | ||
.main { | ||
display: none; | ||
} | ||
``` | ||
|
||
^BlockLevelLinksID2 | ||
|
||
">> | ||
|
||
! Link to the block ID ^091607 | ||
|
||
Adding `^blockID` after the title in the link, will make this link highlight the block with that ID. | ||
|
||
<<wikitext-example-without-html src:"[[Link to BlockLevelLinksID1|Block Level Links in WikiText^BlockLevelLinksID1]]">> | ||
|
||
<<wikitext-example-without-html src:"[[Link to BlockLevelLinksID2|Block Level Links in WikiText^BlockLevelLinksID2]]">> | ||
|
||
<<wikitext-example src:"[[Link to Title|Block Level Links in WikiText^🤗→AddingIDforblock]]">> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the TW core we still use ES5 only. So JS template literals are not possible atm:
`this._isNavigateToHere(event)`
So this code needs to be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is debug statement, forget to delete...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created by this in vscode BTW