Hi Mat,

It helps that I made a PR for snowgoon's plugin a few weeks ago to make a 
few improvements and optimizations, so I understand both his code and some 
of the challenges involved with popups in textareas. I didn't have more 
time or I would have cleaned it up before sharing.

If you are interested in *pursuing my approach further with wikitext, I 
think that is possible* in the sense that the issues you mentioned in your 
original post should be resolved, though I can't say what else may come up 
later down the road. Only one way to find out though!

I will explain to you how my demo works, and if after reading you want to 
work with it further (with wikitext), I'll clean it up and provide you with 
a stripped down bare minimum setup to work with.

Firstly, I would strongly advise you not to worry about this bit for now: 
"I'm hoping EditorMagic can eventually become a plugin that people can rely 
on and not cause problem in core updates."

Get the functionality you want working for now, even if it means 
overwriting a shadow tiddler or two. Once everything is in place, it should 
be possible to find a way to refactor the code to avoid these issues. 
Either by finding a different way to insert the extra bits we need into 
templates, or a pull request for a core change. But if you get hung up on 
this part now, you don't actually have anything to show to justify any 
changes.... hello vicious cycle. I already have ideas for how the shadow 
tiddler editing problems could be worked around later once the rest of the 
puzzle is in place.

OK, about my demo. I favour a more TiddlyWiki approach, which to me means 
using as much of core features as possible and introducing a new custom 
widget or two to get any missing functionality that we need. 

