1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-28 02:50:27 +00:00

Rewrite tag-picker macros as procedures (#7883)

This PR attempts to modernize the tag-picker macros as procedures, as @Jermolene suggested in [#7548 (comment)](https://github.com/Jermolene/TiddlyWiki5/pull/7548#issuecomment-1825458523).

What I changed:

* rewrote macros as procedures/replaced text substitutions with variables
* replaced nested $set and $var widgets with $let widgets/functions
* minor rewrites to reduce redundant code
* some additional linebreaks for clarity

*This is functionally identical to my initial PR, [#7880](https://github.com/Jermolene/TiddlyWiki5/pull/7880)*, minus the extra file I'd accidentally added. Thank you for bearing with me as I learn to navigate GitHub.
This commit is contained in:
etardiff 2024-02-28 06:32:48 -05:00 committed by GitHub
parent 3543fe53ff
commit 4cca77b72d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,32 +1,33 @@
title: $:/core/macros/tag-picker title: $:/core/macros/tag-picker
tags: $:/tags/Macro tags: $:/tags/Global
first-search-filter: [tags[]!is[system]search:title<userInput>sort[]] first-search-filter: [tags[]!is[system]search:title<userInput>sort[]]
second-search-filter: [tags[]is[system]search:title<userInput>sort[]] second-search-filter: [tags[]is[system]search:title<userInput>sort[]]
\define get-tagpicker-focus-selector() [data-tiddler-title="$(currentTiddlerCSSEscaped)$"] .tc-add-tag-name input \procedure get-tagpicker-focus-selector()
\function currentTiddlerCSSEscaped() [<saveTiddler>escapecss[]]
[data-tiddler-title=`$(currentTiddlerCSSEscaped)$`] .tc-add-tag-name input
\end
\define delete-tag-state-tiddlers() <$action-deletetiddler $filter="[<newTagNameTiddler>] [<storeTitle>] [<tagSelectionState>]"/> \procedure delete-tag-state-tiddlers() <$action-deletetiddler $filter="[<newTagNameTiddler>] [<storeTitle>] [<tagSelectionState>]"/>
\define add-tag-actions(actions,tagField:"tags") \procedure add-tag-actions()
\whitespace trim \whitespace trim
<$set name="tag" value={{{ [<__tiddler__>get[text]] }}}> <$let tag=<<tag>>>
<$list <$action-listops $tiddler=<<saveTiddler>> $field=<<tagField>> $subfilter='+[toggle<tag>trim[]]'/>
filter="[<saveTiddler>!contains:$tagField$<tag>!match[]]" <$list
variable="ignore" filter="[<tag>] :intersection[<saveTiddler>get<tagField>enlist-input[]]"
emptyMessage="<$action-listops $tiddler=<<saveTiddler>> $field=<<__tagField__>> $subfilter='-[<tag>]'/>" variable="ignore"
> emptyMessage="<<actions>>"
<$action-listops $tiddler=<<saveTiddler>> $field=<<__tagField__>> $subfilter="[<tag>trim[]]"/> />
<$transclude $variable="__actions__"/> </$let>
</$list>
</$set>
<<delete-tag-state-tiddlers>> <<delete-tag-state-tiddlers>>
<$action-setfield $tiddler=<<refreshTitle>> text="yes"/> <$action-setfield $tiddler=<<refreshTitle>> text="yes"/>
\end \end
\define clear-tags-actions-inner() \procedure clear-tags-actions-inner()
\whitespace trim \whitespace trim
<$list <$list
filter="[<storeTitle>has[text]] [<newTagNameTiddler>has[text]]" filter="[<storeTitle>has[text]] ~[<newTagNameTiddler>has[text]]"
variable="ignore" variable="ignore"
emptyMessage="<<cancel-delete-tiddler-actions 'cancel'>>" emptyMessage="<<cancel-delete-tiddler-actions 'cancel'>>"
> >
@ -34,144 +35,133 @@ second-search-filter: [tags[]is[system]search:title<userInput>sort[]]
</$list> </$list>
\end \end
\define clear-tags-actions() \procedure clear-tags-actions()
\whitespace trim \whitespace trim
<$set name="userInput" value={{{ [<storeTitle>get[text]] }}}> <$let userInput=<<userInput>>>
<$list filter="[<newTagNameTiddler>get[text]!match<userInput>]" emptyMessage="<<clear-tags-actions-inner>>"> <$list
<$action-setfield $tiddler=<<newTagNameTiddler>> text=<<userInput>>/><$action-setfield $tiddler=<<refreshTitle>> text="yes"/> filter="[<newTagNameTiddler>get[text]!match<userInput>]"
emptyMessage="<<clear-tags-actions-inner>>"
>
<$action-setfield $tiddler=<<newTagNameTiddler>> text=<<userInput>>/>
<$action-setfield $tiddler=<<refreshTitle>> text="yes"/>
</$list> </$list>
</$set> </$let>
\end \end
\define tag-picker-inner(actions,tagField:"tags") \procedure add-button-actions()
<$action-listops $tiddler=<<saveTiddler>> $field=<<tagField>> $subfilter="[<tag>trim[]]"/>
<<actions>>
<<delete-tag-state-tiddlers>>
<$action-sendmessage $message="tm-focus-selector" $param=<<get-tagpicker-focus-selector>>/>
\end
\procedure list-tags(filter, suffix)
\whitespace trim \whitespace trim
<$vars <$list
filter="[<userInput>minlength{$:/config/Tags/MinLength}limit[1]]"
emptyMessage="<div class='tc-search-results'>{{$:/language/Search/Search/TooShort}}</div>" variable="listItem"
>
<$list filter=<<filter>> variable="tag">
<$let
button-classes=`tc-btn-invisible ${ [<tag>addsuffix<suffix>] -[<tagSelectionState>get[text]] :then[[]] ~tc-tag-button-selected }$`
currentTiddler=<<tag>>
>
{{||$:/core/ui/TagPickerTagTemplate}}
</$let>
</$list>
</$list>
\end
\procedure tag-picker-inner()
\whitespace trim
<$let
newTagNameInputTiddlerQualified=<<qualify "$:/temp/NewTagName/input">> newTagNameInputTiddlerQualified=<<qualify "$:/temp/NewTagName/input">>
newTagNameSelectionTiddlerQualified=<<qualify "$:/temp/NewTagName/selected-item">> newTagNameSelectionTiddlerQualified=<<qualify "$:/temp/NewTagName/selected-item">>
fallbackTarget={{$(palette)$##tag-background}} fallbackTarget={{{ [<palette>getindex[tag-background]] }}}
colourA={{$(palette)$##foreground}} colourA={{{ [<palette>getindex[foreground]] }}}
colourB={{$(palette)$##background}} colourB={{{ [<palette>getindex[background]] }}}
storeTitle={{{ [<newTagNameInputTiddler>!match[]] ~[<newTagNameInputTiddlerQualified>] }}}
tagSelectionState={{{ [<newTagNameSelectionTiddler>!match[]] ~[<newTagNameSelectionTiddlerQualified>] }}}
tagAutoComplete=<<qualify "$:/state/popup/tags-auto-complete">>
refreshTitle=<<qualify "$:/temp/NewTagName/refresh">>
nonSystemTagsFilter="[tags[]!is[system]search:title<userInput>sort[]]"
systemTagsFilter="[tags[]is[system]search:title<userInput>sort[]]"
> >
<$vars <div class="tc-edit-add-tag">
storeTitle={{{ [<newTagNameInputTiddler>!match[]] ~[<newTagNameInputTiddlerQualified>] }}} <div>
tagSelectionState={{{ [<newTagNameSelectionTiddler>!match[]] ~[<newTagNameSelectionTiddlerQualified>] }}} <span class="tc-add-tag-name tc-small-gap-right">
> <$transclude
<$vars $variable="keyboard-driven-input"
refreshTitle=<<qualify "$:/temp/NewTagName/refresh">> tiddler=<<newTagNameTiddler>>
nonSystemTagsFilter="[tags[]!is[system]search:title<userInput>sort[]]" storeTitle=<<storeTitle>>
systemTagsFilter="[tags[]is[system]search:title<userInput>sort[]]" refreshTitle=<<refreshTitle>>
> selectionStateTitle=<<tagSelectionState>>
<div class="tc-edit-add-tag"> inputAcceptActions=<<add-tag-actions>>
<div> inputCancelActions=<<clear-tags-actions>>
<span class="tc-add-tag-name tc-small-gap-right"> tag="input"
<$macrocall placeholder={{$:/language/EditTemplate/Tags/Add/Placeholder}}
$name="keyboard-driven-input" focusPopup=<<tagAutoComplete>>
tiddler=<<newTagNameTiddler>> class="tc-edit-texteditor tc-popup-handle"
storeTitle=<<storeTitle>> tabindex=<<tabIndex>>
refreshTitle=<<refreshTitle>> focus={{{ [{$:/config/AutoFocus}match[tags]then[true]] ~[[false]] }}}
selectionStateTitle=<<tagSelectionState>> filterMinLength={{$:/config/Tags/MinLength}}
inputAcceptActions="<$macrocall $name='add-tag-actions' actions=<<__actions__>> tagField=<<__tagField__>>/>" cancelPopups=<<cancelPopups>>
inputCancelActions=<<clear-tags-actions>> configTiddlerFilter="[[$:/core/macros/tag-picker]]"
tag="input" />
placeholder={{$:/language/EditTemplate/Tags/Add/Placeholder}} </span>
focusPopup=<<qualify "$:/state/popup/tags-auto-complete">> <$button popup=<<tagAutoComplete>>
class="tc-edit-texteditor tc-popup-handle" class="tc-btn-invisible tc-btn-dropdown"
tabindex=<<tabIndex>> tooltip={{$:/language/EditTemplate/Tags/Dropdown/Hint}}
focus={{{ [{$:/config/AutoFocus}match[tags]then[true]] ~[[false]] }}} aria-label={{$:/language/EditTemplate/Tags/Dropdown/Caption}}
filterMinLength={{$:/config/Tags/MinLength}} >
cancelPopups=<<cancelPopups>> {{$:/core/images/down-arrow}}
configTiddlerFilter="[[$:/core/macros/tag-picker]]" </$button>
/> <$reveal state=<<storeTitle>> type="nomatch" text="">
</span> <$button actions=<<delete-tag-state-tiddlers>>
<$button popup=<<qualify "$:/state/popup/tags-auto-complete">> class="tc-btn-invisible tc-small-gap tc-btn-dropdown"
class="tc-btn-invisible tc-btn-dropdown" tooltip={{$:/language/EditTemplate/Tags/ClearInput/Hint}}
tooltip={{$:/language/EditTemplate/Tags/Dropdown/Hint}} aria-label={{$:/language/EditTemplate/Tags/ClearInput/Caption}}
aria-label={{$:/language/EditTemplate/Tags/Dropdown/Caption}} >
{{$:/core/images/close-button}}
</$button>
</$reveal>
<span class="tc-add-tag-button tc-small-gap-left">
<$let tag=<<tag>>>
<$button set=<<newTagNameTiddler>> setTo=""
actions=<<add-button-actions>>
> >
{{$:/core/images/down-arrow}} {{$:/language/EditTemplate/Tags/Add/Button}}
</$button> </$button>
<$reveal state=<<storeTitle>> type="nomatch" text=""> </$let>
<$button class="tc-btn-invisible tc-small-gap tc-btn-dropdown" </span>
tooltip={{$:/language/EditTemplate/Tags/ClearInput/Hint}} </div>
aria-label={{$:/language/EditTemplate/Tags/ClearInput/Caption}} <div class="tc-block-dropdown-wrapper">
> <$reveal state=<<tagAutoComplete>> type="nomatch" text="">
{{$:/core/images/close-button}}<<delete-tag-state-tiddlers>> <div class="tc-block-dropdown tc-block-tags-dropdown">
</$button> <$let userInput=<<userInput>>>
</$reveal> <$transclude $variable="list-tags" filter=<<nonSystemTagsFilter>> suffix="-primaryList" />
<span class="tc-add-tag-button tc-small-gap-left"> <hr>
<$set name="tag" value={{{ [<newTagNameTiddler>get[text]] }}}> <$transclude $variable="list-tags" filter=<<systemTagsFilter>> suffix="-secondaryList" />
<$button set=<<newTagNameTiddler>> setTo="" class=""> </$let>
<$action-listops $tiddler=<<saveTiddler>> $field=<<__tagField__>> $subfilter="[<tag>trim[]]"/>
<$transclude $variable="__actions__"/>
<$set name="currentTiddlerCSSEscaped" value={{{ [<saveTiddler>escapecss[]] }}}>
<<delete-tag-state-tiddlers>><$action-sendmessage $message="tm-focus-selector" $param=<<get-tagpicker-focus-selector>>/>
</$set>
{{$:/language/EditTemplate/Tags/Add/Button}}
</$button>
</$set>
</span>
</div> </div>
<div class="tc-block-dropdown-wrapper"> </$reveal>
<$reveal state=<<qualify "$:/state/popup/tags-auto-complete">> type="nomatch" text="" default=""> </div>
<div class="tc-block-dropdown tc-block-tags-dropdown"> </div>
<$set name="userInput" value={{{ [<storeTitle>get[text]] }}}> </$let>
<$list
filter="[<userInput>minlength{$:/config/Tags/MinLength}limit[1]]"
emptyMessage="<div class='tc-search-results'
>
{{$:/language/Search/Search/TooShort}}</div>" variable="listItem">
<$list filter=<<nonSystemTagsFilter>> variable="tag">
<$list
filter="[<tag>addsuffix[-primaryList]] -[<tagSelectionState>get[text]]"
emptyMessage="<$vars button-classes='tc-btn-invisible tc-tag-button-selected' actions=<<__actions__>> tagField=<<__tagField__>> currentTiddler=<<tag>>>{{||$:/core/ui/TagPickerTagTemplate}}</$vars>"
>
<$vars button-classes="tc-btn-invisible"
actions=<<__actions__>>
tagField=<<__tagField__>>
currentTiddler=<<tag>>
>
{{||$:/core/ui/TagPickerTagTemplate}}
</$vars>
</$list>
</$list>
</$list>
<hr>
<$list filter="[<userInput>minlength{$:/config/Tags/MinLength}limit[1]]" emptyMessage="<div class='tc-search-results'>
{{$:/language/Search/Search/TooShort}}</div>" variable="listItem">
<$list filter=<<systemTagsFilter>> variable="tag">
<$list filter="[<tag>addsuffix[-secondaryList]] -[<tagSelectionState>get[text]]"
emptyMessage="<$vars button-classes='tc-btn-invisible tc-tag-button-selected' actions=<<__actions__>> tagField=<<__tagField__>> currentTiddler=<<tag>>>{{||$:/core/ui/TagPickerTagTemplate}}</$vars>"
>
<$vars button-classes="tc-btn-invisible"
actions=<<__actions__>>
tagField=<<__tagField__>>
currentTiddler=<<tag>>
>
{{||$:/core/ui/TagPickerTagTemplate}}
</$vars>
</$list>
</$list>
</$list>
</$set>
</div>
</$reveal>
</div>
</div>
</$vars>
</$vars>
</$vars>
\end \end
\define tag-picker(actions,tagField:"tags") \procedure tag-picker(actions, tagField:"tags")
\function userInput() [<storeTitle>get[text]]
\function tag() [<newTagNameTiddler>get[text]]
\whitespace trim \whitespace trim
<$vars saveTiddler=<<currentTiddler>> palette={{$:/palette}}> <$let
<$list saveTiddler=<<currentTiddler>>
filter="[<newTagNameTiddler>match[]]" palette={{$:/palette}}
emptyMessage="<$macrocall $name='tag-picker-inner' actions=<<__actions__>> tagField=<<__tagField__>>/>" qualified=<<qualify "$:/temp/NewTagName">>
> newTagNameTiddler={{{ [<newTagNameTiddler>!match[]] ~[<qualified>] }}}
<$set name="newTagNameTiddler" value=<<qualify "$:/temp/NewTagName">>> >
<$macrocall $name="tag-picker-inner" actions=<<__actions__>> tagField=<<__tagField__>>/> <$transclude $variable="tag-picker-inner" />
</$set> </$let>
</$list>
</$vars>
\end \end