Skip to content

Commit 8ccb26b

Browse files
feat: Stop editor scrolling to top (apache#26754)
Co-authored-by: Puridach Wutthihathaithamrong <> Co-authored-by: JUST.in DO IT <[email protected]>
1 parent 32b55e7 commit 8ccb26b

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

superset-frontend/src/SqlLab/actions/sqlLab.js

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export const QUERY_EDITOR_SET_SCHEMA = 'QUERY_EDITOR_SET_SCHEMA';
5959
export const QUERY_EDITOR_SET_TITLE = 'QUERY_EDITOR_SET_TITLE';
6060
export const QUERY_EDITOR_SET_AUTORUN = 'QUERY_EDITOR_SET_AUTORUN';
6161
export const QUERY_EDITOR_SET_SQL = 'QUERY_EDITOR_SET_SQL';
62+
export const QUERY_EDITOR_SET_CURSOR_POSITION =
63+
'QUERY_EDITOR_SET_CURSOR_POSITION';
6264
export const QUERY_EDITOR_SET_QUERY_LIMIT = 'QUERY_EDITOR_SET_QUERY_LIMIT';
6365
export const QUERY_EDITOR_SET_TEMPLATE_PARAMS =
6466
'QUERY_EDITOR_SET_TEMPLATE_PARAMS';
@@ -881,6 +883,10 @@ export function queryEditorSetSql(queryEditor, sql, queryId) {
881883
return { type: QUERY_EDITOR_SET_SQL, queryEditor, sql, queryId };
882884
}
883885

886+
export function queryEditorSetCursorPosition(queryEditor, position) {
887+
return { type: QUERY_EDITOR_SET_CURSOR_POSITION, queryEditor, position };
888+
}
889+
884890
export function queryEditorSetAndSaveSql(targetQueryEditor, sql, queryId) {
885891
return function (dispatch, getState) {
886892
const queryEditor = getUpToDateQuery(getState(), targetQueryEditor);

superset-frontend/src/SqlLab/components/AceEditorWrapper/AceEditorWrapper.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const setup = (queryEditor: QueryEditor, store?: Store) =>
5151
onChange={jest.fn()}
5252
onBlur={jest.fn()}
5353
autocomplete
54+
onCursorPositionChange={jest.fn()}
5455
/>,
5556
{
5657
useRedux: true,

superset-frontend/src/SqlLab/components/AceEditorWrapper/index.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { queryEditorSetSelectedText } from 'src/SqlLab/actions/sqlLab';
2525
import { FullSQLEditor as AceEditor } from 'src/components/AsyncAceEditor';
2626
import type { KeyboardShortcut } from 'src/SqlLab/components/KeyboardShortcutButton';
2727
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
28+
import type { CursorPosition } from 'src/SqlLab/types';
2829
import { useAnnotations } from './useAnnotations';
2930
import { useKeywords } from './useKeywords';
3031

@@ -40,6 +41,7 @@ type AceEditorWrapperProps = {
4041
onBlur: (sql: string) => void;
4142
onChange: (sql: string) => void;
4243
queryEditorId: string;
44+
onCursorPositionChange: (position: CursorPosition) => void;
4345
height: string;
4446
hotkeys: HotKey[];
4547
};
@@ -69,6 +71,7 @@ const AceEditorWrapper = ({
6971
onBlur = () => {},
7072
onChange = () => {},
7173
queryEditorId,
74+
onCursorPositionChange,
7275
height,
7376
hotkeys,
7477
}: AceEditorWrapperProps) => {
@@ -79,10 +82,11 @@ const AceEditorWrapper = ({
7982
'sql',
8083
'schema',
8184
'templateParams',
85+
'cursorPosition',
8286
]);
8387

8488
const currentSql = queryEditor.sql ?? '';
85-
89+
const cursorPosition = queryEditor.cursorPosition ?? { row: 0, column: 0 };
8690
const [sql, setSql] = useState(currentSql);
8791

8892
// The editor changeSelection is called multiple times in a row,
@@ -143,6 +147,15 @@ const AceEditorWrapper = ({
143147

144148
currentSelectionCache.current = selectedText;
145149
});
150+
editor.selection.on('changeCursor', () => {
151+
const cursor = editor.getCursorPosition();
152+
onCursorPositionChange(cursor);
153+
});
154+
155+
const { row, column } = cursorPosition;
156+
editor.moveCursorToPosition({ row, column });
157+
editor.focus();
158+
editor.scrollToLine(row, true, true);
146159
};
147160

148161
const onChangeText = (text: string) => {

superset-frontend/src/SqlLab/components/SqlEditor/index.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ import {
4242
QueryResponse,
4343
Query,
4444
} from '@superset-ui/core';
45-
import type { QueryEditor, SqlLabRootState } from 'src/SqlLab/types';
45+
import type {
46+
QueryEditor,
47+
SqlLabRootState,
48+
CursorPosition,
49+
} from 'src/SqlLab/types';
4650
import type { DatabaseObject } from 'src/features/databases/types';
4751
import { debounce, throttle, isBoolean, isEmpty } from 'lodash';
4852
import Modal from 'src/components/Modal';
@@ -63,6 +67,7 @@ import {
6367
postStopQuery,
6468
queryEditorSetAutorun,
6569
queryEditorSetSql,
70+
queryEditorSetCursorPosition,
6671
queryEditorSetAndSaveSql,
6772
queryEditorSetTemplateParams,
6873
runQueryFromSqlEditor,
@@ -757,6 +762,10 @@ const SqlEditor: React.FC<Props> = ({
757762
);
758763
};
759764

765+
const handleCursorPositionChange = (newPosition: CursorPosition) => {
766+
dispatch(queryEditorSetCursorPosition(queryEditor, newPosition));
767+
};
768+
760769
const queryPane = () => {
761770
const { aceEditorHeight, southPaneHeight } =
762771
getAceEditorAndSouthPaneHeights(height, northPercent, southPercent);
@@ -787,6 +796,7 @@ const SqlEditor: React.FC<Props> = ({
787796
onBlur={onSqlChanged}
788797
onChange={onSqlChanged}
789798
queryEditorId={queryEditor.id}
799+
onCursorPositionChange={handleCursorPositionChange}
790800
height={`${aceEditorHeight}px`}
791801
hotkeys={hotkeys}
792802
/>

superset-frontend/src/SqlLab/reducers/getInitialState.ts

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default function getInitialState({
6565
queryLimit: common.conf.DEFAULT_SQLLAB_LIMIT,
6666
hideLeftBar: false,
6767
remoteId: null,
68+
cursorPosition: { row: 0, column: 0 },
6869
};
6970
let unsavedQueryEditor: UnsavedQueryEditor = {};
7071

superset-frontend/src/SqlLab/reducers/sqlLab.js

+12
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,18 @@ export default function sqlLabReducer(state = {}, action) {
544544
),
545545
};
546546
},
547+
[actions.QUERY_EDITOR_SET_CURSOR_POSITION]() {
548+
return {
549+
...state,
550+
...alterUnsavedQueryEditorState(
551+
state,
552+
{
553+
cursorPosition: action.position,
554+
},
555+
action.queryEditor.id,
556+
),
557+
};
558+
},
547559
[actions.QUERY_EDITOR_SET_QUERY_LIMIT]() {
548560
return {
549561
...state,

superset-frontend/src/SqlLab/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export enum QueryEditorVersion {
3535

3636
export const LatestQueryEditorVersion = QueryEditorVersion.v1;
3737

38+
export interface CursorPosition {
39+
row: number;
40+
column: number;
41+
}
42+
3843
export interface QueryEditor {
3944
version: QueryEditorVersion;
4045
id: string;
@@ -56,6 +61,7 @@ export interface QueryEditor {
5661
northPercent?: number;
5762
southPercent?: number;
5863
updatedAt?: number;
64+
cursorPosition?: CursorPosition;
5965
}
6066

6167
export type toastState = {

0 commit comments

Comments
 (0)