How to keep the focus when editing a $:/tags/Macro tagged tiddler with an edit/edit-text widget?

I’m trying to edit tiddlers in the view template with the use of the edit-text widget, this works for every tiddler except tiddlers tagged with $:/tags/Macro. I mean, it works but the focus is lost on every keystroke (the cursor goes to the next input field every time). I guess this is a refresh issue, is there any way to fix this ?

Demo : Demos — Answers and demos for questions about tiddlywiki

If you don’t really need the macro to be global, then you could use the import variable widget:

<$edit-text tiddler="This tiddler is NOT tagged with $:/tags/Macro" class="tc-edit-texteditor" 

/>

<$importvariables filter="[[This tiddler is NOT tagged with $:/tags/Macro]]">
<<stuff>>
</$importvariables>

So when you type in:

\define stuff() mystuff

“mystuff” appears – and focus is retained.

2 Likes

Mh that could work for my end goal - I’m trying to make a code editor like the one on codepen, with a tab for the wikitext, one for the style and one for the macros, to be able to create “ui component” for tiddlywiki in a cleaner way (here’s my WIP). The importvariable widget would work for a single component but ideally I’d prefer to be able to use $:/tags/Macro to make the macro global.

If there is no other way then I will work with that but it should be possible to do since the lost of focus doesnt happen while editing this kind of tiddler in the standard way, I just dont understand how to replicate that behavior … maybe there is some variables or state tiddlers that needs to be set ?

@telumire I see what is happening. I think the issue is an edit to a tiddler containing a global macro needs to immediately trigger an update thus you loose focus. Normally you are editing the draft.of not the actual tiddler, and even preview can work, but global updates only occur with tiddler save.

  • You could copy the macro text to a temporary tiddler and add a done button which would update the original tiddler. Effectively creating your own draft process.
  • There are a number of other ways to do this.
    • Have a custom story in the current tiddler containing the tiddler you want only
    • Use a modified form of toc internal nav with a draft process
  • I will see if I can find some “Prior Art” of mine.
1 Like

The refresh occurs because the PageTemplate (defined in $:/core/ui/PageTemplate) includes this line:

\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]

This line is what actually causes “global” macros to be available to all other tiddlers.

As you can see, the second “run” in the filter imports macro definitions from all shadows+tiddlers that are tagged with $:/tags/Macro, except for those tiddlers that have the draft.of field.

Thus, when editing a macro tiddler via an $edit-text widget in a ViewTemplate, it triggers a refresh every time the tiddler content is changed (i.e. on each keystroke) but NOT when “editing this kind of tiddler in the standard way” since editing a tiddler actually creates a temporary tiddler - “Draft of …”, which has the draft.of field set.

Perhaps you could try emulating this TWCore behavior by wrapping your $edit-text widget within an $eventcatcher widget… something like this:

<$eventcatcher
   $focusin="""<$action-setfield $tiddler=<<targetTiddler>> draft.of="yes"/>"""
   $focusout="""<$action-deletefield $tiddler=<<targetTiddler>> draft.of/>""">

In this way, you could still make changes to the contents of <<targetTiddler>>, but since it will be marked with a draft.of field, it won’t be an “active” macro until the focus moves away from the $edit-text input.

-e

3 Likes

Or try building your tool with the use of a substory.

Try this on your demo wiki SubStory.json (711 Bytes)

1 Like

Thank you very much @TW_Tones and @EricShulman , I will try your suggestions this evening, they look very promising !

I think Erics Idea is a cunning one,

however remember any other macro in that tiddler will then not be available. You thus will need to make sure you are not relying on any of the macros defined therein.

I to think provide a “edit”, button that copies the text to a temp tiddler (without tags etc…) and on setting a state tiddler reveal it for edit, Try this {{$:/temp/macro-tiddler||$:/core/ui/EditTemplate/body}}

Then provide a separate “save” button that copies the temp text to the original tiddler ie set field text. And provide a cancel for good measure.

all the above should work well.