Core variable that contains the selected text?

Hello everyone, how would you recommend creating a wikitext variable that contains the current text selection? I don’t believe one exists out of the box, but it may be useful to implement, for example, a Create New Tiddler functionality that uses the text selection as title.

Something like <<currentTiddler>> would be nice, e.g. <<textSelection>>.

Thanks for the tips!

[Edited to Read/show code]

How would you use it?

  • I think I can answer this question a number of ways
  • Do you mean to have a global variable name whos value you re-place with the selected text ?

I have in the past looked at adding to every text selection button an action to copy every selection to a data tiddler called “MRU - most recently used selections” so you can access past selections.

I would use it to create a button and keyboard shortcut to rapidly create a new tiddler from the selected text in another tiddler.

I find often that I will write some plain text in a tiddler, and later on, decide to create a tiddler about some of the words/expression I previously used (e.g. provide extra information about it). So the optimal workflow would look like select the text in the paragraph I want to turn into a new tiddler, hit a keyboard sequence, and that would 1) replace the selected text with a wikitext link, and 2) create a new tiddler with the selected text as its title.

As an example, say I create a tiddler about gizmo X, and in the tiddler, state that gizmo X is created by Company Y.
Later on, I may wish to rapidly create a Company Y tiddler. A quick way to do that would be to highlight the words “Company Y”, hit a button/keyboard shortcut, and have that turn into a link and create a new tiddler with the exact title.

Please note that I don’t rely on CamelCase auto-linking normally. I feel it would be a relatively easy problem to solve with a <<textSelection>> core variable (but I am most certainly overlooking a few details).

Can you not make use of the Editor toolbar excise button to do this?

  • I think that does exactly what you ask for with no need for a variable

Please note you need to wrap code in Discourse with the backtick like in tiddlywiki or we cant read it, that is why I edited your first post. You can also use the code block.

Have you tried the Editor toolbar stamp?

  • and ‘add your own’

Try this to collect selections, I now see its off topic but share anyway;

Create a tiddler tagged $:/tags/Macro containing

\define selected-text() {{selected-text}}

Create the tiddler selected-text and put some content in it.

Now where you want to use the content use <<selected-text>>

If that works for you we can create an Editor toolbar that uses the setfeild action to set the “selected-text” tiddler to the selection.

ok, will give these ideas a try and report back… Though I wish to be able to do this without having to enter edit mode. Would excise work in view mode?
Cheers!

2 Likes

Ahh,

  • I am not sure the method I share will help unless you are prepared to use edit.

No it does not, It is designed to change replace the selection with a transclusion, macro or link.

  • But I recently raised the desire to be able to write ViewToolbar buttons that can respond to the selection on the view like in the editor.
  • However have you thought of one extra step? Copy the text to the clipboard and paste it somewhere else to do what you want.
  • There are other solutions like quicklinks that in part address what you ask by another means.

May I ask why you do not want to go into edit? Think deeply.

  • I sometimes use a button I toggle to just edit the text field for example.
  • The excise is very powerful, and so is the ability to write new “Editor Toolbar buttons”.

For one extra step, I have a solution already: I can use the CommandPalette plugin, it has features that lets you define commands that use the selected text as a command parameter. The intermediary step creates the command from the selection and key press, and pops up the basic prompt interface, then the enter key executes the command.

I would normally need not to enter the edit mode, so I would consider options from edit mode to also be one extra step. Still useful information and with some of these I can probably bolt together a “one step” solution.

Are you saying with selected text in view mode?

I need to do this for myself anyway, so I will share my new inline edit solution here for you to try. It is a very small step to use edit.

[Edited FYI]

In order to implement an use this “inline edit tool” I am building a toggle button, while I am doing that I am building a template toggle button I can reuse and I am stuck on this Here I go again, I just want to colourise a button in the Toolbars

  • I know it looks like I am straying from the OT however I belive if I can make this as easy as I can envision it may be a good work around if not solution.
  • However regardless it would be nice if we could make viewToolbar buttons that can respond to selected content on the rendered tiddler.

Yes precisely, though perhaps the plug-in would work equally well from the edit mode (I can’t think of a reason why it wouldn’t, but I’ll try tomorrow to confirm. It leverages JavaScript for the text selection information and I would expect that to work from either mode)…

It will be hard to create plugin that will work with tiddlers that contain widgets. eg. There are 2 tiddlers

First tiddler

title: test 

{{my text}}

Second tiddler

title: my text

Some text and a future link to a book

If you open first tiddler “test” in view mode you will see the transcluded text “Some text and a future link to a book” … So if you select some text, it may be possible to create a tiddler with that selected text, … but it doesn’t make sense to edit the “test” tiddler, because you will need to edit “my text” …

It is even more complex to find the right tiddler to modify if you use list-widget. … So it should be straight forward to do what you want in edit mode. …

But editing transcluded tiddlers in view mode is a challenge.

Good point.

Perhaps I can leverage the search/replace features that many plugins use already (e.g. @Mohammad’s Kara 0.6.4 — small in tiddler plain checklist and interstitial journaling, @stobot’s Stickies — lightweight todo system) on the story list tiddlers, and accept the fact that if the text is a transclusion/widget/etc, the step 2 (replace selection with a wikitext link) will not work in those situations.

(@TW_Tones : confirmed, Command Palette — a command palette for TiddlyWiki open-command-palette-selection works from either view or edit mode)

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: