Przeglądaj źródła

Added model instance to the Documentselection.

Oskar Wróbel 8 lat temu
rodzic
commit
76c70c36bb

+ 6 - 4
packages/ckeditor5-engine/src/model/document.js

@@ -76,7 +76,7 @@ export default class Document {
 		 * @readonly
 		 * @member {module:engine/model/documentselection~DocumentSelection}
 		 */
-		this.selection = new DocumentSelection( this );
+		this.selection = new DocumentSelection( this, this.model );
 
 		/**
 		 * List of roots that are owned and managed by this document. Use {@link #createRoot} and
@@ -268,8 +268,10 @@ export default class Document {
 	 * @returns {module:engine/model/range~Range|null} Nearest selection range or `null` if one cannot be found.
 	 */
 	getNearestSelectionRange( position, direction = 'both' ) {
+		const schema = this.model.schema;
+
 		// Return collapsed range if provided position is valid.
-		if ( this.model.schema.check( { name: '$text', inside: position } ) ) {
+		if ( schema.check( { name: '$text', inside: position } ) ) {
 			return new Range( position );
 		}
 
@@ -287,11 +289,11 @@ export default class Document {
 			const type = ( data.walker == backwardWalker ? 'elementEnd' : 'elementStart' );
 			const value = data.value;
 
-			if ( value.type == type && this.schema.objects.has( value.item.name ) ) {
+			if ( value.type == type && schema.objects.has( value.item.name ) ) {
 				return Range.createOn( value.item );
 			}
 
-			if ( this.schema.check( { name: '$text', inside: value.nextPosition } ) ) {
+			if ( schema.check( { name: '$text', inside: value.nextPosition } ) ) {
 				return new Range( value.nextPosition );
 			}
 		}

+ 22 - 15
packages/ckeditor5-engine/src/model/documentselection.js

@@ -52,15 +52,24 @@ export default class DocumentSelection extends Selection {
 	 * Creates an empty live selection for given {@link module:engine/model/document~Document}.
 	 *
 	 * @param {module:engine/model/document~Document} document Document which owns this selection.
+	 * @param {module:engine/model/model~Model} model
 	 */
-	constructor( document ) {
+	constructor( document, model ) {
 		super();
 
 		/**
 		 * Document which owns this selection.
 		 *
 		 * @protected
-		 * @member {module:engine/model/document~Document} module:engine/model/documentselection~DocumentSelection#_document
+		 * @member {module:engine/model/model~Model}
+		 */
+		this._model = model;
+
+		/**
+		 * Document which owns this selection.
+		 *
+		 * @protected
+		 * @member {module:engine/model/document~Document}
 		 */
 		this._document = document;
 
@@ -83,9 +92,14 @@ export default class DocumentSelection extends Selection {
 				this._updateAttributes( false );
 			}
 
-			// Whenever element which had selection's attributes stored in it stops being empty,
-			// the attributes need to be removed.
-			clearAttributesStoredInElement( changes, batch, this._document );
+			// Batch may not be passed to the document#change event in some tests.
+			// See https://github.com/ckeditor/ckeditor5-engine/issues/1001#issuecomment-314202352
+			// Ignore also transparent batches because they are... transparent.
+			if ( batch && batch.type !== 'transparent' ) {
+				// Whenever element which had selection's attributes stored in it stops being empty,
+				// the attributes need to be removed.
+				clearAttributesStoredInElement( changes, this._model );
+			}
 		} );
 	}
 
@@ -711,14 +725,7 @@ function getAttrsIfCharacter( node ) {
 }
 
 // Removes selection attributes from element which is not empty anymore.
-function clearAttributesStoredInElement( changes, batch, document ) {
-	// Batch may not be passed to the document#change event in some tests.
-	// See https://github.com/ckeditor/ckeditor5-engine/issues/1001#issuecomment-314202352
-	// Ignore also transparent batches because they are... transparent.
-	if ( !batch || batch.type == 'transparent' ) {
-		return;
-	}
-
+function clearAttributesStoredInElement( changes, model ) {
 	const changeParent = changes.range && changes.range.start.parent;
 
 	// `changes.range` is not set in case of rename, root and marker operations.
@@ -727,11 +734,11 @@ function clearAttributesStoredInElement( changes, batch, document ) {
 		return;
 	}
 
-	document.enqueueChanges( () => {
+	model.change( writer => {
 		const storedAttributes = Array.from( changeParent.getAttributeKeys() ).filter( key => key.startsWith( storePrefix ) );
 
 		for ( const key of storedAttributes ) {
-			batch.removeAttribute( key, changeParent );
+			writer.removeAttribute( key, changeParent );
 		}
 	} );
 }