submit editor on cmd+enter

This commit is contained in:
Vyacheslav Matyukhin 2022-06-26 21:30:24 +03:00
parent 9208330038
commit 4fe30107a4
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
2 changed files with 21 additions and 4 deletions

View File

@ -1,5 +1,5 @@
import _ from "lodash"; import _ from "lodash";
import React, { FC, useMemo } from "react"; import React, { FC, useMemo, useRef } from "react";
import AceEditor from "react-ace"; import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-golang"; import "ace-builds/src-noconflict/mode-golang";
@ -8,6 +8,7 @@ import "ace-builds/src-noconflict/theme-github";
interface CodeEditorProps { interface CodeEditorProps {
value: string; value: string;
onChange: (value: string) => void; onChange: (value: string) => void;
onSubmit?: () => void;
oneLine?: boolean; oneLine?: boolean;
width?: number; width?: number;
height: number; height: number;
@ -17,6 +18,7 @@ interface CodeEditorProps {
export const CodeEditor: FC<CodeEditorProps> = ({ export const CodeEditor: FC<CodeEditorProps> = ({
value, value,
onChange, onChange,
onSubmit,
oneLine = false, oneLine = false,
showGutter = false, showGutter = false,
height, height,
@ -24,6 +26,10 @@ export const CodeEditor: FC<CodeEditorProps> = ({
const lineCount = value.split("\n").length; const lineCount = value.split("\n").length;
const id = useMemo(() => _.uniqueId(), []); 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<typeof onSubmit | null>(null);
onSubmitRef.current = onSubmit;
return ( return (
<AceEditor <AceEditor
value={value} value={value}
@ -46,6 +52,13 @@ export const CodeEditor: FC<CodeEditorProps> = ({
enableBasicAutocompletion: false, enableBasicAutocompletion: false,
enableLiveAutocompletion: false, enableLiveAutocompletion: false,
}} }}
commands={[
{
name: "submit",
bindKey: { mac: "Cmd-Enter", win: "Ctrl-Enter" },
exec: () => onSubmitRef.current?.(),
},
]}
/> />
); );
}; };

View File

@ -330,6 +330,11 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
} }
}; };
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 = ( const samplingSettings = (
<div className="space-y-6 p-3 max-w-xl"> <div className="space-y-6 p-3 max-w-xl">
<div> <div>
@ -503,6 +508,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
<CodeEditor <CodeEditor
value={code ?? ""} value={code ?? ""}
onChange={setCode} onChange={setCode}
onSubmit={manualPlay}
oneLine={false} oneLine={false}
showGutter={true} showGutter={true}
height={height - 1} height={height - 1}
@ -547,9 +553,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
<PlayControls <PlayControls
autoplay={vars.autoplay || false} autoplay={vars.autoplay || false}
isStale={!vars.autoplay && renderedCode !== code} isStale={!vars.autoplay && renderedCode !== code}
onPlay={() => { onPlay={manualPlay}
setRenderedCode(code);
}}
onAutoplayChange={(newValue) => { onAutoplayChange={(newValue) => {
if (!newValue) setRenderedCode(code); if (!newValue) setRenderedCode(code);
setFormValue("autoplay", newValue); setFormValue("autoplay", newValue);