Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Stop editor scrolling to top #26754

Merged
merged 9 commits into from
Jan 25, 2024
6 changes: 6 additions & 0 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export const QUERY_EDITOR_SET_SCHEMA = 'QUERY_EDITOR_SET_SCHEMA';
export const QUERY_EDITOR_SET_TITLE = 'QUERY_EDITOR_SET_TITLE';
export const QUERY_EDITOR_SET_AUTORUN = 'QUERY_EDITOR_SET_AUTORUN';
export const QUERY_EDITOR_SET_SQL = 'QUERY_EDITOR_SET_SQL';
export const QUERY_EDITOR_SET_CURSOR_POSITION =
'QUERY_EDITOR_SET_CURSOR_POSITION';
export const QUERY_EDITOR_SET_QUERY_LIMIT = 'QUERY_EDITOR_SET_QUERY_LIMIT';
export const QUERY_EDITOR_SET_TEMPLATE_PARAMS =
'QUERY_EDITOR_SET_TEMPLATE_PARAMS';
Expand Down Expand Up @@ -882,6 +884,10 @@ export function queryEditorSetSql(queryEditor, sql, queryId) {
return { type: QUERY_EDITOR_SET_SQL, queryEditor, sql, queryId };
}

export function queryEditorSetCursorPosition(queryEditor, position) {
return { type: QUERY_EDITOR_SET_CURSOR_POSITION, queryEditor, position };
}

export function queryEditorSetAndSaveSql(targetQueryEditor, sql, queryId) {
return function (dispatch, getState) {
const queryEditor = getUpToDateQuery(getState(), targetQueryEditor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const setup = (queryEditor: QueryEditor, store?: Store) =>
onChange={jest.fn()}
onBlur={jest.fn()}
autocomplete
onCursorPositionChange={jest.fn()}
/>,
{
useRedux: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ type HotKey = {
func: (aceEditor: IAceEditor) => void;
};

type CursorPosition = {
row: number;
column: number;
};

type AceEditorWrapperProps = {
autocomplete: boolean;
onBlur: (sql: string) => void;
onChange: (sql: string) => void;
queryEditorId: string;
onCursorPositionChange: (position: CursorPosition) => void;
height: string;
hotkeys: HotKey[];
};
Expand Down Expand Up @@ -69,6 +75,7 @@ const AceEditorWrapper = ({
onBlur = () => {},
onChange = () => {},
queryEditorId,
onCursorPositionChange,
height,
hotkeys,
}: AceEditorWrapperProps) => {
Expand All @@ -79,10 +86,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,
Expand Down Expand Up @@ -143,6 +151,16 @@ 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.renderer.updateFontSize();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation(updateFontSize) seems redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I already modify the code to remove that line of code.

editor.scrollToLine(row, true, true);
};

const onChangeText = (text: string) => {
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
postStopQuery,
queryEditorSetAutorun,
queryEditorSetSql,
queryEditorSetCursorPosition,
queryEditorSetAndSaveSql,
queryEditorSetTemplateParams,
runQueryFromSqlEditor,
Expand Down Expand Up @@ -759,6 +760,10 @@ const SqlEditor: React.FC<Props> = ({
);
};

const handleCursorPositionChange = (newPosition: any) => {
dispatch(queryEditorSetCursorPosition(queryEditor, newPosition));
};

const queryPane = () => {
const { aceEditorHeight, southPaneHeight } =
getAceEditorAndSouthPaneHeights(height, northPercent, southPercent);
Expand Down Expand Up @@ -789,6 +794,7 @@ const SqlEditor: React.FC<Props> = ({
onBlur={onSqlChanged}
onChange={onSqlChanged}
queryEditorId={queryEditor.id}
onCursorPositionChange={handleCursorPositionChange}
height={`${aceEditorHeight}px`}
hotkeys={hotkeys}
/>
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/SqlLab/reducers/getInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function getInitialState({
queryLimit: common.conf.DEFAULT_SQLLAB_LIMIT,
hideLeftBar: false,
remoteId: null,
cursorPosition: { row: 0, column: 0 },
};
let unsavedQueryEditor: UnsavedQueryEditor = {};

Expand Down Expand Up @@ -93,6 +94,7 @@ export default function getInitialState({
queryLimit: activeTab.query_limit,
hideLeftBar: activeTab.hide_left_bar,
updatedAt: activeTab.extra_json?.updatedAt,
cursorPosition: activeTab.cursor_position || undefined,
};
} else {
// dummy state, actual state will be loaded on tab switch
Expand Down
12 changes: 12 additions & 0 deletions superset-frontend/src/SqlLab/reducers/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,18 @@ export default function sqlLabReducer(state = {}, action) {
),
};
},
[actions.QUERY_EDITOR_SET_CURSOR_POSITION]() {
return {
...state,
...alterUnsavedQueryEditorState(
state,
{
cursorPosition: action.position,
},
action.queryEditor.id,
),
};
},
[actions.QUERY_EDITOR_SET_QUERY_LIMIT]() {
return {
...state,
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/SqlLab/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export enum QueryEditorVersion {

export const LatestQueryEditorVersion = QueryEditorVersion.v1;

export interface CursorPosition {
row: number;
column: number;
}

export interface QueryEditor {
version: QueryEditorVersion;
id: string;
Expand All @@ -56,6 +61,7 @@ export interface QueryEditor {
northPercent?: number;
southPercent?: number;
updatedAt?: number;
cursorPosition?: CursorPosition;
}

export type toastState = {
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/hooks/apiResources/sqlLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import type { QueryResponse } from '@superset-ui/core';
import type { JsonResponse } from './queryApi';
import { api } from './queryApi';

export interface CursorPosition {
row: number;
column: number;
}

export type InitialState = {
active_tab: {
id: number;
Expand Down Expand Up @@ -51,6 +56,7 @@ export type InitialState = {
hide_left_bar?: boolean;
saved_query: { id: number } | null;
extra_json?: Record<string, any>;
cursor_position?: CursorPosition;
};
databases: object[];
queries: Record<
Expand Down