Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 5c99c06

Browse files
committed
Fix: Alignment can be added only on allowed blocks when multiple blocks are selected.
1 parent e0720c1 commit 5c99c06

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/alignmentcommand.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class AlignmentCommand extends Command {
6060
const firstBlock = first( this.editor.document.selection.getSelectedBlocks() );
6161

6262
// As first check whether to enable or disable command as value will be always false if command cannot be enabled.
63-
this.isEnabled = this._checkEnabled( firstBlock );
63+
this.isEnabled = !!firstBlock && this._canBeAligned( firstBlock );
6464
this.value = this._getValue( firstBlock );
6565
}
6666

@@ -78,7 +78,9 @@ export default class AlignmentCommand extends Command {
7878

7979
document.enqueueChanges( () => {
8080
const batch = options.batch || document.batch();
81-
const blocks = Array.from( document.selection.getSelectedBlocks() );
81+
82+
// Get only those blocks from selected that can have alignment set
83+
const blocks = Array.from( document.selection.getSelectedBlocks() ).filter( block => this._canBeAligned( block ) );
8284

8385
// Remove alignment attribute if current alignment is as selected or is default one.
8486
// Default alignment should not be stored in model as it will bloat model data.
@@ -91,24 +93,20 @@ export default class AlignmentCommand extends Command {
9193
}
9294

9395
/**
94-
* Checks whether the command can be enabled in the current context.
96+
* Checks whether block can have aligned set.
9597
*
98+
* @param {module:engine/model/element~Element} block A block to be checked.
99+
* @returns {Boolean}
96100
* @private
97-
* @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
98-
* @returns {Boolean} Whether the command should be enabled.
99101
*/
100-
_checkEnabled( firstBlock ) {
101-
if ( !firstBlock ) {
102-
return false;
103-
}
104-
102+
_canBeAligned( block ) {
105103
const schema = this.editor.document.schema;
106104

107105
// Check if adding alignment attribute to selected block is allowed.
108106
return schema.check( {
109-
name: firstBlock.name,
107+
name: block.name,
110108
// Apparently I must pass current attributes as otherwise adding alignment on listItem will fail.
111-
attributes: [ ...firstBlock.getAttributeKeys(), 'alignment' ]
109+
attributes: [ ...block.getAttributeKeys(), 'alignment' ]
112110
} );
113111
}
114112

tests/integration.js

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ describe( 'Alignment', () => {
4949

5050
expect( getModelData( doc ) ).to.equal( '<image src="foo.png"><caption>Foo[]</caption></image>' );
5151
} );
52+
it( 'does not work inside image caption when selection overlaps image', () => {
53+
setModelData(
54+
doc,
55+
'<paragraph>foo[foo</paragraph>' +
56+
'<image src="foo.png"><caption>bar</caption></image>' +
57+
'<paragraph>baz]baz</paragraph>'
58+
);
59+
60+
editor.execute( 'alignCenter' );
61+
62+
expect( getModelData( doc ) ).to.equal(
63+
'<paragraph alignment="center">foo[foo</paragraph>' +
64+
'<image src="foo.png"><caption>bar</caption></image>' +
65+
'<paragraph alignment="center">baz]baz</paragraph>'
66+
);
67+
} );
5268
} );
5369

5470
describe( 'compatibility with blockQuote', () => {

0 commit comments

Comments
 (0)