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

@arunnbabu81 : here’s a minor update which provides two options (create new tiddler from selection, and make todo from selection).

I have added some error checking, type conversion, and the selected words are TitleCased for the new tiddler (using the [[selection|Selection]] text replacement).

You will also need the Stickies — lightweight todo system plugin for the new option.

same caveat: the selected text is from the rendered WikiText; the search-replace is done on the raw WikiText; if the two aren’t identical, the source tiddler replacement will not work.


plugin_fastfreddy_ContextMenuAddin_v0.0.1_20220922.json (9.7 KB)

3 Likes

@fastfreddy I tried to modify the code.

<<sticky "">>

This is the sticky todo macro.

  • I was able to insert the macro only when I changed double-quotes to single quotes in the macro.
    But then the todo macro won’t work then. Is there any way solve this ?
  • Also can we use a widget other than tm-new-tiddler because everytime I tried to insert the macro from the context menu, a new tiddler gets created which is not needed.

This was the mesage I had typed yesterday night before I slept off (was really busy at work yesterday) …when I woke up today , I saw that you have updated it…will check and message today when time permits. Thank you for the fast update.

Yes, this is very true, and it gets even worse if the content are produced from transclusion or some computation like a the output of $list widgets, macros, etc…

I just wondering if dynanotate has the same issues (@Mark_S).

True, but if the selection is not found in the raw tiddler, this tells us it is sourced elsewhere.

  • We could indicate somehow the selection is not in the raw tiddler?, and allow some actions, only if it is found, otherwise open a title or search?
    • Perhaps also consider when found more than once in the raw tiddler or elsewhere.
  • In reality I think such a matter of fact, that what you select is not in the raw tiddler is something a regular user of tiddlywiki would be aware of. Often all they need to do is open a link and rename the tiddler to achieve the equivalent.
  • We could get tricky and wrap code in a var that tells the context menu the official source tiddler if available, thus enabling search and replace in additional cases other than just when in the underlying raw tiddler.
  • I have previously worked out a way to identify the “code tiddler” in which some wiki code exists, which may also help.
    • So a heading “that is in view”, that is in raw text of a transclude/code/macro we would use the code tiddler variable to find the source tiddler on which a search and replace can take place.
    • This would also be helpful to designers, highlight some text on screen and gain a link to the source tiddler.

Out of the box, dynannotate seems to be able to highlight (either through annotation or the search result feature) regardless of origin (raw Wikitext or fancy macro/transclusions). It seems to capture the rendered wikitext and adds position-based overlays to the DOM as follows:

It does not seem to make changes/replacements to the underlying tiddler, but instead creates CSS class-based overlays based on relative position within the DOM object !?!

Interesting…and surprisingly capable… Will continue to investigate, but it may not provide insights if one wishes to change the underlying tiddler with a search/replace functionality…

@fastfreddy have you seen this context menu using wiki text by @Yaisog

Can it be modified to work with macro made by JanJo.


<<ToDo "" "" "">>

Will it by possible to do all basic wiki text formatting using like bold, underline, headings, code blocks with this menu.

yes, all of this is possible and relatively trivial.

yes @arunnbabu81, all of this is relatively trivial, with the one limitation that it works well only when the underlying wikitext is simple text. It will not work when the rendered wikitext stems from macros, transclusions or raw text with line breaks. You should have the building blocks you need in my last json file (or this one with a minor improvements on the wikitext link syntax.)

plugin_fastfreddy_ContextMenuAddin_v0.0.2_20220922.json (9.7 KB)

proof of concept @arunnbabu81

Interestingly, I stumbled another shortcoming. If the word selected appears multiple times, the replacement may or may not occur on the instance you want.
The work around is to substitute all instances which is perhaps what you want, perhaps not.

This json provides will replace all instances of the selection text in the current tiddler for “bold”, “underline”, and “make new tiddler from selection”. For some reason, I don’t think it is appropriate for the sticky todo option, so if multiple matches do exist, only the first one will turn into a todo item.
plugin_fastfreddy_ContextMenuAddin_v0.0.3_20220923.json (11.2 KB)

@fastfreddy I tried to use the version 0.0.3, but seem to be the same as version 0.0.2.

ContextListener.js still has the todo option. Although the bold and underline comes in the context menu, I couldn’t find it in the ContextListener.js

How to modify the code to use this macro ?

please try this repackaged (and refresh, because of javascript). I am confident you can modify the $:/plugins/ahanniga/context-menu/ContextListener.js tiddler to suit your specific needs.

plugin_fastfreddy_ContextMenuAddin_v0.0.4_20220923.json (10.8 KB)

