Add copy text on contex menu.
This commit is contained in:
parent
0cd5ef9ab4
commit
5cbd366cf2
4 changed files with 40 additions and 34 deletions
|
@ -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 />
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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>,
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue