fix(app): move the caret to the line edges on home and end (#1997)

This commit is contained in:
Hampus
2026-08-25 12:56:13 +02:00
committed by GitHub
parent e32c5b73a7
commit 3f5c8d8d0a
@@ -156,6 +156,22 @@ export interface LexicalComposerInputProps {
onSlashCommandStateChange?: (state: SlashCommandComposerState) => void;
}
function moveSelectionToLineBoundary(event: React.KeyboardEvent<HTMLElement>): boolean {
if (event.key !== 'Home' && event.key !== 'End') {
return false;
}
if (event.ctrlKey || event.metaKey || event.altKey) {
return false;
}
const selection = event.currentTarget.ownerDocument.defaultView?.getSelection();
if (selection == null || selection.rangeCount === 0) {
return false;
}
selection.modify(event.shiftKey ? 'extend' : 'move', event.key === 'Home' ? 'backward' : 'forward', 'lineboundary');
event.preventDefault();
return true;
}
export const LexicalComposerInput = observer((props: LexicalComposerInputProps) => {
const plainText = ChatInputSettings.renderComposerAsPlainText;
const selectionToolbar = (props.selectionToolbar == null ? true : props.selectionToolbar) && !MobileLayout.enabled;
@@ -626,6 +642,9 @@ const ComposerInner = ({
const handleEditableKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLElement>) => {
if (!event.defaultPrevented && !isIMEComposing(event)) {
if (moveSelectionToLineBoundary(event)) {
return;
}
if (onKeyDown != null) {
onKeyDown(event);
}