Skip to content

Commit bda5e68

Browse files
committed
Resize the Ace editor when the focus changes
I misread the react-ace changelog! However, this solution is a lot cleaner. Closes #223
1 parent f83eee9 commit bda5e68

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

ui/frontend/AdvancedEditor.tsx

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
33

4+
import { Focus } from './reducers/output/meta';
45
import State from './state';
56
import { CommonEditorProps } from './types';
67

@@ -116,6 +117,19 @@ class AdvancedEditor extends React.PureComponent<AdvancedEditorProps> {
116117
}
117118

118119
public componentDidUpdate(prevProps, _prevState) {
120+
// There's a tricky bug with Ace:
121+
//
122+
// 1. Open the page
123+
// 2. Fill up the page with text but do not cause scrolling
124+
// 3. Run the code (causing the pane to cover some of the text
125+
// 4. Try to scroll
126+
//
127+
// Ace doesn't know that we changed the visible area and so
128+
// doesn't recalculate. Knowing if the focus changed is enough
129+
// to force such a recalculation.
130+
if (this.props.focus !== prevProps.focus) {
131+
this._editor.editor.resize();
132+
}
119133
this.gotoPosition(prevProps.position, this.props.position);
120134
}
121135

@@ -150,6 +164,7 @@ interface AdvancedEditorProps {
150164
name: string,
151165
version: string,
152166
}>;
167+
focus?: Focus;
153168
}
154169

155170
// The ACE editor weighs in at ~250K. Adding all of the themes and the
@@ -245,11 +260,17 @@ interface AdvancedEditorAsyncState {
245260
interface PropsFromState {
246261
theme: string;
247262
keybinding?: string;
263+
focus?: Focus;
248264
}
249265

250-
const mapStateToProps = ({ configuration: { theme, keybinding } }: State) => ({
251-
theme,
252-
keybinding: keybinding === 'ace' ? null : keybinding,
253-
});
266+
const mapStateToProps = (state: State) => {
267+
const { configuration: { theme, keybinding } } = state;
268+
269+
return {
270+
theme,
271+
keybinding: keybinding === 'ace' ? null : keybinding,
272+
focus: state.output.meta.focus,
273+
};
274+
};
254275

255276
export default connect<PropsFromState, undefined, CommonEditorProps>(mapStateToProps)(AdvancedEditorAsync);

0 commit comments

Comments
 (0)