Can we access the 'selection' in the WidgetMessage: tm-edit-text-operation

Folks,

The various tm-edit-text-operation actions allow us to perform a number of different transformations of the selection. We can also add additional actions to do other things. This is used for defining editor toolbar buttons.

I would like to pass the selected text to one of these other non tm-edit-text-operations but there is no documented variable or otherwise to access the selected text.

Can anyone determine if there is an undocumented variable or other method and if not perhaps I will raise an issue to include one?

I think there are no undocumented variables. You would need to be more specific about “one of the other non tm-edit-text-operations” …

Code can only be created, if there is a specific description, what should be achieved. The best way to start is:

As a user / editor / … I would like to …

The “problem” is simple, if not hard to describe.

The editor toolbar buttons contain one or more actions like any other actions you can trigger from a button.

I have used this to extend or build more sopisticated buttons.

We can set a field or template to the selection then use that, but it would be simpler if a variable was available was set to the selected text. The fact is the text operation actions actualy must have access to this value, most likely a variable already, to work.

I dont have the skills to read the text operation actions code to see if there is a variable available or if not see if one can be surfaced.

This feature would allow me to simplify and share code when developing solutions based on plain language in topics I posted recently.

  • this includes parsing the selected text with filters and responding with variouse actions.

Advanced functionality: to also make buttons and keyboard shortcuts outside the editor that have access to the current read only selection would be useful.

I see in the actions javascript reference

text: operation.selection,

Hi @TW_Tones I think you are asking if within the edit text widget we can make the current selection available as a variable.

The technical answer is that it is not practical at the moment because it would cause the contents of the edit widget to fully refreshed every time the selection changes, which would have a performance impact. That is why the ‘tm-edit-text-operation’ message works the way that it does: it allows authors to manipulate the selected text without needing to know its contents.

An alternative approach that might work would be to add a new operation for ‘tm-edit-text-operation’ that takes a parameter specifying a filter that is used to manipulate the selection (and also optionally the text before and after the selection). That would make it possible to make edit toolbar buttons that did transformations like “convert to uppercase”.

2 Likes

Yes. Othe toolbar buttons I have created that could use this is to sort the selected lines a-z and z-a. Currently I am having difficulty making them reliable.

what a great idea @TW_Tones Core variable that contains the selected text?

Think of the possibilities of having a global core variable that contains the text selection; any widget or macro or filter could use this information and do something useful with it :slight_smile:

1 Like

So if I do understand it right.

As a user you want to

  • select some text
  • manipulate the text with wikitext functions
  • create a new text in a variable eg: returnText
  • the returnText should replace the current text selection

That’s more or less, what the stamp-function does. …

The “only” thing missing is a “selectedText” variable that can be used to start with. … That should be doable. …


I did create a GH issue that should be actionable. … BUT I’m not 100% sure, if that’s what you want.

1 Like

I dont know if it helps but what I did to avoid issues when manipulating text like this is to use filtered transclusion on the attributes of the tm-edit-text-operation widgets, e.g to comment some code :

\define delimiter-type() [tag[$:/tags/Stylesheet]] [type[text/css]] [type[application/javascript]]
<$action-sendmessage
 $message="tm-edit-text-operation"
 $param="save-selection"
 tiddler=<<qualify "$:/temp/text">>
/>
<$action-sendmessage
 $message="tm-edit-text-operation"
 $param={{{[<qualify "$:/temp/text">has[text]then[wrap-selection]else[wrap-lines]]}}}
 prefix={{{ [<targetTiddler>filter<delimiter-type>then[/*]else[<!--]] }}}
 suffix={{{ [<targetTiddler>filter<delimiter-type>then[*/]else[-->]] }}}
/>

But I agree, it would be nice to have a built-in variable instead of a temporary tiddler.

2 Likes

@jeremyruston: With “every time the selection changes” do you mean during the process of selecting something? Like while I’m dragging the mouse to select a couple of words, the widget would update a bunch of times while the selection expands.
Or would it only update once after releasing the mouse button to finish the selection process? I can’t imagine that this would have a performance impact since it’s done only once. So I guess you mean the former…

