Переглянути джерело

Feature: Introduced `DocumentSelection#event:change:marker`.

Szymon Cofalik 6 роки тому
батько
коміт
877e5344b5
1 змінених файлів з 23 додано та 0 видалено
  1. 23 0
      packages/ckeditor5-engine/src/model/documentselection.js

+ 23 - 0
packages/ckeditor5-engine/src/model/documentselection.js

@@ -63,6 +63,7 @@ export default class DocumentSelection {
 
 
 		this._selection.delegate( 'change:range' ).to( this );
 		this._selection.delegate( 'change:range' ).to( this );
 		this._selection.delegate( 'change:attribute' ).to( this );
 		this._selection.delegate( 'change:attribute' ).to( this );
+		this._selection.delegate( 'change:marker' ).to( this );
 	}
 	}
 
 
 	/**
 	/**
@@ -542,6 +543,17 @@ mix( DocumentSelection, EmitterMixin );
  * @param {Array.<String>} attributeKeys Array containing keys of attributes that changed.
  * @param {Array.<String>} attributeKeys Array containing keys of attributes that changed.
  */
  */
 
 
+/**
+ * Fired when selection marker(s) changed.
+ *
+ * @event change:marker
+ * @param {Boolean} directChange This is always set to `false` in case of `change:marker` event as there is no possibility
+ * to change markers directly through {@link module:engine/model/documentselection~DocumentSelection} API.
+ * See also {@link module:engine/model/documentselection~DocumentSelection#event:change:range} and
+ * {@link module:engine/model/documentselection~DocumentSelection#event:change:attribute}.
+ * @param {Array.<module:engine/model/markercollection~Marker>} oldMarkers Markers in which the selection was before the change.
+ */
+
 // `LiveSelection` is used internally by {@link module:engine/model/documentselection~DocumentSelection} and shouldn't be used directly.
 // `LiveSelection` is used internally by {@link module:engine/model/documentselection~DocumentSelection} and shouldn't be used directly.
 //
 //
 // LiveSelection` is automatically updated upon changes in the {@link module:engine/model/document~Document document}
 // LiveSelection` is automatically updated upon changes in the {@link module:engine/model/document~Document document}
@@ -830,6 +842,7 @@ class LiveSelection extends Selection {
 
 
 	_updateMarkers() {
 	_updateMarkers() {
 		const markers = [];
 		const markers = [];
+		let changed = false;
 
 
 		for ( const marker of this._model.markers ) {
 		for ( const marker of this._model.markers ) {
 			const markerRange = marker.getRange();
 			const markerRange = marker.getRange();
@@ -841,17 +854,27 @@ class LiveSelection extends Selection {
 			}
 			}
 		}
 		}
 
 
+		const oldMarkers = Array.from( this.markers );
+
 		for ( const marker of markers ) {
 		for ( const marker of markers ) {
 			if ( !this.markers.has( marker ) ) {
 			if ( !this.markers.has( marker ) ) {
 				this.markers.add( marker );
 				this.markers.add( marker );
+
+				changed = true;
 			}
 			}
 		}
 		}
 
 
 		for ( const marker of Array.from( this.markers ) ) {
 		for ( const marker of Array.from( this.markers ) ) {
 			if ( !markers.includes( marker ) ) {
 			if ( !markers.includes( marker ) ) {
 				this.markers.remove( marker );
 				this.markers.remove( marker );
+
+				changed = true;
 			}
 			}
 		}
 		}
+
+		if ( changed ) {
+			this.fire( 'change:marker', { oldMarkers } );
+		}
 	}
 	}
 
 
 	// Updates this selection attributes according to its ranges and the {@link module:engine/model/document~Document model document}.
 	// Updates this selection attributes according to its ranges and the {@link module:engine/model/document~Document model document}.