Преглед изворни кода

Merge branch t/ckeditor5-engine/858

Internal: Update API usage after merge t/858 on engine.
Piotr Jasiun пре 8 година
родитељ
комит
f1bcd3f9c1

+ 15 - 12
packages/ckeditor5-list/src/converters.js

@@ -9,10 +9,8 @@
 
 import ViewListItemElement from './viewlistitemelement';
 
-import ModelDocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
 import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
-import modelWriter from '@ckeditor/ckeditor5-engine/src/model/writer';
 
 import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
@@ -351,16 +349,18 @@ export function modelViewMergeAfter( evt, data, consumable, conversionApi ) {
  */
 export function viewModelConverter( evt, data, consumable, conversionApi ) {
 	if ( consumable.consume( data.input, { name: true } ) ) {
+		const batch = data.batch;
+
 		// 1. Create `listItem` model element.
-		const listItem = new ModelElement( 'listItem' );
+		const listItem = batch.createElement( 'listItem' );
 
 		// 2. Handle `listItem` model element attributes.
 		data.indent = data.indent ? data.indent : 0;
-		listItem.setAttribute( 'indent', data.indent );
+		batch.setAttribute( 'indent', data.indent, listItem );
 
 		// Set 'bulleted' as default. If this item is pasted into a context,
 		const type = data.input.parent && data.input.parent.name == 'ol' ? 'numbered' : 'bulleted';
-		listItem.setAttribute( 'type', type );
+		batch.setAttribute( 'type', type, listItem );
 
 		// 3. Handle `<li>` children.
 		data.context.push( listItem );
@@ -369,8 +369,8 @@ export function viewModelConverter( evt, data, consumable, conversionApi ) {
 		data.indent++;
 
 		// `listItem`s will be kept in flat structure.
-		const items = new ModelDocumentFragment();
-		items.appendChildren( listItem );
+		const items = batch.createDocumentFragment();
+		batch.append( listItem, items );
 
 		// Check all children of the converted `<li>`.
 		// At this point we assume there are no "whitespace" view text nodes in view list, between view list items.
@@ -382,11 +382,13 @@ export function viewModelConverter( evt, data, consumable, conversionApi ) {
 			// If this is a view list element, we will convert it and concat the result (`listItem` model elements)
 			// with already gathered results (in `items` array). `converted` should be a `ModelDocumentFragment`.
 			if ( child.name == 'ul' || child.name == 'ol' ) {
-				items.appendChildren( Array.from( converted.getChildren() ) );
+				for ( const child of Array.from( converted.getChildren() ) ) {
+					batch.append( child, items );
+				}
 			}
 			// If it was not a list it was a "regular" list item content. Just append it to `listItem`.
 			else {
-				modelWriter.insert( ModelPosition.createAt( listItem, 'end' ), converted );
+				batch.append( converted, listItem );
 			}
 		}
 
@@ -612,7 +614,8 @@ export function modelChangePostFixer( document ) {
 
 			// Element name is changed from list to something else. Remove useless attributes.
 			document.enqueueChanges( () => {
-				batch.removeAttribute( element, 'indent' ).removeAttribute( element, 'type' );
+				batch.removeAttribute( 'indent', element );
+				batch.removeAttribute( 'type', element );
 			} );
 
 			const changePos = ModelPosition.createAfter( changes.element );
@@ -666,7 +669,7 @@ function _fixItemsIndent( changePosition, document, batch ) {
 			if ( items.length > 0 ) {
 				// Since we are outdenting list items, it is safer to start from the last one (it will maintain correct model state).
 				for ( const item of items.reverse() ) {
-					batch.setAttribute( item.item, 'indent', item.indent );
+					batch.setAttribute( 'indent', item.indent, item.item );
 				}
 			}
 		} );
@@ -700,7 +703,7 @@ function _fixItemsType( changePosition, fixPrevious, document, batch ) {
 
 		while ( item && item.is( 'listItem' ) && item.getAttribute( 'indent' ) >= refIndent ) {
 			if ( item.getAttribute( 'type' ) != refType && item.getAttribute( 'indent' ) == refIndent ) {
-				batch.setAttribute( item, 'type', refType );
+				batch.setAttribute( 'type', refType, item );
 			}
 
 			item = item[ fixPrevious ? 'previousSibling' : 'nextSibling' ];

+ 3 - 3
packages/ckeditor5-list/src/indentcommand.js

@@ -87,7 +87,7 @@ export default class IndentCommand extends Command {
 				}
 				// If indent is >= 0, change the attribute value.
 				else {
-					batch.setAttribute( item, 'indent', indent );
+					batch.setAttribute( 'indent', indent, item );
 				}
 			}
 
@@ -155,7 +155,7 @@ function _fixType( item, batch ) {
 
 	// If found, fix type.
 	if ( prev ) {
-		batch.setAttribute( item, 'type', prev.getAttribute( 'type' ) );
+		batch.setAttribute( 'type', prev.getAttribute( 'type' ), item );
 
 		return;
 	}
@@ -165,7 +165,7 @@ function _fixType( item, batch ) {
 
 	// If found, fix type.
 	if ( next ) {
-		batch.setAttribute( item, 'type', next.getAttribute( 'type' ) );
+		batch.setAttribute( 'type', next.getAttribute( 'type' ), item );
 	}
 }
 

+ 4 - 3
packages/ckeditor5-list/src/listcommand.js

@@ -154,7 +154,7 @@ export default class ListCommand extends Command {
 				changes = changes.reverse();
 
 				for ( const item of changes ) {
-					batch.setAttribute( item.element, 'indent', item.indent );
+					batch.setAttribute( 'indent', item.indent, item.element );
 				}
 			}
 
@@ -208,11 +208,12 @@ export default class ListCommand extends Command {
 				} else if ( !turnOff && element.name != 'listItem' ) {
 					// We are turning on and the element is not a `listItem` - it should be converted to `listItem`.
 					// The order of operations is important to keep model in correct state.
-					batch.setAttribute( element, 'type', this.type ).setAttribute( element, 'indent', 0 ).rename( element, 'listItem' );
+					batch.setAttributes( { type: this.type, indent: 0 }, element );
+					batch.rename( element, 'listItem' );
 				} else if ( !turnOff && element.name == 'listItem' && element.getAttribute( 'type' ) != this.type ) {
 					// We are turning on and the element is a `listItem` but has different type - change it's type and
 					// type of it's all siblings that have same indent.
-					batch.setAttribute( element, 'type', this.type );
+					batch.setAttribute( 'type', this.type, element );
 				}
 			}
 		} );

+ 29 - 23
packages/ckeditor5-list/tests/listengine.js

@@ -1543,9 +1543,10 @@ describe( 'ListEngine', () => {
 						const item2 = '<listItem indent="1" type="bulleted">d</listItem>';
 
 						modelDoc.enqueueChanges( () => {
-							modelDoc.batch()
-								.insert( ModelPosition.createAt( modelRoot, 'end' ), parseModel( item1, modelDoc.schema ) )
-								.insert( ModelPosition.createAt( modelRoot, 'end' ), parseModel( item2, modelDoc.schema ) );
+							const batch = modelDoc.batch();
+
+							batch.append( parseModel( item1, modelDoc.schema, modelDoc.batch() ), modelRoot );
+							batch.append( parseModel( item2, modelDoc.schema, modelDoc.batch() ), modelRoot );
 						} );
 					}
 				);
@@ -2660,7 +2661,8 @@ describe( 'ListEngine', () => {
 			setModelData( modelDoc, input );
 
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch( 'transparent' ).insert( modelDoc.selection.getFirstPosition(), parseModel( inserted, modelDoc.schema ) );
+				modelDoc.batch( 'transparent' )
+					.insert( parseModel( inserted, modelDoc.schema, modelDoc.batch() ), modelDoc.selection.getFirstPosition() );
 			} );
 
 			expect( getModelData( modelDoc, { withoutSelection: true } ) ).to.equal( output );
@@ -2672,7 +2674,8 @@ describe( 'ListEngine', () => {
 					setModelData( modelDoc, input );
 
 					modelDoc.enqueueChanges( () => {
-						modelDoc.batch().insert( modelDoc.selection.getFirstPosition(), parseModel( inserted, modelDoc.schema ) );
+						modelDoc.batch()
+							.insert( parseModel( inserted, modelDoc.schema, modelDoc.batch() ), modelDoc.selection.getFirstPosition() );
 					} );
 
 					expect( getModelData( modelDoc, { withoutSelection: true } ) ).to.equal( output );
@@ -2806,9 +2809,10 @@ describe( 'ListEngine', () => {
 				const item2 = '<listItem indent="1" type="bulleted">d</listItem>';
 
 				modelDoc.enqueueChanges( () => {
-					modelDoc.batch()
-						.insert( ModelPosition.createAt( modelRoot, 'end' ), parseModel( item1, modelDoc.schema ) )
-						.insert( ModelPosition.createAt( modelRoot, 'end' ), parseModel( item2, modelDoc.schema ) );
+					const batch = modelDoc.batch();
+
+					batch.append( parseModel( item1, modelDoc.schema, modelDoc.batch() ), modelRoot );
+					batch.append( parseModel( item2, modelDoc.schema, modelDoc.batch() ), modelRoot );
 				} );
 
 				expect( getModelData( modelDoc, { withoutSelection: true } ) ).to.equal( output );
@@ -3075,7 +3079,8 @@ describe( 'ListEngine', () => {
 					parseModel(
 						'<listItem type="bulleted" indent="0">X</listItem>' +
 						'<listItem type="bulleted" indent="1">Y</listItem>',
-						modelDoc.schema
+						modelDoc.schema,
+						modelDoc.batch()
 					),
 					modelDoc.selection
 				);
@@ -3348,7 +3353,7 @@ describe( 'ListEngine', () => {
 
 			setModelData( modelDoc, '<listItem indent="0" type="bulleted"></listItem>' );
 
-			modelDoc.batch().setAttribute( modelRoot.getChild( 0 ), 'type', 'numbered' );
+			modelDoc.batch().setAttribute( 'type', 'numbered', modelRoot.getChild( 0 ) );
 
 			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<ul><li></li></ul>' );
 		} );
@@ -3360,7 +3365,7 @@ describe( 'ListEngine', () => {
 
 			setModelData( modelDoc, '<listItem indent="0" type="bulleted">a</listItem><listItem indent="0" type="bulleted">b</listItem>' );
 
-			modelDoc.batch().setAttribute( modelRoot.getChild( 1 ), 'indent', 1 );
+			modelDoc.batch().setAttribute( 'indent', 1, modelRoot.getChild( 1 ) );
 
 			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<ul><li>a</li><li>b</li></ul>' );
 		} );
@@ -3406,7 +3411,7 @@ describe( 'ListEngine', () => {
 				.to.equal( '<ul><li>Foo<span></span></li><li>Bar</li></ul>' );
 
 			// Change indent of the second list item.
-			modelDoc.batch().setAttribute( modelRoot.getChild( 1 ), 'indent', 1 );
+			modelDoc.batch().setAttribute( 'indent', 1, modelRoot.getChild( 1 ) );
 
 			// Check if the new <ul> was added at correct position.
 			expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
@@ -3508,7 +3513,7 @@ describe( 'ListEngine', () => {
 
 		const actionCallback = () => {
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch().insert( modelDoc.selection.getFirstPosition(), parseModel( item, modelDoc.schema ) );
+				modelDoc.batch().insert( parseModel( item, modelDoc.schema, modelDoc.batch() ), modelDoc.selection.getFirstPosition() );
 			} );
 		};
 
@@ -3531,7 +3536,7 @@ describe( 'ListEngine', () => {
 			const newType = element.getAttribute( 'type' ) == 'numbered' ? 'bulleted' : 'numbered';
 
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch().setAttribute( modelDoc.selection.getFirstRange(), 'type', newType );
+				modelDoc.batch().setAttribute( 'type', newType, modelDoc.selection.getFirstRange() );
 			} );
 		};
 
@@ -3543,10 +3548,11 @@ describe( 'ListEngine', () => {
 			const element = modelDoc.selection.getFirstPosition().nodeAfter;
 
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch()
-					.rename( element, 'paragraph' )
-					.removeAttribute( element, 'type' )
-					.removeAttribute( element, 'indent' );
+				const batch = modelDoc.batch();
+
+				batch.rename( element, 'paragraph' );
+				batch.removeAttribute( 'type', element );
+				batch.removeAttribute( 'indent', element );
 			} );
 		};
 
@@ -3558,10 +3564,10 @@ describe( 'ListEngine', () => {
 			const element = modelDoc.selection.getFirstPosition().nodeAfter;
 
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch()
-					.setAttribute( element, 'type', 'bulleted' )
-					.setAttribute( element, 'indent', newIndent )
-					.rename( element, 'listItem' );
+				const batch = modelDoc.batch();
+
+				batch.setAttributes( { type: 'bulleted', indent: newIndent }, element );
+				batch.rename( element, 'listItem' );
 			} );
 		};
 
@@ -3571,7 +3577,7 @@ describe( 'ListEngine', () => {
 	function testChangeIndent( testName, newIndent, input, output ) {
 		const actionCallback = () => {
 			modelDoc.enqueueChanges( () => {
-				modelDoc.batch().setAttribute( modelDoc.selection.getFirstRange(), 'indent', newIndent );
+				modelDoc.batch().setAttribute( 'indent', newIndent, modelDoc.selection.getFirstRange() );
 			} );
 		};