diff --git a/src/core/selection.coffee b/src/core/selection.coffee index 851f0fb6f4..e2103ecbb1 100644 --- a/src/core/selection.coffee +++ b/src/core/selection.coffee @@ -42,6 +42,20 @@ class Selection else fn() + scrollIntoView: () -> + return unless @range + editor = @emitter.editor + startBounds = editor.getBounds(@range.start) + endBounds = if @range.isCollapsed() then startBounds else editor.getBounds(@range.end) + containerBounds = editor.root.parentNode.getBoundingClientRect() + containerHeight = containerBounds.bottom - containerBounds.top + if containerHeight < endBounds.top + endBounds.height + [line, offset] = editor.doc.findLineAt(@range.end) + line.node.scrollIntoView(false) + else if startBounds.top < 0 + [line, offset] = editor.doc.findLineAt(@range.start) + line.node.scrollIntoView() + setRange: (range, source) -> if range? [startNode, startOffset] = this._indexToPosition(range.start) diff --git a/src/modules/keyboard.coffee b/src/modules/keyboard.coffee index 66b6c76fd6..19d152b205 100644 --- a/src/modules/keyboard.coffee +++ b/src/modules/keyboard.coffee @@ -73,6 +73,7 @@ class Keyboard @toolbar.setActive(format, value) if @toolbar? return ) + @quill.editor.selection.scrollIntoView() return false ) @@ -91,6 +92,7 @@ class Keyboard @quill.deleteText(range.start - 1, range.start, Quill.sources.USER) else if range.start < @quill.getLength() - 1 @quill.deleteText(range.start, range.start + 1, Quill.sources.USER) + @quill.editor.selection.scrollIntoView() return false ) diff --git a/src/modules/paste-manager.coffee b/src/modules/paste-manager.coffee index 67cd5b8177..e141d15993 100644 --- a/src/modules/paste-manager.coffee +++ b/src/modules/paste-manager.coffee @@ -39,10 +39,7 @@ class PasteManager @quill.updateContents(delta, 'user') @quill.setSelection(range.start + lengthAdded, range.start + lengthAdded) # Make sure bottom of pasted content is visible - [line, offset] = @quill.editor.doc.findLineAt(range.start + lengthAdded) - lineBottom = line.node.getBoundingClientRect().bottom - windowBottom = document.documentElement.clientHeight - line.node.scrollIntoView(false) if lineBottom > windowBottom + @quill.editor.selection.scrollIntoView() @container.innerHTML = "" ) diff --git a/test/unit/core/selection.coffee b/test/unit/core/selection.coffee index ef48a0b182..3e46f96a70 100644 --- a/test/unit/core/selection.coffee +++ b/test/unit/core/selection.coffee @@ -324,4 +324,49 @@ describe('Selection', -> ) ) ) + + describe('scrollIntoView()', -> + beforeEach( -> + @container.innerHTML = '