1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-01 11:49:10 +00:00

Update render.js

This commit is contained in:
Simon Huber 2024-10-14 06:52:21 +02:00 committed by GitHub
parent e44d6f4d7b
commit 0a7c57c860
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,50 +50,15 @@ exports.startup = function() {
$tw.styleElement = document.createElement("style"); $tw.styleElement = document.createElement("style");
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles; $tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
document.head.insertBefore($tw.styleElement,document.head.firstChild); document.head.insertBefore($tw.styleElement,document.head.firstChild);
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
if($tw.styleWidgetNode.refresh(changes,$tw.styleContainer,null)) {
var newStyles = $tw.styleContainer.textContent;
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
$tw.styleWidgetNode.assignedStyles = newStyles;
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
}
}
}));
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
$tw.perf.report("mainRender",function() {
$tw.pageWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_TEMPLATE_TITLE,{document: document, parentWidget: $tw.rootWidget, recursionMarker: "no"});
$tw.pageContainer = document.createElement("div");
$tw.utils.addClass($tw.pageContainer,"tc-page-container-wrapper");
document.body.insertBefore($tw.pageContainer,document.body.firstChild);
$tw.pageWidgetNode.render($tw.pageContainer,null);
$tw.hooks.invokeHook("th-page-refreshed");
})();
// Remove any splash screen elements
var removeList = document.querySelectorAll(".tc-remove-when-wiki-loaded");
$tw.utils.each(removeList,function(removeItem) {
if(removeItem.parentNode) {
removeItem.parentNode.removeChild(removeItem);
}
});
// Prepare refresh mechanism // Prepare refresh mechanism
var deferredChanges = Object.create(null), var deferredChanges = Object.create(null),
timerId; timerId,
function refresh() { throttledRefreshFn = function(changes,callback,mainCondition,styleCondition) {
// Process the refresh
$tw.hooks.invokeHook("th-page-refreshing");
$tw.pageWidgetNode.refresh(deferredChanges);
deferredChanges = Object.create(null);
$tw.hooks.invokeHook("th-page-refreshed");
}
var throttledRefresh = $tw.perf.report("throttledRefresh",refresh);
// Add the change event handler
$tw.wiki.addEventListener("change",$tw.perf.report("mainRefresh",function(changes) {
// Check if only tiddlers that are throttled have changed // Check if only tiddlers that are throttled have changed
var onlyThrottledTiddlersHaveChanged = true; var onlyThrottledTiddlersHaveChanged = true;
for(var title in changes) { for(var title in changes) {
var tiddler = $tw.wiki.getTiddler(title); var tiddler = $tw.wiki.getTiddler(title);
if(!$tw.wiki.isVolatileTiddler(title) && (!tiddler || !(tiddler.hasField("draft.of") || tiddler.hasField("throttle.refresh")))) { if(!$tw.wiki.isVolatileTiddler(title) && (!tiddler || !(tiddler.hasField("draft.of") || tiddler.hasField("throttle.refresh") || (mainCondition && tiddler.hasField("throttle.refresh.main")) || (styleCondition && tiddler.hasField("throttle.refresh.style"))))) {
onlyThrottledTiddlersHaveChanged = false; onlyThrottledTiddlersHaveChanged = false;
} }
} }
@ -111,8 +76,49 @@ exports.startup = function() {
$tw.utils.extend(deferredChanges,changes); $tw.utils.extend(deferredChanges,changes);
} else { } else {
$tw.utils.extend(deferredChanges,changes); $tw.utils.extend(deferredChanges,changes);
refresh(); callback(changes);
} }
};
function styleRefresh(changes) {
if($tw.styleWidgetNode.refresh(changes,$tw.styleContainer,null)) {
var newStyles = $tw.styleContainer.textContent;
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
$tw.styleWidgetNode.assignedStyles = newStyles;
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
}
}
}
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
throttledRefreshFn(changes,styleRefresh,false,true);
}));
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
$tw.perf.report("mainRender",function() {
$tw.pageWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_TEMPLATE_TITLE,{document: document, parentWidget: $tw.rootWidget, recursionMarker: "no"});
$tw.pageContainer = document.createElement("div");
$tw.utils.addClass($tw.pageContainer,"tc-page-container-wrapper");
document.body.insertBefore($tw.pageContainer,document.body.firstChild);
$tw.pageWidgetNode.render($tw.pageContainer,null);
$tw.hooks.invokeHook("th-page-refreshed");
})();
// Remove any splash screen elements
var removeList = document.querySelectorAll(".tc-remove-when-wiki-loaded");
$tw.utils.each(removeList,function(removeItem) {
if(removeItem.parentNode) {
removeItem.parentNode.removeChild(removeItem);
}
});
function refresh() {
// Process the refresh
$tw.hooks.invokeHook("th-page-refreshing");
$tw.pageWidgetNode.refresh(deferredChanges);
deferredChanges = Object.create(null);
$tw.hooks.invokeHook("th-page-refreshed");
}
var throttledRefresh = $tw.perf.report("throttledRefresh",refresh);
// Add the change event handler
$tw.wiki.addEventListener("change",$tw.perf.report("mainRefresh",function(changes) {
throttledRefreshFn(changes,refresh,true,false);
})); }));
// Fix up the link between the root widget and the page container // Fix up the link between the root widget and the page container
$tw.rootWidget.domNodes = [$tw.pageContainer]; $tw.rootWidget.domNodes = [$tw.pageContainer];