1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add support for $:/info/darkmode (and for dynamic info tiddlers)

This commit is contained in:
jeremy@jermolene.com 2020-11-24 19:01:33 +00:00
parent 7327a3fb92
commit c854e518fa
3 changed files with 36 additions and 20 deletions

View File

@ -12,7 +12,7 @@ Initialise basic platform $:/info/ tiddlers
/*global $tw: false */ /*global $tw: false */
"use strict"; "use strict";
exports.getInfoTiddlerFields = function() { exports.getInfoTiddlerFields = function(updateInfoTiddlersCallback) {
var mapBoolean = function(value) {return value ? "yes" : "no";}, var mapBoolean = function(value) {return value ? "yes" : "no";},
infoTiddlerFields = []; infoTiddlerFields = [];
// Basics // Basics
@ -36,6 +36,13 @@ exports.getInfoTiddlerFields = function() {
// Screen size // Screen size
infoTiddlerFields.push({title: "$:/info/browser/screen/width", text: window.screen.width.toString()}); infoTiddlerFields.push({title: "$:/info/browser/screen/width", text: window.screen.width.toString()});
infoTiddlerFields.push({title: "$:/info/browser/screen/height", text: window.screen.height.toString()}); infoTiddlerFields.push({title: "$:/info/browser/screen/height", text: window.screen.height.toString()});
// Dark mode through event listener on MediaQueryList
var mqList = window.matchMedia("(prefers-color-scheme: dark)"),
getDarkModeTiddler = function() {return {title: "$:/info/darkmode", text: mqList.matches ? "yes" : "no"};};
infoTiddlerFields.push(getDarkModeTiddler());
mqList.addEventListener("change", function(event) {
updateInfoTiddlersCallback([getDarkModeTiddler()]);
});
// Language // Language
infoTiddlerFields.push({title: "$:/info/browser/language", text: navigator.language || ""}); infoTiddlerFields.push({title: "$:/info/browser/language", text: navigator.language || ""});
} }

View File

@ -21,17 +21,14 @@ exports.synchronous = true;
var TITLE_INFO_PLUGIN = "$:/temp/info-plugin"; var TITLE_INFO_PLUGIN = "$:/temp/info-plugin";
exports.startup = function() { exports.startup = function() {
// Collect up the info tiddlers // Function to bake the info plugin with new tiddlers
var infoTiddlerFields = {}; var updateInfoPlugin = function(tiddlerFieldsArray) {
// Give each info module a chance to fill in as many info tiddlers as they want // Get the existing tiddlers
$tw.modules.forEachModuleOfType("info",function(title,moduleExports) { var json = $tw.wiki.getTiddlerData(TITLE_INFO_PLUGIN,{tiddlers: {}});
if(moduleExports && moduleExports.getInfoTiddlerFields) { // Add the new ones
var tiddlerFieldsArray = moduleExports.getInfoTiddlerFields(infoTiddlerFields);
$tw.utils.each(tiddlerFieldsArray,function(fields) { $tw.utils.each(tiddlerFieldsArray,function(fields) {
if(fields) { if(fields && fields.title) {
infoTiddlerFields[fields.title] = fields; json.tiddlers[fields.title] = fields;
}
});
} }
}); });
// Bake the info tiddlers into a plugin. We use the non-standard plugin-type "info" because ordinary plugins are only registered asynchronously after being loaded dynamically // Bake the info tiddlers into a plugin. We use the non-standard plugin-type "info" because ordinary plugins are only registered asynchronously after being loaded dynamically
@ -39,11 +36,22 @@ exports.startup = function() {
title: TITLE_INFO_PLUGIN, title: TITLE_INFO_PLUGIN,
type: "application/json", type: "application/json",
"plugin-type": "info", "plugin-type": "info",
text: JSON.stringify({tiddlers: infoTiddlerFields},null,$tw.config.preferences.jsonSpaces) text: JSON.stringify(json,null,$tw.config.preferences.jsonSpaces)
}; };
$tw.wiki.addTiddler(new $tw.Tiddler(fields)); $tw.wiki.addTiddler(new $tw.Tiddler(fields));
$tw.wiki.readPluginInfo([TITLE_INFO_PLUGIN]);
$tw.wiki.registerPluginTiddlers("info"); };
// Collect up the info tiddlers
var tiddlerFieldsArray = [];
// Give each info module a chance to provide as many info tiddlers as they want as an array, and give them a callback for dynamically updating them
$tw.modules.forEachModuleOfType("info",function(title,moduleExports) {
if(moduleExports && moduleExports.getInfoTiddlerFields) {
Array.prototype.push.apply(tiddlerFieldsArray,moduleExports.getInfoTiddlerFields(updateInfoPlugin));
}
});
updateInfoPlugin(tiddlerFieldsArray);
var changes = $tw.wiki.readPluginInfo([TITLE_INFO_PLUGIN]);
$tw.wiki.registerPluginTiddlers("info",[TITLE_INFO_PLUGIN]);
$tw.wiki.unpackPluginTiddlers(); $tw.wiki.unpackPluginTiddlers();
}; };

View File

@ -1,5 +1,5 @@
created: 20140720164948099 created: 20140720164948099
modified: 20200506110435897 modified: 20201124185829706
tags: Mechanisms tags: Mechanisms
title: InfoMechanism title: InfoMechanism
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -27,3 +27,4 @@ System tiddlers in the namespace `$:/info/` are used to expose information about
|[[$:/info/url/port]] |<<.from-version "5.1.14">> Port portion of URL of wiki (eg, ''<<example port>>'') | |[[$:/info/url/port]] |<<.from-version "5.1.14">> Port portion of URL of wiki (eg, ''<<example port>>'') |
|[[$:/info/url/protocol]] |<<.from-version "5.1.14">> Protocol portion of URL of wiki (eg, ''<<example protocol>>'') | |[[$:/info/url/protocol]] |<<.from-version "5.1.14">> Protocol portion of URL of wiki (eg, ''<<example protocol>>'') |
|[[$:/info/url/search]] |<<.from-version "5.1.14">> Search portion of URL of wiki (eg, ''<<example search>>'') | |[[$:/info/url/search]] |<<.from-version "5.1.14">> Search portion of URL of wiki (eg, ''<<example search>>'') |
|[[$:/info/darkmode]] |<<.from-version "5.1.23">> Is dark mode enabled? ("yes" or "no") |