Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions components/sender/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,44 @@

// ============================ Submit ============================
const isCompositionRef = React.useRef(false);
const isCompositionEndRef = React.useRef(false);
const pendingKeyDownRef = React.useRef<React.KeyboardEvent<HTMLElement> | null>(null);

const onInternalCompositionStart = () => {
isCompositionRef.current = true;
};

const onInternalCompositionEnd = () => {
isCompositionRef.current = false;
if (isCompositionRef.current) {
isCompositionEndRef.current = true;
isCompositionRef.current = false;

Check warning on line 226 in components/sender/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/sender/index.tsx#L225-L226

Added lines #L225 - L226 were not covered by tests
}
// After processing the input Chinese, use CapsLock
if (pendingKeyDownRef.current?.key === 'CapsLock') {
isCompositionEndRef.current = false;

Check warning on line 230 in components/sender/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/sender/index.tsx#L230

Added line #L230 was not covered by tests
}
};

const onInternalKeyPress: TextareaProps['onKeyPress'] = (e) => {
const canSubmit = e.key === 'Enter' && !isCompositionRef.current;
if (isCompositionRef.current || isCompositionEndRef.current) {
// Parameter isCompositionRef.current is the event execution: onInternalCompositionStart -> onInternalKeyPress -> onInternalCompositionEnd
// Parameter isCompositionEndRef.current is the event execution: onInternalCompositionStart -> onInternalCompositionEnd -> onInternalKeyPress
handleActualKeyPress(e, false);
isCompositionRef.current = false;

Check warning on line 239 in components/sender/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/sender/index.tsx#L238-L239

Added lines #L238 - L239 were not covered by tests
if (isCompositionEndRef.current) {
isCompositionEndRef.current = false;
pendingKeyDownRef.current = null;

Check warning on line 242 in components/sender/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/sender/index.tsx#L241-L242

Added lines #L241 - L242 were not covered by tests
}
} else {
handleActualKeyPress(e, true);
}
};

const handleActualKeyPress = (
e: React.KeyboardEvent<HTMLTextAreaElement>,
isCanSubmit: boolean,
) => {
const canSubmit = e.key === 'Enter' && isCanSubmit;

// Check for `submitType` to submit
switch (submitType) {
Expand All @@ -245,6 +272,16 @@
onKeyPress?.(e);
};

const onInternalKeyDown: React.KeyboardEventHandler<HTMLElement> = (e) => {
if (e.key !== 'Unidentified') {
// Record e in the keydown event
pendingKeyDownRef.current = e;
}
if (onKeyDown) {
onKeyDown(e);

Check warning on line 281 in components/sender/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/sender/index.tsx#L281

Added line #L281 was not covered by tests
}
};

// ============================ Paste =============================
const onInternalPaste: React.ClipboardEventHandler<HTMLElement> = (e) => {
// Get files
Expand Down Expand Up @@ -346,7 +383,7 @@
onPressEnter={onInternalKeyPress}
onCompositionStart={onInternalCompositionStart}
onCompositionEnd={onInternalCompositionEnd}
onKeyDown={onKeyDown}
onKeyDown={onInternalKeyDown}
onPaste={onInternalPaste}
variant="borderless"
readOnly={readOnly}
Expand Down