*Requirements:*

   - the ability for *edit-text widget* to fire actions on input, taken 
   from this *PR *(https://github.com/Jermolene/TiddlyWiki5/pull/4725) No 
   reason to hesitate to use this. The actions part of it can even be made a 
   separate PR to get it merged sooner, or be packaged as a tiny plugin until 
   it is merged.
   
   - *$:/plugins/snowgoon88/edit-comptext/cursor-position.js* from 
   snowgoon, just this tiddler not the entire plugin.
   
   - the latest *pre-release*. It contains an already merged patch for the 
   edit widget that is needed.
   
   - *action-popupcaret* widget - introduced by me. When fired it triggers 
   a popup at the caret position. It needs to be fired by the edit widget, so 
   that it knows which textarea to work with, and so that it inherits 
   variables like currentTiddler. Note this refers to a standard core popup, 
   i.e. a Reveal Widget.
   
   This action widget also sets a field in the state tiddler for the popup, 
   telling us the position of the caret. This is the position in characters 
   from the start of the text. We can access this in the popup.
   
   - The *default text-editor has not been changed*. (CodeMirror is 
   installed just for convenience during development, it is not a 
   requirement). The shadow tiddler *$:/core/ui/EditTemplate/body/editor* has 
   been changed to *add actions to the edit widget*, and *add the reveal 
   widget for the popup* we want to show. This reveal widget contains a few 
   lines of wikitext which *transclude your EditorMagic popup* tiddler. 
   
   - The rest of the logic for what to show in the popup is still all wiki 
   text based and mostly your original wikitext.

*Flow*

   - User typing in textarea triggers action-popupcaret widget
   - Popup is created at caret position
   - What happens inside the popup is up to you and based entirely on 
   wikitext.
   You do have access to the caret position, this is the position in 
   characters from the start of the text.

*Changes to the wikitext inside the popup*

   - Since we have access to the caret position, I split the tiddler text 
   at the caret position, discarding everything after the caret. This makes it 
   very easy to know where our string ends.
   - I changed the logic a bit so that in case there are multiple unclosed 
   [[ brackets, we only consider the last one before the caret.
   - There was a bug in your code where autocompleting [[tid to [[tidone]] 
   would also mangle already existing links that matched [[tid. I changed the 
   logic for the text replacement so we take everything before the trigger, 
   plus the link + everything after the caret and join them together. All of 
   this is easy since we have the caret position.
   - Your code also wasn't matching if the typed part of the tiddler title 
   contained spaces, that should be fixed too.

If you want to work with this, I can clean it up. Then you can go crazy 
with wikitext in *EditorMagic/_Popup*

 My advice would be to try and get it to a working title autocomplete 
solution. We should be very close already. That will help iron out any 
kinks in the popup display logic etc. Then once that is working well, you 
can use that as a foundation to try and implement other EditorMagic 
functionality.

Still lots more room for improvement, like only showing a popup if the 
> triggers are found, rather than showing an empty popup (not visible) if 
> there are no trigger matches.


This can be done via wikitext as well by just moving around some of your 
wikitext from the popup tiddler. But its an incremental improvement that 
can be done later, and no need to get into it now.

*In conclusion:*
While there may be some rough edges, I think this provides the basis for 
reliably triggering a popup (reveal widget) at the caret position in both 
core editors, the simple one and the framed one (with toolbar), in response 
to a user typing.

Cheers,
Saq

On Wednesday, July 8, 2020 at 9:11:52 PM UTC+2, Mat wrote:
>
> @Saq - as noted previously; thank you for your support!
>
> https://saqimtiaz.github.io/sq-tw/editormagic-poc.html 
>> <https://www.google.com/url?q=https%3A%2F%2Fsaqimtiaz.github.io%2Fsq-tw%2Feditormagic-poc.html&sa=D&sntz=1&usg=AFQjCNGTm_PVMDJvlE4WAKZFm7ImI7yvEg>
>>  
>
> You get full access to variables like currentTiddler; there are 
>> improvements that let you know exactly where the text caret is, so you can 
>> ignore text before or after, and I tweaked your popup code so that it 
>> doesn't give multiple results if there are multiple unclosed link brackets 
>> .... amongst other things. 
>>
> How anyone can throw together that in 30 minutes is astonishing, to say 
> the least. And you've immediately grasped some of the big struggles I've 
> had/have. 
>
>
> [...] if you have unfinished links that can cause some issues, and it will 
>> only in textareas/inputs where it has been setup to work.
>
>
> Do you refer to what I see in you 
> $:/core/modules/widgets/action-popupcaret - i.e the reference to Snowgoons 
> cursor-position.js ? If yes, I'm assuming this is why the default text 
> editor is changed. Does this mean the solution cannot work with the regular 
> text editor? I'm hoping EditorMagic can eventually become a plugin that 
> people can rely on and not cause problem in core updates.
>
>
> Still lots more room for improvement, like only showing a popup if the 
>> triggers are found, rather than showing an empty popup (not visible) if 
>> there are no trigger matches.
>>
>
> You're absolutely right but if I have any forte it would be css - or, 
> rather, *patience *with css - so that's the only way I figured out how to 
> control show or not without js. If you have better solutions, I'd be more 
> than happy to see them ;-)
>
> Actually -  I have no idea what to do next in this, mostly because of 
> limits in what wikitext can do since I don't know JS. A long time ago I 
> posted gh request for popups via custom strings 
> <https://github.com/Jermolene/TiddlyWiki5/issues/3784> exactly because it 
> is beyond wikitext, and because I think such a mechanism would enable a lot 
> of things for wikitext coders i.e to focus on the popup *content.* From 
> your demo, you seem to have created a solution for this so do you think you 
> could generalize it into a plugin? For one thing, I note that you announced 
> a floating editor toolbar 
> <https://groups.google.com/forum/#!topic/tiddlywiki/1oFNPGtQj3c> - well 
> done!!! - which seems to also use a popup mechanism so it is clear that 
> there are use cases.
>
> Sorry if I'm a bit incoherent. EditorMagic, or something like it, still 
> doesn't exist but I have no idea what I can do as a next step.
>
> <:-)
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywikidev/d095b8e9-8dd5-4d6a-bad4-feef806d8f11o%40googlegroups.com.

Reply via email to