Explorar el Código

Align code to changes in heading command changes.

Maciej Gołaszewski hace 7 años
padre
commit
a82dbb1ccb

+ 16 - 12
packages/ckeditor5-autoformat/src/autoformat.js

@@ -103,24 +103,28 @@ export default class Autoformat extends Plugin {
 	 *
 	 * It is using a number at the end of the command name to associate it with the proper trigger:
 	 *
-	 * * `heading1` will be executed when typing `#`,
-	 * * `heading2` will be executed when typing `##`,
+	 * * `heading` with value `heading1` will be executed when typing `#`,
+	 * * `heading` with value `heading2` will be executed when typing `##`,
 	 * * ... up to `heading6` and `######`.
 	 *
 	 * @private
 	 */
 	_addHeadingAutoformats() {
-		Array.from( this.editor.commands.names() )
-			.filter( name => name.match( /^heading[1-6]$/ ) )
-			.forEach( commandName => {
-				const level = commandName[ 7 ];
-				const pattern = new RegExp( `^(#{${ level }})\\s$` );
-
-				// eslint-disable-next-line no-new
-				new BlockAutoformatEditing( this.editor, pattern, () => {
-					this.editor.execute( commandName );
+		const command = this.editor.commands.get( 'heading' );
+
+		if ( command ) {
+			command.modelElements
+				.filter( name => name.match( /^heading[1-6]$/ ) )
+				.forEach( commandValue => {
+					const level = commandValue[ 7 ];
+					const pattern = new RegExp( `^(#{${ level }})\\s$` );
+
+					// eslint-disable-next-line no-new
+					new BlockAutoformatEditing( this.editor, pattern, () => {
+						this.editor.execute( 'heading', { value: commandValue } );
+					} );
 				} );
-			} );
+		}
 	}
 
 	/**

+ 10 - 17
packages/ckeditor5-autoformat/tests/autoformat.js

@@ -19,7 +19,7 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
-import Command from '@ckeditor/ckeditor5-core/src/command';
+import HeadingCommand from '@ckeditor/ckeditor5-heading/src/headingcommand';
 
 testUtils.createSinonSandbox();
 
@@ -148,23 +148,12 @@ describe( 'Autoformat', () => {
 		} );
 
 		it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
-			const spy1 = sinon.spy();
-			const spy6 = sinon.spy();
+			const command = new HeadingCommand( editor, [ 'heading1', 'heading6' ] );
 
-			class Heading6 extends Command {
-				execute() {
-					spy6();
-				}
-			}
-			class Heading1 extends Command {
-				execute() {
-					spy1();
-				}
-			}
+			const spy = sinon.spy( command, 'execute' );
 
 			function HeadingPlugin( editor ) {
-				editor.commands.add( 'heading1', new Heading1( editor ) );
-				editor.commands.add( 'heading6', new Heading6( editor ) );
+				editor.commands.add( 'heading', command );
 			}
 
 			return VirtualTestEditor
@@ -182,14 +171,18 @@ describe( 'Autoformat', () => {
 						writer.insertText( ' ', doc.selection.getFirstPosition() );
 					} );
 
-					expect( spy1.calledOnce ).to.be.true;
+					sinon.assert.calledOnce( spy );
+					sinon.assert.calledWithExactly( spy, { value: 'heading1' } );
+
+					spy.resetHistory();
 
 					setData( model, '<paragraph>######[]</paragraph>' );
 					model.change( writer => {
 						writer.insertText( ' ', doc.selection.getFirstPosition() );
 					} );
 
-					expect( spy6.calledOnce ).to.be.true;
+					sinon.assert.calledOnce( spy );
+					sinon.assert.calledWithExactly( spy, { value: 'heading6' } );
 
 					return editor.destroy();
 				} );