Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix focus; Add shortcut

Tomas Valenta7391ec1715d0
diff --git a/components/Editor/BlockEditor.tsx b/components/Editor/BlockEditor.tsx
index 8d9cca67c..637ddadde 100644
--- a/components/Editor/BlockEditor.tsx
+++ b/components/Editor/BlockEditor.tsx
@@ -11,6 +11,9 @@ export interface Props {
onDelete: () => void
block: Block
isLast?: boolean
+ isFirst?: boolean
+ isFocused?: boolean
+ onFocus?: () => void
}
export default function BlockEditor({
@@ -18,6 +21,9 @@ export default function BlockEditor({
onDelete,
block,
isLast,
+ isFirst,
+ isFocused,
+ onFocus,
}: Props) {
const [confirmDelete, setConfirmDelete] = useState(false)
const ref = useRef<HTMLTextAreaElement>(null)
@@ -34,11 +40,11 @@ export default function BlockEditor({
[confirmDelete],
)
- // useEffect(function autofocus() {
- // if (isLast) {
- // ref.current?.focus()
- // }
- // }, [isLast])
+ useEffect(function autofocus() {
+ if (isFocused) {
+ ref.current?.focus()
+ }
+ }, [isFocused])
function handleDelete(
e: MouseEvent<HTMLButtonElement, globalThis.MouseEvent>,
@@ -106,6 +112,7 @@ export default function BlockEditor({
placeholder="Prompt"
value={block.prompt}
onChange={e => onChange({ ...block, prompt: e.target.value })}
+ onFocus={onFocus}
ref={ref}
className={clsx(
'w-full',
@@ -115,7 +122,6 @@ export default function BlockEditor({
'leading-6',
'tracking-wide',
'font-sans',
- 'min-h-[42px]',
'text-slate-500',
'focus:text-slate-600',
'no-scroller',
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index a5b2db72d..4bf69d770 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -1,6 +1,8 @@
import { shallow } from 'zustand/shallow'
import useSWRMutation from 'swr/mutation'
-import { Fragment } from 'react'
+import { Fragment, useState } from 'react'
+import { toast } from 'sonner'
+import Hotkeys from 'react-hot-keys'
import { State, Block, methods, Method } from 'state/store'
import { getStoreContext } from 'state/StoreProvider'
@@ -11,7 +13,7 @@ import BlockEditor from './BlockEditor'
import Button from '../Button'
import ConnectionLine from './ConnectionLine'
import AddBlockButton from './AddBlockButton'
-import { toast } from 'sonner'
+import { nanoid } from 'nanoid'
const selector = (state: State) => ({
blocks: state.blocks,
@@ -56,6 +58,8 @@ export default function Editor({ }: Props) {
changeMethod,
} = useStore(selector, shallow)
+ const [focusedBlock, setFocusedBlock] = useState(0)
+
const { trigger: generate } = useSWRMutation('/api/generate', handlePostGenerate)
async function deploy() {
@@ -68,7 +72,26 @@ export default function Editor({ }: Props) {
}
return (
- <div className="
+ <Hotkeys
+ allowRepeat
+ keyName="command+enter,control+enter,shift+command+enter,shift+control+enter"
+ onKeyDown={(s) => {
+ if (s === 'command+enter' || s === 'control+enter') {
+ setFocusedBlock(b => {
+ if (blocks.length === 0 || b === blocks.length - 1) {
+ addBlock({ prompt: '', id: nanoid() })
+ }
+ return b + 1
+ })
+ } else if (s === 'shift+command+enter' || s === 'shift+control+enter') {
+ setFocusedBlock(b => b > 0 ? b - 1 : b)
+ }
+ }}
+ filter={() => {
+ return true;
+ }}
+ >
+ <div className="
flex
flex-1
p-8
@@ -78,51 +101,55 @@ export default function Editor({ }: Props) {
scroller
relative
">
- <div className="flex items-center space-x-2">
- <Text
- text="Incoming"
- className='font-bold'
- />
- <Select
- direction="left"
- selectedValue={{ key: method, title: method }}
- values={methods.map(m => ({ key: m, title: m }))}
- onChange={m => changeMethod(m.title as Method)}
- />
- <Text
- text="Request"
- className='font-bold'
- />
- </div>
- <div className="
+ <div className="flex items-center space-x-2">
+ <Text
+ text="Incoming"
+ className='font-bold'
+ />
+ <Select
+ direction="left"
+ selectedValue={{ key: method, title: method }}
+ values={methods.map(m => ({ key: m, title: m }))}
+ onChange={m => changeMethod(m.title as Method)}
+ />
+ <Text
+ text="Request"
+ className='font-bold'
+ />
+ </div>
+ <div className="
flex
flex-col
items-center
transition-all
">
- {blocks.map((b, i, a) =>
- <Fragment
- key={b.id}
- >
- <ConnectionLine className='h-4' />
- <BlockEditor
- block={b}
- onDelete={() => removeBlock(i)}
- onChange={(b) => changeBlock(i, b)}
- isLast={i === a.length - 1}
- />
- </Fragment>
- )}
- </div>
- <ConnectionLine className='h-4' />
- <AddBlockButton addBlock={addBlock} />
- <div className="absolute right-4 top-4">
- <Button
- text="Deploy"
- onClick={deploy}
- variant={Button.variant.Full}
- />
+ {blocks.map((b, i, a) =>
+ <Fragment
+ key={b.id}
+ >
+ <ConnectionLine className='h-4' />
+ <BlockEditor
+ block={b}
+ onDelete={() => removeBlock(i)}
+ onChange={(b) => changeBlock(i, b)}
+ isLast={i === a.length - 1}
+ isFirst={i === 0}
+ isFocused={i === focusedBlock}
+ onFocus={() => setFocusedBlock(i)}
+ />
+ </Fragment>
+ )}
+ </div>
+ <ConnectionLine className='h-4' />
+ <AddBlockButton addBlock={addBlock} />
+ <div className="absolute right-4 top-4">
+ <Button
+ text="Deploy"
+ onClick={deploy}
+ variant={Button.variant.Full}
+ />
+ </div>
</div>
- </div>
+ </Hotkeys>
)
}
diff --git a/package.json b/package.json
index 24f3ef986..2b0f41680 100644
--- a/package.json
+++ b/package.json
@@ -30,6 +30,7 @@
"randomcolor": "^0.6.2",
"react": "18.2.0",
"react-dom": "18.2.0",
+ "react-hot-keys": "^2.7.2",
"react-textarea-autosize": "^8.4.0",
"sonner": "^0.1.6",
"superjson": "^1.12.2",
diff --git a/yarn.lock b/yarn.lock
index d3fb6c810..8984f8f5a 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4534,6 +4534,11 @@ hoist-non-react-statics@^3.3.2:
dependencies:
react-is "^16.7.0"
+hotkeys-js@^3.8.1:
+ version "3.10.1"
+ resolved "https://registry.yarnpkg.com/hotkeys-js/-/hotkeys-js-3.10.1.tgz#0c67e72298f235c9200e421ab112d156dc81356a"
+ integrity sha512-mshqjgTqx8ee0qryHvRgZaZDxTwxam/2yTQmQlqAWS3+twnq1jsY9Yng9zB7lWq6WRrjTbTOc7knNwccXQiAjQ==
+
http-cache-semantics@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a"
@@ -6710,7 +6715,7 @@ promise-inflight@^1.0.1:
resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3"
integrity sha512-6zWPyEOFaQBJYcGMHBKTKJ3u6TBsnMFOIZSa6ce1e/ZrrsOlnHRHbabMjLiBYKp+n44X9eUI6VUPaukCXHuG4g==
-prop-types@^15.8.1:
+prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
@@ -6836,6 +6841,14 @@ [email protected]:
loose-envify "^1.1.0"
scheduler "^0.23.0"
+react-hot-keys@^2.7.2:
+ version "2.7.2"
+ resolved "https://registry.yarnpkg.com/react-hot-keys/-/react-hot-keys-2.7.2.tgz#7d2b02b7e2cf69182ea71ca01885446ebfae01d2"
+ integrity sha512-Z7eSh7SU6s52+zP+vkfFoNk0x4kgEmnwqDiyACKv53crK2AZ7FUaBLnf+vxLor3dvtId9murLmKOsrJeYgeHWw==
+ dependencies:
+ hotkeys-js "^3.8.1"
+ prop-types "^15.7.2"
+
react-is@^16.13.1, react-is@^16.7.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"