Skip to content

Commit

Permalink
feat: add ⌥+⏎ as a shortcut for the verb form (#3546)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Wes <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent ae653a6 commit 091f75e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
15 changes: 14 additions & 1 deletion frontend/console/e2e/cron.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test'
import { navigateToDecl } from './helpers'
import { navigateToDecl, pressShortcut } from './helpers'

test('shows cron verb form', async ({ page }) => {
await navigateToDecl(page, 'cron', 'thirtySeconds')
Expand All @@ -24,3 +24,16 @@ test('send cron request', async ({ page }) => {

expect(responseJson).toEqual({})
})

test('submit cron form using ⌘+⏎ shortcut', async ({ page }) => {
await navigateToDecl(page, 'cron', 'thirtySeconds')

await pressShortcut(page, 'Enter')

const responseEditor = page.locator('#response-editor .cm-content[role="textbox"]')
await expect(responseEditor).toBeVisible()

const responseText = await responseEditor.textContent()
const responseJson = JSON.parse(responseText?.trim() || '{}')
expect(responseJson).toEqual({})
})
24 changes: 22 additions & 2 deletions frontend/console/src/features/modules/decls/verb/VerbFormInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Copy01Icon } from 'hugeicons-react'
import { useEffect, useRef } from 'react'
import { Button } from '../../../../components/Button'

export const VerbFormInput = ({
Expand All @@ -18,13 +19,32 @@ export const VerbFormInput = ({
onSubmit: (path: string) => void
handleCopyButton?: () => void
}) => {
const formRef = useRef<HTMLFormElement>(null)

const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (event) => {
event.preventDefault()
onSubmit(path)
}

const shortcutText = `Send ${window.navigator.userAgent.includes('Mac') ? '⌘ + ⏎' : 'Ctrl + ⏎'}`

useEffect(() => {
const handleKeydown = (event: KeyboardEvent) => {
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.preventDefault()
formRef.current?.dispatchEvent(new Event('submit', { cancelable: true, bubbles: true }))
}
}

window.addEventListener('keydown', handleKeydown)

return () => {
window.removeEventListener('keydown', handleKeydown)
}
}, [path, readOnly, onSubmit])

return (
<form onSubmit={handleSubmit} className='rounded-lg'>
<form ref={formRef} onSubmit={handleSubmit} className='rounded-lg'>
<div className='flex rounded-md'>
<span id='call-type' className='inline-flex items-center rounded-l-md border border-r-0 border-gray-300 dark:border-gray-500 px-3 ml-4 sm:text-sm'>
{requestType}
Expand All @@ -38,7 +58,7 @@ export const VerbFormInput = ({
readOnly={readOnly}
onChange={(event) => setPath(event.target.value)}
/>
<Button variant='primary' size='md' type='submit' title='Send' className='mx-2'>
<Button variant='primary' size='md' type='submit' title={shortcutText} className='mx-2'>
Send
</Button>
<Button variant='secondary' size='md' type='button' title='Copy' onClick={handleCopyButton} className='mr-2'>
Expand Down

0 comments on commit 091f75e

Please sign in to comment.