Skip to content

Commit

Permalink
feat: ๐ŸŽธ evaluate expression on Ctrl + Enter press (#36138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
streamich authored May 9, 2019
1 parent c6bfa02 commit 977f28f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions x-pack/plugins/canvas/public/components/expression/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/canvas/public/lib/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,8 @@ export const keymap = {
),
REFRESH: refreshShortcut,
},
EXPRESSION: {
displayName: 'Expression controls',
RUN: { ...getShortcuts('enter', ['ctrl']), help: 'Run whole expression' },
},
};

0 comments on commit 977f28f

Please sign in to comment.