2022-01-02 21:33:11 +01:00
|
|
|
import { clipboard, shell } from 'electron';
|
|
|
|
import * as remote from '@electron/remote';
|
2021-10-08 05:47:39 +02:00
|
|
|
import { convertToShareLink } from 'util/lbryURI';
|
2019-11-07 20:39:22 +01:00
|
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
2018-05-16 23:34:07 +02:00
|
|
|
|
2018-05-21 03:00:59 +02:00
|
|
|
function injectDevelopmentTemplate(event, templates) {
|
|
|
|
if (!isDev) return templates;
|
|
|
|
const { screenX, screenY } = event;
|
|
|
|
const separator = { type: 'separator' };
|
|
|
|
const developmentTemplateAddition = [
|
2018-05-16 23:34:07 +02:00
|
|
|
{
|
2018-05-21 03:00:59 +02:00
|
|
|
label: 'Inspect element',
|
|
|
|
accelerator: 'CmdOrCtrl+Shift+I',
|
|
|
|
click: () => {
|
|
|
|
remote.getCurrentWindow().inspectElement(screenX, screenY);
|
|
|
|
},
|
2018-05-16 23:34:07 +02:00
|
|
|
},
|
|
|
|
];
|
2018-05-21 03:00:59 +02:00
|
|
|
if (templates.length > 0) {
|
|
|
|
templates.push(separator);
|
2018-05-16 23:34:07 +02:00
|
|
|
}
|
2018-05-21 03:00:59 +02:00
|
|
|
templates.push(...developmentTemplateAddition);
|
|
|
|
return templates;
|
2018-05-16 23:34:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 22:42:49 +02:00
|
|
|
export function openContextMenu(event, templates = [], canEdit = false, selection = '') {
|
2018-06-21 19:21:57 +02:00
|
|
|
const { type, value } = event.target;
|
2018-06-04 02:17:58 +02:00
|
|
|
const isInput = event.target.matches('input') && (type === 'text' || type === 'number');
|
2018-06-21 19:21:57 +02:00
|
|
|
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;
|
2018-06-21 19:21:57 +02:00
|
|
|
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
|
2018-06-21 19:21:57 +02:00
|
|
|
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
|
2018-06-21 19:21:57 +02:00
|
|
|
if (isTextField && value) {
|
2018-06-04 02:17:58 +02:00
|
|
|
templates.push({
|
|
|
|
label: 'Select All',
|
|
|
|
accelerator: 'CmdOrCtrl+A',
|
|
|
|
role: 'selectall',
|
|
|
|
});
|
2018-05-16 23:34:07 +02:00
|
|
|
}
|
2018-05-21 03:00:59 +02:00
|
|
|
|
|
|
|
injectDevelopmentTemplate(event, templates);
|
2018-08-10 06:21:02 +02:00
|
|
|
remote.Menu.buildFromTemplate(templates).popup({});
|
2018-05-21 03:00:59 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:49:05 +01:00
|
|
|
export function openClaimPreviewMenu(claim, event) {
|
2021-02-17 08:28:58 +01:00
|
|
|
// @if TARGET='app'
|
2021-02-17 06:49:05 +01:00
|
|
|
let templates = [];
|
|
|
|
|
|
|
|
if (claim) {
|
|
|
|
const shareLink = convertToShareLink(claim.canonical_url || claim.permanent_url);
|
2021-02-17 07:50:43 +01:00
|
|
|
const claimId = claim.claim_id;
|
|
|
|
|
2021-02-17 06:49:05 +01:00
|
|
|
templates.push({
|
2018-05-21 03:00:59 +02:00
|
|
|
label: 'Copy link',
|
|
|
|
click: () => {
|
2021-02-17 06:49:05 +01:00
|
|
|
clipboard.writeText(shareLink);
|
2018-05-21 03:00:59 +02:00
|
|
|
},
|
2021-02-17 06:49:05 +01:00
|
|
|
});
|
2021-02-17 07:50:43 +01:00
|
|
|
|
|
|
|
templates.push({ type: 'separator' });
|
|
|
|
|
|
|
|
templates.push({
|
|
|
|
label: 'Report content',
|
|
|
|
click: () => {
|
|
|
|
shell.openExternal(`https://lbry.com/dmca/${claimId}`);
|
|
|
|
},
|
|
|
|
});
|
2021-02-17 06:49:05 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:55:38 +01:00
|
|
|
injectDevelopmentTemplate(event, templates);
|
|
|
|
|
|
|
|
if (templates.length !== 0) {
|
|
|
|
remote.Menu.buildFromTemplate(templates).popup({});
|
|
|
|
}
|
2021-02-17 08:28:58 +01:00
|
|
|
// @endif
|
2018-05-21 03:00:59 +02:00
|
|
|
}
|
2018-07-29 01:48:54 +02:00
|
|
|
|
|
|
|
// Block context menu
|
|
|
|
export function stopContextMenu(event) {
|
2020-02-02 00:36:47 +01:00
|
|
|
if (navigator.userAgent.toLowerCase().indexOf(' electron/') > -1) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2018-07-29 01:48:54 +02:00
|
|
|
}
|