refactor context-menu logic

- Add support for textarea
This commit is contained in:
btzr-io 2018-06-21 11:21:57 -06:00 committed by Sean Yesmunt
parent 9a8d48777b
commit 8debe7dcc9

View file

@ -21,11 +21,11 @@ function injectDevelopmentTemplate(event, templates) {
return templates; return templates;
} }
export function openContextMenu(event, templates = []) { export function openContextMenu(event, templates = [], canEdit = false) {
const { type, value } = event.target;
const isSomethingSelected = window.getSelection().toString().length > 0; const isSomethingSelected = window.getSelection().toString().length > 0;
const { type } = event.target;
const isInput = event.target.matches('input') && (type === 'text' || type === 'number'); const isInput = event.target.matches('input') && (type === 'text' || type === 'number');
const { value } = event.target; const isTextField = canEdit || isInput || event.target.matches('textarea');
templates.push({ templates.push({
label: 'Copy', label: 'Copy',
@ -36,12 +36,12 @@ export function openContextMenu(event, templates = []) {
// If context menu is opened on Input and there is text on the input and something is selected. // If context menu is opened on Input and there is text on the input and something is selected.
const { selectionStart, selectionEnd } = event.target; const { selectionStart, selectionEnd } = event.target;
if (!!value && isInput && selectionStart !== selectionEnd) { if (!!value && isTextField && selectionStart !== selectionEnd) {
templates.push({ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' }); templates.push({ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' });
} }
// If context menu is opened on Input and text is present on clipboard // If context menu is opened on Input and text is present on clipboard
if (clipboard.readText().length > 0 && isInput) { if (clipboard.readText().length > 0 && isTextField) {
templates.push({ templates.push({
label: 'Paste', label: 'Paste',
accelerator: 'CmdOrCtrl+V', accelerator: 'CmdOrCtrl+V',
@ -50,7 +50,7 @@ export function openContextMenu(event, templates = []) {
} }
// If context menu is opened on Input // If context menu is opened on Input
if (isInput && value) { if (isTextField && value) {
templates.push({ templates.push({
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
@ -70,5 +70,5 @@ export function openCopyLinkMenu(text, event) {
}, },
}, },
]; ];
openContextMenu(event, templates, false); openContextMenu(event, templates);
} }