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

Commit

Permalink
Merge pull request #36 from ckeditor/t/35
Browse files Browse the repository at this point in the history
Feature: Add possibility to force quoting or un-quoting in `execute()`. Closes: #35.
  • Loading branch information
Piotr Jasiun committed May 20, 2019
2 parents 2156d1e + a539eb3 commit e9a5027
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/blockquotecommand.js
Expand Up @@ -39,16 +39,21 @@ export default class BlockQuoteCommand extends Command {
* a block quote.
*
* @fires execute
* @param {Object} [options] Command options.
* @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will apply a block quote,
* otherwise the command will remove the block quote. If not set, the command will act basing on its current value.
*/
execute() {
execute( options = {} ) {
const model = this.editor.model;
const schema = model.schema;
const selection = model.document.selection;

const blocks = Array.from( selection.getTopMostBlocks() );

const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;

model.change( writer => {
if ( this.value ) {
if ( !value ) {
this._removeQuote( writer, blocks.filter( findQuote ) );
} else {
const blocksToQuote = blocks.filter( block => {
Expand Down Expand Up @@ -189,7 +194,7 @@ function findQuote( elementOrPosition ) {
// Returns a minimal array of ranges containing groups of subsequent blocks.
//
// content: abcdefgh
// blocks: [ a, b, d , f, g, h ]
// blocks: [ a, b, d, f, g, h ]
// output ranges: [ab]c[d]e[fgh]
//
// @param {Array.<module:engine/model/element~Element>} blocks
Expand Down
45 changes: 45 additions & 0 deletions tests/blockquotecommand.js
Expand Up @@ -438,6 +438,29 @@ describe( 'BlockQuoteCommand', () => {
'</blockQuote>'
);
} );

it( 'should handle forceValue = true param', () => {
setModelData(
model,
'<blockQuote>' +
'<paragraph>x[x</paragraph>' +
'</blockQuote>' +
'<paragraph>d]ef</paragraph>'
);

editor.execute( 'blockQuote', { forceValue: true } );

expect( getModelData( model ) ).to.equal(
'<blockQuote>' +
'<paragraph>x[x</paragraph>' +
'<paragraph>d]ef</paragraph>' +
'</blockQuote>'
);

expect( getViewData( editor.editing.view ) ).to.equal(
'<blockquote><p>x{x</p><p>d}ef</p></blockquote>'
);
} );
} );

describe( 'removing quote', () => {
Expand Down Expand Up @@ -598,6 +621,28 @@ describe( 'BlockQuoteCommand', () => {
'<blockquote><p>ghi</p></blockquote>'
);
} );

it( 'should handle forceValue = false param', () => {
setModelData(
model,
'<paragraph>a[bc</paragraph>' +
'<blockQuote>' +
'<paragraph>x]x</paragraph>' +
'</blockQuote>'
);

editor.execute( 'blockQuote', { forceValue: false } );

// Incorrect selection.
expect( getModelData( model ) ).to.equal(
'<paragraph>a[bc]</paragraph>' +
'<paragraph>xx</paragraph>'
);

expect( getViewData( editor.editing.view ) ).to.equal(
'<p>a{bc}</p><p>xx</p>'
);
} );
} );
} );
} );

0 comments on commit e9a5027

Please sign in to comment.