8
0
Просмотр исходного кода

Implemented the scrollViewportToShowTarget and scrollAncestorsToShowTarget helpers.

Aleksander Nowodzinski 8 лет назад
Родитель
Сommit
e1dddc5d02
2 измененных файлов с 573 добавлено и 0 удалено
  1. 159 0
      packages/ckeditor5-utils/src/dom/scroll.js
  2. 414 0
      packages/ckeditor5-utils/tests/dom/scroll.js

+ 159 - 0
packages/ckeditor5-utils/src/dom/scroll.js

@@ -0,0 +1,159 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global Node */
+
+/**
+ * @module utils/dom/scroll
+ */
+
+import global from './global';
+import isRange from './isrange';
+import Rect from './rect';
+
+/**
+ * Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
+ * This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
+ * the user. If the `target` is already visible, nothing will happen.
+ *
+ * @param {HTMLElement|Range} options.target A target, which supposed to become visible to the user.
+ * @param {Number} [options.viewportOffset] An offset from the edge of the viewport (in pixels)
+ * the `target` will be moved by when the viewport is scrolled. It enhances the user experience
+ * by keeping the `target` some distance from the edge of the viewport and thus making it easier to
+ * read or edit by the user.
+ */
+export function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
+	// Scroll the ancestors of the target to reveal it first, then focus on scrolling
+	// the viewport, when the position of the target is fixed.
+	scrollAncestorsToShowTarget( target );
+
+	const targetRect = new Rect( target );
+	const targetShiftedDownRect = targetRect.clone().moveBy( 0, viewportOffset );
+	const targetShiftedUpRect = targetRect.clone().moveBy( 0, -viewportOffset );
+	const viewportRect = Rect.getViewportRect( {
+		excludeScrollbars: true
+	} );
+
+	// Avoid the situation where the caret is still in the viewport, but totally
+	// at the edge. If it moved beyond the viewport in the next action e.g. due to enter,
+	// the scrolling would move it to the viewportOffset level and it all would look like the
+	// caret visually moved up/down.
+	//
+	// To prevent this, we're checking the rects moved by the viewportOffset to cover
+	// the upper/lower edge of the viewport.
+	const rects = [ targetShiftedUpRect, targetShiftedDownRect ];
+
+	if ( !rects.every( rect => viewportRect.contains( rect ) ) ) {
+		let { scrollX, scrollY } = global.window;
+
+		if ( isAbove( targetShiftedUpRect, viewportRect ) ) {
+			scrollY -= viewportRect.top - targetRect.top + viewportOffset;
+		} else if ( isBelow( targetShiftedDownRect, viewportRect ) ) {
+			scrollY += targetRect.bottom - viewportRect.bottom + viewportOffset;
+		}
+
+		// TODO: Web browsers scroll natively to place the target in the middle
+		// of the viewport. It's not a very popular case, though.
+		if ( isLeftOf( targetRect, viewportRect ) ) {
+			scrollX -= viewportRect.left - targetRect.left + viewportOffset;
+		} else if ( isRightOf( targetRect, viewportRect ) ) {
+			scrollX += targetRect.right - viewportRect.right + viewportOffset;
+		}
+
+		global.window.scrollTo( scrollX, scrollY );
+	}
+}
+
+/**
+ * Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
+ * e.g. if they have `overflow: scroll` CSS style.
+ *
+ * @param {HTMLElement|Range} target A target, which supposed to become visible to the user.
+ */
+export function scrollAncestorsToShowTarget( target ) {
+	let parent, parentRect, targetRect;
+
+	if ( isRange( target ) ) {
+		parent = target.commonAncestorContainer;
+
+		// If a Range is attached to the Text, use the closest element ancestor.
+		if ( parent.nodeType == Node.TEXT_NODE ) {
+			parent = parent.parentNode;
+		}
+	} else {
+		parent = target.parentNode;
+	}
+
+	do {
+		if ( parent === global.document.body ) {
+			return;
+		}
+
+		targetRect = new Rect( target );
+		parentRect = new Rect( parent, {
+			excludeScrollbarsAndBorders: true
+		} );
+
+		if ( !parentRect.contains( targetRect ) ) {
+			scrollAncestorToShowTarget( { targetRect, parent, parentRect } );
+		}
+	} while ( ( parent = parent.parentNode ) );
+}
+
+// Makes any page `HTMLElement` or `Range` (target) visible within its parent.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} options.targetRect The `Rect` of the `target`.
+// @param {HTMLElement} options.parent The parent element of the `target`.
+// @param {module:utils/dom/rect~Rect} options.parentRect The `Rect` of the parent.
+function scrollAncestorToShowTarget( { targetRect, parent, parentRect } ) {
+	if ( isAbove( targetRect, parentRect ) ) {
+		parent.scrollTop -= parentRect.top - targetRect.top;
+	} else if ( isBelow( targetRect, parentRect ) ) {
+		parent.scrollTop += targetRect.bottom - parentRect.bottom;
+	}
+
+	if ( isLeftOf( targetRect, parentRect ) ) {
+		parent.scrollLeft -= parentRect.left - targetRect.left;
+	} else if ( isRightOf( targetRect, parentRect ) ) {
+		parent.scrollLeft += targetRect.right - parentRect.right;
+	}
+}
+
+// Determines if a given `Rect` extends beyond the bottom edge of the second `Rect`.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} firstRect
+// @param {module:utils/dom/rect~Rect} secondRect
+function isBelow( firstRect, secondRect ) {
+	return firstRect.bottom > secondRect.bottom;
+}
+
+// Determines if a given `Rect` extends beyond the top edge of the second `Rect`.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} firstRect
+// @param {module:utils/dom/rect~Rect} secondRect
+function isAbove( firstRect, secondRect ) {
+	return firstRect.top < secondRect.top;
+}
+
+// Determines if a given `Rect` extends beyond the left edge of the second `Rect`.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} firstRect
+// @param {module:utils/dom/rect~Rect} secondRect
+function isLeftOf( firstRect, secondRect ) {
+	return firstRect.left < secondRect.left;
+}
+
+// Determines if a given `Rect` extends beyond the right edge of the second `Rect`.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} firstRect
+// @param {module:utils/dom/rect~Rect} secondRect
+function isRightOf( firstRect, secondRect ) {
+	return firstRect.right > secondRect.right;
+}

