scroll to cursor after inserting clipboard text

This commit is contained in:
CREDO23 2026-03-25 16:35:23 +02:00
parent f3d6ae95e1
commit 7cbb67f0dd

View file

@ -119,11 +119,13 @@ export const InlineMentionEditor = forwardRef<InlineMentionEditorRef, InlineMent
useEffect(() => {
if (!initialText || !editorRef.current) return;
// Insert the text and add trailing line breaks for typing space
editorRef.current.innerText = initialText;
editorRef.current.appendChild(document.createElement("br"));
editorRef.current.appendChild(document.createElement("br"));
setIsEmpty(false);
onChange?.(initialText, Array.from(mentionedDocs.values()));
// Place cursor at the end of the content
editorRef.current.focus();
const sel = window.getSelection();
const range = document.createRange();
@ -131,6 +133,11 @@ export const InlineMentionEditor = forwardRef<InlineMentionEditorRef, InlineMent
range.collapse(false);
sel?.removeAllRanges();
sel?.addRange(range);
// Scroll to cursor via a temporary anchor element
const anchor = document.createElement("span");
range.insertNode(anchor);
anchor.scrollIntoView({ block: "end" });
anchor.remove();
}, [initialText]); // eslint-disable-line react-hooks/exhaustive-deps
// Focus at the end of the editor