fix cursor position restoration after URL trimming (#3087)

This commit is contained in:
Pragadesh-45 2024-09-15 00:08:35 +05:30 committed by GitHub
parent 9614ab069f
commit 8856e8ec71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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 }) => {
}}
>
<SingleLineEditor
ref={editorRef}
value={url}
onSave={(finalValue) => onSave(finalValue)}
theme={storedTheme}