Skip to content

Commit

Permalink
Changed a way that items for updating are obtained.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Aug 20, 2020
1 parent 39f0604 commit ac39cef
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions packages/ckeditor5-list/src/liststylecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,28 @@ export default class ListStyleCommand extends Command {
execute( options = {} ) {
const model = this.editor.model;
const document = model.document;
const position = findPositionInsideList( document.selection );

if ( !position ) {
return;
}
// For all selected blocks find all list items that are being selected
// and update the `listStyle` attribute in those lists.
let listItems = [ ...document.selection.getSelectedBlocks() ]
.filter( element => element.is( 'element', 'listItem' ) )
.map( element => {
const position = model.change( writer => writer.createPositionAt( element, 0 ) );

return [
...getSiblingNodes( position, 'backward' ),
...getSiblingNodes( position, 'forward' )
];
} )
.flat();

const listItems = [
...getSiblingNodes( position, 'backward' ),
...getSiblingNodes( position, 'forward' )
];
// Since `getSelectedBlocks()` can return items that belong to the same list, and
// `getSiblingNodes()` returns the entire list, we need to remove duplicated items.
listItems = [ ...new Set( listItems ) ];

model.change( writer => {
for ( const item of listItems ) {
writer.setAttribute( 'listStyle', options.type || 'default', item );
writer.setAttribute( 'listStyle', options.type || this._defaultType, item );
}
} );
}
Expand Down Expand Up @@ -104,27 +112,6 @@ export default class ListStyleCommand extends Command {
}
}

// Returns a position that is hooked in the `listItem` element.
// It can be the selection starting position or end.
//
// @param {module:engine/model/selection~Selection} selection
// @returns {module:engine/model/position~Position|null}
function findPositionInsideList( selection ) {
const startPosition = selection.getFirstPosition();

if ( startPosition.parent.is( 'element', 'listItem' ) ) {
return startPosition;
}

const lastPosition = selection.getLastPosition();

if ( lastPosition.parent.is( 'element', 'listItem' ) ) {
return lastPosition;
}

return null;
}

// Returns an array with all `listItem` elements that represents the same list.
//
// It means that values for `listIndent`, `listType`, and `listStyle` for all items
Expand Down

0 comments on commit ac39cef

Please sign in to comment.