From c854e518faa2d2661b7b7278634b10607ab0a5f5 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Tue, 24 Nov 2020 19:01:33 +0000 Subject: [PATCH] Add support for $:/info/darkmode (and for dynamic info tiddlers) --- core/modules/info/platform.js | 9 +++- core/modules/startup/info.js | 44 +++++++++++-------- .../tiddlers/mechanisms/InfoMechanism.tid | 3 +- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/core/modules/info/platform.js b/core/modules/info/platform.js index 29ebdd594..a73a5e5df 100644 --- a/core/modules/info/platform.js +++ b/core/modules/info/platform.js @@ -12,7 +12,7 @@ Initialise basic platform $:/info/ tiddlers /*global $tw: false */ "use strict"; -exports.getInfoTiddlerFields = function() { +exports.getInfoTiddlerFields = function(updateInfoTiddlersCallback) { var mapBoolean = function(value) {return value ? "yes" : "no";}, infoTiddlerFields = []; // Basics @@ -36,6 +36,13 @@ exports.getInfoTiddlerFields = function() { // Screen size infoTiddlerFields.push({title: "$:/info/browser/screen/width", text: window.screen.width.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 infoTiddlerFields.push({title: "$:/info/browser/language", text: navigator.language || ""}); } diff --git a/core/modules/startup/info.js b/core/modules/startup/info.js index 7efaa5b0e..ed2305930 100644 --- a/core/modules/startup/info.js +++ b/core/modules/startup/info.js @@ -21,29 +21,37 @@ exports.synchronous = true; var TITLE_INFO_PLUGIN = "$:/temp/info-plugin"; exports.startup = function() { + // Function to bake the info plugin with new tiddlers + var updateInfoPlugin = function(tiddlerFieldsArray) { + // Get the existing tiddlers + var json = $tw.wiki.getTiddlerData(TITLE_INFO_PLUGIN,{tiddlers: {}}); + // Add the new ones + $tw.utils.each(tiddlerFieldsArray,function(fields) { + if(fields && fields.title) { + 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 + var fields = { + title: TITLE_INFO_PLUGIN, + type: "application/json", + "plugin-type": "info", + text: JSON.stringify(json,null,$tw.config.preferences.jsonSpaces) + }; + $tw.wiki.addTiddler(new $tw.Tiddler(fields)); + + }; // Collect up the info tiddlers - var infoTiddlerFields = {}; - // Give each info module a chance to fill in as many info tiddlers as they want + 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) { - var tiddlerFieldsArray = moduleExports.getInfoTiddlerFields(infoTiddlerFields); - $tw.utils.each(tiddlerFieldsArray,function(fields) { - if(fields) { - infoTiddlerFields[fields.title] = fields; - } - }); + Array.prototype.push.apply(tiddlerFieldsArray,moduleExports.getInfoTiddlerFields(updateInfoPlugin)); } }); - // 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 - var fields = { - title: TITLE_INFO_PLUGIN, - type: "application/json", - "plugin-type": "info", - text: JSON.stringify({tiddlers: infoTiddlerFields},null,$tw.config.preferences.jsonSpaces) - }; - $tw.wiki.addTiddler(new $tw.Tiddler(fields)); - $tw.wiki.readPluginInfo([TITLE_INFO_PLUGIN]); - $tw.wiki.registerPluginTiddlers("info"); + updateInfoPlugin(tiddlerFieldsArray); + var changes = $tw.wiki.readPluginInfo([TITLE_INFO_PLUGIN]); + $tw.wiki.registerPluginTiddlers("info",[TITLE_INFO_PLUGIN]); $tw.wiki.unpackPluginTiddlers(); }; diff --git a/editions/tw5.com/tiddlers/mechanisms/InfoMechanism.tid b/editions/tw5.com/tiddlers/mechanisms/InfoMechanism.tid index 62d907076..c56bd47eb 100644 --- a/editions/tw5.com/tiddlers/mechanisms/InfoMechanism.tid +++ b/editions/tw5.com/tiddlers/mechanisms/InfoMechanism.tid @@ -1,5 +1,5 @@ created: 20140720164948099 -modified: 20200506110435897 +modified: 20201124185829706 tags: Mechanisms title: InfoMechanism 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, ''<>'') | |[[$:/info/url/protocol]] |<<.from-version "5.1.14">> Protocol portion of URL of wiki (eg, ''<>'') | |[[$:/info/url/search]] |<<.from-version "5.1.14">> Search portion of URL of wiki (eg, ''<>'') | +|[[$:/info/darkmode]] |<<.from-version "5.1.23">> Is dark mode enabled? ("yes" or "no") |