Skip to content

Commit

Permalink
feat(Context Menu): Add Lookup, Search Google for
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Dec 4, 2018
1 parent 18040d4 commit 5d5aa0c
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/webview/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { clipboard, remote, ipcRenderer, shell } from 'electron';

import { isDevMode } from '../environment';
import { isDevMode, isMac } from '../environment';

const debug = require('debug')('Franz:contextMenu');

Expand All @@ -23,19 +23,28 @@ function delUnusedElements(menuTpl) {

const buildMenuTpl = (props, suggestions) => {
const { editFlags } = props;
const hasText = props.selectionText.trim().length > 0;
const textSelection = props.selectionText.trim();
const hasText = textSelection.length > 0;
const can = type => editFlags[`can${type}`] && hasText;

console.log(props);

let menuTpl = [
{
type: 'separator',
}, {
id: 'lookup',
label: `Look Up "${textSelection.length > 15 ? `${textSelection.slice(0, 15)}...` : textSelection}"`,
visible: isMac && props.mediaType === 'none' && hasText,
click() {
debug('Show definition for selection', textSelection);
webContents.showDefinitionForSelection();
},
}, {
type: 'separator',
}, {
id: 'cut',
role: can('Cut') ? 'cut' : '',
enabled: can('Cut'),
visible: !!props.selectionText.trim() && props.isEditable,
visible: hasText && props.isEditable,
}, {
id: 'copy',
label: 'Copy',
Expand All @@ -48,6 +57,18 @@ const buildMenuTpl = (props, suggestions) => {
role: editFlags.canPaste ? 'paste' : '',
enabled: editFlags.canPaste,
visible: props.isEditable,
}, {
type: 'separator',
visible: props.isEditable && hasText,
}, {
id: 'searchTextSelection',
label: `Search Google for "${textSelection.length > 15 ? `${textSelection.slice(0, 15)}...` : textSelection}"`,
visible: hasText,
click() {
const url = `https://www.google.com/search?q=${textSelection}`;
debug('Search on Google', url);
shell.openExternal(url);
},
}, {
type: 'separator',
},
Expand All @@ -60,6 +81,7 @@ const buildMenuTpl = (props, suggestions) => {
id: 'openLink',
label: 'Open Link in Browser',
click() {
debug('Open link in Browser', props.linkURL);
shell.openExternal(props.linkURL);
},
}, {
Expand All @@ -83,6 +105,7 @@ const buildMenuTpl = (props, suggestions) => {
id: 'openImage',
label: 'Open Image in Browser',
click() {
debug('Open image in Browser', props.srcURL);
shell.openExternal(props.srcURL);
},
}, {
Expand Down Expand Up @@ -132,7 +155,6 @@ const buildMenuTpl = (props, suggestions) => {
});
}

console.log('suggestions', suggestions.length, suggestions);
if (suggestions.length > 0) {
suggestions.reverse().map(suggestion => menuTpl.unshift({
id: `suggestion-${suggestion}`,
Expand Down

0 comments on commit 5d5aa0c

Please sign in to comment.