1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

do it correctly

This commit is contained in:
BurningTreeC 2024-10-15 05:09:28 +02:00
parent 6addb77024
commit 8eddb4586a
2 changed files with 37 additions and 29 deletions

View File

@ -51,9 +51,11 @@ exports.startup = function() {
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
document.head.insertBefore($tw.styleElement,document.head.firstChild);
// Prepare refresh mechanism
var deferredChanges = Object.create(null),
timerId,
throttledRefreshFn = function(changes,callback,mainCondition,styleCondition) {
var mainDeferredChanges = Object.create(null),
styleDeferredChanges = Object.create(null),
mainTimerId,
styleTimerId,
throttledRefreshFn = function(changes,deferredChanges,timerId,throttledRefresh,callback,mainCondition,styleCondition) {
// Check if only tiddlers that are throttled have changed
var onlyThrottledTiddlersHaveChanged = true;
for(var title in changes) {
@ -76,20 +78,30 @@ exports.startup = function() {
$tw.utils.extend(deferredChanges,changes);
} else {
$tw.utils.extend(deferredChanges,changes);
callback(changes);
callback();
}
};
function styleRefresh(changes) {
if($tw.styleWidgetNode.refresh(changes,$tw.styleContainer,null)) {
function refresh() {
// Process the refresh
$tw.hooks.invokeHook("th-page-refreshing");
$tw.pageWidgetNode.refresh(mainDeferredChanges);
mainDeferredChanges = Object.create(null);
$tw.hooks.invokeHook("th-page-refreshed");
}
function styleRefresh() {
if($tw.styleWidgetNode.refresh(styleDeferredChanges,$tw.styleContainer,null)) {
var newStyles = $tw.styleContainer.textContent;
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
$tw.styleWidgetNode.assignedStyles = newStyles;
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
}
}
}
styleDeferredChanges = Object.create(null);
}
var mainThrottledRefresh = $tw.perf.report("throttledRefresh",refresh),
styleThrottledRefresh = $tw.perf.report("throttledRefresh",styleRefresh);
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
throttledRefreshFn(changes,styleRefresh,false,true);
throttledRefreshFn(changes,styleDeferredChanges,styleTimerId,styleThrottledRefresh,styleRefresh,false,true);
}));
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
$tw.perf.report("mainRender",function() {
@ -107,18 +119,9 @@ exports.startup = function() {
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);
throttledRefreshFn(changes,mainDeferredChanges,mainTimerId,mainThrottledRefresh,refresh,true,false);
}));
// Fix up the link between the root widget and the page container
$tw.rootWidget.domNodes = [$tw.pageContainer];

View File

@ -81,9 +81,11 @@ exports.startup = function() {
widgetNode = $tw.wiki.makeWidget(parser,{document: srcDocument, parentWidget: $tw.rootWidget, variables: variables});
widgetNode.render(srcDocument.body,srcDocument.body.firstChild);
// Prepare refresh mechanism
var deferredChanges = Object.create(null),
timerId,
throttledRefreshFn = function(changes,callback,mainCondition,styleCondition) {
var mainDeferredChanges = Object.create(null),
styleDeferredChanges = Object.create(null),
mainTimerId,
styleTimerId,
throttledRefreshFn = function(changes,deferredChanges,timerId,throttledRefresh,callback,mainCondition,styleCondition) {
// Check if only tiddlers that are throttled have changed
var onlyThrottledTiddlersHaveChanged = true;
for(var title in changes) {
@ -106,27 +108,30 @@ exports.startup = function() {
$tw.utils.extend(deferredChanges,changes);
} else {
$tw.utils.extend(deferredChanges,changes);
callback(changes);
callback();
}
};
var styleRefresh = function(changes) {
if(styleWidgetNode.refresh(changes,styleContainer,null)) {
var styleRefresh = function() {
if(styleWidgetNode.refresh(styleDeferredChanges,styleContainer,null)) {
var newStyles = styleContainer.textContent;
if(newStyles !== styleWidgetNode.assignedStyles) {
styleWidgetNode.assignedStyles = newStyles;
styleElement.innerHTML = styleWidgetNode.assignedStyles;
}
}
styleDeferredChanges = Object.create(null);
};
var mainRefresh = function(changes) {
widgetNode.refresh(changes);
deferredChanges = Object.create(null);
var mainRefresh = function() {
widgetNode.refresh(mainDeferredChanges);
mainDeferredChanges = Object.create(null);
};
var mainThrottledRefresh = $tw.perf.report("throttledRefresh",mainRefresh),
styleThrottledRefresh = $tw.perf.report("throttledRefresh",styleRefresh);
styleRefreshHandler = function(changes) {
throttledRefreshFn(changes,styleRefresh,false,true);
throttledRefreshFn(changes,styleDeferredChanges,styleTimerId,styleThrottledRefresh,styleRefresh,false,true);
};
mainRefreshHandler = function(changes) {
throttledRefreshFn(changes,mainRefresh,true,false);
throttledRefreshFn(changes,mainDeferredChanges,mainTimerId,mainThrottledRefresh,mainRefresh,true,false);
};
$tw.wiki.addEventListener("change",styleRefreshHandler);
$tw.wiki.addEventListener("change",mainRefreshHandler);