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

Refresh CodeMirror editor when focused, and remove duplicate listener #1294

Merged
merged 2 commits into from Dec 13, 2018

Conversation

Projects
None yet
2 participants
@rickychandra
Copy link
Contributor

rickychandra commented Dec 8, 2018

Closes: #1031
I added response pane tab select listener, then trigger CodeMirror editor refresh when it is selected. For the editor to be able to be refreshed it must be visible; but since the tab selection listener is triggered right before that, I used process.nextTick() to do the refresh.
I also removed a seemingly duplicate line that registers the same callback to CodeMirror, which causes the callback to be called twice everytime the editor's value is changed.

@gschier
Copy link
Collaborator

gschier left a comment

Thanks for the PR @rickychandra! In general, your approach is great but I have some high-level comments for you.

@@ -60,6 +60,12 @@ class ResponseViewer extends React.Component<Props, State> {
};
}

focus() {
if (this._isViewFocusable()) {

This comment has been minimized.

@gschier

gschier Dec 10, 2018

Collaborator

Is there a reason for calling focus() on the editor? I feel like a new method specifically for refreshing may be more appropriate and a more specific fix to the original bug.

This comment has been minimized.

@rickychandra

rickychandra Dec 13, 2018

Contributor

Actually I was thinking about reusing focus(), because it's already been a "contract" function implemented by selectable views under ResponseViewer.
But on second thought, calling refresh() would have clearer meaning.
I've refactored it.

@@ -269,7 +274,6 @@ class CodeEditor extends React.Component {
// Set default listeners
const debounceMillis = typeof ms === 'number' ? ms : DEBOUNCE_MILLIS;
this.codeMirror.on('changes', misc.debounce(this._codemirrorValueChanged, debounceMillis));

This comment has been minimized.

@gschier

gschier Dec 10, 2018

Collaborator

Oh, good catch!

@@ -132,7 +132,12 @@ class CodeEditor extends React.Component {

focus() {
if (this.codeMirror) {
this.codeMirror.focus();
// Refresh must happen when the editor is visible,

This comment has been minimized.

@gschier

gschier Dec 10, 2018

Collaborator

I feel like this nextTick logic would be better located in the caller of focus(). Technically, the editor has no way of knowing that it will be visible on the next frame but the caller hopefully does.

This comment has been minimized.

@rickychandra

rickychandra Dec 13, 2018

Contributor

You're right, nextTick should be called by the caller.
CMIIW, I think nextTick is not a good solution, because it's not clear whether the selected tab will be made visible in the same event loop or not. But I haven't found better solution yet.

@gschier gschier force-pushed the getinsomnia:develop branch from 9b1c261 to 6f355bc Dec 11, 2018

@gschier
Copy link
Collaborator

gschier left a comment

Awesome stuff! Thanks for fixing this one 💯

@@ -133,6 +139,17 @@ class ResponsePane extends React.PureComponent<Props> {
});
}

_handleTabSelect(index: number, lastIndex: number) {
if (this._responseViewer != null && index === 0 && index !== lastIndex) {
// Fix for CodeMirror editor not updating its content.

This comment has been minimized.

@gschier

gschier Dec 13, 2018

Collaborator

👍 thumbs up for the very descriptive comment

@gschier gschier merged commit 48716d9 into getinsomnia:develop Dec 13, 2018

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@rickychandra rickychandra deleted the rickychandra:fix/response-not-refreshed branch Dec 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment