npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anujdatar/electron-context-menu

v1.1.1

Published

Building simple context menus for Electron

Downloads

24

Readme

electron-context-menu

npm CodeFactor Issues GitHub

Right-click context menus for Electron. Detects context if click area is editable or not.

Basic menu for uneditable areas:

copy_menu

More complex menu for editable areas with spellcheck options:

spellcheck_menu

Module exports a function to build electron context menus and a set of templates and classes to help build/customize said menus. Module exports:

Menu templates:

  • copy: just "copy" option in menu
  • paste: "paste" and "selectAll"
  • reload: just "page reload"
  • editor: "cut/copy/paste" and "selectAll"
  • noSuggest: disabled option for when spell checker cannot find any suggested corrections

Classes:

  • SuggestionMenuItem: constructor for single menu item for one spelling correction at a time, accepts string
  • MenuTemplate: constructor for menu prefix or suffix object

Function:

  • BuildContextMenu(): builds context menus based on arguments
BuildContextMenu(menuTemplate, prefix, suffix)
  /*
    function generates context menu inside a browserwindow of an electron app

    Args:
      menuTemplate (optional): Array - contains a list of menu objects. defaults to editor menu template if no argument is passed
      prefix (optional): Object - contains any prefix options required in the ctx menu
      suffix (optional): Object - contains and suffix options required in the ctx menu
    Returns:
      Menu: Electron Menu
  */

// Usage

buildContextMenu() or buildContextMenu(menuTemplates.editor) // editor_menu for editable textareas
buildContextMenu(menuTemplates.paste) // paste_menu
// for menus with prefixs and or suffixes
buildContextMenu(menuTemplates.editor, prefix, suffix) // editor_menu - prefix and suffix
buildContextMenu(menuTemplates.copy, prefix) // copy_menu and prefix only
buildContextMenu(menuTemplates.editor, {}, suffix) // editor_menu w/ only suffix, no prefix

Installation and testing

Installing and using package with your project

npm install @anujdatar/electron-context-menu

testing the included example electron app

git clone https://github.com/anujdatar/electron-context-menu-example.git
cd electron-context-menu-example
npm install

npm start

installing node modules for example also rebuilds node-spellchecker using electron-rebuild.

Usage example

See example-repository for more details

// in renderer or preload script
const electron = require('electron')
const remote = electron.remote
const { BuildContextMenu, menuTemplates } = require('@anujdatar/electron-context-menu')

window.addEventListener('contextmenu', (e) => {
    e.preventDefault()

    let ctxMenu
    if (!e.target.closest('textarea, input, [contenteditable="true"]')) {
      // if click in uneditable area
      if (window.getSelection().toString() === '') {
        // if no text selected
        ctxMenu = new BuildContextMenu(menuTemplates.reload)
      } else {
        // if text is selected
        ctxMenu = new BuildContextMenu(menuTemplates.copy)
      }
    } else {
      // if click in editable text area
      if (window.getSelection().toString() === '') {
        // if no text is selected
        ctxMenu = new BuildContextMenu(menuTemplates.paste)
      } else {
        ctxMenu = new BuildContextMenu()
      }
    }
    // popup menu
    ctxMenu.popup(remote.getCurrentWindow())
  })

Menu Customization

passing custom template menuTemplate: Array containing Electron.MenuItem(s)

adding prefix or suffix menu items to the context menu prefix and suffix: objects @param: exists - boolean - true when you need to add menu prefix @param menuItems - array - containing objects menuItem = { label - string - displayed in the menu, click -function - activated when label clicked in ctxMenu }

// examples of function params for customization
menuTemplate = [
  {
    label: 'Copy',
    role: 'copy'
  },
  {
    role: 'paste'
  },
  {
    label: 'Some function',
    click: function () {
      //do something
      console.log('Did something')
    }
  }
]

prefix = {
  exists: true,
  menuItems: []
}

Thanks

Jeff Wear and MixMax's electron-editor-context-menu

Copyright and License

Copyright 2019 Anuj Datar, licensed under MIT License

TODO

  1. Add contexts for other type of elements, eg. images, etc.
  2. Any ideas?