Have a nice day
Yaisog

My use case is more about having access to the text selection in view mode, and be able to use it to do interesting things such as creating a new tiddler with that text as its title…

1 Like

@fastfreddy Maybe you want the Dynannotate plugin?

dyna

Code :


<$button>
<$action-createtiddler $basetitle={{selection}} tags="annotation">
<$action-navigate $to=<<createTiddler-title>>/>
</$action-createtiddler>
Create tiddler from selection
</$button>

<$dynannotate selection="selection">
Lorem ipsum dolor sit amet [..]

EDIT: It may also be possible to do this with the eventcatcherwidget but I’m not sure how
EDIT2: Seems like the eventcatcher doesnt work with the select event

5 Likes

Just to be clear, if the widget were only refreshed when the button triggering it was pressed would be sufficient, but I don’t know how practical that is.

@telumire seems to have the view template version working with $dynannotate on the view template, using the selection attribute, and a tiddler.

I need to learn more about $dynannotate.

That looks like what I wanted for the view Template, thanks

For the Editor toolbar buttons. For now I am happy with the tm-edit-text-operation - save-selection and access the temp tiddler field used.

Would it be possible to add the create new tiddler from selection button into the dynannotate’s default pop up.

I think @Mark_S has done some work on dynaannotate previously - see this link.Has someone already made a dynanote annotater?

1 Like

Yes it’s possible, there is one in the example that comes with the plugin. It’s a bit complex tho, that’s why I used a simple button for the demo

EDIT: @Mark_S’s plugin does exactly that : https://talk.tiddlywiki.org/uploads/short-url/ws8eDb61s3eOmFxQ2VN68o31PzV.json

2 Likes

Thanks for all the great suggestions everyone! I was able to use most of them to grab bits and pieces of code (like the plumber I am) and now have solution that is simple and works really well, from view mode. In the end, I found Dynannotate to be a bit of and overkill, and I instead based the solution off the context menu plugin.

I have modified a few things:
1- disable the check for selection (initially, the plugin deliberately does not work if there is text selected);
2- inserted a with a check for the control key (inspired by @saqimtiaz 's link context menu plugin). This way, you can still access the browser’s context menu for selections, if you wish, by pressing the control key. I may choose to inverse the logic, but for now, this is my preference;
3- added an option on the context menu that 1) creates a new tiddler from the text selection and 2) replaces the selection (text search and replace) with a link to the new tiddler, from view mode (this I thought would be much harded than it ended up being, inspired by this search and replace plugin)

(this code goes in $:/plugins/ahanniga/context-menu/ContextListener.js, but you need one more simple tiddler for the new menu item.)

            case "tm-new-tiddler":
                this.dispatchEvent({ type: "tm-new-tiddler", paramObject: {title: ""+selection+""}});
$tw.wiki.setText(targ,"text",null,text.replace(selection,"[["+selection+"]]"));
                break;

I’ll continue to play around but this seems to fit my needs.
One shortcoming noted so far is if the raw text has a line break that is not visible in view mode (or any other fancy wikitext rendering), the search&replace function will not find the selection in the text. I can probably live with that.

before:

after:

3 Likes

Please share a working example if possible.

on it; will do this evening; tried to package twice with Tinka and lost the plugin both times. Will try with @Mohammad packager.

3 Likes

try this @TW_Tones (you will also need this plugin: https://context-menu-plugin.tiddlyhost.com)

plugin_fastfreddy_new-from-selection_v0.0.0_20220922.json (7.9 KB)

1 Like

Thanks it works well @fastfreddy, I had not used the context menu plugin before, its great. Such a menu could be the solution here Has anyone found a solution to title and button overflow on tiddlers? and expect it may work well with MCL Multi-column layout whose weakness in my view is titles/button competition for space.

Now also the answer to this topic is included. Thanks.