Tm-download-file issue with functions in exportFilter

Hello,

I have created a JSON tiddler attached to import into tiddlywiki.com to illustrate my issue but the example code is:

\define exportJsonButton(exportFilter:"[!is[system]sort[title]]",baseFilename:"JSONexport")
<$set name="extension" value=".json">
<$button class="tc-btn-invisible" dragFilter=<<__exportFilter__>> tooltip="Click button to download a JSON archive or drag to another wiki to copy.">
{{$:/core/images/export-button}}
<$action-sendmessage $message="tm-download-file" $param="$:/core/templates/exporters/JsonFile" exportFilter=<<__exportFilter__>> filename=<<exportButtonFilename """$baseFilename$""">>/>
</$button>
</$set>
\end

\function currentSelection() [<currentSelectionStateTid>get[text]]

\function currentSelectionStateTid() [[tm-download-file Issue]get[currentSelectionTiddler]]

<$select tiddler={{tm-download-file Issue!!currentSelectionTiddler}}>
<option></option>
<$list filter="[tags[]]">
<option><$view field='title'/></option>
</$list>
</$select>

Current selection: <<currentSelection>>

Current selection state tiddler:  {{{[<currentSelectionStateTid>]}}}

selection state tiddler is hardcoded: Works for both click and drag: <$transclude $variable="exportJsonButton" exportFilter="""[search:title,tags{$:/state/currentSelection}]"""  baseFilename=<<currentSelection>> />

Attempt to make selection state tiddler configurable in field ``currentSelectionTiddler``. Works for drag but click matches all tiddlers: <$transclude $variable="exportJsonButton" exportFilter="""[search:title,tags<currentSelection>]"""  baseFilename=<<currentSelection>> />

I was trying to use a function to abstract a state tiddler location so that you can define the location of the state tidder in a field of a config tiddler and use that info from the state tiddler in the filter fed to the tm-download-file message.

For some reason the exportFilter="""[search:title,tags<currentSelection>]""" does not work for tm-download-file and matches on all tiddlers but if you “hardcode” the state location with exportFilter="""[search:title,tags{$:/state/currentSelection}]""" it properly matches tiddlers with tags and titles containing the words contained in $:/state/currentSelection.

The both filters work just fine when used in the dragFilter for the button widget.

In the end, I had to “hardcode” in the state tiddler and it seems that tm-download-file message is broken with functions. Is this supposed to be how functions are used? Does anyone know of a workaround?

Thanks,
/Mike

tm-download-file Issue.json (1.7 KB)

The tm-download-file message is processed asynchronously by the TWCore. The issue with your <currentSelection> reference is that, by the time the tm-download-file is handled, it is no longer running within the same context (aka, “scope”) as when the $action-sendmessage was initially invoked, so the value of the currentSelection variable is no longer available when the exportFilter is evaluated and your filter uses tags[] which, as you noted, “matches on all tiddlers”.

To address this, you can pass the desired variable as an extra param to the tm-download-file message, like this:

<$action-sendmessage $message="tm-download-file" $param="$:/core/templates/exporters/JsonFile" exportFilter=<<__exportFilter__>> filename=<<exportButtonFilename """$baseFilename$""">> currentSelection=<<currentSelection>>/>

This usage IS documented (but not adequately explained) here: WidgetMessage: tm-download-file

Let me know how it goes…

-e

1 Like

Thanks @EricShulman .

Your explanation makes complete sense and I tried every variation of it:

currentSelection=<<currentSelection>>
$currentSelection=<<currentSelection>>
currentSelection={{{[<currentSelection>]}}}
$currentSelection={{{[<currentSelection>]}}}

But nothing seems to prevent that <currentSelection> in the filter passed into the message from resolving to null and causing the filter to match all tiddlers.

This would be a very elegant solution if I could get it to work.

I have found a workaround by forcing the evaluation of the currentSelection function before passing it into the exportJsonButton macro in the first place by piecing the plain filter together via filtered transclusion concatenation. I.e. {{{=[<lb>] [[search:title,tags]] =[<lb>][<currentSelection>] =[<rb>] =[<rb>] +[join[]]}}}

Cludgy solution attached.

Not the prettiest thing but avoids variables in the nested contexts.

Thanks,
/Mike

tm-download-file Issue (1).json (1.9 KB)