Skip to content
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
24 changes: 17 additions & 7 deletions packages/ckeditor5-list/src/list/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export function reconvertItemsOnDataChange(
function collectListItemsToRefresh( listHead: ListElement, changedItems: Set<ModelNode> ) {
const itemsToRefresh = [];
const visited = new Set();
const stack: Array<ListItemAttributesMap> = [];
const stack: Array<{
modelAttributes: ListItemAttributesMap;
modelElement: ListElement;
}> = [];

for ( const { node, previous } of new SiblingListBlocksIterator( listHead ) ) {
if ( visited.has( node ) ) {
Expand All @@ -213,10 +216,13 @@ export function reconvertItemsOnDataChange(
}

// Update the stack for the current indent level.
stack[ itemIndent ] = Object.fromEntries(
Array.from( node.getAttributes() )
.filter( ( [ key ] ) => attributeNames.includes( key ) )
);
stack[ itemIndent ] = {
modelAttributes: Object.fromEntries(
Array.from( node.getAttributes() )
.filter( ( [ key ] ) => attributeNames.includes( key ) )
),
modelElement: node
};

// Find all blocks of the current node.
const blocks = getListItemBlocks( node, { direction: 'forward' } );
Expand Down Expand Up @@ -271,7 +277,10 @@ export function reconvertItemsOnDataChange(

function doesItemWrappingRequiresRefresh(
item: ModelElement,
stack: Array<ListItemAttributesMap>,
stack: Array<{
modelAttributes: ListItemAttributesMap;
modelElement: ListElement;
}>,
changedItems: Set<ModelNode>
) {
// Items directly affected by some "change" don't need a refresh, they will be converted by their own changes.
Expand All @@ -298,7 +307,8 @@ export function reconvertItemsOnDataChange(
const eventName = `checkAttributes:${ isListItemElement ? 'item' : 'list' }` as const;
const needsRefresh = listEditing.fire<ListEditingCheckAttributesEvent>( eventName, {
viewElement: element as ViewElement,
modelAttributes: stack[ indent ]
modelAttributes: stack[ indent ].modelAttributes,
modelReferenceElement: stack[ indent ].modelElement
} );

if ( needsRefresh ) {
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-list/src/list/listediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ export type ListEditingCheckAttributesEvent = {
args: [ {
viewElement: ViewElement & { id?: string };
modelAttributes: ListItemAttributesMap;
modelReferenceElement: ListElement;
} ];
return: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ export class ListPropertiesEditing extends Plugin {
// Verify if the list view element (ul or ol) requires refreshing.
listEditing.on<ListEditingCheckAttributesEvent>(
'checkAttributes:list',
( evt, { viewElement, modelAttributes } ) => {
( evt, { viewElement, modelAttributes, modelReferenceElement } ) => {
for ( const strategy of strategies ) {
if ( !strategy.appliesToListItem( modelReferenceElement ) ) {
continue;
}

if ( strategy.getAttributeOnUpcast( viewElement ) != modelAttributes[ strategy.attributeName ] ) {
evt.return = true;
evt.stop();
Expand Down
164 changes: 164 additions & 0 deletions packages/ckeditor5-list/tests/listproperties/listpropertiesediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VirtualTestEditor } from '@ckeditor/ckeditor5-core/tests/_utils/virtual
import { testUtils } from '@ckeditor/ckeditor5-core/tests/_utils/utils.js';
import { Paragraph } from '@ckeditor/ckeditor5-paragraph/src/paragraph.js';
import { UndoEditing } from '@ckeditor/ckeditor5-undo/src/undoediting.js';
import { BoldEditing } from '@ckeditor/ckeditor5-basic-styles';
import { _setModelData, _getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model.js';
import { ListPropertiesEditing } from '../../src/listproperties/listpropertiesediting.js';
import { modelList } from '../list/_utils/utils.js';
Expand Down Expand Up @@ -1089,4 +1090,167 @@ describe( 'ListPropertiesEditing', () => {
} );
} );
} );

describe( 'all styles enabled', () => {
beforeEach( async () => {
editor = await VirtualTestEditor.create( {
plugins: [ Paragraph, ListPropertiesEditing, BoldEditing ],
list: {
properties: { styles: true, startIndex: true, reversed: true }
}
} );

model = editor.model;

stubUid();
} );

afterEach( () => {
return editor.destroy();
} );

it( 'should not reconvert the whole list on single item marker bold set', () => {
editor.setData(
'<ul>' +
'<li>foo 1.</li>' +
'<li>foo 2.' +
'<ul>' +
'<li>foo 2.1.</li>' +
'<li>foo 2.2.</li>' +
'</ul>' +
'</li>' +
'</ul>'
);

editor.model.change( writer => writer.setSelection( editor.model.document.getRoot().getChild( 2 ), 'in' ) );

expect( _getModelData( model ) ).to.equalMarkup(
'<paragraph listIndent="0" listItemId="a00" listStyle="default" listType="bulleted">' +
'foo 1.' +
'</paragraph>' +
'<paragraph listIndent="0" listItemId="a03" listStyle="default" listType="bulleted">' +
'foo 2.' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a01" listStyle="default" listType="bulleted">' +
'[foo 2.1.]' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a02" listStyle="default" listType="bulleted">' +
'foo 2.2.' +
'</paragraph>'
);

const changeDataListener = sinon.spy( () => {
const changes = editor.model.document.differ.getChanges();

expect( changes.length ).to.equal( 2 );

expect( changes[ 0 ].type ).to.equal( 'attribute' );
expect( changes[ 0 ].attributeKey ).to.equal( 'listItemBold' );
expect( changes[ 0 ].attributeOldValue ).to.be.null;
expect( changes[ 0 ].attributeNewValue ).to.be.true;
expect( changes[ 0 ].range.start.path ).to.deep.equal( [ 2 ] );
expect( changes[ 0 ].range.end.path ).to.deep.equal( [ 2, 0 ] );

expect( changes[ 1 ].type ).to.equal( 'attribute' );
expect( changes[ 1 ].attributeKey ).to.equal( 'bold' );
expect( changes[ 1 ].attributeOldValue ).to.be.null;
expect( changes[ 1 ].attributeNewValue ).to.be.true;
expect( changes[ 1 ].range.start.path ).to.deep.equal( [ 2, 0 ] );
expect( changes[ 1 ].range.end.path ).to.deep.equal( [ 2, 8 ] );
} );

editor.model.document.on( 'change:data', changeDataListener );

editor.execute( 'bold' );

expect( changeDataListener.calledOnce ).to.be.true;

expect( _getModelData( model ) ).to.equalMarkup(
'<paragraph listIndent="0" listItemId="a00" listStyle="default" listType="bulleted">' +
'foo 1.' +
'</paragraph>' +
'<paragraph listIndent="0" listItemId="a03" listStyle="default" listType="bulleted">' +
'foo 2.' +
'</paragraph>' +
'<paragraph listIndent="1" listItemBold="true" listItemId="a01" listStyle="default" listType="bulleted">' +
'[<$text bold="true">foo 2.1.</$text>]' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a02" listStyle="default" listType="bulleted">' +
'foo 2.2.' +
'</paragraph>'
);
} );

it( 'should not reconvert the whole list on single item marker bold reset', () => {
editor.setData(
'<ul>' +
'<li>foo 1.</li>' +
'<li>foo 2.' +
'<ul>' +
'<li><b>foo 2.1.</b></li>' +
'<li>foo 2.2.</li>' +
'</ul>' +
'</li>' +
'</ul>'
);

editor.model.change( writer => writer.setSelection( editor.model.document.getRoot().getChild( 2 ), 'in' ) );

expect( _getModelData( model ) ).to.equalMarkup(
'<paragraph listIndent="0" listItemId="a00" listStyle="default" listType="bulleted">' +
'foo 1.' +
'</paragraph>' +
'<paragraph listIndent="0" listItemId="a03" listStyle="default" listType="bulleted">' +
'foo 2.' +
'</paragraph>' +
'<paragraph listIndent="1" listItemBold="true" listItemId="a01" listStyle="default" listType="bulleted">' +
'[<$text bold="true">foo 2.1.</$text>]' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a02" listStyle="default" listType="bulleted">' +
'foo 2.2.' +
'</paragraph>'
);

const changeDataListener = sinon.spy( () => {
const changes = editor.model.document.differ.getChanges();

expect( changes.length ).to.equal( 2 );

expect( changes[ 0 ].type ).to.equal( 'attribute' );
expect( changes[ 0 ].attributeKey ).to.equal( 'listItemBold' );
expect( changes[ 0 ].attributeOldValue ).to.be.true;
expect( changes[ 0 ].attributeNewValue ).to.be.null;
expect( changes[ 0 ].range.start.path ).to.deep.equal( [ 2 ] );
expect( changes[ 0 ].range.end.path ).to.deep.equal( [ 2, 0 ] );

expect( changes[ 1 ].type ).to.equal( 'attribute' );
expect( changes[ 1 ].attributeKey ).to.equal( 'bold' );
expect( changes[ 1 ].attributeOldValue ).to.be.true;
expect( changes[ 1 ].attributeNewValue ).to.be.null;
expect( changes[ 1 ].range.start.path ).to.deep.equal( [ 2, 0 ] );
expect( changes[ 1 ].range.end.path ).to.deep.equal( [ 2, 8 ] );
} );

editor.model.document.on( 'change:data', changeDataListener );

editor.execute( 'bold' );

expect( changeDataListener.calledOnce ).to.be.true;

expect( _getModelData( model ) ).to.equalMarkup(
'<paragraph listIndent="0" listItemId="a00" listStyle="default" listType="bulleted">' +
'foo 1.' +
'</paragraph>' +
'<paragraph listIndent="0" listItemId="a03" listStyle="default" listType="bulleted">' +
'foo 2.' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a01" listStyle="default" listType="bulleted">' +
'[foo 2.1.]' +
'</paragraph>' +
'<paragraph listIndent="1" listItemId="a02" listStyle="default" listType="bulleted">' +
'foo 2.2.' +
'</paragraph>'
);
} );
} );
} );