feat: 🎸 evaluate expression on Ctrl + Enter press (#36138) (#36341)

* feat: 🎸 evaluate expression on Ctrl + Enter press

* feat: 🎸 add ctrl + enter shortcut to canvas keymap

* feat: 🎸 use react-shortcuts to run expressions

* feat: 🎸 run expression selection on Shift + Ctrl + Enter

* feat: 🎸 don't run exec shortcuts when full expression invalid

* feat: 🎸 don't trigger autocompletion on Ctrl + Enter

* feat: 🎸 exec expressions shortcuts only when input is focused

* fix: 🐛 remove Shift + ... shortcut
This commit is contained in:
Va Da 2019-05-09 14:01:40 +02:00 committed by GitHub
parent 8aa6fb52f8
commit 2e58cf4e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View file

@ -17,11 +17,29 @@ import {
EuiRange,
EuiToolTip,
} from '@elastic/eui';
import { Shortcuts } from 'react-shortcuts';
import { ExpressionInput } from '../expression_input';
const { useRef } = React;
const minFontSize = 12;
const maxFontSize = 32;
const shortcut = (ref, cmd, callback) => (
<Shortcuts
name="EXPRESSION"
handler={(command, event) => {
const isInputActive = ref.current && ref.current.ref === event.target;
if (isInputActive && command === cmd) {
callback();
}
}}
targetNodeSelector="body"
global
stopPropagation
/>
);
export const Expression = ({
functionDefinitions,
formState,
@ -36,11 +54,18 @@ export const Expression = ({
isCompact,
toggleCompactView,
}) => {
const refExpressionInput = useRef(null);
return (
<EuiPanel
className={`canvasTray__panel canvasExpression--${isCompact ? 'compactSize' : 'fullSize'}`}
>
{shortcut(refExpressionInput, 'RUN', () => {
if (!error) {
setExpression(formState.expression);
}
})}
<ExpressionInput
ref={refExpressionInput}
fontSize={fontSize}
isCompact={isCompact}
functionDefinitions={functionDefinitions}

View file

@ -59,6 +59,15 @@ export class ExpressionInput extends React.Component {
this.props.onChange(value);
}
getSelection() {
if (!this.ref) {
return null;
}
const start = this.ref.selectionStart;
const finish = this.ref.selectionEnd;
return this.ref.value.substring(start, finish);
}
stash = debounce(
value => {
this.undoHistory.push(value);

View file

@ -121,4 +121,8 @@ export const keymap = {
),
REFRESH: refreshShortcut,
},
EXPRESSION: {
displayName: 'Expression controls',
RUN: { ...getShortcuts('enter', ['ctrl']), help: 'Run whole expression' },
},
};