Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1057 from ckeditor/t/660
Browse files Browse the repository at this point in the history
Feature: Implemented `view.Document#scrollToTheSelection()` method. Closes #660.
  • Loading branch information
Reinmar authored Aug 15, 2017
2 parents 1902d7a + 630bbb8 commit 4479c40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/view/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import KeyObserver from './observer/keyobserver';
import FakeSelectionObserver from './observer/fakeselectionobserver';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import { scrollViewportToShowTarget } from '@ckeditor/ckeditor5-utils/src/dom/scroll';

/**
* Document class creates an abstract layer over the content editable area.
Expand Down Expand Up @@ -301,6 +302,21 @@ export default class Document {
}
}

/**
* Scrolls the page viewport and {@link #domRoots} with their ancestors to reveal the
* caret, if not already visible to the user.
*/
scrollToTheSelection() {
const range = this.selection.getFirstRange();

if ( range ) {
scrollViewportToShowTarget( {
target: this.domConverter.viewRangeToDom( range ),
viewportOffset: 20
} );
}
}

/**
* Disables all added observers.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/view/document/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import DomConverter from '../../../src/view/domconverter';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import count from '@ckeditor/ckeditor5-utils/src/count';
import log from '@ckeditor/ckeditor5-utils/src/log';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';

testUtils.createSinonSandbox();

Expand Down Expand Up @@ -323,6 +324,26 @@ describe( 'Document', () => {
} );
} );

describe( 'scrollToTheSelection()', () => {
it( 'does nothing when there are no ranges in the selection', () => {
const stub = testUtils.sinon.stub( global.window, 'scrollTo' );

viewDocument.scrollToTheSelection();
sinon.assert.notCalled( stub );
} );

it( 'scrolls to the first range in selection with an offset', () => {
const stub = testUtils.sinon.stub( global.window, 'scrollTo' );
const root = viewDocument.createRoot( document.createElement( 'div' ) );
const range = ViewRange.createIn( root );

viewDocument.selection.addRange( range );

viewDocument.scrollToTheSelection();
sinon.assert.calledWithMatch( stub, sinon.match.number, sinon.match.number );
} );
} );

describe( 'disableObservers()', () => {
it( 'should disable observers', () => {
const addedObserverMock = viewDocument.addObserver( ObserverMock );
Expand Down

0 comments on commit 4479c40

Please sign in to comment.