From 4fe30107a4f953f3be4b8d8f8f266af8b3996365 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 26 Jun 2022 21:30:24 +0300 Subject: [PATCH] submit editor on cmd+enter --- packages/components/src/components/CodeEditor.tsx | 15 ++++++++++++++- .../src/components/SquigglePlayground.tsx | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/components/src/components/CodeEditor.tsx b/packages/components/src/components/CodeEditor.tsx index c869b2ea..15802131 100644 --- a/packages/components/src/components/CodeEditor.tsx +++ b/packages/components/src/components/CodeEditor.tsx @@ -1,5 +1,5 @@ import _ from "lodash"; -import React, { FC, useMemo } from "react"; +import React, { FC, useMemo, useRef } from "react"; import AceEditor from "react-ace"; import "ace-builds/src-noconflict/mode-golang"; @@ -8,6 +8,7 @@ import "ace-builds/src-noconflict/theme-github"; interface CodeEditorProps { value: string; onChange: (value: string) => void; + onSubmit?: () => void; oneLine?: boolean; width?: number; height: number; @@ -17,6 +18,7 @@ interface CodeEditorProps { export const CodeEditor: FC = ({ value, onChange, + onSubmit, oneLine = false, showGutter = false, height, @@ -24,6 +26,10 @@ export const CodeEditor: FC = ({ const lineCount = value.split("\n").length; const id = useMemo(() => _.uniqueId(), []); + // this is necessary because AceEditor binds commands on mount, see https://github.com/securingsincity/react-ace/issues/684 + const onSubmitRef = useRef(null); + onSubmitRef.current = onSubmit; + return ( = ({ enableBasicAutocompletion: false, enableLiveAutocompletion: false, }} + commands={[ + { + name: "submit", + bindKey: { mac: "Cmd-Enter", win: "Ctrl-Enter" }, + exec: () => onSubmitRef.current?.(), + }, + ]} /> ); }; diff --git a/packages/components/src/components/SquigglePlayground.tsx b/packages/components/src/components/SquigglePlayground.tsx index 74f48455..06b3fd3d 100644 --- a/packages/components/src/components/SquigglePlayground.tsx +++ b/packages/components/src/components/SquigglePlayground.tsx @@ -330,6 +330,11 @@ export const SquigglePlayground: FC = ({ } }; + const manualPlay = () => { + if (vars.autoplay) return; // should we allow reruns even in autoplay mode? + setRenderedCode(code); // TODO - force play even if code hasn't changed + }; + const samplingSettings = (
@@ -503,6 +508,7 @@ export const SquigglePlayground: FC = ({ = ({ { - setRenderedCode(code); - }} + onPlay={manualPlay} onAutoplayChange={(newValue) => { if (!newValue) setRenderedCode(code); setFormValue("autoplay", newValue);