I am trying to find a way to keep selected text highlighted when a user activates a context menu (right-click menu) using Sencha’s EXT GWT 2.x (though I’m fine with a 3.x solution if there is one).
The use case:
- User is viewing content on the screen.
- User selects some block of text (and phrase for example).
- User right-clicks to view the context menu so they may take action on the selected text and sees a context menu. The selected text remains selected when the menu appears.
In testing it appears that the GXT context menu automatically de-selects the text when the menu appears. Is there a way to prevent this and take action on the selected text?
So far I have tried:
a. Add a listener to the panel for the Context Menu event (Events.OnContextMenu) to see if there is a property that I can change (something like contextMenu.disableTextSelection(false) even though it was already set on construction of the view).
b. Overriding the de-select effect created by the appearance of the context menu by adding a native method to the same listener (Events.OnContextMenu) which then uses JS to try and grab the currently selected text, copy it to a temp variable, and then immediately add it back to the range on the page (effectively re-selecting the already selected text), but this didn’t work either. I was able to confirm that the native method fired, detected the text and appeared to drop it back into the range, but it seems that perhaps another event fires or some other action occurs which still clears the selection out before the menu appears.
Any ideas would be greatly appreciated.
I’m still not aware of an official solution and I am starting to think this is a bug in GXT 2.x but I was able to come up with a workaround and am posting it here in case someone else should run into a similar problem.
For starters, the issue appears to occur in Firefox and not in IE. For Firefox, the following worked (example code snippets after the summary):
Events.OnContextMenuevent on your component/container/panel/etc.handleEventmethod and add a native method to use JS to detect if the user has selected text on the screen. If text is selected, save it off to a JavaScriptObject (opaque wrapper of JS objects provided by GWT, see GWT documentation for details).Events.Showevent on the context menu object.handleEventmethod and add a native method to use JS to re-select the previous selection.Events.Hideevent on the context menu object.Using these steps I was able to keep the text selected when making a context menu appear in Firefox.
Example Code
(these are just sample code blocks to help better illustrate the steps – a copy/paste of this code won’t give you everything you need)