Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the config.forceValue option in ListCommand #9371

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make tests showing that lisItem with forceValue true does not change anything, and similarly for paragraphs and false. AFAICS, these tests describe "normal" behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two assertions for each test to check both scenarios. But I can simplify these tests and split the 4 assertions into 4 tests.

Copy link
Contributor

@scofalik scofalik Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 let's say it was late when I was reviewing

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