SwellRT / swellrt

SwellRT main project. Server, JavaScript and Java clients
http://swellrt.org/
Apache License 2.0
234 stars 34 forks source link

Autoscroll after new lines added does not work in editor #204

Closed atfornes closed 7 years ago

atfornes commented 8 years ago

After adding a new line at the end of the screen in a SwellRT pad, the pad is not scrolled to follow the cursor. This is an unexpected behavior.

It works after adding some characters (but not when you add a line break)

see https://github.com/P2Pvalue/teem/issues/326

pablojan commented 7 years ago

I implemented this in the app, not in the editor component due to calculation of whether the cursor is outside of visible area requires info of the actual layout: this is code from jetpad and swellrt beta, let notice constant this.TOP_BAR_OFFSET

note: focusNode is got here from the handler's selection parameter, but it can be retrieved from window.getSelection()[1]

this.editor.setSelectionHandler((range, editor, selection) => {

          if (selection && selection.focusNode) {
            let focusParent = selection.focusNode.parentElement;
            if (focusParent.getBoundingClientRect) {
              let rect = focusParent.getBoundingClientRect();
              if (rect.top > (window.innerHeight - this.TOP_BAR_OFFSET)) {
                focusParent.scrollIntoView();
              }
            }
          }
};

[1] https://developer.mozilla.org/es/docs/Web/API/Selection