Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get the input element when clicked? #102

Closed
amanharwara opened this issue Apr 28, 2020 · 8 comments
Closed

Is there a way to get the input element when clicked? #102

amanharwara opened this issue Apr 28, 2020 · 8 comments

Comments

@amanharwara
Copy link

I want to add certain text into a textbox when the user clicks an item in the context menu. For this, it will be necessary to get the textbox element itself when clicked. Is this possible somehow?

@sindresorhus
Copy link
Owner

The menu options (menu/append/prepend) include the params object, which contain x/y positions, which you could pass to document.elementFromPoint(x, y);, to get the element.

@amanharwara
Copy link
Author

Thanks, that works.

@amanharwara
Copy link
Author

Turns out it doesn't quite work as intended. The x and y co-ordinates seem to be where the user clicked the menu item, not the element.

@sindresorhus
Copy link
Owner

Then I don't know. You could try opening a feature request on https://github.com/electron/electron to include the DOM element in the params object.

@amanharwara
Copy link
Author

Alright, I'll do that. Thanks for replying. I'll close the issue.

@IProudNoob
Copy link

It's not work

@drschwabe
Copy link

drschwabe commented Nov 29, 2020

@amanharwara You can accomplish this by capturing the click in the render process and using ipcRenderer.send to send the context you need, which you can then use apart of your logic in electron-context-menu function.

For example, in your render process (browser context):

const ipcRenderer = window.require('electron').ipcRenderer

$(document).mousedown(event => {
  if( event.which == 3) {
   //this is a right click, so electron-context-menu will be appearing momentarily... 
   let textBoxClicked = $(event.target).closest('.MY-TEXTBOX') 
   if(textBoxClicked.length) ipcRenderer.send('right-click/' + $(textBoxClicked).attr('id') ) 
  }
})

then in your electron process:

let clickedElemId = null
//this will run before the electron-context-menu function: 
electron.ipcMain.on('right-click', (event, elemId) => {
  if(!elemId) {
    clickedElemId = null
    return
  } 
  clickedElemId = docId
})

require('electron-context-menu')({
    showSearchWithGoogle : false,
    append: (defaultActions, params, browserWindow) => [
    {
      label: 'Add Text to Box',
      // Now we can use this logic to show the menu item, or add context to it, etc: 
      visible: _.isString(clickedElemId), 
      click () {
        //and send a response back if u need: 
        mainWindow.webContents.send('add-text-to-box')
      }
    }]
  })
}

@erikjalevik
Copy link

If you're landing here because your custom context menus override electron-context-menu on Mac, but don't show on Windows, it might be as simple as adding an event.preventDefault() to your renderer-side oncontextmenu handlers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants