Skip to content

Commit

Permalink
fix(sql lab): Selecting edit on a query from query history doesn't up…
Browse files Browse the repository at this point in the history
…date the SQL Editor properly (apache#19290)
  • Loading branch information
diegomedina248 authored and philipher29 committed Jun 9, 2022
1 parent e6cf8f0 commit a800123
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions superset-frontend/src/SqlLab/components/AceEditorWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ interface Props {

interface State {
sql: string;
selectedText: string;
words: AceCompleterKeyword[];
}

Expand All @@ -80,13 +79,20 @@ class AceEditorWrapper extends React.PureComponent<Props, State> {
extendedTables: [],
};

private currentSelectionCache;

constructor(props: Props) {
super(props);
this.state = {
sql: props.sql,
selectedText: '',
words: [],
};

// The editor changeSelection is called multiple times in a row,
// faster than React reconciliation process, so the selected text
// needs to be stored out of the state to ensure changes to it
// get saved immediately
this.currentSelectionCache = '';
this.onChange = this.onChange.bind(this);
}

Expand Down Expand Up @@ -146,17 +152,19 @@ class AceEditorWrapper extends React.PureComponent<Props, State> {
editor.$blockScrolling = Infinity; // eslint-disable-line no-param-reassign
editor.selection.on('changeSelection', () => {
const selectedText = editor.getSelectedText();

// Backspace trigger 1 character selection, ignoring
if (
selectedText !== this.state.selectedText &&
selectedText !== this.currentSelectionCache &&
selectedText.length !== 1
) {
this.setState({ selectedText });
this.props.actions.queryEditorSetSelectedText(
this.props.queryEditor,
selectedText,
);
}

this.currentSelectionCache = selectedText;
});
}

Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ class SqlEditor extends React.PureComponent {
});
}

componentDidUpdate() {
if (this.props.queryEditor.sql !== this.state.sql) {
this.onSqlChanged(this.props.queryEditor.sql);
}
}

componentWillUnmount() {
window.removeEventListener('resize', this.handleWindowResize);
window.removeEventListener('beforeunload', this.onBeforeUnload);
Expand Down

0 comments on commit a800123

Please sign in to comment.