Skip to content

Commit f52424a

Browse files
authored
fix(browser-repl): reset autocomplete on editor focus COMPASS-4760 (#1345)
1 parent 077f890 commit f52424a

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

packages/browser-repl/src/components/editor.tsx

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,71 @@ export class Editor extends Component<EditorProps> {
4444
this.editor = editor;
4545
this.visibleCursorDisplayStyle = this.editor.renderer.$cursorLayer.element.style.display;
4646

47+
editor.commands.on('afterExec', (e: any) => {
48+
if (
49+
// Only suggest autocomplete if autocompleter was set
50+
this.autocompleter &&
51+
e.command.name === 'insertstring' &&
52+
/^[\w.]$/.test(e.args)
53+
) {
54+
this.editor.execCommand('startAutocomplete');
55+
}
56+
});
57+
};
58+
59+
private autocompleter: AceAutocompleterAdapter | null = null;
60+
61+
private editorId: number = Date.now();
62+
63+
constructor(props: EditorProps) {
64+
super(props);
4765
if (this.props.autocompleter) {
48-
editor.commands.on('afterExec', function(e: any) {
49-
if (e.command.name === 'insertstring' && /^[\w.]$/.test(e.args)) {
50-
editor.execCommand('startAutocomplete');
51-
}
52-
});
66+
this.autocompleter = new AceAutocompleterAdapter(
67+
this.props.autocompleter
68+
);
69+
}
70+
}
5371

54-
tools.setCompleters([new AceAutocompleterAdapter(this.props.autocompleter)]);
72+
private onFocus = () => {
73+
if (this.autocompleter) {
74+
tools.setCompleters([this.autocompleter]);
75+
} else {
76+
tools.setCompleters([]);
5577
}
5678
};
5779

58-
componentDidUpdate(): void {
59-
if (this.props.operationInProgress) {
60-
this.hideCursor();
61-
} else {
62-
this.showCursor();
80+
componentDidUpdate(prevProps: EditorProps): void {
81+
if (prevProps.operationInProgress !== this.props.operationInProgress) {
82+
if (this.props.operationInProgress) {
83+
this.hideCursor();
84+
} else {
85+
this.showCursor();
86+
}
87+
}
88+
89+
if (prevProps.moveCursorToTheEndOfInput !== this.props.moveCursorToTheEndOfInput) {
90+
if (this.props.moveCursorToTheEndOfInput) {
91+
this.moveCursorToTheEndOfInput();
92+
}
6393
}
6494

65-
if (this.props.moveCursorToTheEndOfInput) {
66-
this.moveCursorToTheEndOfInput();
95+
if (prevProps.autocompleter !== this.props.autocompleter) {
96+
if (this.props.autocompleter) {
97+
this.autocompleter = new AceAutocompleterAdapter(
98+
this.props.autocompleter
99+
);
100+
tools.setCompleters([this.autocompleter]);
101+
} else {
102+
this.autocompleter = null;
103+
tools.setCompleters([]);
104+
}
67105
}
68106
}
69107

108+
componentWillUnmount(): void {
109+
this.autocompleter = null;
110+
}
111+
70112
private hideCursor(): void {
71113
this.editor.renderer.$cursorLayer.element.style.display = 'none';
72114
}
@@ -95,16 +137,17 @@ export class Editor extends Component<EditorProps> {
95137
tabSize: 2,
96138
useWorker: false
97139
}}
98-
name={`mongosh-ace-${Date.now()}`}
140+
name={`mongosh-ace-${this.editorId}`}
99141
mode="javascript"
100142
ref={(ref: AceEditor | null): void => {
101143
if (this.props.setInputRef && ref !== null) {
102144
this.props.setInputRef(ref as { editor?: IAceEditor });
103145
}
104146
}}
105147
theme="mongosh"
106-
onChange={this.props.onChange}
107148
onLoad={this.onEditorLoad}
149+
onFocus={this.onFocus}
150+
onChange={this.props.onChange}
108151
commands={[
109152
{
110153
name: 'return',

0 commit comments

Comments
 (0)