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 6bcf77c + 7bf4374 commit 627510a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
66 changes: 34 additions & 32 deletions src/blockquotecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,46 @@
* @module block-quote/blockquotecommand
*/

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 Element from '@ckeditor/ckeditor5-engine/src/model/element';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import first from '@ckeditor/ckeditor5-utils/src/first';

/**
* The block quote command.
* The block quote command plugin.
*
* @extends module:core/command/command~Command
* @extends module:core/command~Command
*/
export default class BlockQuoteCommand extends Command {
/**
* @inheritDoc
* Whether the selection starts in a block quote.
*
* @observable
* @readonly
* @member {Boolean} #value
*/
constructor( editor ) {
super( editor );

/**
* Flag indicating whether the command is active. It's on when the selection starts
* in a quoted block.
*
* @readonly
* @observable
* @member {Boolean} #value
*/
this.set( 'value', false );

// Update current value each time changes are done to the document.
this.listenTo( editor.document, 'changesDone', () => {
this.refreshValue();
this.refreshState();
} );
}

/**
* Updates command's {@link #value} based on the current selection.
* @inheritDoc
*/
refreshValue() {
const firstBlock = first( this.editor.document.selection.getSelectedBlocks() );

// In the current implementation, the block quote must be an immediate parent of a block element.
this.value = !!( firstBlock && findQuote( firstBlock ) );
refresh() {
this.value = this._getValue();
this.isEnabled = this._checkEnabled();
}

/**
* Executes the command. When the command {@link #value is on}, then all block quotes within
* the selection will be removed. If it's off, then all selected blocks will be wrapped with
* a block quote.
*
* @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.
*/
_doExecute( options = {} ) {
execute( options = {} ) {
const doc = this.editor.document;
const schema = doc.schema;
const batch = options.batch || doc.batch();
Expand All @@ -84,7 +68,23 @@ export default class BlockQuoteCommand extends Command {
}

/**
* @inheritDoc
* Checks the command's {@link #value}.
*
* @private
* @returns {Boolean} The current value.
*/
_getValue() {
const firstBlock = first( this.editor.document.selection.getSelectedBlocks() );

// In the current implementation, the block quote must be an immediate parent of a block element.
return !!( firstBlock && findQuote( firstBlock ) );
}

/**
* Checks whether the command can be enabled in the current context.
*
* @private
* @returns {Boolean} Whether the command should be enabled.
*/
_checkEnabled() {
if ( this.value ) {
Expand All @@ -110,6 +110,7 @@ export default class BlockQuoteCommand extends Command {
* start it or end it, then the quote will be split (if needed) and the blocks
* will be moved out of it, so other quoted blocks remained quoted.
*
* @private
* @param {module:engine/model/batch~Batch} batch
* @param {Array.<module:engine/model/element~Element>} blocks
*/
Expand Down Expand Up @@ -148,6 +149,7 @@ export default class BlockQuoteCommand extends Command {
/**
* Applies the quote to the given blocks.
*
* @private
* @param {module:engine/model/batch~Batch} batch
* @param {Array.<module:engine/model/element~Element>} blocks
*/
Expand Down
2 changes: 1 addition & 1 deletion src/blockquoteengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class BlockQuoteEngine extends Plugin {
const editor = this.editor;
const schema = editor.document.schema;

editor.commands.set( 'blockQuote', new BlockQuoteCommand( editor ) );
editor.commands.add( 'blockQuote', new BlockQuoteCommand( editor ) );

schema.registerItem( 'blockQuote' );
schema.allow( { name: 'blockQuote', inside: '$root' } );
Expand Down
4 changes: 2 additions & 2 deletions tests/blockquotecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';

import Command from '@ckeditor/ckeditor5-core/src/command/command';
import Command from '@ckeditor/ckeditor5-core/src/command';

describe( 'BlockQuoteCommand', () => {
let editor, doc, command;
Expand Down Expand Up @@ -139,7 +139,7 @@ describe( 'BlockQuoteCommand', () => {
// } );
} );

describe( '_doExecute()', () => {
describe( 'execute()', () => {
describe( 'applying quote', () => {
it( 'should wrap a single block', () => {
setModelData(
Expand Down

0 comments on commit 627510a

Please sign in to comment.