From 95cce9a553ddbe2293d5b79f55f1bd6a8915c9b4 Mon Sep 17 00:00:00 2001 From: kenjiding <872570730@qq.com> Date: Wed, 27 Nov 2024 01:47:21 +1030 Subject: [PATCH] fix: fixed search input cursor position issue --- packages/excalidraw/components/SearchMenu.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/excalidraw/components/SearchMenu.tsx b/packages/excalidraw/components/SearchMenu.tsx index b4bb91b90..e2aaf9ea9 100644 --- a/packages/excalidraw/components/SearchMenu.tsx +++ b/packages/excalidraw/components/SearchMenu.tsx @@ -137,6 +137,22 @@ export const SearchMenu = () => { } }; + const fixInputCursorPosition = () => { + requestAnimationFrame(() => { + const searchInput = searchInputRef.current; + const isFocus = document.activeElement === searchInput; + // if input is not focus and input instance is not exist return + if (!searchInput || !isFocus) { + return; + } + const cursorPosition = searchInput.selectionStart; + const textLength = searchInput.value.length!; + if (cursorPosition !== textLength) { + searchInput.setSelectionRange(textLength, textLength); + } + }); + }; + useEffect(() => { setAppState((state) => { return { @@ -282,6 +298,7 @@ export const SearchMenu = () => { if (event.key === KEYS.ARROW_UP) { event.stopPropagation(); stableState.goToPreviousItem(); + fixInputCursorPosition(); } else if (event.key === KEYS.ARROW_DOWN) { event.stopPropagation(); stableState.goToNextItem();