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

Commit

Permalink
Internal: Extracted fake selection container from the _updateFakeSele…
Browse files Browse the repository at this point in the history
…ction method.

Selection binding is now done also for fake selection that do not need DOM re-render. Otherwise there's a regression: #1792 (comment).
  • Loading branch information
mlewand committed Sep 10, 2019
1 parent 6fbda53 commit 473248a
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/view/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,29 +691,14 @@ export default class Renderer {
*/
_updateFakeSelection( domRoot ) {
const domDocument = domRoot.ownerDocument;
let container = this._fakeSelectionContainer;

if ( !this._fakeSelectionNeedsUpdate( domRoot ) ) {
// Container did not change, but the selection might point a different element with same fake selection.
// See https://github.com/ckeditor/ckeditor5-engine/pull/1792#issuecomment-529814641.
this.domConverter.bindFakeSelection( container, this.selection );
return;
}

// Create fake selection container if one does not exist.
if ( !container ) {
this._fakeSelectionContainer = container = domDocument.createElement( 'div' );
const container = this._fakeSelectionContainer = this._fakeSelectionContainer || createFakeSelectionContainer( domDocument );

Object.assign( container.style, {
position: 'fixed',
top: 0,
left: '-9999px',
// See https://github.com/ckeditor/ckeditor5/issues/752.
width: '42px'
} );
// Bind fake selection container with current selection.
this.domConverter.bindFakeSelection( container, this.selection );

// Fill it with a text node so we can update it later.
container.textContent = '\u00A0';
if ( !this._fakeSelectionNeedsUpdate( domRoot ) ) {
return;
}

if ( !container.parentElement || container.parentElement != domRoot ) {
Expand All @@ -730,9 +715,6 @@ export default class Renderer {
domSelection.removeAllRanges();
domRange.selectNodeContents( container );
domSelection.addRange( domRange );

// Bind fake selection container with current selection.
this.domConverter.bindFakeSelection( container, this.selection );
}

/**
Expand Down Expand Up @@ -1005,3 +987,25 @@ function filterOutFakeSelectionContainer( domChildList, fakeSelectionContainer )

return childList;
}

// Creates a fake selection container for a given document.
//
// @private
// @param {Document} domDocument
// @returns {HTMLElement}
function createFakeSelectionContainer( domDocument ) {
const container = domDocument.createElement( 'div' );

Object.assign( container.style, {
position: 'fixed',
top: 0,
left: '-9999px',
// See https://github.com/ckeditor/ckeditor5/issues/752.
width: '42px'
} );

// Fill it with a text node so we can update it later.
container.textContent = '\u00A0';

return container;
}

0 comments on commit 473248a

Please sign in to comment.