Skip to content

Commit

Permalink
fix cursor position restoration after URL trimming (usebruno#3087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragadesh-45 authored Sep 14, 2024
1 parent 9614ab0 commit 8856e8e
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/bruno-app/src/components/RequestPane/QueryUrl/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand All @@ -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) => {
Expand All @@ -68,6 +79,7 @@ const QueryUrl = ({ item, collection, handleRun }) => {
}}
>
<SingleLineEditor
ref={editorRef}
value={url}
onSave={(finalValue) => onSave(finalValue)}
theme={storedTheme}
Expand Down

0 comments on commit 8856e8e

Please sign in to comment.