From 5cc1600072f5aa50c8ff5f5d2e748d81a7067420 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Sun, 25 Oct 2020 16:22:21 +0000 Subject: [PATCH] Introduce post-render startup actions --- core/modules/startup/render.js | 2 ++ core/modules/startup/startup.js | 11 +++------- core/modules/widgets/widget.js | 10 +++++++++ .../tiddlers/features/StartupActions.tid | 21 +++++++++++++------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/core/modules/startup/render.js b/core/modules/startup/render.js index 05ba9844d..fc93bcdb3 100644 --- a/core/modules/startup/render.js +++ b/core/modules/startup/render.js @@ -106,6 +106,8 @@ exports.startup = function() { // Fix up the link between the root widget and the page container $tw.rootWidget.domNodes = [$tw.pageContainer]; $tw.rootWidget.children = [$tw.pageWidgetNode]; + // Run any post-render startup actions + $tw.rootWidget.executeStartupTiddlers("$:/tags/StartupAction/PostRender"); }; })(); diff --git a/core/modules/startup/startup.js b/core/modules/startup/startup.js index ad1416bf3..b7897a5ce 100755 --- a/core/modules/startup/startup.js +++ b/core/modules/startup/startup.js @@ -64,17 +64,12 @@ exports.startup = function() { document: $tw.browser ? document : $tw.fakeDocument }); // Execute any startup actions - var executeStartupTiddlers = function(tag) { - $tw.utils.each($tw.wiki.filterTiddlers("[all[shadows+tiddlers]tag[" + tag + "]!has[draft.of]]"),function(title) { - $tw.rootWidget.invokeActionString($tw.wiki.getTiddlerText(title),$tw.rootWidget); - }); - }; - executeStartupTiddlers("$:/tags/StartupAction"); + $tw.rootWidget.executeStartupTiddlers("$:/tags/StartupAction"); if($tw.browser) { - executeStartupTiddlers("$:/tags/StartupAction/Browser"); + $tw.rootWidget.executeStartupTiddlers("$:/tags/StartupAction/Browser"); } if($tw.node) { - executeStartupTiddlers("$:/tags/StartupAction/Node"); + $tw.rootWidget.executeStartupTiddlers("$:/tags/StartupAction/Node"); } // Kick off the language manager and switcher $tw.language = new $tw.Language(); diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index e0444573d..bd66438e2 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -571,6 +571,16 @@ Widget.prototype.invokeActionString = function(actions,triggeringWidget,event,va return widgetNode.invokeActions(this,event); }; +/* +Execute action tiddlers by tag +*/ +Widget.prototype.executeStartupTiddlers = function(tag) { + var self = this; + $tw.utils.each(self.wiki.filterTiddlers("[all[shadows+tiddlers]tag[" + tag + "]!has[draft.of]]"),function(title) { + self.invokeActionString(self.wiki.getTiddlerText(title),self); + }); +}; + Widget.prototype.allowActionPropagation = function() { return true; }; diff --git a/editions/tw5.com/tiddlers/features/StartupActions.tid b/editions/tw5.com/tiddlers/features/StartupActions.tid index 3b5583f47..79a23b3d9 100644 --- a/editions/tw5.com/tiddlers/features/StartupActions.tid +++ b/editions/tw5.com/tiddlers/features/StartupActions.tid @@ -1,16 +1,21 @@ created: 20180323092308399 -modified: 20190307183936035 +modified: 20201025161723719 tags: Features title: StartupActions type: text/vnd.tiddlywiki TiddlyWiki executes any ActionWidgets found in tiddlers with the following system tags during startup: -* <> (executed on all platforms) -* <> (only executed when running in the browser) -* <> (only executed when running under Node.js) +* Executed during initial startup: +** <> (executed on all platforms) +** <> (only executed when running in the browser) +** <> (only executed when running under Node.js) +* Executed after startup rendering: +** <<.from-version "5.1.23">> <> (only executed when running in the browser) -Startup actions are useful for customising TiddlyWiki according to environmental factors such as the screen size. For example, the following action widgets when placed in a tiddler tagged `$:/tags/StartupAction/Browser` will cause the sidebar to be hidden by default when the screen width is less than 1000 pixels: +!! Initial Startup Actions + +The initial startup actions are useful for customising TiddlyWiki according to environmental factors such as the screen size. For example, the following action widgets when placed in a tiddler tagged `$:/tags/StartupAction/Browser` will cause the sidebar to be hidden by default when the screen width is less than 1000 pixels: ``` <$reveal type="lt" state="$:/info/browser/screen/width" text="3000"> @@ -28,8 +33,12 @@ Startup actions are useful for customising TiddlyWiki according to environmental <$action-setfield $tiddler="$:/language" text={{{ [[$:/languages/en-GB]] [plugin-type[language]sort[description]removeprefix[$:/languages/]] +[prefix{$:/info/browser/language}] ~[[en-GB]] +[addprefix[$:/languages/]] }}}/> ``` -Note that global macros are not available within startup action tiddlers by default. If you need to access them then you'll need to explicitly include them with an ''import'' [[pragma|Pragma]] at the top of the tiddler: +Note that global macros are not available within initial startup action tiddlers by default. If you need to access them then you'll need to explicitly include them with an ''import'' [[pragma|Pragma]] at the top of the tiddler: ``` \import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]] ``` + +!! Post-Render Startup Actions + +<<.from-version "5.1.23">> Post-render startup actions occur after the TiddlyWiki user interface has been rendered. This makes it possible to perform actions that depend on the rendered display (such as displaying modals).