From 9faaa312998d48c56bd50335820b6b881266af4b Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Tue, 1 Jun 2021 09:41:14 +0100 Subject: [PATCH] Extend action-createtiddler to make new title available as a variable I'm not sure if the docs are clear, but this is quite a big deal, and along with 582b156d5f2b9b8b92f9289aa1288e1edb637450 makes working with action widgets a lot easier. --- core/modules/widgets/action-createtiddler.js | 16 +++++++++++----- .../ActionCreateTiddlerWidget Example 3.tid | 2 +- .../ActionCreateTiddlerWidget Example 4.tid | 4 ++-- .../widgets/ActionCreateTiddlerWidget.tid | 16 ++++++++++------ .../ActionCreateTiddlerWidget_Example.tid | 8 +++++--- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/core/modules/widgets/action-createtiddler.js b/core/modules/widgets/action-createtiddler.js index d1d1786f7..693c33282 100644 --- a/core/modules/widgets/action-createtiddler.js +++ b/core/modules/widgets/action-createtiddler.js @@ -27,8 +27,11 @@ CreateTiddlerWidget.prototype = new Widget(); Render this widget into the DOM */ CreateTiddlerWidget.prototype.render = function(parent,nextSibling) { + this.parentDomNode = parent; this.computeAttributes(); this.execute(); + // Render children + this.renderChildren(parent,nextSibling); }; /* @@ -44,7 +47,8 @@ CreateTiddlerWidget.prototype.execute = function() { this.actionTemplate = this.getAttribute("$template"); this.useTemplate = !!this.actionTemplate; this.actionOverwrite = this.getAttribute("$overwrite","no"); - + // Construct the child widgets + this.makeChildWidgets(); }; /* @@ -86,18 +90,20 @@ CreateTiddlerWidget.prototype.invokeAction = function(triggeringWidget,event) { if (!this.hasBase && this.useTemplate) { title = this.wiki.generateNewTitle(this.actionTemplate); } else if (!this.hasBase && !this.useTemplate) { - // If NO $basetitle AND NO $template use initial title - // DON'T overwrite any stuff + // If no $basetitle and no $template then use initial title title = this.wiki.generateNewTitle(title); } var templateTiddler = this.wiki.getTiddler(this.actionTemplate) || {}; - var tiddler = this.wiki.addTiddler(new $tw.Tiddler(templateTiddler.fields,creationFields,fields,modificationFields,{title: title})); + this.wiki.addTiddler(new $tw.Tiddler(templateTiddler.fields,creationFields,fields,modificationFields,{title: title})); + var draftTitle = this.wiki.generateDraftTitle(title); if(this.actionSaveTitle) { this.wiki.setTextReference(this.actionSaveTitle,title,this.getVariable("currentTiddler")); } if(this.actionSaveDraftTitle) { - this.wiki.setTextReference(this.actionSaveDraftTitle,this.wiki.generateDraftTitle(title),this.getVariable("currentTiddler")); + this.wiki.setTextReference(this.actionSaveDraftTitle,draftTitle,this.getVariable("currentTiddler")); } + this.setVariable("createTiddler-title",title); + this.setVariable("createTiddler-draftTitle",draftTitle); return true; // Action was invoked }; diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 3.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 3.tid index 54524108a..6c66ef165 100644 --- a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 3.tid +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 3.tid @@ -25,4 +25,4 @@ Create Tiddler <$button actions=<> > <$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> Create Tiddler - - Clicking this button, will also open the Right sidebar: Recent tab + - Clicking this button will also open the Right sidebar: Recent tab diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 4.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 4.tid index b1eb7275f..c3e955b55 100644 --- a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 4.tid +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget Example 4.tid @@ -8,7 +8,7 @@ type: text/vnd.tiddlywiki <$action-createtiddler $basetitle="base" $template="ActionCreateTiddlerWidget Template" aa="new field aa" bb="new field bb" /> \end -This example will uses a base-title: "base" and a template: [[ActionCreateTiddlerWidget Template]]. +This example uses a base-title "base" and a template: [[ActionCreateTiddlerWidget Template]]. There will be new fields "aa" and "bb" which are added to the new tiddlers. @@ -25,4 +25,4 @@ Create Tiddler <$button actions=<> > <$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> Create Tiddler - - Clicking this button, will also open the Right sidebar: Recent tab + - Clicking this button will also open the Right sidebar: Recent tab diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid index ca02c2285..04631f4af 100644 --- a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid @@ -1,6 +1,6 @@ caption: action-createtiddler created: 20161020152745942 -modified: 20200131151847266 +modified: 20210601092956998 tags: Widgets ActionWidgets title: ActionCreateTiddlerWidget type: text/vnd.tiddlywiki @@ -16,17 +16,21 @@ There are several differences from the [[tm-new-tiddler message|WidgetMessage: t ! Content and Attributes -The ''action-createtiddler'' widget is invisible. Any content within it is ignored. +The ''action-createtiddler'' widget is invisible. + +<<.from-version "5.1.24">> The action widgets contained within the ''action-createtiddler'' widget are executed after the new tiddler has been created with the title of the tiddler in the variable `createTiddler-title` and `createTiddler-draftTitle`. |!Attribute |!Description | |$basetitle |The initial title that will be attempted. If a tiddler with that title already exists, then a numerical counter is added to the title and incremented until it is unique| -|$savetitle |A text reference identifying a field or index into which the title of the newly created tiddler will be stored after it is created | -|$savedrafttitle |<<.from-version "5.1.20">> A text reference identifying a field or index into which the draft title associated with the newly created tiddler will be stored after it is created. This is useful when using a sequence of action widgets to create a new tiddler, put it into edit mode, and position it within the list of its parent tag | +|$savetitle |//(deprecated – see below))// A text reference identifying a field or index into which the title of the newly created tiddler will be stored after it is created | +|$savedrafttitle |//(deprecated – see below))// <<.from-version "5.1.20">> A text reference identifying a field or index into which the draft title associated with the newly created tiddler will be stored after it is created. This is useful when using a sequence of action widgets to create a new tiddler, put it into edit mode, and position it within the list of its parent tag | |$timestamp |Specifies whether the timestamp(s) of the target tiddler will be updated (''modified'' and ''modifier'', plus ''created'' and ''creator'' for newly created tiddlers). Can be "yes" (the default) or "no" | -|$template |<<.from-version "5.1.22">> The title of a template tiddler, that will be used to create a new tiddler | -|$overwrite |<<.from-version "5.1.22">> If set to "yes", it will overwrite existing tiddlers. ''Be careful!'' | +|$template |<<.from-version "5.1.22">> The title of a template tiddler, that will be used to create a new tiddler | +|$overwrite |<<.from-version "5.1.22">> If set to "yes", it will overwrite existing tiddlers. ''Be careful!'' | |//{any attributes not starting with $}// |Each attribute name specifies a field to be created in the new tiddler | +<<.from-version "5.1.24">> Note that the attributes `$savetitle` and `$savedrafttitle` are no longer needed. Instead, any action widgets that need to use the title of the newly created tiddler should be contained within the ''action-createtiddler'' widget, and reference the variables `createTiddler-title` and `createTiddler-draftTitle` to obtain the title. + ! Examples <<< diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid index 2be233566..2eb37f9fc 100644 --- a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid @@ -4,9 +4,11 @@ tags: ActionCreateTiddlerWidget title: ActionCreateTiddlerWidget Example type: text/vnd.tiddlywiki -New button caption: <$edit-text tiddler="$:/state/new-button-caption" tag="input" default=""/> +Caption for new button: <$edit-text tiddler="$:/state/new-button-caption" tag="input" default=""/> <$button> -<$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}/> -Create non-functional page control button +<$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}> +<$action-navigate $to=<>/> + +Create a new non-functional page control button and open the tiddler \ No newline at end of file