aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/contextMenu.js
blob: eeb825ece5f84cb45c4940dfda9475317f24ea6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { remote } from 'electron';
import { ContextMenuBuilder, ContextMenuListener } from 'electron-spellchecker';

const webContents = remote.getCurrentWebContents();

export default async function setupContextMenu(handler) {
  const addCustomMenuItems = (menu, menuInfo) => {
    // Add "Paste as plain text" item when right-clicking editable content
    if (
      menuInfo.editFlags.canPaste
      && !menuInfo.linkText
      && !menuInfo.hasImageContents
    ) {
      menu.insert(
        3,
        new remote.MenuItem({
          label: 'Paste as plain text',
          accelerator: 'CommandOrControl+Shift+V',
          click: () => webContents.pasteAndMatchStyle(),
        }),
      );
    }

    // Add "Open Link in Ferdi" item for links
    if (menuInfo.linkURL) {
      menu.insert(
        2,
        new remote.MenuItem({
          label: 'Open Link in Ferdi',
          click: () => {
            window.location.href = menuInfo.linkURL;
          },
        }),
      );
    }

    return menu;
  };

  const contextMenuBuilder = new ContextMenuBuilder(
    handler,
    null,
    true,
    addCustomMenuItems,
  );
  // eslint-disable-next-line no-new
  new ContextMenuListener((info) => {
    contextMenuBuilder.showPopupMenu(info);
  });
}