mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-07 06:14:44 +00:00
feat: adapt for other story views
This commit is contained in:
parent
ebf84b59e8
commit
507d004a57
@ -28,14 +28,12 @@ ClassicStoryView.prototype.navigateTo = function(historyInfo) {
|
|||||||
targetElement = listItemWidget.findFirstDomNode();
|
targetElement = listItemWidget.findFirstDomNode();
|
||||||
// If anchor is provided, find the element the anchor pointing to
|
// If anchor is provided, find the element the anchor pointing to
|
||||||
var foundAnchor = false;
|
var foundAnchor = false;
|
||||||
if(targetElement && historyInfo.anchor) {
|
if(listItemWidget && historyInfo.anchor) {
|
||||||
var anchorElement = targetElement.querySelector("[data-anchor-id='" + historyInfo.anchor + "']");
|
var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) {
|
||||||
if(anchorElement) {
|
return widget.anchorId === historyInfo.anchor;
|
||||||
targetElement = anchorElement.parentNode;
|
});
|
||||||
var isBefore = anchorElement.dataset.anchorPreviousSibling === "true";
|
if(anchorWidget) {
|
||||||
if(isBefore) {
|
targetElement = anchorWidget.findAnchorTargetDomNode()
|
||||||
targetElement = targetElement.previousSibling;
|
|
||||||
}
|
|
||||||
foundAnchor = true;
|
foundAnchor = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,23 @@ PopStoryView.prototype.navigateTo = function(historyInfo) {
|
|||||||
}
|
}
|
||||||
var listItemWidget = this.listWidget.children[listElementIndex],
|
var listItemWidget = this.listWidget.children[listElementIndex],
|
||||||
targetElement = listItemWidget.findFirstDomNode();
|
targetElement = listItemWidget.findFirstDomNode();
|
||||||
|
// If anchor is provided, find the element the anchor pointing to
|
||||||
|
var foundAnchor = false;
|
||||||
|
if(listItemWidget && historyInfo.anchor) {
|
||||||
|
var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) {
|
||||||
|
return widget.anchorId === historyInfo.anchor;
|
||||||
|
});
|
||||||
|
if(anchorWidget) {
|
||||||
|
targetElement = anchorWidget.findAnchorTargetDomNode()
|
||||||
|
foundAnchor = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
// Abandon if the list entry isn't a DOM element (it might be a text node)
|
// Abandon if the list entry isn't a DOM element (it might be a text node)
|
||||||
if(!targetElement || targetElement.nodeType === Node.TEXT_NODE) {
|
if(!targetElement || targetElement.nodeType === Node.TEXT_NODE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Scroll the node into view
|
// Scroll the node into view
|
||||||
this.listWidget.dispatchEvent({type: "tm-scroll", target: targetElement});
|
this.listWidget.dispatchEvent({type: "tm-scroll", target: targetElement, highlight: foundAnchor});
|
||||||
};
|
};
|
||||||
|
|
||||||
PopStoryView.prototype.insert = function(widget) {
|
PopStoryView.prototype.insert = function(widget) {
|
||||||
|
@ -51,6 +51,16 @@ ZoominListView.prototype.navigateTo = function(historyInfo) {
|
|||||||
}
|
}
|
||||||
var listItemWidget = this.listWidget.children[listElementIndex],
|
var listItemWidget = this.listWidget.children[listElementIndex],
|
||||||
targetElement = listItemWidget.findFirstDomNode();
|
targetElement = listItemWidget.findFirstDomNode();
|
||||||
|
// If anchor is provided, find the element the anchor pointing to
|
||||||
|
var anchorElement = null;
|
||||||
|
if(listItemWidget && historyInfo.anchor) {
|
||||||
|
var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) {
|
||||||
|
return widget.anchorId === historyInfo.anchor;
|
||||||
|
});
|
||||||
|
if(anchorWidget) {
|
||||||
|
anchorElement = anchorWidget.findAnchorTargetDomNode()
|
||||||
|
}
|
||||||
|
}
|
||||||
// Abandon if the list entry isn't a DOM element (it might be a text node)
|
// Abandon if the list entry isn't a DOM element (it might be a text node)
|
||||||
if(!targetElement) {
|
if(!targetElement) {
|
||||||
return;
|
return;
|
||||||
@ -119,7 +129,10 @@ ZoominListView.prototype.navigateTo = function(historyInfo) {
|
|||||||
},duration);
|
},duration);
|
||||||
}
|
}
|
||||||
// Scroll the target into view
|
// Scroll the target into view
|
||||||
// $tw.pageScroller.scrollIntoView(targetElement);
|
if(anchorElement) {
|
||||||
|
this.listWidget.dispatchEvent({type: "tm-scroll", target: anchorElement, highlight: true});
|
||||||
|
}
|
||||||
|
// $tw.pageScroller.scrollIntoView(targetElement);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -103,6 +103,21 @@ exports.findParseTreeNode = function(nodeArray,search) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.findChildNodeInTree = function(root,searchFn) {
|
||||||
|
if(searchFn(root)) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if(root.children && root.children.length > 0) {
|
||||||
|
for(var i=0; i<root.children.length; i++) {
|
||||||
|
var result = exports.findChildNodeInTree(root.children[i], searchFn);
|
||||||
|
if(result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Helper to get the text of a parse tree node or array of nodes
|
Helper to get the text of a parse tree node or array of nodes
|
||||||
*/
|
*/
|
||||||
|
@ -21,7 +21,7 @@ AnchorWidget.prototype.render = function(parent,nextSibling) {
|
|||||||
this.execute();
|
this.execute();
|
||||||
// Create an invisible DOM element with data that can be accessed from JS or CSS
|
// Create an invisible DOM element with data that can be accessed from JS or CSS
|
||||||
this.idNode = this.document.createElement("span");
|
this.idNode = this.document.createElement("span");
|
||||||
this.idNode.setAttribute("data-anchor-id",this.id);
|
this.idNode.setAttribute("data-anchor-id",this.anchorId);
|
||||||
this.idNode.setAttribute("data-anchor-title",this.tiddlerTitle);
|
this.idNode.setAttribute("data-anchor-title",this.tiddlerTitle);
|
||||||
// if the actual block is before this node, we need to add a flag to the node
|
// if the actual block is before this node, we need to add a flag to the node
|
||||||
if(this.previousSibling) {
|
if(this.previousSibling) {
|
||||||
@ -37,13 +37,29 @@ Compute the internal state of the widget
|
|||||||
*/
|
*/
|
||||||
AnchorWidget.prototype.execute = function() {
|
AnchorWidget.prototype.execute = function() {
|
||||||
// Get the id from the parse tree node or manually assigned attributes
|
// Get the id from the parse tree node or manually assigned attributes
|
||||||
this.id = this.getAttribute("id");
|
this.anchorId = this.getAttribute("id");
|
||||||
this.tiddlerTitle = this.getVariable("currentTiddler");
|
this.tiddlerTitle = this.getVariable("currentTiddler");
|
||||||
this.previousSibling = this.getAttribute("previousSibling") === "yes";
|
this.previousSibling = this.getAttribute("previousSibling") === "yes";
|
||||||
// Make the child widgets
|
// Make the child widgets
|
||||||
this.makeChildWidgets();
|
this.makeChildWidgets();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the DOM node pointed by this anchor
|
||||||
|
*/
|
||||||
|
Widget.prototype.findAnchorTargetDomNode = function() {
|
||||||
|
if(!this.idNode) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// the actual block is always at the parent level
|
||||||
|
targetElement = this.idNode.parentNode;
|
||||||
|
// need to check if the block is before this node
|
||||||
|
if(this.previousSibling) {
|
||||||
|
targetElement = targetElement.previousSibling;
|
||||||
|
}
|
||||||
|
return targetElement;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user