feat: Stop editor scrolling to top (#26754)

Co-authored-by: Puridach Wutthihathaithamrong <>
Co-authored-by: JUST.in DO IT <justin.park@airbnb.com>
This commit is contained in:
Puridach wutthihathaithamrong
2024-01-26 04:04:20 +07:00
committed by GitHub
parent 11f0dd91db
commit ed934a93e1
7 changed files with 51 additions and 2 deletions

View File

@@ -51,6 +51,7 @@ const setup = (queryEditor: QueryEditor, store?: Store) =>
onChange={jest.fn()}
onBlur={jest.fn()}
autocomplete
onCursorPositionChange={jest.fn()}
/>,
{
useRedux: true,

View File

@@ -25,6 +25,7 @@ import { queryEditorSetSelectedText } from 'src/SqlLab/actions/sqlLab';
import { FullSQLEditor as AceEditor } from 'src/components/AsyncAceEditor';
import type { KeyboardShortcut } from 'src/SqlLab/components/KeyboardShortcutButton';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
import type { CursorPosition } from 'src/SqlLab/types';
import { useAnnotations } from './useAnnotations';
import { useKeywords } from './useKeywords';
@@ -40,6 +41,7 @@ type AceEditorWrapperProps = {
onBlur: (sql: string) => void;
onChange: (sql: string) => void;
queryEditorId: string;
onCursorPositionChange: (position: CursorPosition) => void;
height: string;
hotkeys: HotKey[];
};
@@ -69,6 +71,7 @@ const AceEditorWrapper = ({
onBlur = () => {},
onChange = () => {},
queryEditorId,
onCursorPositionChange,
height,
hotkeys,
}: AceEditorWrapperProps) => {
@@ -79,10 +82,11 @@ const AceEditorWrapper = ({
'sql',
'schema',
'templateParams',
'cursorPosition',
]);
const currentSql = queryEditor.sql ?? '';
const cursorPosition = queryEditor.cursorPosition ?? { row: 0, column: 0 };
const [sql, setSql] = useState(currentSql);
// The editor changeSelection is called multiple times in a row,
@@ -143,6 +147,15 @@ const AceEditorWrapper = ({
currentSelectionCache.current = selectedText;
});
editor.selection.on('changeCursor', () => {
const cursor = editor.getCursorPosition();
onCursorPositionChange(cursor);
});
const { row, column } = cursorPosition;
editor.moveCursorToPosition({ row, column });
editor.focus();
editor.scrollToLine(row, true, true);
};
const onChangeText = (text: string) => {