From 8856e8ec714fe1ffb0a5d15d8453607141a9a7bb Mon Sep 17 00:00:00 2001 From: Pragadesh-45 <54320162+Pragadesh-45@users.noreply.github.com> Date: Sun, 15 Sep 2024 00:08:35 +0530 Subject: [PATCH] fix cursor position restoration after `URL` trimming (#3087) --- .../components/RequestPane/QueryUrl/index.js | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/bruno-app/src/components/RequestPane/QueryUrl/index.js b/packages/bruno-app/src/components/RequestPane/QueryUrl/index.js index e80ff8a32..15334d2f7 100644 --- a/packages/bruno-app/src/components/RequestPane/QueryUrl/index.js +++ b/packages/bruno-app/src/components/RequestPane/QueryUrl/index.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import get from 'lodash/get'; import { useDispatch } from 'react-redux'; import { requestUrlChanged, updateRequestMethod } from 'providers/ReduxStore/slices/collections'; @@ -17,6 +17,7 @@ const QueryUrl = ({ item, collection, handleRun }) => { const url = item.draft ? get(item, 'draft.request.url', '') : get(item, 'request.url', ''); const isMac = isMacOS(); const saveShortcut = isMac ? 'Cmd + S' : 'Ctrl + S'; + const editorRef = useRef(null); const [methodSelectorWidth, setMethodSelectorWidth] = useState(90); @@ -26,22 +27,32 @@ const QueryUrl = ({ item, collection, handleRun }) => { }, [method]); const onSave = (finalValue) => { - dispatch(requestUrlChanged({ - itemUid: item.uid, - collectionUid: collection.uid, - url: finalValue && typeof finalValue === 'string' ? finalValue.trim() : value - })); dispatch(saveRequest(item.uid, collection.uid)); }; const onUrlChange = (value) => { + if (!editorRef.current?.editor) return; + const editor = editorRef.current.editor; + const cursor = editor.getCursor(); + + const finalUrl = value?.trim() ?? value; + dispatch( requestUrlChanged({ itemUid: item.uid, collectionUid: collection.uid, - url: (value && typeof value === 'string') && value + url: finalUrl }) ); + + // Restore cursor position only if URL was trimmed + if (finalUrl !== value) { + setTimeout(() => { + if (editor) { + editor.setCursor(cursor); + } + }, 0); + } }; const onMethodSelect = (verb) => { @@ -68,6 +79,7 @@ const QueryUrl = ({ item, collection, handleRun }) => { }} > onSave(finalValue)} theme={storedTheme}