electron-userland / electron-spellchecker

Implement spellchecking, correctly
MIT License
237 stars 83 forks source link

How to add "Paste as plaintext"? #125

Closed Nantris closed 5 years ago

Nantris commented 5 years ago

I might be overlooking something simple here, but I don't understand how we get the data to paste in a custom menu item added using the 4th argument of ContextMenuBuilder, a processMenu function.

How can we order the menu so that Paste as plaintext appears below Paste, instead of below the separator.

image

Nantris commented 5 years ago

This should work for most people

  const processMenu = (menu, menuInfo) => {
    const target = webContents.getFocusedWebContents();
    if (menuInfo.editFlags.canPaste && !menuInfo.linkText && !menuInfo.hasImageContents) {
      menu.insert(3, new MenuItem({
        label: 'Paste as plain text',
        accelerator: 'CommandOrControl+Shift+V',
        click: () => target.pasteAndMatchStyle()
      }));
    }
    return menu
  }

  const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true, processMenu);