8
0
Просмотр исходного кода

Don't rename blocks which are paragraphs. Don't block execution if #value is true because there might be more blocks to rename after the first one.

Aleksander Nowodzinski 9 лет назад
Родитель
Сommit
673567862c

+ 4 - 6
packages/ckeditor5-paragraph/src/paragraphcommand.js

@@ -48,10 +48,6 @@ export default class ParagraphCommand extends Command {
 	 * By default, if not provided, the command is applied to {@link module:engine/model/document~Document#selection}.
 	 * By default, if not provided, the command is applied to {@link module:engine/model/document~Document#selection}.
 	 */
 	 */
 	_doExecute( options = {} ) {
 	_doExecute( options = {} ) {
-		if ( this.value && !options.selection ) {
-			return;
-		}
-
 		const document = this.editor.document;
 		const document = this.editor.document;
 
 
 		document.enqueueChanges( () => {
 		document.enqueueChanges( () => {
@@ -59,7 +55,9 @@ export default class ParagraphCommand extends Command {
 			const blocks = ( options.selection || document.selection ).getSelectedBlocks();
 			const blocks = ( options.selection || document.selection ).getSelectedBlocks();
 
 
 			for ( let block of blocks ) {
 			for ( let block of blocks ) {
-				batch.rename( block, 'paragraph' );
+				if ( !block.is( 'paragraph' ) ) {
+					batch.rename( block, 'paragraph' );
+				}
 			}
 			}
 		} );
 		} );
 	}
 	}
@@ -73,7 +71,7 @@ export default class ParagraphCommand extends Command {
 		const block = this.editor.document.selection.getSelectedBlocks().next().value;
 		const block = this.editor.document.selection.getSelectedBlocks().next().value;
 
 
 		if ( block ) {
 		if ( block ) {
-			this.value = block.name == 'paragraph';
+			this.value = block.is( 'paragraph' );
 		}
 		}
 	}
 	}
 }
 }

+ 21 - 2
packages/ckeditor5-paragraph/tests/paragraphcommand.js

@@ -9,7 +9,7 @@ import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 
-describe( 'HeadingCommand', () => {
+describe( 'ParagraphCommand', () => {
 	let editor, document, command, root, schema;
 	let editor, document, command, root, schema;
 
 
 	beforeEach( () => {
 	beforeEach( () => {
@@ -72,6 +72,16 @@ describe( 'HeadingCommand', () => {
 			expect( command.value ).to.be.true;
 			expect( command.value ).to.be.true;
 		} );
 		} );
 
 
+		it( 'should not rename blocks which already are pargraphs', () => {
+			const batch = editor.document.batch();
+
+			setData( document, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
+			expect( batch.deltas.length ).to.equal( 0 );
+
+			command._doExecute( { batch } );
+			expect( batch.deltas.length ).to.equal( 1 );
+		} );
+
 		describe( 'custom options', () => {
 		describe( 'custom options', () => {
 			it( 'should use provided batch', () => {
 			it( 'should use provided batch', () => {
 				const batch = editor.document.batch();
 				const batch = editor.document.batch();
@@ -120,12 +130,21 @@ describe( 'HeadingCommand', () => {
 				schema.registerItem( 'heading2', '$block' );
 				schema.registerItem( 'heading2', '$block' );
 
 
 				setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
 				setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
-				command._doExecute();
 
 
+				command._doExecute();
 				expect( getData( document ) ).to.equal(
 				expect( getData( document ) ).to.equal(
 					'<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
 					'<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
 				);
 				);
 			} );
 			} );
+
+			it( 'converts all elements even if already anchored in paragraph', () => {
+				schema.registerItem( 'heading2', '$block' );
+
+				setData( document, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
+
+				command._doExecute();
+				expect( getData( document ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
+			} );
 		} );
 		} );
 	} );
 	} );
 } );
 } );