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

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 582b156d5f makes working with action widgets a lot easier.
This commit is contained in:
jeremy@jermolene.com 2021-06-01 09:41:14 +01:00
parent 582b156d5f
commit 9faaa31299
5 changed files with 29 additions and 17 deletions

View File

@ -27,8 +27,11 @@ CreateTiddlerWidget.prototype = new Widget();
Render this widget into the DOM Render this widget into the DOM
*/ */
CreateTiddlerWidget.prototype.render = function(parent,nextSibling) { CreateTiddlerWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes(); this.computeAttributes();
this.execute(); this.execute();
// Render children
this.renderChildren(parent,nextSibling);
}; };
/* /*
@ -44,7 +47,8 @@ CreateTiddlerWidget.prototype.execute = function() {
this.actionTemplate = this.getAttribute("$template"); this.actionTemplate = this.getAttribute("$template");
this.useTemplate = !!this.actionTemplate; this.useTemplate = !!this.actionTemplate;
this.actionOverwrite = this.getAttribute("$overwrite","no"); 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) { if (!this.hasBase && this.useTemplate) {
title = this.wiki.generateNewTitle(this.actionTemplate); title = this.wiki.generateNewTitle(this.actionTemplate);
} else if (!this.hasBase && !this.useTemplate) { } else if (!this.hasBase && !this.useTemplate) {
// If NO $basetitle AND NO $template use initial title // If no $basetitle and no $template then use initial title
// DON'T overwrite any stuff
title = this.wiki.generateNewTitle(title); title = this.wiki.generateNewTitle(title);
} }
var templateTiddler = this.wiki.getTiddler(this.actionTemplate) || {}; 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) { if(this.actionSaveTitle) {
this.wiki.setTextReference(this.actionSaveTitle,title,this.getVariable("currentTiddler")); this.wiki.setTextReference(this.actionSaveTitle,title,this.getVariable("currentTiddler"));
} }
if(this.actionSaveDraftTitle) { 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 return true; // Action was invoked
}; };

View File

@ -25,4 +25,4 @@ Create Tiddler
<$button actions=<<testCreate>> > <$button actions=<<testCreate>> >
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> <$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
Create Tiddler Create Tiddler
</$button> - Clicking this button, will also open the Right sidebar: Recent tab </$button> - Clicking this button will also open the Right sidebar: Recent tab

View File

@ -8,7 +8,7 @@ type: text/vnd.tiddlywiki
<$action-createtiddler $basetitle="base" $template="ActionCreateTiddlerWidget Template" aa="new field aa" bb="new field bb" /> <$action-createtiddler $basetitle="base" $template="ActionCreateTiddlerWidget Template" aa="new field aa" bb="new field bb" />
\end \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. There will be new fields "aa" and "bb" which are added to the new tiddlers.
@ -25,4 +25,4 @@ Create Tiddler
<$button actions=<<testCreate>> > <$button actions=<<testCreate>> >
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> <$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
Create Tiddler Create Tiddler
</$button> - Clicking this button, will also open the Right sidebar: Recent tab </$button> - Clicking this button will also open the Right sidebar: Recent tab

View File

@ -1,6 +1,6 @@
caption: action-createtiddler caption: action-createtiddler
created: 20161020152745942 created: 20161020152745942
modified: 20200131151847266 modified: 20210601092956998
tags: Widgets ActionWidgets tags: Widgets ActionWidgets
title: ActionCreateTiddlerWidget title: ActionCreateTiddlerWidget
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -16,17 +16,21 @@ There are several differences from the [[tm-new-tiddler message|WidgetMessage: t
! Content and Attributes ! 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 | |!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| |$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 | |$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 |<<.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 | |$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" | |$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 | |$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!'' | |$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 | |//{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 ! Examples
<<< <<<

View File

@ -4,9 +4,11 @@ tags: ActionCreateTiddlerWidget
title: ActionCreateTiddlerWidget Example title: ActionCreateTiddlerWidget Example
type: text/vnd.tiddlywiki 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> <$button>
<$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}/> <$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}>
Create non-functional page control button <$action-navigate $to=<<createTiddler-title>>/>
</$action-createtiddler>
Create a new non-functional page control button and open the tiddler
</$button> </$button>