Add copy text on contex menu.

This commit is contained in:
Daniel Dominguez 2018-06-03 21:17:58 -03:00
parent 0cd5ef9ab4
commit 5cbd366cf2
4 changed files with 40 additions and 34 deletions

View file

@ -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<Props> {
render() {
return (
<div id="window">
<div id="window" onContextMenu={e => openContextMenu(e)}>
<Theme />
<main className="page">
<SideBar />

View file

@ -62,6 +62,8 @@ class FileCard extends React.PureComponent<Props> {
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);
};

View file

@ -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(
<Provider store={store}>
<div>
<App onContextMenu={e => openContextMenu(e)} />
<App />
<SnackBar />
</div>
</Provider>,

View file

@ -21,26 +21,41 @@ function injectDevelopmentTemplate(event, templates) {
return templates;
}
export function openContextMenu(event, templates = [], addDefaultTemplates = true) {
if (addDefaultTemplates) {
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;
const inputTemplates = [
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{
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',
enabled: clipboard.readText().length > 0,
},
{
});
}
// If context menu is opened on Input
if (isInput && value) {
templates.push({
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
enabled: !!value,
},
];
templates.push(...inputTemplates);
});
}
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();
}
}