Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Implement a new editor view with React.js - #1863

Merged
nathansobo merged 155 commits into
masterfrom
ns-react-editor-view
Apr 18, 2014
Merged

nathansobo merged 155 commits into
masterfrom
ns-react-editor-view

Conversation

@nathansobo

Copy link
Copy Markdown
Contributor

This PR is about making it easier to optimize the editor view and harder to introduce changes that de-optimize it.

Problems with the existing editor

Experience building the original editor view taught us that avoiding extra reflows is essential to good performance. We can optimize the shit out of our JS, but if we read from the DOM after touching it, performance is going to suck. If we want to be fast. It's critical that we batch all reads and writes in a reflow-minimizing way, and the less we access the DOM the better. The DOM needs to be treated with kid gloves.

Another hurdle in optimization is the complexity of the original editor implementation. To minimize DOM updates, we had a headache-inducing region invalidation routine. Combine that with lots of ad-hoc DOM updates for various aspects of the view that have evolved over time, and it's pretty difficult to reason about the behavior of the editor.

The editor is one of Atom's most essential components. It needs to be as fast as possible, and we can't afford to get mired in complexity every time we work on it.

How React.js helps

The core idea behind React is that you express the state of the DOM at a given point of time declaratively, allowing React to compute a minimal set of DOM updates based on a diff with the previous state. This is great, because one source of confusion in the old editor is the sequence of actions that update the DOM. Now that's all abstracted away. React makes DOM programming similar to traditional server-side web programming, where you get to assume a page refresh in between view states.

Rewriting the editor view was also an opportunity to make other improvements, which were easier thanks to the simplicity afforded by starting with a clean slate and using a superior tool:

  • Autoscroll is handled entirely in the model layer, which will avoid expensive DOM reads when moving the cursor and allow us to implement page up, page down, etc as methods on the model.
  • The model is now aware of the display width of characters, allowing pixel positions to be computed in the model and paving the way for optimization of extremely long lines. We'll be able to break long lines into smaller chunks and only render what's on screen, just like we only render what lines are on screen in the vertical direction. This is only possible if we know how wide characters are. This also paves the way for soft-wrap based on pixel dimensions rather than wrapping at a specific number of characters, which will address a number of issues with soft wrap with non latin characters. It would also make Atom viable as a more general word processor with first class support for variable width fonts.
  • The "plate glass window" called the overlayer has been removed. It was present to capture mousewheel events so velocity scrolling didn't jerk to a halt when the target of the scroll event was removed. We now delay removal of lines until after mousewheel events stop firing. Now users can access tokens directly with the DOM inspector, and a bunch of complexity involved in relaying click events between layers can be removed.
  • The hidden input is now absolutely positioned relative to the scroll view rather than the content inside it and is never allowed to stray out of the visible bounds of the scroll view. This eliminates code required to avoid Chromium's native autoscroll behavior when the input is focused.
  • Detecting of insertion of accented characters has been simplified.

Deployment plan

Changing editor rendering is a pretty huge deal and there are bound to be issues. This editor view has a superior design for the future, but the existing editor view has been battle tested and is stable. We need feedback about the new editor without forcing people to use it, so my plan is to initially deploy it behind a feature flag. The new editor will only be used if core.useReactEditor is true.

Steps:

  • Get the test suite green with the feature flag turned off. There were a few small changes to the model layer that affect the original editor that need to work.
  • Get the test suite green with the feature flag turned on. This ensures that shims are in place to at least support Atom's default packages. We can learn what else is missing based on feedback from users.
  • Do an optimization pass on the React editor. I satisfied myself that React could be fast enough early on, but then added a bunch of features without paying attention to performance. I don't want to roll it out until it's faster than what exists, so we provide an incentive for people to try it.
  • Merge this PR, with some features missing
  • Implement additional features in subsequent PRs
    • Invisibles
    • IME character composition support
    • fold decoration
    • page up / page down / etc
  • Make the react editor the default when it's stable

Comment thread package.json Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is this for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was added when I installed React. I don't really know why. I will try removing it and seeing if anything breaks.

Nathan Sobo added 27 commits April 17, 2014 16:52
The space-pen view is now a simple wrapper around the entire React
component to integrate it cleanly into our existing system. React
components can't adopt existing DOM nodes, otherwise I would just have
the react component take over the entire view instead of wrapping.
Might as well go with the flow
The overlayer is absolutely positioned to exactly fill the scroll-view.
If we can retain this strategy and never give the input a position that
exceeds the bounds of the overlayer, we can guarantee that it never
forces the scroll position of the scroll view to change when it is
focused due to the browsers default behavior.
This bakes character width tracking into display buffer directly, which
moves us toward a world where all rendering decisions can be made in the
model to strictly control DOM reads.
Whenever new lines are added to the screen, we measure and store any
unseen scope/character combinations in the DisplayBuffer.
Scroll positions, height, width, line height. We force update when one
of these observed properties changes.
Nathan Sobo added 19 commits April 17, 2014 16:52
We accumulate pending changes and pass them to the lines and the gutter
to help them determine whether to update. The lines only update if the
visible row range changed or if there was a change in the visible row
range.
This ensures we don't perform two updates of the cursors component when
cursors move as part of a larger change, such as typing text.
When the target of a mousewheel event is removed, it breaks velocity
scrolling.

Previously, we were preserving the entire screen range when scrolling
with the mouse wheel, which caused a lot of DOM nodes to accumulate. Now
we only preserve the individual line and line number associated with the
target of the mousewheel event, moving them just off screen below all
the on-screen lines and line numbers. This keeps the number of DOM nodes
limited while retaining velocity effects.
It impacts scrolling performance. We can measure when scrolling comes
to a halt.
Emitting the event *before* update, rather than after. This is because
we read from the DOM after update to measure new characters, which
forces layout, so emitting the event after measuring forces another
layout when the position is updated.
@nathansobo

Copy link
Copy Markdown
Contributor Author

Okay, there's a bit more to do but I'm going to merge this so we can start dogfooding it. Check "Use React Editor" in your config to try it out.

@nathansobo
nathansobo merged this pull request into master Apr 18, 2014
@nathansobo
nathansobo deleted the ns-react-editor-view branch April 18, 2014 00:05
@nathansobo
nathansobo restored the ns-react-editor-view branch April 18, 2014 00:35
@nathansobo
nathansobo deleted the ns-react-editor-view branch April 18, 2014 00:39
@nathansobo
nathansobo restored the ns-react-editor-view branch April 18, 2014 00:50
@nathansobo

Copy link
Copy Markdown
Contributor Author

Okay, I regretted merging this and force pushed it back. It's not ready to be tested yet. Lots of minor but frustrating issues are preventing it from really being usable.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants