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

Commit

Permalink
Other: Aligned the implementation to the new Command API (see https:/…
Browse files Browse the repository at this point in the history
…/github.com/ckeditor/ckeditor5-core/issues/88).

BREAKING CHANGES: The command API has been changed.
  • Loading branch information
szymonkups committed Jun 13, 2017
2 parents 6fba44b + 18ab9fb commit c2a1559
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/paragraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
60 changes: 18 additions & 42 deletions src/paragraphcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,50 @@
* @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 )
} );
}

/**
* 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( () => {
Expand All @@ -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 )
} );
}
}
28 changes: 12 additions & 16 deletions tests/paragraphcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand All @@ -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;
Expand Down Expand Up @@ -81,23 +77,23 @@ 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 );

// Purposely not putting it in `document.enqueueChanges` to update command manually.
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;
Expand All @@ -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 );
} );

Expand All @@ -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 );
} );

Expand All @@ -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>'
);
Expand All @@ -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>' );
} );
Expand All @@ -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>' );
} );
Expand All @@ -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>'
);
Expand All @@ -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>' );
} );
} );
Expand Down

0 comments on commit c2a1559

Please sign in to comment.