Skip to content

Commit

Permalink
fix: scroll follow on paste (fix #970)
Browse files Browse the repository at this point in the history
pr #995
  • Loading branch information
최규우/FE개발랩/NE authored and seonim-ryu committed Jan 6, 2020
1 parent bfaf470 commit 59935f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 13 additions & 4 deletions apps/editor/src/js/wysiwygEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class WysiwygEditor {
_initEvent() {
this.eventManager.listen('wysiwygSetValueBefore', html => this._preprocessForInlineElement(html));
this.eventManager.listen('wysiwygKeyEvent', ev => this._runKeyEventHandlers(ev.data, ev.keyMap));
this.eventManager.listen('wysiwygRangeChangeAfter', () => this._scrollToRangeIfNeed());
}

/**
Expand Down Expand Up @@ -517,11 +518,16 @@ class WysiwygEditor {
* @private
*/
_scrollToRangeIfNeed() {
const $editorContainerEl = this.$editorContainerEl;
const range = this.getRange();
const cursorTop = this.getEditor().getCursorPosition(range).top - this.$editorContainerEl.offset().top;
const cursorTop = this.getEditor().getCursorPosition(range).top - $editorContainerEl.offset().top;

if (cursorTop >= this.get$Body().height()) {
range.endContainer.scrollIntoView();
if (cursorTop >= $editorContainerEl.height()) {
let target = range.endContainer;
if (!(target instanceof Element)) {
target = target.parentNode;
}
target.scrollIntoView(false);
}
}

Expand Down Expand Up @@ -973,7 +979,10 @@ class WysiwygEditor {
*/
moveCursorToEnd() {
this.getEditor().moveCursorToEnd();
this.scrollTop(this.$editorContainerEl.height());
const contentNodes = this.get$Body().get(0).childNodes;
if (contentNodes.length > 0) {
contentNodes[contentNodes.length - 1].scrollIntoView(false);
}
this._correctRangeAfterMoveCursor('end');
}

Expand Down
10 changes: 10 additions & 0 deletions apps/editor/test/wysiwygEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ describe('WysiwygEditor', () => {
});
});

it('scroll if needed on wysiwygRangeChangeAfter', () => {
wwe.setHeight(30);
wwe.setValue('a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>');
wwe.scrollTop(0);

em.emit('wysiwygRangeChangeAfter');

expect(wwe.scrollTop()).not.toEqual(0);
});

describe('get current range', () => {
it('get range', () => {
const range = wwe.getEditor().getSelection();
Expand Down

0 comments on commit 59935f3

Please sign in to comment.