2019-02-22 06:01:59 +01:00
|
|
|
// @if TARGET='app'
|
2018-05-16 23:34:07 +02:00
|
|
|
import { clipboard, remote } from 'electron';
|
|
|
|
import isDev from 'electron-is-dev';
|
2019-02-22 06:01:59 +01:00
|
|
|
// @endif
|
|
|
|
// @if TARGET='web'
|
|
|
|
import { remote, isDev } from 'web/stubs';
|
|
|
|
// @endif
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-21 03:00:59 +02:00
|
|
|
export function openCopyLinkMenu(text, event) {
|
|
|
|
const templates = [
|
|
|
|
{
|
|
|
|
label: 'Copy link',
|
|
|
|
click: () => {
|
|
|
|
clipboard.writeText(text);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
2018-06-21 19:21:57 +02:00
|
|
|
openContextMenu(event, templates);
|
2018-05-21 03:00:59 +02:00
|
|
|
}
|
2018-07-29 01:48:54 +02:00
|
|
|
|
|
|
|
// Block context menu
|
|
|
|
export function stopContextMenu(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|