Procházet zdrojové kódy

Other: Aligned the implementation to the new Command API (see https://github.com/ckeditor/ckeditor5-core/issues/88).

BREAKING CHANGES: The command API has been changed.
Szymon Kupś před 8 roky
rodič
revize
d747a8776b

+ 1 - 1
packages/ckeditor5-paragraph/src/paragraph.js

@@ -39,7 +39,7 @@ export default class Paragraph extends Plugin {
 		const data = editor.data;
 		const editing = editor.editing;
 
-		editor.commands.set( 'paragraph', new ParagraphCommand( editor ) );
+		editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
 
 		// Schema.
 		doc.schema.registerItem( 'paragraph', '$block' );

+ 18 - 42
packages/ckeditor5-paragraph/src/paragraphcommand.js

@@ -7,38 +7,35 @@
  * @module paragraph/paragraphcommand
  */
 
-import Command from '@ckeditor/ckeditor5-core/src/command/command';
+import Command from '@ckeditor/ckeditor5-core/src/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The paragraph command.
  *
- * @extends module:core/command/command~Command
+ * @extends module:core/command~Command
  */
 export default class ParagraphCommand extends Command {
 	/**
-	 * Creates an instance of the command.
+	 * The value of the command. Indicates whether the selection's start is placed in a paragraph.
 	 *
-	 * @param {module:core/editor/editor~Editor} editor Editor instance.
+	 * @readonly
+	 * @observable
+	 * @member {Boolean} #value
 	 */
-	constructor( editor ) {
-		super( editor );
 
-		/**
-		 * Value of the command, indicating whether it is applied in the context
-		 * of current {@link module:engine/model/document~Document#selection selection}.
-		 *
-		 * @readonly
-		 * @observable
-		 * @member {Boolean}
-		 */
-		this.set( 'value', false );
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const block = first( this.editor.document.selection.getSelectedBlocks() );
 
-		// Update current value each time changes are done on document.
-		this.listenTo( editor.document, 'changesDone', () => {
-			this.refreshValue();
-			this.refreshState();
+		this.value = !!block && block.is( 'paragraph' );
+
+		this.isEnabled = !!block && this.editor.document.schema.check( {
+			name: 'paragraph',
+			inside: Position.createBefore( block )
 		} );
 	}
 
@@ -46,14 +43,14 @@ export default class ParagraphCommand extends Command {
 	 * Executes the command. All the blocks (see {@link module:engine/model/schema~Schema}) in the selection
 	 * will be turned to paragraphs.
 	 *
-	 * @protected
+	 * @fires execute
 	 * @param {Object} [options] Options for executed command.
 	 * @param {module:engine/model/batch~Batch} [options.batch] Batch to collect all the change steps.
 	 * New batch will be created if this option is not set.
 	 * @param {module:engine/model/selection~Selection} [options.selection] Selection the command should be applied to.
 	 * By default, if not provided, the command is applied to {@link module:engine/model/document~Document#selection}.
 	 */
-	_doExecute( options = {} ) {
+	execute( options = {} ) {
 		const document = this.editor.document;
 
 		document.enqueueChanges( () => {
@@ -67,25 +64,4 @@ export default class ParagraphCommand extends Command {
 			}
 		} );
 	}
-
-	/**
-	 * Updates command's {@link #value value} based on current selection.
-	 */
-	refreshValue() {
-		const block = first( this.editor.document.selection.getSelectedBlocks() );
-
-		this.value = !!block && block.is( 'paragraph' );
-	}
-
-	/**
-	 * @inheritDoc
-	 */
-	_checkEnabled() {
-		const block = first( this.editor.document.selection.getSelectedBlocks() );
-
-		return !!block && this.editor.document.schema.check( {
-			name: 'paragraph',
-			inside: Position.createBefore( block )
-		} );
-	}
 }

+ 12 - 16
packages/ckeditor5-paragraph/tests/paragraphcommand.js

@@ -20,7 +20,7 @@ describe( 'ParagraphCommand', () => {
 			command = new ParagraphCommand( editor );
 			root = document.getRoot();
 
-			editor.commands.set( 'paragraph', command );
+			editor.commands.add( 'paragraph', command );
 			schema.registerItem( 'paragraph', '$block' );
 			schema.registerItem( 'heading1', '$block' );
 
@@ -35,10 +35,6 @@ describe( 'ParagraphCommand', () => {
 	} );
 
 	describe( 'value', () => {
-		it( 'has default value', () => {
-			expect( command.value ).to.be.false;
-		} );
-
 		it( 'responds to changes in selection (collapsed selection)', () => {
 			setData( document, '<heading1>foo[]bar</heading1>' );
 			expect( command.value ).to.be.false;
@@ -81,7 +77,7 @@ describe( 'ParagraphCommand', () => {
 			expect( command.value ).to.be.false;
 		} );
 
-		it( 'should be refreshed after calling refreshValue()', () => {
+		it( 'should be refreshed after calling refresh()', () => {
 			setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
 			const element = document.getRoot().getChild( 1 );
 
@@ -89,15 +85,15 @@ describe( 'ParagraphCommand', () => {
 			document.selection.setRanges( [ Range.createIn( element ) ] );
 
 			expect( command.value ).to.be.true;
-			command.refreshValue();
+			command.refresh();
 			expect( command.value ).to.be.false;
 		} );
 	} );
 
-	describe( '_doExecute', () => {
+	describe( 'execute()', () => {
 		it( 'should update value after execution', () => {
 			setData( document, '<heading1>[]</heading1>' );
-			command._doExecute();
+			command.execute();
 
 			expect( getData( document ) ).to.equal( '<paragraph>[]</paragraph>' );
 			expect( command.value ).to.be.true;
@@ -109,7 +105,7 @@ describe( 'ParagraphCommand', () => {
 			setData( document, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
 			expect( batch.deltas.length ).to.equal( 0 );
 
-			command._doExecute( { batch } );
+			command.execute( { batch } );
 			expect( batch.deltas.length ).to.equal( 1 );
 		} );
 
@@ -120,7 +116,7 @@ describe( 'ParagraphCommand', () => {
 				setData( document, '<heading1>foo[]bar</heading1>' );
 				expect( batch.deltas.length ).to.equal( 0 );
 
-				command._doExecute( { batch } );
+				command.execute( { batch } );
 				expect( batch.deltas.length ).to.be.above( 0 );
 			} );
 
@@ -132,7 +128,7 @@ describe( 'ParagraphCommand', () => {
 				const selection = new Selection();
 				selection.addRange( Range.createFromParentsAndOffsets( secondTolastHeading, 0, lastHeading, 0 ) );
 
-				command._doExecute( { selection } );
+				command.execute( { selection } );
 				expect( getData( document ) ).to.equal(
 					'<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>'
 				);
@@ -142,7 +138,7 @@ describe( 'ParagraphCommand', () => {
 		describe( 'collapsed selection', () => {
 			it( 'does nothing when executed with already applied', () => {
 				setData( document, '<paragraph>foo[]bar</paragraph>' );
-				command._doExecute();
+				command.execute();
 
 				expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
 			} );
@@ -152,7 +148,7 @@ describe( 'ParagraphCommand', () => {
 				schema.allow( { name: '$text', inside: 'inlineImage' } );
 
 				setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
-				command._doExecute();
+				command.execute();
 
 				expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
 			} );
@@ -164,7 +160,7 @@ describe( 'ParagraphCommand', () => {
 
 				setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
 
-				command._doExecute();
+				command.execute();
 				expect( getData( document ) ).to.equal(
 					'<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
 				);
@@ -175,7 +171,7 @@ describe( 'ParagraphCommand', () => {
 
 				setData( document, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
 
-				command._doExecute();
+				command.execute();
 				expect( getData( document ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
 			} );
 		} );