浏览代码

Added: model.Selection API to save marker names in which Selection is placed.

Szymon Cofalik 9 年之前
父节点
当前提交
09ea7b79b1
共有 2 个文件被更改,包括 92 次插入1 次删除
  1. 48 1
      packages/ckeditor5-engine/src/model/selection.js
  2. 44 0
      packages/ckeditor5-engine/tests/model/selection.js

+ 48 - 1
packages/ckeditor5-engine/src/model/selection.js

@@ -46,9 +46,17 @@ export default class Selection {
 		 * List of attributes set on current selection.
 		 *
 		 * @protected
-		 * @member {Map} module:engine/model/liveselection~LiveSelection#_attrs
+		 * @member {Map} module:engine/model/selection~Selection#_attrs
 		 */
 		this._attrs = new Map();
+
+		/**
+		 * List of markers in which the current selection is placed.
+		 *
+		 * @protected
+		 * @member {Set} module:engine/model/selection~Selection#_markers
+		 */
+		this._markers = new Set();
 	}
 
 	/**
@@ -543,6 +551,45 @@ export default class Selection {
 	}
 
 	/**
+	 * Returns iterator that iterates over names of all markers which contain this selection.
+	 *
+	 * **Note:** In case of `Selection`, you have to manage list of those markers on your own. Use {@link ~addMarker},
+	 * {@link ~removeMarker} and {@link ~clearMarkers} methods. {@link module:engine/model/liveselection~LiveSelection LiveSelection}
+	 * manages list of those markers on it's own.
+	 *
+	 * @returns {Iterator.<String>}
+	 */
+	getMarkers() {
+		return this._markers.values();
+	}
+
+	/**
+	 * Adds name of a marker which contains this selection.
+	 *
+	 * @param {String} markerName Marker name to add.
+	 */
+	addMarker( markerName ) {
+		this._markers.add( markerName );
+	}
+
+	/**
+	 * Removes name of a marker from the list of markers which contains this selection.
+	 *
+	 * @param {String} markerName Marker name to remove.
+	 * @returns {Boolean} `true` if marker name was on the list and was removed, `false` otherwise.
+	 */
+	removeMarker( markerName ) {
+		return this._markers.delete( markerName );
+	}
+
+	/**
+	 * Removes all names from the list of markers which contains this selection.
+	 */
+	clearMarkers() {
+		this._markers.clear();
+	}
+
+	/**
 	 * Returns the selected element. {@link module:engine/model/element~Element Element} is considered as selected if there is only
 	 * one range in the selection, and that range contains exactly one element.
 	 * Returns `null` if there is no selected element.

+ 44 - 0
packages/ckeditor5-engine/tests/model/selection.js

@@ -987,6 +987,50 @@ describe( 'Selection', () => {
 		} );
 	} );
 
+	describe( 'markers interface', () => {
+		it( 'getMarkers should not iterate over anything if no markers were added', () => {
+			expect( Array.from( selection.getMarkers() ) ).to.deep.equal( [] );
+		} );
+
+		it( 'addMarker should add marker name to the list of markers containing selection', () => {
+			selection.addMarker( 'name' );
+			selection.addMarker( 'name2' );
+			selection.addMarker( 'name' ); // Duplicates should be filtered.
+
+			expect( Array.from( selection.getMarkers() ) ).to.deep.equal( [ 'name', 'name2' ] );
+		} );
+
+		it( 'removeMarker should remove marker name from the list of markers containing selection', () => {
+			let result;
+
+			selection.addMarker( 'name' );
+			selection.addMarker( 'name2' );
+
+			result = selection.removeMarker( 'name' );
+
+			expect( result ).to.be.true;
+			expect( Array.from( selection.getMarkers() ) ).to.deep.equal( [ 'name2' ] );
+
+			result = selection.removeMarker( 'name2' );
+
+			expect( result ).to.be.true;
+			expect( Array.from( selection.getMarkers() ) ).to.deep.equal( [] );
+
+			result = selection.removeMarker( 'name2' );
+
+			expect( result ).to.be.false;
+		} );
+
+		it( 'clearMarkers should remove all marker names from the list of markers containing selection', () => {
+			selection.addMarker( 'name' );
+			selection.addMarker( 'name2' );
+
+			selection.clearMarkers();
+
+			expect( Array.from( selection.getMarkers() ) ).to.deep.equal( [] );
+		} );
+	} );
+
 	describe( 'getSelectedElement', () => {
 		let schema;