Adding custom actions when saving a tiddler

Earlier today in the TiddlyWiki Discord server (see https://discord.com/invite/HFFZVQ8), @gavcloud asked:

As this seems to be a relatively common question that has been asked before, I’m sharing my reply here:

First, edit shadow tiddler $:/core/ui/EditTemplate. In the “save-tiddler-actions()” macro definition, add <<save-tiddler-custom-actions>> just before the <<delete-edittemplate-state-tiddlers>>.

Then, create a new tiddler (e.g., “MyCustomSaveActions”), tagged with $:/tags/Global, and enter the following wikitext code:

\define save-tiddler-custom-actions()
<$list filter="[<currentTiddler>tag[Bookmark Tag]]" variable=none>
<$action-sendmessage $message="tm-add-field" $name="view" $value="[sort[title]]"/>
</$list>
\end

Now, whenever the core save-tiddler-actions() macro is triggered by pressing the “done” button or by using the ctrl-enter keyboard shortcut, your save-tiddler-custom-actions() macro will also be invoked… and, if the tiddler is tagged with “Bookmark Tag”, the tm-add-field action will automatically add a view field containing [sort[title]].

Of course, this technique can be used to add all sorts of “automatically create/set a field” handling side effects when saving a tiddler… and, if you want to “automatically add a tag”, you can use a tm-add-tag message instead of (or in addition to) the tm-add-field message:

<$action-sendmessage $message="tm-add-tag" $param="sometag"/>

enjoy,
-e

7 Likes

I’ve asked a similar (if not identical) question recently Are tiddler save hooks possible?

Also see https://all-button-actions.tiddlyhost.com which is mentioned in that thread.

Edit: This Tix v0.10 — A Novel Tiddler Experience also popped up in a recent discussion about fonts, if you try to edit a tiddler, you will see multiple save buttons that work differently. I only bookmarked this, did not look at implementation yet, but perhaps @telmiger could comment on this.

Oh no :frowning:

Are there any public logs online?

While I’ve never used (and never will use) Discord, I don’t mind people using it for banter and private chats. Albeit I know one case when an unfortunate decision to completely shift the main place for community interaction from a classic forum to Discord (not the case of TiddlyWiki though) has ruined said community in one year. I’d like to think I’m not an anti-Discord zealot, yet when it’s about technical topics, that remains locked behind a gate and non-participants never get access to that knowledge.

1 Like

I don’t think you need to worry about Discord replacing this forum; the Discord server actually predates Talk TW, and it hasn’t fractured the community yet. :wink: I’m a member of the server too, and Talk TW has always been substantially more active — and we regularly refer people here.

I suspect some people simply prefer the format (using Discourse on a phone is a headache!) or find it less intimidating to ask a question in Discord. Personally, I think it’s nice that TW fans can find a welcoming community no matter their preferred format!

4 Likes

You actually found my Edit Buttons plugin, available from Plugins — Utilities for TiddlyWiki. If you inspect the readme and the contents of the plugin you will find two buttons that save the tiddler AND do something else (close/reopen).
The relevant button code looks like this:

<$fieldmangler><$button tooltip={{$:/language/Buttons/Save-Open/Hint}} aria-label={{$:/language/Buttons/Save-Open/Caption}} class=<<tv-config-toolbar-class>>>
<<save-tiddler-actions>>
<$list filter="[all[current]get[draft.title]]">
<$action-sendmessage $message="tm-edit-tiddler"/>
</$list>
<$list filter="[<tv-config-toolbar-icons>prefix[yes]]">
{{$:/plugins/telmiger/EditButtons/images/done-open-button}}
</$list>
<$list filter="[<tv-config-toolbar-text>prefix[yes]]">
<span class="tc-btn-text"><$text text={{$:/language/Buttons/Save-Open/Caption}}/></span>
</$list>
</$button></$fieldmangler>

After the (draft !) tiddler is saved using <<save-tiddler-actions>> we have to retreive the title of the (now saved) actual tiddler and do something with it (re-open for editing in this case):

<$list filter="[all[current]get[draft.title]]">
<$action-sendmessage $message="tm-edit-tiddler"/>
</$list>

I hope this helps.

Cheers, Thomas

2 Likes

thanks for contributing it here @EricShulman.

I tried a variant of this, but it doesn’t seem to catch or add these fields when saving a new tiddler with that title prefix. Any thoughts ?

Also, I don’t fully understand the variable=none declaration. Could you elaborate ?

<$list filter="[<currentTiddler>prefix[$:/widget/dj-mix/]]" variable=none>
	<$action-sendmessage $message="tm-add-field" $name="url" $value=""/>
</$list>

man, talk about lucky timing, I was just trying to recall how to do that, and this is a much better approach than i used in the past.

this will make adding revisions to tiddlers much simpler. (save tiddler increases revision field by one count each save, which is shown as a suffix to the subtitle using a modified versioning stylesheet from mr. TWaddle, in case anyone elses wants to do the same, you can find his stylesheet here)

The problem here is that when the <<save-tiddler-custom-actions>> macro is invoked, the value of currentTiddler is the draft title (i.e., Draft of "Some Tiddler Title"), so the prefix[...] filter does not match. To account for this, you could try using {!!draft.title} instead of <currentTiddler>, like this:

<$list filter="[{!!draft.title}prefix[$:/widget/dj-mix/]]" variable=none>
	<$action-sendmessage $message="tm-add-field" $name="url" $value=""/>
</$list>

The purpose of variable=none is to prevent the $list widget from setting the value of currentTiddler based on the result of the filter. This permits the tm-add-field message to be applied to the fields of the draft tiddler (i.e., the currentTiddler) before it is saved to the “real” tiddler by the tm-save-tiddler message that follows it.

-e

2 Likes

thank you @EricShulman it works! It would have taken me ages to figure this one out. Really appreciate it.