How can I make a form with a button that does something?

84 views
Skip to first unread message

Joe Armstrong

unread,
Dec 9, 2018, 7:10:42 AM12/9/18
to TiddlyWiki
I want to make a simple form with three fields and a button
The fields are regular html  input fields (like <input type='text'>)

The fields contain the name of a new tiddler and two text strings.
Call these fields 'tiddler-name' text1 and text2

When I press the button I want to execute some custom JS code which I write
this should:

Read the name of the new tiddler from one of the entries. Read the string in text1, Read the string in text2
then:

   1) reverse the string read from text2
   2) create a new tiddler using the name in the 'tiddler-name' field
       note: or issue an error if such a tiddler already exists
       populate the new tiddler with some wiki text that my custom JS
       button code creates

I'd really like to see exactly how to do this - note: NOT using any fancy plugins or
non-standard macros to achieve this. I want to see and understand the raw code to
do this.

What's the best way to do this?

Cheers

/Joe



Jed Carty

unread,
Dec 9, 2018, 8:52:45 AM12/9/18
to TiddlyWiki
What you are describing is a non-standard thing so doing it without using non-standard macros or a plugin isn't really possible.

There are ways to do the equivalent using tiddlywiki, but instead of having html input elements you should use edit-text widgets, to make the tiddler with the given name and the fields use a set-text widget. Reversing the string would be a non-standard macro, the core doesn't have anything to do that build-in.

Forms in wikitext are simple, just put this inside a tiddler for all of the things you described aside from the reversing the text and the custom javascript part, because they require custom macros which  you explicitly said you didn't want to use:

Tiddler name: <$edit-text tiddler='$:/temp/NewTiddlerThing' field='tiddler-name'/>

Text1: <$edit-text tiddler='$:/temp/NewTiddlerThing' field='text1'/>

Text2: <$edit-text tiddler='$:/temp/NewTiddlerThing' field='text2'/>

<$list filter='[field:title{$:/temp/NewTiddlerThing!!tiddler-name}]'>
Warning! The tiddler <<currentTiddler>> already exists!!
</$list>
<$button>
Make it!
<$action-setfield $
tiddler={{$:/temp/NewTiddlerThing!!tiddler-name}} text={{$:/temp/NewTiddlerThing!!text1}} text2={{$:/temp/NewTiddlerThing!!text2}}/>
</$button>

BurningTreeC

unread,
Dec 9, 2018, 9:19:21 AM12/9/18
to TiddlyWiki
Hi Joe,

to reverse a string you can use a crazy thing like this, putting it in
\define print-next(string:"",prefix:"",count:"")
<$macrocall $name="print-single-letters" string=<<__string__>> prefix=<<__prefix__>> count=<<__count__>>/>
\end

\define print-single-letters(string:"",prefix:"",count:"0")
<$list filter="[<__string__>minlength[1]]">
<$set name="newcount" filter=" [range[0,100]after[$count$]]">
<$set name="printprefix" value={{{ [<__string__>splitbefore[]addprefix<__count__>] }}}>
<$set name="newstring" value={{{ [<__string__>removeprefix<__prefix__>] }}}>
<$text text=<<printprefix>>/
>
<$list filter="[<newstring>minlength[1]]">
<$macrocall $name="print-next" string=<<newstring>> prefix={{{ [<newstring>splitbefore[]] }}} count=<<newcount>>/>
</
$list>
</$set>
</
$set>
</$set>
</
$list>
\end

\define range-enlist()
<$list filter="[range[0,$(length)$]reverse[]]">
<$set name="nnth" value={{{ [range[0,100]after<currentTiddler>] }}}>
<$list filter="[enlist[$(singleLetters)$]nth<nnth>removeprefix<currentTiddler>]">
<<currentTiddler>>
</$list>
</
$set>
</$list>
\end
\define reverse-single-letters()
<$set name="length" filter="[enlist[$(singleLetters)$]] +[count[]]">
<<range-enlist>>
</
$set>
\end

\define reverse-ma-string(string:"")
<$wikify name="singleLetters" text="""<$macrocall $name="print-single-letters" string=<<__string__>> prefix={{{ [<__string__>splitbefore[]] }}}/>""">
<$wikify name="reversedLetters" text=<<reverse-single-letters>>>
<<reversedLetters>>
</$wikify>
</
$wikify>
\end

a tiddler tagged $:/tags/Macro and calling it from another tiddler with <<reverse-ma-string"thisismystring">> or <$macrocall $name="reverse-ma-string" string={{transcludethistiddler}}/>


Reply all
Reply to author
Forward
0 new messages