lbry-desktop/ui/util/context-menu.js

149 lines
4 KiB
JavaScript
Raw Permalink Normal View History

import { clipboard, remote, shell } from 'electron';
import { convertToShareLink } from 'util/lbryURI';
2019-11-07 20:39:22 +01:00
const isDev = process.env.NODE_ENV !== 'production';
function injectDevelopmentTemplate(event, templates) {
if (!isDev) return templates;
const { screenX, screenY } = event;
const separator = { type: 'separator' };
const developmentTemplateAddition = [
{
label: 'Inspect element',
accelerator: 'CmdOrCtrl+Shift+I',
click: () => {
remote.getCurrentWindow().inspectElement(screenX, screenY);
},
},
];
if (templates.length > 0) {
templates.push(separator);
}
templates.push(...developmentTemplateAddition);
return templates;
}
2018-06-25 22:42:49 +02:00
export function openContextMenu(event, templates = [], canEdit = false, selection = '') {
const { type, value } = event.target;
2018-06-04 02:17:58 +02:00
const isInput = event.target.matches('input') && (type === 'text' || type === 'number');
const isTextField = canEdit || isInput || event.target.matches('textarea');
2018-07-29 01:48:54 +02:00
const isSomethingSelected = selection.length > 0 || window.getSelection().toString().length > 0;
2018-06-04 02:17:58 +02:00
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 && isTextField && selectionStart !== selectionEnd) {
2018-06-04 02:17:58 +02:00
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 && isTextField) {
2018-06-04 02:17:58 +02:00
templates.push({
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste',
});
}
// If context menu is opened on Input
if (isTextField && value) {
2018-06-04 02:17:58 +02:00
templates.push({
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
});
}
injectDevelopmentTemplate(event, templates);
remote.Menu.buildFromTemplate(templates).popup({});
}
2018-06-25 22:42:49 +02:00
2018-07-03 06:37:48 +02:00
// This function is used for the markdown description on the publish page
2018-07-29 01:48:54 +02:00
export function openEditorMenu(codeMirror, event) {
2018-06-25 22:42:49 +02:00
const value = codeMirror.doc.getValue();
const selection = codeMirror.doc.getSelection();
const templates = [
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
click: () => {
codeMirror.execCommand('selectAll');
},
enabled: value.length > 0,
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut',
enabled: selection.length > 0,
},
];
openContextMenu(event, templates, true, selection);
}
2018-07-29 01:48:54 +02:00
// This function is used for the CodeViewer component
export function openSnippetMenu(codeMirror, event) {
const value = codeMirror.doc.getValue();
const selection = codeMirror.doc.getSelection();
const templates = [
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall',
click: () => {
codeMirror.execCommand('selectAll');
},
// Enabled if there is text to select
enabled: value.length > 0,
},
];
openContextMenu(event, templates, false, selection);
}
export function openClaimPreviewMenu(claim, event) {
2021-02-17 08:28:58 +01:00
// @if TARGET='app'
let templates = [];
if (claim) {
const shareLink = convertToShareLink(claim.canonical_url || claim.permanent_url);
const claimId = claim.claim_id;
templates.push({
label: 'Copy link',
click: () => {
clipboard.writeText(shareLink);
},
});
templates.push({ type: 'separator' });
templates.push({
label: 'Report content',
click: () => {
shell.openExternal(`https://lbry.com/dmca/${claimId}`);
},
});
}
injectDevelopmentTemplate(event, templates);
if (templates.length !== 0) {
remote.Menu.buildFromTemplate(templates).popup({});
}
2021-02-17 08:28:58 +01:00
// @endif
}
2018-07-29 01:48:54 +02:00
// Block context menu
export function stopContextMenu(event) {
if (navigator.userAgent.toLowerCase().indexOf(' electron/') > -1) {
event.preventDefault();
event.stopPropagation();
}
2018-07-29 01:48:54 +02:00
}