-
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
Introduce background actions and media query tracker mechanisms #8555
Draft
Jermolene
wants to merge
10
commits into
master
Choose a base branch
from
media-query-tracker
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.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ba17e34
Introduce new MediaQueryTrackerMechanism, and use it for dark mode
Jermolene 7f9b8bf
Fixes suggested by @pmario
Jermolene ea2426e
Merge branch 'master' into media-query-tracker
Jermolene e78d3ae
Introduce new generic filter tracker
Jermolene 964ced3
Add untrack method to filter tracker
Jermolene ed2c7c9
Be more defensive about missing parameters
Jermolene 806960a
Remove duplicates from filter tracker results
Jermolene 508c74f
Refactor filter tracker
Jermolene 818a997
Introduce background actions
Jermolene 365b734
Add another background action demo
Jermolene 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
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,87 @@ | ||
/*\ | ||
title: $:/core/modules/filter-tracker.js | ||
type: application/javascript | ||
module-type: global | ||
|
||
Class to track the results of a filter string | ||
|
||
\*/ | ||
(function(){ | ||
|
||
/*jslint node: true, browser: true */ | ||
/*global $tw: false */ | ||
"use strict"; | ||
|
||
function FilterTracker(wiki) { | ||
this.wiki = wiki; | ||
this.trackers = []; | ||
this.wiki.addEventListener("change",this.handleChangeEvent.bind(this)); | ||
} | ||
|
||
FilterTracker.prototype.handleChangeEvent = function(changes) { | ||
this.processTrackers(); | ||
this.processChanges(changes); | ||
}; | ||
|
||
/* | ||
Add a tracker to the filter tracker | ||
filterString: the filter string to track | ||
fnEnter: function to call when a title enters the filter results. Called even if the tiddler does not actually exist. Called as (title), and should return a truthy value that is stored in the tracker as the "enterValue" | ||
fnLeave: function to call when a title leaves the filter results. Called as (title,enterValue) | ||
fnChange: function to call when a tiddler changes in the filter results. Only called for filter results that identify a tiddler or shadow tiddler. Called as (title,enterValue), and may optionally return a replacement enterValue | ||
*/ | ||
FilterTracker.prototype.track = function(filterString,fnEnter,fnLeave,fnChange) { | ||
// Add the tracker details | ||
var index = this.trackers.length; | ||
this.trackers.push({ | ||
filterString: filterString, | ||
fnEnter: fnEnter, | ||
fnLeave: fnLeave, | ||
fnChange: fnChange, | ||
previousResults: [], // Results from the previous time the tracker was processed | ||
resultValues: {} // Map by title to the value returned by fnEnter | ||
}); | ||
// Process the tracker | ||
this.processTracker(index); | ||
}; | ||
|
||
FilterTracker.prototype.processTrackers = function() { | ||
for(var t=0; t<this.trackers.length; t++) { | ||
this.processTracker(t); | ||
} | ||
}; | ||
|
||
FilterTracker.prototype.processTracker = function(index) { | ||
var tracker = this.trackers[index], | ||
results = this.wiki.filterTiddlers(tracker.filterString); | ||
// Process the results | ||
$tw.utils.each(results,function(title) { | ||
if(tracker.previousResults.indexOf(title) === -1 && !tracker.resultValues[title]) { | ||
tracker.resultValues[title] = tracker.fnEnter(title) || true; | ||
} | ||
}); | ||
$tw.utils.each(tracker.previousResults,function(title) { | ||
if(results.indexOf(title) === -1 && tracker.resultValues[title]) { | ||
tracker.fnLeave(title,tracker.resultValues[title]); | ||
delete tracker.resultValues[title]; | ||
} | ||
}); | ||
// Update the previous results | ||
tracker.previousResults = results; | ||
}; | ||
|
||
FilterTracker.prototype.processChanges = function(changes) { | ||
for(var t=0; t<this.trackers.length; t++) { | ||
var tracker = this.trackers[t]; | ||
$tw.utils.each(changes,function(change,title) { | ||
if(tracker.previousResults.indexOf(title) !== -1) { | ||
// Call the change function and if it doesn't return a value then keep the old value | ||
tracker.resultValues[title] = tracker.fnChange(title,tracker.resultValues[title]) || tracker.resultValues[title]; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
exports.FilterTracker = FilterTracker; | ||
|
||
})(); |
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
4 changes: 2 additions & 2 deletions
4
editions/tw5.com/tiddlers/mechanisms/MediaQueryTrackerMechanism.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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
title: MediaQueryTrackerMechanism | ||
tags: Mechanisms | ||
|
||
The media query tracker mechanism allows you to define [[custom CSS media queries|https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries]] to be bound to a specified [[info|InfoMechanism]] tiddler. The info tiddler will be dynamically update to reflect the current state of the media query. | ||
<<.from-version "5.3.7">> The media query tracker mechanism allows you to define [[custom CSS media queries|https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries]] to be bound to a specified [[info|InfoMechanism]] tiddler. The info tiddler will be dynamically update to reflect the current state of the media query. | ||
|
||
Adding or modifying a tiddler tagged $:/tags/MediaQueryTracker will only take effect when the wiki is reloaded | ||
Adding or modifying a tiddler tagged $:/tags/MediaQueryTracker takes effect immediately. | ||
|
||
The media queries are always applied against the main window. This is relevant for viewport related media queries such as `min-width` which will always respect the main window and ignore the sizes of any external windows. |
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.
I am not 100% sure, but there seem to be no tests if the
fn*
parameters are undefined of if they actually arefunction()