From d7ba07f139e3be3c2f4d4c98f569600b10906c04 Mon Sep 17 00:00:00 2001 From: Eric Mannes Date: Mon, 3 Aug 2015 07:33:39 -0700 Subject: [PATCH] Scrolls to cursor if not visible after enter/delete/backspace pressed, fixing issue #433 --- src/core/editor.coffee | 13 +++++++++++++ src/modules/keyboard.coffee | 2 ++ test/unit/core/editor.coffee | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/core/editor.coffee b/src/core/editor.coffee index 6736f61a64..7cfcf3f4f4 100644 --- a/src/core/editor.coffee +++ b/src/core/editor.coffee @@ -109,6 +109,19 @@ class Editor top: bounds.top - containerBounds.top } + scrollCursorIntoView: () -> + return unless @selection.range + startBounds = this.getBounds(@selection.range.start) + endBounds = this.getBounds(@selection.range.end) + containerBounds = @root.parentNode.getBoundingClientRect() + containerHeight = containerBounds.bottom - containerBounds.top + if containerHeight < endBounds.top + endBounds.height + [line, offset] = @doc.findLineAt(@selection.range.end) + line.node.scrollIntoView(false) + else if startBounds.top < 0 + [line, offset] = @doc.findLineAt(@selection.range.start) + line.node.scrollIntoView() + _deleteAt: (index, length) -> return if length <= 0 @selection.shiftAfter(index, -1 * length, => diff --git a/src/modules/keyboard.coffee b/src/modules/keyboard.coffee index 8118d80f99..980c4cee5d 100644 --- a/src/modules/keyboard.coffee +++ b/src/modules/keyboard.coffee @@ -60,6 +60,7 @@ class Keyboard @toolbar.setActive(format, value) if @toolbar? return ) + @quill.editor.scrollCursorIntoView() return false ) @@ -78,6 +79,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.scrollCursorIntoView() return false ) diff --git a/test/unit/core/editor.coffee b/test/unit/core/editor.coffee index 825b0b271c..f111a8615e 100644 --- a/test/unit/core/editor.coffee +++ b/test/unit/core/editor.coffee @@ -395,4 +395,33 @@ describe('Editor', -> ) ) ) + + describe('scrollCursorIntoView()', -> + beforeEach( -> + @editor.root.style.height = "50px" + @editor.root.style.overflow = "auto" + @editor.root.innerHTML = '
a
b
c
d
' + ) + + it('scrolls down when cursor too low', -> + bounds = @editor.getBounds(7) + expect(bounds.top).toBeGreaterThan(100) + @editor.selection.setRange(new Quill.Lib.Range(7, 7)) + @editor.scrollCursorIntoView() + bounds = @editor.getBounds(7) + expect(bounds.top).not.toBeLessThan(0) + expect(bounds.top + bounds.height).not.toBeGreaterThan(100) + ) + + it('scrolls up when cursor too high', -> + @editor.root.scrollTop = 50 + bounds = @editor.getBounds(1) + expect(bounds.top + bounds.height).toBeLessThan(0) + @editor.selection.setRange(new Quill.Lib.Range(1, 1)) + @editor.scrollCursorIntoView() + bounds = @editor.getBounds(1) + expect(bounds.top).not.toBeLessThan(0) + expect(bounds.top + bounds.height).not.toBeGreaterThan(100) + ) + ) )