Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wwayne committed May 27, 2024
1 parent edaebee commit 63a0477
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
56 changes: 37 additions & 19 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ export default function ChatPage() {
const maxWidth = isFromVSCode ? '5xl' : undefined

useEffect(() => {
window.addEventListener('message', ({ data }) => {
const onMessage = ({
data
}: {
data: {
style?: string
themeClass?: string
}
}) => {
// Sync with VSCode CSS variable
if (data.style) {
const styleWithHslValue = data.style
Expand All @@ -65,7 +72,35 @@ export default function ChatPage() {
if (data.themeClass) {
document.documentElement.className = data.themeClass
}
})
}

window.addEventListener('message', onMessage)
return () => {
window.removeEventListener('message', onMessage)
}
}, [])

// VSCode bug: not support shortcuts like copy/paste
// @see - https://github.com/microsoft/vscode/issues/129178
useEffect(() => {
if (!isFromVSCode) return

const onKeyDown = (event: KeyboardEvent) => {
if ((event.ctrlKey || event.metaKey) && event.code === 'KeyC') {
document.execCommand('copy')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyX') {
document.execCommand('cut')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyV') {
document.execCommand('paste')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyA') {
document.execCommand('selectAll')
}
}

window.addEventListener('keydown', onKeyDown)
return () => {
window.removeEventListener('keydown', onKeyDown)
}
}, [])

const sendMessage = (message: ChatMessage) => {
Expand Down Expand Up @@ -99,22 +134,6 @@ export default function ChatPage() {
server?.navigate(context)
}

// VSCode bug: not support shortcuts like copy/paste
// @see - https://github.com/microsoft/vscode/issues/129178
const onInputKeyDown = isFromVSCode
? (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if ((event.ctrlKey || event.metaKey) && event.code === 'KeyC') {
document.execCommand('copy')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyX') {
document.execCommand('cut')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyV') {
document.execCommand('paste')
} else if ((event.ctrlKey || event.metaKey) && event.code === 'KeyA') {
document.execCommand('selectAll')
}
}
: undefined

if (!isInit || !fetcherOptions) return <></>
const headers = {
Authorization: `Bearer ${fetcherOptions.authorization}`
Expand All @@ -129,7 +148,6 @@ export default function ChatPage() {
onNavigateToContext={onNavigateToContext}
onLoaded={onChatLoaded}
maxWidth={maxWidth}
onInputKeyDown={onInputKeyDown}
/>
)
}
5 changes: 1 addition & 4 deletions ee/tabby-ui/components/chat/chat-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface ChatPanelProps
onSubmit: (content: string) => Promise<any>
reload: () => void
chatMaxWidthClass: string
onInputKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
}

export function ChatPanel({
Expand All @@ -27,8 +26,7 @@ export function ChatPanel({
setInput,
className,
onSubmit,
chatMaxWidthClass,
onInputKeyDown
chatMaxWidthClass
}: ChatPanelProps) {
const promptFormRef = React.useRef<PromptFormRef>(null)
const { container, onClearMessages, qaPairs, isLoading } =
Expand Down Expand Up @@ -81,7 +79,6 @@ export function ChatPanel({
input={input}
setInput={setInput}
isLoading={isLoading}
onInputKeyDown={onInputKeyDown}
/>
<FooterText className="hidden sm:block" />
</div>
Expand Down
5 changes: 1 addition & 4 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ interface ChatProps extends React.ComponentProps<'div'> {
docQuery?: boolean
generateRelevantQuestions?: boolean
maxWidth?: string
onInputKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
}

function ChatRenderer(
Expand All @@ -131,8 +130,7 @@ function ChatRenderer(
fetcher,
docQuery,
generateRelevantQuestions,
maxWidth,
onInputKeyDown
maxWidth
}: ChatProps,
ref: React.ForwardedRef<ChatRef>
) {
Expand Down Expand Up @@ -402,7 +400,6 @@ function ChatRenderer(
input={input}
setInput={setInput}
chatMaxWidthClass={chatMaxWidthClass}
onInputKeyDown={onInputKeyDown}
/>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions ee/tabby-ui/components/chat/prompt-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ export interface PromptProps
extends Pick<UseChatHelpers, 'input' | 'setInput'> {
onSubmit: (value: string) => Promise<void>
isLoading: boolean
onInputKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
}

export interface PromptFormRef {
focus: () => void
}

function PromptFormRenderer(
{ onSubmit, input, setInput, isLoading, onInputKeyDown }: PromptProps,
{ onSubmit, input, setInput, isLoading }: PromptProps,
ref: React.ForwardedRef<PromptFormRef>
) {
const { formRef, onKeyDown } = useEnterSubmit({
onKeyDown: onInputKeyDown
})
const { formRef, onKeyDown } = useEnterSubmit()
const [queryCompletionUrl, setQueryCompletionUrl] = React.useState<
string | null
>(null)
Expand Down
8 changes: 1 addition & 7 deletions ee/tabby-ui/lib/hooks/use-enter-submit.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { useRef, type RefObject } from 'react'

export function useEnterSubmit({
onKeyDown
}: {
onKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
}): {
export function useEnterSubmit(): {
formRef: RefObject<HTMLFormElement>
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
} {
Expand All @@ -21,8 +17,6 @@ export function useEnterSubmit({
formRef.current?.requestSubmit()
event.preventDefault()
}

onKeyDown?.(event)
}

return { formRef, onKeyDown: handleKeyDown }
Expand Down

0 comments on commit 63a0477

Please sign in to comment.