1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-06-05 16:14:07 +00:00

Introduce background actions

Background actions are invoked when the results of a filter change.

The sample background action is just for demo purposes to show how an automatic dark/light mode feature would be powered
This commit is contained in:
Jeremy Ruston 2025-01-07 15:09:13 +00:00
parent 508c74f8e4
commit 818a9977a3
3 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,117 @@
/*\
title: $:/core/modules/background-actions.js
type: application/javascript
module-type: global
Class to dispatch actions when filters change
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function BackgroundActionDispatcher(filterTracker,wiki) {
var self = this;
this.filterTracker = filterTracker;
this.wiki = wiki;
this.nextTrackedFilterId = 1;
this.trackedFilters = Object.create(null); // Hashmap by id
// Track the filter for the background actions
this.filterTracker.track({
filterString: "[all[tiddlers+shadows]tag[$:/tags/BackgroundAction]!is[draft]]",
fnEnter: function fnEnter(title) {
return self.trackFilter(title);
},
fnLeave: function fnLeave(title,enterValue) {
self.untrackFilter(enterValue);
},
fnChange: function fnChange(title,enterValue) {
self.untrackFilter(enterValue);
return self.trackFilter(title);
},
fnProcess: function fnProcess(changes) {
self.process(changes);
}
});
}
BackgroundActionDispatcher.prototype.trackFilter = function(title) {
var tiddler = this.wiki.getTiddler(title),
id = this.nextTrackedFilterId++,
tracker = new BackgroundActionTracker({
wiki: this.wiki,
title: title,
trackFilter: tiddler.fields["track-filter"],
actions: tiddler.fields.text
});
this.trackedFilters[id] = tracker;
return id;
};
BackgroundActionDispatcher.prototype.untrackFilter = function(enterValue) {
var tracker = this.trackedFilters[enterValue];
if(tracker) {
tracker.destroy();
}
delete this.trackedFilters[enterValue];
};
BackgroundActionDispatcher.prototype.process = function(changes) {
for(var id in this.trackedFilters) {
this.trackedFilters[id].process(changes);
}
};
/*
Represents an individual tracked filter. Options include:
wiki: wiki to use
title: title of the tiddler being tracked
trackFilter: filter string to track changes
actions: actions to be executed when the filter changes
*/
function BackgroundActionTracker(options) {
var self = this;
this.wiki = options.wiki;
this.title = options.title;
this.trackFilter = options.trackFilter;
this.actions = options.actions
this.filterTracker = new $tw.FilterTracker(this.wiki);
this.hasChanged = false;
this.trackerID = this.filterTracker.track({
filterString: this.trackFilter,
fnEnter: function(title) {
self.hasChanged = true;
},
fnLeave: function(title,enterValue) {
self.hasChanged = true;
},
fnProcess: function(changes) {
if(self.hasChanged) {
self.hasChanged = false;
self.wiki.invokeActionString(
self.actions,
null,
{
title: self.title
},{
parentWidget: $tw.rootWidget
}
);
}
}
});
}
BackgroundActionTracker.prototype.process = function(changes) {
this.filterTracker.handleChangeEvent(changes);
};
BackgroundActionTracker.prototype.destroy = function() {
this.filterTracker.untrack(this.trackerID);
};
exports.BackgroundActionDispatcher = BackgroundActionDispatcher;
})();

View File

@ -49,6 +49,8 @@ exports.startup = function() {
$tw.wiki.addEventListener("change",function(changes) {
$tw.filterTracker.handleChangeEvent(changes);
});
// Kick off the background action dispatcher
$tw.backgroundActionDispatcher = new $tw.BackgroundActionDispatcher($tw.filterTracker,$tw.wiki);
};
})();

View File

@ -0,0 +1,9 @@
title: SampleBackgroundAction
tags: $:/tags/BackgroundAction
track-filter: [{$:/info/browser/darkmode}]
<%if [{$:/info/browser/darkmode}match[no]] %>
<$action-setfield $tiddler="$:/palette" text="$:/palettes/Vanilla"/>
<%else%>
<$action-setfield $tiddler="$:/palette" text="$:/palettes/SolarizedDark"/>
<%endif%>