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

Commit

Permalink
Refactor internal helpers in DocumentSelection#_fixGraveyardSelection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Mar 31, 2020
1 parent 1d9d8ce commit ac8078d
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,14 +1137,16 @@ class LiveSelection extends Selection {
liveRange.detach();

// If nearest valid selection range has been found - add it in the place of old range.
if ( selectionRange && !isIntersecting( this._ranges, selectionRange ) ) {
// If range is intersecting with other selection ranges then it is probably due to contents
// of a multi-range selection being removed. See ckeditor/ckeditor5#6501.
if ( selectionRange && !isRangeIntersectingWithSelection( selectionRange, this ) ) {
// Check the range, convert it to live range, bind events, etc.
const newRange = this._prepareRange( selectionRange );

// Add new range in the place of old range.
this._ranges.splice( index, 0, newRange );
}
// If nearest valid selection range cannot be found - just removing the old range is fine.
// If nearest valid selection range cannot be found or is intersecting with other selection ranges removing the old range is fine.
}
}

Expand All @@ -1164,7 +1166,6 @@ function getAttrsIfCharacter( node ) {

// Removes selection attributes from element which is not empty anymore.
//
// @private
// @param {module:engine/model/model~Model} model
// @param {module:engine/model/batch~Batch} batch
function clearAttributesStoredInElement( model, batch ) {
Expand All @@ -1191,14 +1192,7 @@ function clearAttributesStoredInElement( model, batch ) {
}
}

// Checks if range intersects with any given ranges.
function isIntersecting( ranges, selectionRange ) {
for ( let i = 0; i < ranges.length; i++ ) {
if ( selectionRange.isIntersecting( ranges[ i ] ) ) {
// It is also fine - as we can have multiple ranges in the selection.
return true;
}
}

return false;
// Checks if range intersects with any of selection ranges.
function isRangeIntersectingWithSelection( range, selection ) {
return !selection._ranges.every( selectionRange => !range.isIntersecting( selectionRange ) );
}

0 comments on commit ac8078d

Please sign in to comment.