Преглед на файлове

Fix: Alignment can be added only on allowed blocks when multiple blocks are selected.

Maciej Gołaszewski преди 8 години
родител
ревизия
044fb4e783
променени са 2 файла, в които са добавени 26 реда и са изтрити 12 реда
  1. 10 12
      packages/ckeditor5-alignment/src/alignmentcommand.js
  2. 16 0
      packages/ckeditor5-alignment/tests/integration.js

+ 10 - 12
packages/ckeditor5-alignment/src/alignmentcommand.js

@@ -60,7 +60,7 @@ export default class AlignmentCommand extends Command {
 		const firstBlock = first( this.editor.document.selection.getSelectedBlocks() );
 
 		// As first check whether to enable or disable command as value will be always false if command cannot be enabled.
-		this.isEnabled = this._checkEnabled( firstBlock );
+		this.isEnabled = !!firstBlock && this._canBeAligned( firstBlock );
 		this.value = this._getValue( firstBlock );
 	}
 
@@ -78,7 +78,9 @@ export default class AlignmentCommand extends Command {
 
 		document.enqueueChanges( () => {
 			const batch = options.batch || document.batch();
-			const blocks = Array.from( document.selection.getSelectedBlocks() );
+
+			// Get only those blocks from selected that can have alignment set
+			const blocks = Array.from( document.selection.getSelectedBlocks() ).filter( block => this._canBeAligned( block ) );
 
 			// Remove alignment attribute if current alignment is as selected or is default one.
 			// Default alignment should not be stored in model as it will bloat model data.
@@ -91,24 +93,20 @@ export default class AlignmentCommand extends Command {
 	}
 
 	/**
-	 * Checks whether the command can be enabled in the current context.
+	 * Checks whether block can have aligned set.
 	 *
+	 * @param {module:engine/model/element~Element} block A block to be checked.
+	 * @returns {Boolean}
 	 * @private
-	 * @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
-	 * @returns {Boolean} Whether the command should be enabled.
 	 */
-	_checkEnabled( firstBlock ) {
-		if ( !firstBlock ) {
-			return false;
-		}
-
+	_canBeAligned( block ) {
 		const schema = this.editor.document.schema;
 
 		// Check if adding alignment attribute to selected block is allowed.
 		return schema.check( {
-			name: firstBlock.name,
+			name: block.name,
 			// Apparently I must pass current attributes as otherwise adding alignment on listItem will fail.
-			attributes: [ ...firstBlock.getAttributeKeys(), 'alignment' ]
+			attributes: [ ...block.getAttributeKeys(), 'alignment' ]
 		} );
 	}
 

+ 16 - 0
packages/ckeditor5-alignment/tests/integration.js

@@ -49,6 +49,22 @@ describe( 'Alignment', () => {
 
 			expect( getModelData( doc ) ).to.equal( '<image src="foo.png"><caption>Foo[]</caption></image>' );
 		} );
+		it( 'does not work inside image caption when selection overlaps image', () => {
+			setModelData(
+				doc,
+				'<paragraph>foo[foo</paragraph>' +
+				'<image src="foo.png"><caption>bar</caption></image>' +
+				'<paragraph>baz]baz</paragraph>'
+			);
+
+			editor.execute( 'alignCenter' );
+
+			expect( getModelData( doc ) ).to.equal(
+				'<paragraph alignment="center">foo[foo</paragraph>' +
+				'<image src="foo.png"><caption>bar</caption></image>' +
+				'<paragraph alignment="center">baz]baz</paragraph>'
+			);
+		} );
 	} );
 
 	describe( 'compatibility with blockQuote', () => {