+ 414 - 0
packages/ckeditor5-utils/tests/dom/scroll.js

@@ -0,0 +1,414 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global document, Text */
+
+import global from '../../src/dom/global';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import isRange from '../../src/dom/isrange';
+import {
+	scrollViewportToShowTarget,
+	scrollAncestorsToShowTarget
+} from '../../src/dom/scroll';
+
+testUtils.createSinonSandbox();
+
+describe( 'scrollAncestorsToShowTarget()', () => {
+	let target, element, firstAncestor, secondAncestor, body;
+
+	beforeEach( () => {
+		element = document.createElement( 'p' );
+		firstAncestor = document.createElement( 'blockquote' );
+		secondAncestor = document.createElement( 'div' );
+		body = document.createElement( 'div' );
+
+		testUtils.sinon.stub( global, 'document', { body } );
+
+		document.body.appendChild( body );
+		body.appendChild( secondAncestor );
+		secondAncestor.appendChild( firstAncestor );
+		firstAncestor.appendChild( element );
+
+		stubRect( firstAncestor, {
+			top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
+		}, {
+			scrollLeft: 100, scrollTop: 100
+		} );
+
+		stubRect( secondAncestor, {
+			top: -100, right: 0, bottom: 0, left: -100, width: 100, height: 100
+		}, {
+			scrollLeft: 100, scrollTop: 100
+		} );
+
+		stubRect( body, {
+			top: 1000, right: 2000, bottom: 1000, left: 1000, width: 1000, height: 1000
+		}, {
+			scrollLeft: 1000, scrollTop: 1000
+		} );
+	} );
+
+	afterEach( () => {
+		body.remove();
+	} );
+
+	describe( 'for an HTMLElement', () => {
+		beforeEach( () => {
+			target = element;
+		} );
+
+		test();
+	} );
+
+	describe( 'for a DOM Range', () => {
+		beforeEach( () => {
+			target = document.createRange();
+			target.setStart( firstAncestor, 0 );
+			target.setEnd( firstAncestor, 0 );
+		} );
+
+		test();
+
+		it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above, attached to the Text)', () => {
+			const text = new Text( 'foo' );
+			firstAncestor.appendChild( text );
+			target.setStart( text, 1 );
+			target.setEnd( text, 2 );
+
+			stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
+		} );
+	} );
+
+	function test() {
+		it( 'should not touch the #scrollTop #scrollLeft of the ancestor if target is visible', () => {
+			stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollLeft: 100, scrollTop: 100 } );
+		} );
+
+		it( 'should not touch the #scrollTop #scrollLeft of the document.body', () => {
+			stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( body, { scrollLeft: 1000, scrollTop: 1000 } );
+		} );
+
+		it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above)', () => {
+			stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
+		} );
+
+		it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (below)', () => {
+			stubRect( target, { top: 200, right: 75, bottom: 300, left: 25, width: 50, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 300, scrollLeft: 100 } );
+		} );
+
+		it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (left of)', () => {
+			stubRect( target, { top: 0, right: 0, bottom: 100, left: -100, width: 100, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 0 } );
+		} );
+
+		it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (right of)', () => {
+			stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
+		} );
+
+		it( 'should set #scrollTop and #scrollLeft of all the ancestors', () => {
+			stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
+
+			scrollAncestorsToShowTarget( target );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
+			// Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
+			// the getBoundingClientRect geometry of the target. That's why scrolling secondAncestor
+			// works like the target remained in the original position and hence scrollLeft is 300 instead
+			// of 200.
+			assertScrollPosition( secondAncestor, { scrollTop: 200, scrollLeft: 300 } );
+		} );
+	}
+} );
+
+describe( 'scrollViewportToShowTarget()', () => {
+	let target, firstAncestor, element;
+	const viewportOffset = 30;
+
+	beforeEach( () => {
+		element = document.createElement( 'p' );
+		firstAncestor = document.createElement( 'blockquote' );
+
+		document.body.appendChild( firstAncestor );
+		firstAncestor.appendChild( element );
+
+		stubRect( firstAncestor, {
+			top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
+		}, {
+			scrollLeft: 100, scrollTop: 100
+		} );
+
+		testUtils.sinon.stub( global, 'window', {
+			innerWidth: 1000,
+			innerHeight: 500,
+			scrollX: 100,
+			scrollY: 100,
+			scrollTo: sinon.spy(),
+			getComputedStyle: () => ( {
+				borderTopWidth: '0px',
+				borderRightWidth: '0px',
+				borderBottomWidth: '0px',
+				borderLeftWidth: '0px'
+			} )
+		} );
+
+		// Assuming 20px v- and h-scrollbars here.
+		testUtils.sinon.stub( global, 'document', {
+			body: document.body,
+			documentElement: {
+				clientWidth: 980,
+				clientHeight: 480
+			}
+		} );
+	} );
+
+	afterEach( () => {
+		firstAncestor.remove();
+	} );
+
+	describe( 'for an HTMLElement', () => {
+		beforeEach( () => {
+			target = element;
+		} );
+
+		testNoOffset();
+
+		describe( 'with a viewportOffset', () => {
+			testWithOffset();
+		} );
+	} );
+
+	describe( 'for a DOM Range', () => {
+		beforeEach( () => {
+			target = document.createRange();
+			target.setStart( firstAncestor, 0 );
+			target.setEnd( firstAncestor, 0 );
+		} );
+
+		testNoOffset();
+
+		describe( 'with a viewportOffset', () => {
+			testWithOffset();
+		} );
+	} );
+
+	// Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
+	// the getBoundingClientRect geometry of the target. That's why scrolling the viewport
+	// works like the target remained in the original position. It's tricky but much faster
+	// and still shows that the whole thing works as expected.
+	//
+	// Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
+	// initial geometry and scroll position they give the right, relative picture of what's going on.
+	function testNoOffset() {
+		it( 'does not scroll the viewport when the target is fully visible', () => {
+			stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
+			sinon.assert.notCalled( global.window.scrollTo );
+		} );
+
+		it( 'scrolls the viewport to show the target (above)', () => {
+			stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, -100 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially above)', () => {
+			stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 50 );
+		} );
+
+		it( 'scrolls the viewport to show the target (below)', () => {
+			stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 320 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially below)', () => {
+			stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 170 );
+		} );
+
+		it( 'scrolls the viewport to show the target (to the left)', () => {
+			stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, -100, 100 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially to the left)', () => {
+			stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 50, 100 );
+		} );
+
+		it( 'scrolls the viewport to show the target (to the right)', () => {
+			stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 320, 100 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially to the right)', () => {
+			stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 170, 100 );
+		} );
+	}
+
+	// Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
+	// the getBoundingClientRect geometry of the target. That's why scrolling the viewport
+	// works like the target remained in the original position. It's tricky but much faster
+	// and still shows that the whole thing works as expected.
+	//
+	// Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
+	// initial geometry and scroll position they give the right, relative picture of what's going on.
+	function testWithOffset() {
+		it( 'does not scroll the viewport when the target is fully visible', () => {
+			stubRect( target, { top: 50, right: 200, bottom: 150, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 150, scrollLeft: 200 } );
+			sinon.assert.notCalled( global.window.scrollTo );
+		} );
+
+		it( 'scrolls the viewport to show the target (above)', () => {
+			stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, -130 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially above)', () => {
+			stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 20 );
+		} );
+
+		it( 'scrolls the viewport to show the target (below)', () => {
+			stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 350 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially below)', () => {
+			stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 100, 200 );
+		} );
+
+		it( 'scrolls the viewport to show the target (to the left)', () => {
+			stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, -130, 70 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially to the left)', () => {
+			stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 20, 70 );
+		} );
+
+		it( 'scrolls the viewport to show the target (to the right)', () => {
+			stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 350, 70 );
+		} );
+
+		it( 'scrolls the viewport to show the target (partially to the right)', () => {
+			stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
+			sinon.assert.calledWithExactly( global.window.scrollTo, 200, 70 );
+		} );
+	}
+} );
+
+function stubRect( target, geometryStub, scrollStub ) {
+	if ( isRange( target ) ) {
+		testUtils.sinon.stub( target, 'getClientRects' ).returns( [ geometryStub ] );
+	} else {
+		testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( geometryStub );
+	}
+
+	if ( scrollStub ) {
+		let { scrollLeft, scrollTop } = scrollStub;
+
+		// There's no way to stub scrollLeft|Top with Sinon. defineProperties
+		// must be used instead.
+		Object.defineProperties( target, {
+			scrollLeft: {
+				get() {
+					return scrollLeft;
+				},
+				set( value ) {
+					scrollLeft = value;
+				}
+			},
+			scrollTop: {
+				get() {
+					return scrollTop;
+				},
+				set( value ) {
+					scrollTop = value;
+				}
+			}
+		} );
+	}
+}
+
+function assertScrollPosition( element, expected ) {
+	expect( element.scrollTop ).to.equal( expected.scrollTop, 'scrollTop' );
+	expect( element.scrollLeft ).to.equal( expected.scrollLeft, 'scrollLeft' );
+}