Operating on selection via context-menu?


#1

i’m trying to do something seemingly pretty basic:

add a ‘cut’ operation to the context-menu for an editor

so i went thru docs, tutorial and examples of creating packages.

seems pretty straight forward, but…

it seems like popping the context menu nukes the current selection?

do any package authors have any suggestions to make this work?

regards,
tony.


#2

Can you share part of your code? Or is there a repor where we can see the code?


#3

sure, (using 0.92.0 btw), it’s not very involved. this is in a cson file under the menus folder:

'context-menu':
  '.editor':
    'cut': 'context-ccp:cut'
    'core-cut': 'core:cut'
    'core-copy': 'core:copy'
    'core-paste': 'core:paste'

and this is in the ‘main’ package coffee file under lib:

module.exports =
  activate: (state) ->
    atom.workspaceView.command 'context-ccp:cut', '.editor', ->
      editor = atom.workspace.getActiveEditor()
      console.log 'ccp.cut: text=%s', editor.getSelection().getText()
      #editor.cutSelectedText()

log msg shows getText() returning an empty string…

also, experimenting with the call to core:cut seems to show that the current selection turns into a single spot where the right-click occurred to pop the context menu (it cuts a single character at that spot).

i was trying to figure out a way to get a callback before the context menu pops so i could stash away the current selection somewhere, but i couldn’t figure out how (perhaps some ‘on’ call?). ideally tho, perhaps the core code could preserve it…?

regards,
tony


#4

so, i tried intercepting the mouse-down event that triggers the context menu to pop, but it seems like the selection is already gone by the time the mouse-down event callback fires. here is the current contents of my lib/<package-name>/coffee file:

{$} = require 'atom'

module.exports =
  activate: (state) ->
    $('.editor:not(.mini)').on 'mousedown', (event) ->
      if event.which is 3
        editor = atom.workspace.getActiveEditor()
        console.log 'ccp.cut.mousedown: text=%s', editor.getSelection().getText()

and editor.getSelection().getText() is returning nothing.

any suggestions as to how to approach this?

regards,
tony.


#5

this seems like a problem needing a solution by now. …what im leaning towards now is just maintaining a var that always has the last selection that wasn’t empty.


#6

here’s the crumby way im doing it now:

setInterval(function() {
		var selection = atom.workspace.getActiveEditor().getSelection().getText();
		if(selection) window.lastSelection = selection;
}, 1000);

Then obviously you can use lastSelection any where you want.


#7

I am still having this issue. Has anyone else had a better solution than @celebvidy? Hope the Atom community came figure something out.

Update: There is an Atom issue here: https://github.com/atom/atom/issues/2350