Sfoglia il codice sorgente

Introduced isSimilar and getTrimmed methods for view/selection.

Krzysztof Krztoń 8 anni fa
parent
commit
bf4963fe74
1 ha cambiato i file con 32 aggiunte e 0 eliminazioni
  1. 32 0
      packages/ckeditor5-engine/src/view/selection.js

+ 32 - 0
packages/ckeditor5-engine/src/view/selection.js

@@ -330,6 +330,17 @@ export default class Selection {
 		return true;
 	}
 
+	/**
+	 * Checks whether, this selection is similar to given selection. Selections are similar if they are initially
+	 * equal (see {@link #isEqual isEqual}) or if their trimmed (see {@link #getTrimmed getTrimmed}) representations are equal.
+	 *
+	 * @param {module:engine/view/selection~Selection} otherSelection Selection to compare with.
+	 * @returns {Boolean} `true` if selections are similar, `false` otherwise.
+	 */
+	isSimilar( otherSelection ) {
+		return this.isEqual( otherSelection ) || this.getTrimmed().isEqual( otherSelection.getTrimmed() );
+	}
+
 	/**
 	 * Removes all ranges that were added to the selection.
 	 *
@@ -482,6 +493,27 @@ export default class Selection {
 		return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
 	}
 
+	/**
+	 * Returns a copy of a selection with all of its ranges
+	 * trimmed (see {@link module:engine/view/range~Range#getTrimmed getTrimmed}).
+	 *
+	 * @returns {module:engine/view/selection~Selection} Selection with all ranges shrank.
+	 */
+	getTrimmed() {
+		const selection = Selection.createFromSelection( this );
+		const ranges = this.getRanges();
+
+		let trimmedRanges = [];
+
+		for ( let range of ranges ) {
+			trimmedRanges.push( range.getTrimmed() );
+		}
+
+		selection.setRanges( trimmedRanges );
+
+		return selection;
+	}
+
 	/**
 	 * Creates and returns an instance of `Selection` that is a clone of given selection, meaning that it has same
 	 * ranges and same direction as this selection.