1 Like

This will happen only if the selection is a single word rite. For multi word selections, it will be fine

Tell me what you try that does not work and I will help you troubleshoot.
Look for this portion of $:/plugins/ahanniga/context-menu/ContextListener.js:

case "tm-todo":
            if (selection.trim().length > 0) {
                        $tw.wiki.setText(targ,"text",null,text.replace(selection,"<<sticky \""+selection+"\">>"));
            }
            break;

a few more styles added; I find it pretty handy actually…

image
plugin_fastfreddy_ContextMenuAddin_v0.0.5.json (13.2 KB)

1 Like

Will test it today itself once I am back on my desktop. Thank you

I would like to point out that if given a selection providing the following as information to the user and or context buttons is valuable in it’s own right, rather than simply a problem to solve;

  • Perhaps also the selection once available can be used in a sidebar tab or window, selectively opened, that presents the result of using the selection to make a variety of queries and or actions, that is the response to a selection need not only be in the context menu.

This can possibly be simplified further but is a semi-comprehensive list.

  • If the selection is not present in the tiddlers raw text
  • If the selection is present in the tiddlers raw text
    • Only one instance detected
    • Multiple instances detected
  • If the selection is present in the raw text when different conditions are applied
    • eg; all words are present if not in current literal string (this jumps new lines and other content but is not a replacable string.
  • If the selection is the same as an existing tiddler title, including missing
  • If the selection can be found as a result of a wikiwide search
    • Or other custom filters making use of the selection
      • eg is selection found in the keywords field throughout the wiki?

I tried to add those extra double quotes of the todo macro by adding few backslashes in between, but the way I was doing, it was not working. Since I have no knowledge in javascript, I dont know how to correct it.

The new styles are working. I will have to do more testing though.

How to create monoblocks ?

Also can there be an option to clear the wikitext formatting applied - I don’t think seperate undo command for each formatting is a practical - instead an option to undo anytype of formatting already applied to the selected text to return just plain text can be an option. Dont know whether it’s doable or not.

hi @arunnbabu81 if you paste your code here I can provide you feedback on what is missing. You may just need to escape the quote marks

monoblocks is doable. Simplest to insert a blank line before the wikitext markup and a blank line after the wikitext markup. A better version would see if there is already a blank line and not add another one.

clear formatting is a bit harder. A simple version would remove the markup characters just before and just after. It is normally two (e.g. ~~) but not always (`). It gets complicated if there are multiple formats applied (e.g. hello), and if the format is applied to a larger string and you wish to just remove the format from the selection (e.g.

This is a test of the emergency broadcast system
to become
This is a test of the emergency broadcast system )

I’d like to add a feature to hide the context menu options that rely on other plugins when those plugins are not installed; it’ll make the add-in more flexible for options not everyone may want/need. I’d also like to improve the search/replace regular expressions to avoid mangling strings within wikitext markup.

Two major improvements:

First, I overhauled the logic to reduce the need to change the javascript and increase flexibility. Markup menu items are basically handled by two logic blocks, one for line-based markups (e.g. bold, italics, etc…) and block-based markups (e.g. mono-block). If you want to add options, you only need to clone and adjust one of the existing context menu tiddlers, ensuring to specify (as fields) the markup-prefix and markup-suffix, and you are off to the races. This approach works for macros also, such as Stobot’s Sticky. This means you shouldn’t need to touch the $:/plugins/ahanniga/context-menu/ContextListener.js for anything to do with styling and simple macros…

Second, I included a conditional logic handler, which means I can package a bunch of context menu items that are relevant to me but not to others. If the dependent plugins are not loaded, the matching context menu items are going to be hidden. For example, I can now package a menu entry for Stobot’s Sticky as well as one from JanJo’s ToDo (by popular demand @arunnbabu81 !), realizing full well that most people won’t have both. The correct menu entry will be displayed. In my setup, I also have the Mark highlighter, which will show up if you also have it loaded.

with Sticky:

same plugin with ToDo instead, and Highlight:

plugin_fastfreddy_ContextMenuAddin_v0.0.6.json (18.1 KB)

4 Likes
case "tm-todos":
if (selection.trim().length > 0) {$tw.wiki.setText(targ,"text",null,text.replace(selection,"<<ToDo \""+selection+"\" "" ""\>>"));}

This was my code which was not working.

Edit: I will try out the new json you uploaded.
Sorry for the delay. I had to debug my wiki (Still not over). The context menu was not showing up in my main wiki due to some conflict with context menu plug in and some of my modifications or multicolumn plug in. Have to find out the cause. So all my context menu experiments were in a newly created wiki.