瀏覽代碼

Fix scrolling in textarea when adding context

Saoud Rizwan 1 年之前
父節點
當前提交
6d251e37c2
共有 2 個文件被更改,包括 20 次插入2 次删除
  1. 20 1
      webview-ui/src/components/ChatTextArea.tsx
  2. 0 1
      webview-ui/src/components/ContextMenu.tsx

+ 20 - 1
webview-ui/src/components/ChatTextArea.tsx

@@ -126,7 +126,15 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
 					const newCursorPosition = newValue.indexOf(" ", mentionIndex + insertValue.length) + 1
 					setCursorPosition(newCursorPosition)
 					setIntendedCursorPosition(newCursorPosition)
-					textAreaRef.current.focus()
+					// textAreaRef.current.focus()
+
+					// scroll to cursor
+					setTimeout(() => {
+						if (textAreaRef.current) {
+							textAreaRef.current.blur()
+							textAreaRef.current.focus()
+						}
+					}, 0)
 				}
 			},
 			[setInputValue, cursorPosition]
@@ -311,6 +319,17 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
 					setCursorPosition(newCursorPosition)
 					setIntendedCursorPosition(newCursorPosition)
 					setShowContextMenu(false)
+
+					// Scroll to new cursor position
+					// https://stackoverflow.com/questions/29899364/how-do-you-scroll-to-the-position-of-the-cursor-in-a-textarea/40951875#40951875
+					setTimeout(() => {
+						if (textAreaRef.current) {
+							textAreaRef.current.blur()
+							textAreaRef.current.focus()
+						}
+					}, 0)
+					// NOTE: callbacks dont utilize return function to cleanup, but it's fine since this timeout immediately executes and will be cleaned up by the browser (no chance component unmounts before it executes)
+
 					return
 				}
 

+ 0 - 1
webview-ui/src/components/ContextMenu.tsx

@@ -117,7 +117,6 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
 					flexDirection: "column",
 					maxHeight: "200px",
 					overflowY: "auto",
-					overflow: "hidden",
 				}}>
 				{/* Can't use virtuoso since it requires fixed height and menu height is dynamic based on # of items */}
 				{filteredOptions.map((option, index) => (