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

Fix cursor jumping while editing in codemirror #145

Merged
merged 1 commit into from
May 20, 2016

Conversation

tizmagik
Copy link
Collaborator

Fixes #139

The reason why this happens is because the <Editor /> component re-renders when code changes by the editor itself. So when the handleChange() event is fired, it's re-rendering the <Editor /> which resets where the cursor position is.

There's 2 ways to fix this. One way is to capture the current cursor position in componentWillReceiveProps() and then set it again in componentDidUpdate(), but that's a bit hacky and a lot of machinery for no real reason.

The other, and I think simpler approach (and what is implemented in this PR), is to just not have the <Editor /> update once it's been rendered, so it no longer reacts to property updates entirely. This is okay because we just need the codemirror editor rendered once and then just pass along any code changes to the <Preview /> component from then on, which still responds and updates to code changes as expected.

With this implementation, we can even tighten up the UPDATE_DELAY if we wanted to. I changed it from 100 to 10 in this PR and it works great.

@@ -40,6 +40,10 @@ export default class Editor extends Component {
cssRequire(`./${highlightTheme}.css`);
}

shouldComponentUpdate() {
return false;
Copy link
Member

Choose a reason for hiding this comment

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

I like the simplicity ;-)

@sapegin
Copy link
Member

sapegin commented May 20, 2016

That’s great, thanks!

@sapegin sapegin merged commit af2a7d3 into styleguidist:master May 20, 2016
@tizmagik tizmagik deleted the fix-139 branch May 20, 2016 17:08
@mik01aj
Copy link
Collaborator

mik01aj commented May 23, 2016

Doesn't this break hot-reloading the code for examples? I mean, when you save the examples.md in external editor?

@tizmagik
Copy link
Collaborator Author

tizmagik commented May 23, 2016

@mik01aj no, it still behaves as it used to. Namely, the example code in the live editor gets replaced with what's in examples.md. Is that what you mean?

@mik01aj
Copy link
Collaborator

mik01aj commented May 23, 2016

Yes, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cursor repositioned while editing example
3 participants