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

Commit 152bdab

Browse files
authoredMar 25, 2020
Merge pull request #1833 from ckeditor/i/6485
Feature: Introduced `View#hasDomSelection`. Closes ckeditor/ckeditor5#6485.
2 parents 373a0fb + 7394596 commit 152bdab

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed
 

‎src/view/observer/selectionobserver.js

+4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ export default class SelectionObserver extends Observer {
143143
// It means that the DOM selection is in some way incorrect. Ranges that were in the DOM selection could not be
144144
// converted to the view. This happens when the DOM selection was moved outside of the editable element.
145145
if ( newViewSelection.rangeCount == 0 ) {
146+
this.view.hasDomSelection = false;
147+
146148
return;
147149
}
148150

151+
this.view.hasDomSelection = true;
152+
149153
if ( this.selection.isEqual( newViewSelection ) && this.domConverter.isDomSelectionCorrect( domSelection ) ) {
150154
return;
151155
}

‎src/view/view.js

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ export default class View {
100100
*/
101101
this.set( 'isRenderingInProgress', false );
102102

103+
/**
104+
* Informs whether the DOM selection is inside any of the DOM roots managed by the view.
105+
*
106+
* @readonly
107+
* @member {Boolean} #hasDomSelection
108+
*/
109+
this.set( 'hasDomSelection', false );
110+
103111
/**
104112
* Instance of the {@link module:engine/view/renderer~Renderer renderer}.
105113
*

‎tests/view/view/view.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
44
*/
55

6-
/* globals document, console */
6+
/* globals document, console, setTimeout */
77

88
import View from '../../../src/view/view';
99
import Observer from '../../../src/view/observer/observer';
@@ -514,6 +514,64 @@ describe( 'view', () => {
514514
} );
515515
} );
516516

517+
describe( 'hasDomSelection', () => {
518+
let domElement, domP, domSelection;
519+
520+
beforeEach( () => {
521+
const viewRoot = createViewRoot( viewDocument, 'div', 'main' );
522+
523+
view.attachDomRoot( domRoot );
524+
525+
viewRoot._appendChild( new ViewElement( viewDocument, 'p' ) );
526+
view.forceRender();
527+
528+
domElement = createElement( document, 'div', { contenteditable: 'true' } );
529+
document.body.appendChild( domElement );
530+
531+
domSelection = document.getSelection();
532+
domP = domRoot.childNodes[ 0 ];
533+
} );
534+
535+
afterEach( () => {
536+
domElement.remove();
537+
} );
538+
539+
it( 'should be true if selection is inside a DOM root element', done => {
540+
domSelection.collapse( domP, 0 );
541+
542+
// Wait for async selectionchange event on DOM document.
543+
setTimeout( () => {
544+
expect( view.hasDomSelection ).to.be.true;
545+
546+
done();
547+
}, 100 );
548+
} );
549+
550+
it( 'should be true if selection is inside a DOM root element - no focus', done => {
551+
domSelection.collapse( domP, 0 );
552+
domRoot.blur();
553+
554+
// Wait for async selectionchange event on DOM document.
555+
setTimeout( () => {
556+
expect( view.hasDomSelection ).to.be.true;
557+
expect( view.document.isFocused ).to.be.false;
558+
559+
done();
560+
}, 100 );
561+
} );
562+
563+
it( 'should be false if selection is outside DOM root element', done => {
564+
domSelection.collapse( domElement, 0 );
565+
566+
// Wait for async selectionchange event on DOM document.
567+
setTimeout( () => {
568+
expect( view.hasDomSelection ).to.be.false;
569+
570+
done();
571+
}, 100 );
572+
} );
573+
} );
574+
517575
describe( 'forceRender()', () => {
518576
it( 'disable observers, renders and enable observers', () => {
519577
const observerMock = view.addObserver( ObserverMock );

0 commit comments

Comments
 (0)
This repository has been archived.