diff --git a/src/renderer/component/app/view.jsx b/src/renderer/component/app/view.jsx index 8f3472213..a6a225df0 100644 --- a/src/renderer/component/app/view.jsx +++ b/src/renderer/component/app/view.jsx @@ -7,6 +7,7 @@ import ReactModal from 'react-modal'; import throttle from 'util/throttle'; import SideBar from 'component/sideBar'; import Header from 'component/header'; +import { openContextMenu } from '../../util/contextMenu'; type Props = { alertError: (string | {}) => void, @@ -79,7 +80,7 @@ class App extends React.PureComponent { render() { return ( -
+
openContextMenu(e)}>
diff --git a/src/renderer/component/fileCard/view.jsx b/src/renderer/component/fileCard/view.jsx index 3655c7c61..6189ac7bd 100644 --- a/src/renderer/component/fileCard/view.jsx +++ b/src/renderer/component/fileCard/view.jsx @@ -62,6 +62,8 @@ class FileCard extends React.PureComponent { const shouldObscureNsfw = obscureNsfw && metadata && metadata.nsfw; const isRewardContent = claim && rewardedContentClaimIds.includes(claim.claim_id); const handleContextMenu = event => { + event.preventDefault(); + event.stopPropagation(); openCopyLinkMenu(convertToShareLink(uri), event); }; diff --git a/src/renderer/index.js b/src/renderer/index.js index 811a48b47..974df0384 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -17,7 +17,6 @@ import store from 'store'; import app from './app'; import analytics from './analytics'; import doLogWarningConsoleMessage from './logWarningConsoleMessage'; -import { initContextMenu } from './util/contextMenu'; const { autoUpdater } = remote.require('electron-updater'); const APPPAGEURL = 'lbry://?'; @@ -113,8 +112,6 @@ document.addEventListener('click', event => { } }); -document.addEventListener('contextmenu', initContextMenu); - const init = () => { autoUpdater.on('update-downloaded', () => { app.store.dispatch(doAutoUpdate()); @@ -149,7 +146,7 @@ const init = () => { ReactDOM.render(
- openContextMenu(e)} /> +
, diff --git a/src/renderer/util/contextMenu.js b/src/renderer/util/contextMenu.js index 79f47da5b..900ebf54a 100644 --- a/src/renderer/util/contextMenu.js +++ b/src/renderer/util/contextMenu.js @@ -21,26 +21,41 @@ function injectDevelopmentTemplate(event, templates) { return templates; } -export function openContextMenu(event, templates = [], addDefaultTemplates = true) { - if (addDefaultTemplates) { - const { value } = event.target; - const inputTemplates = [ - { label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' }, - { label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' }, - { - label: 'Paste', - accelerator: 'CmdOrCtrl+V', - role: 'paste', - enabled: clipboard.readText().length > 0, - }, - { - label: 'Select All', - accelerator: 'CmdOrCtrl+A', - role: 'selectall', - enabled: !!value, - }, - ]; - templates.push(...inputTemplates); +export function openContextMenu(event, templates = []) { + const isSomethingSelected = window.getSelection().toString().length > 0; + const { type } = event.target; + const isInput = event.target.matches('input') && (type === 'text' || type === 'number'); + const { value } = event.target; + + templates.push({ + label: 'Copy', + accelerator: 'CmdOrCtrl+C', + role: 'copy', + enabled: isSomethingSelected, + }); + + // If context menu is opened on Input and there is text on the input and something is selected. + const { selectionStart, selectionEnd } = event.target; + if (!!value && isInput && selectionStart !== selectionEnd) { + templates.push({ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' }); + } + + // If context menu is opened on Input and text is present on clipboard + if (clipboard.readText().length > 0 && isInput) { + templates.push({ + label: 'Paste', + accelerator: 'CmdOrCtrl+V', + role: 'paste', + }); + } + + // If context menu is opened on Input + if (isInput && value) { + templates.push({ + label: 'Select All', + accelerator: 'CmdOrCtrl+A', + role: 'selectall', + }); } injectDevelopmentTemplate(event, templates); @@ -57,12 +72,3 @@ export function openCopyLinkMenu(text, event) { ]; openContextMenu(event, templates, false); } - -export function initContextMenu(event) { - const { type } = event.target; - if (event.target.matches('input') && (type === 'text' || type === 'number')) { - openContextMenu(event); - } else { - event.preventDefault(); - } -}