1
1
import { h } from 'preact' ;
2
2
import { useContext , useRef } from 'preact/hooks' ;
3
+ import { eventToTarget } from '../../../../../shared/handlers' ;
3
4
import { ArrowRightIcon } from '../../components/Icons' ;
5
+ import { usePlatformName } from '../../settings.provider' ;
4
6
import { useTypedTranslationWith } from '../../types' ;
5
7
import styles from './AiChatForm.module.css' ;
6
8
import { OmnibarContext } from './OmnibarProvider' ;
@@ -17,19 +19,53 @@ import { OmnibarContext } from './OmnibarProvider';
17
19
export function AiChatForm ( { chat, setChat } ) {
18
20
const { submitChat } = useContext ( OmnibarContext ) ;
19
21
const { t } = useTypedTranslationWith ( /** @type {Strings } */ ( { } ) ) ;
22
+ const platformName = usePlatformName ( ) ;
20
23
21
24
const formRef = useRef ( /** @type {HTMLFormElement|null } */ ( null ) ) ;
22
25
const textAreaRef = useRef ( /** @type {HTMLTextAreaElement|null } */ ( null ) ) ;
23
26
27
+ const disabled = chat . length === 0 ;
28
+
24
29
/** @type {(event: SubmitEvent) => void } */
25
30
const onSubmit = ( event ) => {
26
31
event . preventDefault ( ) ;
32
+ if ( disabled ) return ;
27
33
submitChat ( {
28
34
chat,
29
35
target : 'same-tab' ,
30
36
} ) ;
31
37
} ;
32
38
39
+ /** @type {(event: KeyboardEvent) => void } */
40
+ const onKeyDown = ( event ) => {
41
+ if ( event . key === 'Enter' && ! event . shiftKey ) {
42
+ event . preventDefault ( ) ;
43
+ if ( disabled ) return ;
44
+ submitChat ( {
45
+ chat,
46
+ target : eventToTarget ( event , platformName ) ,
47
+ } ) ;
48
+ }
49
+ } ;
50
+
51
+ /** @type {(event: import('preact').JSX.TargetedEvent<HTMLTextAreaElement>) => void } */
52
+ const onChange = ( event ) => {
53
+ const form = formRef . current ;
54
+ const textArea = event . currentTarget ;
55
+
56
+ const { paddingTop, paddingBottom } = window . getComputedStyle ( textArea ) ;
57
+ textArea . style . height = 'auto' ; // Reset height
58
+ textArea . style . height = `calc(${ textArea . scrollHeight } px - ${ paddingTop } - ${ paddingBottom } )` ;
59
+
60
+ if ( textArea . scrollHeight > textArea . clientHeight ) {
61
+ form ?. classList . add ( styles . hasScroll ) ;
62
+ } else {
63
+ form ?. classList . remove ( styles . hasScroll ) ;
64
+ }
65
+
66
+ setChat ( textArea . value ) ;
67
+ } ;
68
+
33
69
return (
34
70
< form ref = { formRef } class = { styles . form } onClick = { ( ) => textAreaRef . current ?. focus ( ) } onSubmit = { onSubmit } >
35
71
< textarea
@@ -40,22 +76,8 @@ export function AiChatForm({ chat, setChat }) {
40
76
aria-label = { t ( 'aiChatForm_placeholder' ) }
41
77
autoComplete = "off"
42
78
rows = { 1 }
43
- onChange = { ( event ) => {
44
- const form = formRef . current ;
45
- const textArea = event . currentTarget ;
46
-
47
- const { paddingTop, paddingBottom } = window . getComputedStyle ( textArea ) ;
48
- textArea . style . height = 'auto' ; // Reset height
49
- textArea . style . height = `calc(${ textArea . scrollHeight } px - ${ paddingTop } - ${ paddingBottom } )` ;
50
-
51
- if ( textArea . scrollHeight > textArea . clientHeight ) {
52
- form ?. classList . add ( styles . hasScroll ) ;
53
- } else {
54
- form ?. classList . remove ( styles . hasScroll ) ;
55
- }
56
-
57
- setChat ( textArea . value ) ;
58
- } }
79
+ onKeyDown = { onKeyDown }
80
+ onChange = { onChange }
59
81
/>
60
82
< div class = { styles . buttons } >
61
83
< button
0 commit comments