Skip to content

Commit

Permalink
Merge pull request #9371 from ckeditor/cf/3917
Browse files Browse the repository at this point in the history
Other (list): Introduced the `config.forceValue` option to `ListCommand` that forces turning list items on/off instead of toggling.
  • Loading branch information
scofalik authored Mar 30, 2021
2 parents b905aaa + 49e466b commit e164485
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/ckeditor5-list/src/listcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ export default class ListCommand extends Command {
}

/**
* Executes the command.
* Executes the list command.
*
* @protected
* @fires execute
* @param {Object} [options] Command options.
* @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will try to convert the
* selected items and potentially the neighbor elements to the proper list items. If set to `false` it will convert selected elements
* to paragraphs. If not set, the command will toggle selected elements to list items or paragraphs, depending on the selection.
*/
execute() {
execute( options = {} ) {
const model = this.editor.model;
const document = model.document;
const blocks = Array.from( document.selection.getSelectedBlocks() )
.filter( block => checkCanBecomeListItem( block, model.schema ) );

// Whether we are turning off some items.
const turnOff = this.value === true;
const turnOff = options.forceValue !== undefined ? !options.forceValue : this.value;

// If we are turning off items, we are going to rename them to paragraphs.

model.change( writer => {
Expand Down
27 changes: 27 additions & 0 deletions packages/ckeditor5-list/tests/listcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,33 @@ describe( 'ListCommand', () => {
} );
} );

describe( 'options.forceValue', () => {
it( 'should force converting into the list if the `options.forceValue` is set to `true`', () => {
setData( model, '<paragraph>fo[]o</paragraph>' );

command.execute( { forceValue: true } );

expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );

command.execute( { forceValue: true } );

expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
} );

it( 'should force converting into the paragraph if the `options.forceValue` is set to `false`', () => {
setData( model, '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );

command.execute( { forceValue: false } );

// Attributes will be removed by post fixer.
expect( getData( model ) ).to.equal( '<paragraph listIndent="0" listType="bulleted">fo[]o</paragraph>' );

command.execute( { forceValue: false } );

expect( getData( model ) ).to.equal( '<paragraph listIndent="0" listType="bulleted">fo[]o</paragraph>' );
} );
} );

describe( 'collapsed selection', () => {
it( 'should rename closest block to listItem and set correct attributes', () => {
setData( model, '<paragraph>fo[]o</paragraph>' );
Expand Down

0 comments on commit e164485

Please sign in to comment.