Sfoglia il codice sorgente

Aligned code with the engine changes.

Oskar Wróbel 8 anni fa
parent
commit
5b3314da16

+ 32 - 32
packages/ckeditor5-list/src/converters.js

@@ -349,18 +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 = conversionApi.batch;
+		const writer = conversionApi.writer;
 
 		// 1. Create `listItem` model element.
-		const listItem = batch.createElement( 'listItem' );
+		const listItem = writer.createElement( 'listItem' );
 
 		// 2. Handle `listItem` model element attributes.
 		data.indent = data.indent ? data.indent : 0;
-		batch.setAttribute( 'indent', data.indent, listItem );
+		writer.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';
-		batch.setAttribute( 'type', type, listItem );
+		writer.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 = batch.createDocumentFragment();
-		batch.append( listItem, items );
+		const items = writer.createDocumentFragment();
+		writer.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,11 @@ 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' ) {
-				batch.append( converted, items );
+				writer.append( converted, items );
 			}
 			// If it was not a list it was a "regular" list item content. Just append it to `listItem`.
 			else {
-				batch.append( converted, listItem );
+				writer.append( converted, listItem );
 			}
 		}
 
@@ -568,10 +568,10 @@ export function viewToModelPosition( evt, data ) {
  *		<listItem type="bulleted" indent=0>Item 1</listItem>
  *		<listItem type="bulleted" indent=1>Item 3</listItem>   <--- note that indent got post-fixed.
  *
- * @param {module:engine/model/document~Document} document The document to observe.
+ * @param {module:engine/model/model~Model} model The data model.
  * @returns {Function} A callback to be attached to the {@link module:engine/model/document~Document#event:change document change event}.
  */
-export function modelChangePostFixer( document ) {
+export function modelChangePostFixer( model ) {
 	return ( evt, type, changes, batch ) => {
 		if ( batch.type == 'transparent' ) {
 			return;
@@ -583,67 +583,67 @@ export function modelChangePostFixer( document ) {
 
 			// Fix list items after the cut-out range.
 			// This fix is needed if items in model after cut-out range have now wrong indents compared to their previous siblings.
-			_fixItemsIndent( sourcePos, document, batch );
+			_fixItemsIndent( sourcePos, model, batch );
 			// This fix is needed if two different nested lists got merged, change types of list items "below".
-			_fixItemsType( sourcePos, false, document, batch );
+			_fixItemsType( sourcePos, false, model, batch );
 		} else if ( type == 'move' ) {
 			const howMany = changes.range.end.offset - changes.range.start.offset;
 			const sourcePos = changes.sourcePosition._getTransformedByInsertion( changes.range.start, howMany, true );
 
 			// Fix list items after the cut-out range.
 			// This fix is needed if items in model after cut-out range have now wrong indents compared to their previous siblings.
-			_fixItemsIndent( sourcePos, document, batch );
+			_fixItemsIndent( sourcePos, model, batch );
 			// This fix is needed if two different nested lists got merged, change types of list items "below".
-			_fixItemsType( sourcePos, false, document, batch );
+			_fixItemsType( sourcePos, false, model, batch );
 
 			// Fix items in moved range.
 			// This fix is needed if inserted items are too deeply intended.
-			_fixItemsIndent( changes.range.start, document, batch );
+			_fixItemsIndent( changes.range.start, model, batch );
 			// This fix is needed if one or more first inserted items have different type.
-			_fixItemsType( changes.range.start, false, document, batch );
+			_fixItemsType( changes.range.start, false, model, batch );
 
 			// Fix list items after inserted range.
 			// This fix is needed if items in model after inserted range have wrong indents.
-			_fixItemsIndent( changes.range.end, document, batch );
+			_fixItemsIndent( changes.range.end, model, batch );
 			// This fix is needed if one or more last inserted items have different type.
-			_fixItemsType( changes.range.end, true, document, batch );
+			_fixItemsType( changes.range.end, true, model, batch );
 		} else if ( type == 'rename' && changes.oldName == 'listItem' && changes.newName != 'listItem' ) {
 			const element = changes.element;
 
 			// Element name is changed from list to something else. Remove useless attributes.
-			document.enqueueChanges( () => {
-				batch.removeAttribute( 'indent', element );
-				batch.removeAttribute( 'type', element );
+			model.enqueueChange( batch, writer => {
+				writer.removeAttribute( 'indent', element );
+				writer.removeAttribute( 'type', element );
 			} );
 
 			const changePos = ModelPosition.createAfter( changes.element );
 
 			// Fix list items after the renamed element.
 			// This fix is needed if there are items after renamed element, those items should start from indent = 0.
-			_fixItemsIndent( changePos, document, batch );
+			_fixItemsIndent( changePos, model, batch );
 		} else if ( type == 'insert' ) {
 			// Fix list items in inserted range.
 			// This fix is needed if inserted items are too deeply intended.
-			_fixItemsIndent( changes.range.start, document, batch );
+			_fixItemsIndent( changes.range.start, model, batch );
 			// This fix is needed if one or more first inserted items have different type.
-			_fixItemsType( changes.range.start, false, document, batch );
+			_fixItemsType( changes.range.start, false, model, batch );
 
 			// Fix list items after inserted range.
 			// This fix is needed if items in model after inserted range have wrong indents.
-			_fixItemsIndent( changes.range.end, document, batch );
+			_fixItemsIndent( changes.range.end, model, batch );
 			// This fix is needed if one or more last inserted items have different type.
-			_fixItemsType( changes.range.end, true, document, batch );
+			_fixItemsType( changes.range.end, true, model, batch );
 		}
 	};
 }
 
 // Helper function for post fixer callback. Performs fixing of model `listElement` items indent attribute. Checks the model at the
 // `changePosition`. Looks at the node before position where change occurred and uses that node as a reference for following list items.
-function _fixItemsIndent( changePosition, document, batch ) {
+function _fixItemsIndent( changePosition, model, batch ) {
 	let nextItem = changePosition.nodeAfter;
 
 	if ( nextItem && nextItem.name == 'listItem' ) {
-		document.enqueueChanges( () => {
+		model.enqueueChange( batch, writer => {
 			const prevItem = nextItem.previousSibling;
 			// This is the maximum indent that following model list item may have.
 			const maxIndent = prevItem && prevItem.is( 'listItem' ) ? prevItem.getAttribute( 'indent' ) + 1 : 0;
@@ -667,7 +667,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( 'indent', item.indent, item.item );
+					writer.setAttribute( 'indent', item.indent, item.item );
 				}
 			}
 		} );
@@ -677,7 +677,7 @@ function _fixItemsIndent( changePosition, document, batch ) {
 // Helper function for post fixer callback. Performs fixing of model nested `listElement` items type attribute.
 // Checks the model at the `changePosition`. Looks at nodes after/before that position and changes those items type
 // to the same as node before/after `changePosition`.
-function _fixItemsType( changePosition, fixPrevious, document, batch ) {
+function _fixItemsType( changePosition, fixPrevious, model, batch ) {
 	let item = changePosition[ fixPrevious ? 'nodeBefore' : 'nodeAfter' ];
 
 	if ( !item || !item.is( 'listItem' ) || item.getAttribute( 'indent' ) === 0 ) {
@@ -687,7 +687,7 @@ function _fixItemsType( changePosition, fixPrevious, document, batch ) {
 		return;
 	}
 
-	document.enqueueChanges( () => {
+	model.enqueueChange( batch, writer => {
 		const refItem = _getBoundaryItemOfSameList( item, !fixPrevious );
 
 		if ( !refItem || refItem == item ) {
@@ -701,7 +701,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( 'type', refType, item );
+				writer.setAttribute( 'type', refType, item );
 			}
 
 			item = item[ fixPrevious ? 'previousSibling' : 'nextSibling' ];

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

@@ -49,11 +49,11 @@ export default class IndentCommand extends Command {
 	 * @fires execute
 	 */
 	execute() {
-		const doc = this.editor.document;
-		const batch = doc.batch();
+		const model = this.editor.model;
+		const doc = model.document;
 		let itemsToChange = Array.from( doc.selection.getSelectedBlocks() );
 
-		doc.enqueueChanges( () => {
+		model.change( writer => {
 			const lastItem = itemsToChange[ itemsToChange.length - 1 ];
 
 			// Indenting a list item should also indent all the items that are already sub-items of indented item.
@@ -83,11 +83,11 @@ export default class IndentCommand extends Command {
 					// To keep the model as correct as possible, first rename listItem, then remove attributes,
 					// as listItem without attributes is very incorrect and will cause problems in converters.
 					// No need to remove attributes, will be removed by post fixer.
-					batch.rename( item, 'paragraph' );
+					writer.rename( item, 'paragraph' );
 				}
 				// If indent is >= 0, change the attribute value.
 				else {
-					batch.setAttribute( 'indent', indent, item );
+					writer.setAttribute( 'indent', indent, item );
 				}
 			}
 
@@ -98,7 +98,7 @@ export default class IndentCommand extends Command {
 			}
 
 			for ( const item of itemsToChange ) {
-				_fixType( item, batch );
+				_fixType( item, writer );
 			}
 		} );
 	}
@@ -111,7 +111,7 @@ export default class IndentCommand extends Command {
 	 */
 	_checkEnabled() {
 		// Check whether any of position's ancestor is a list item.
-		const listItem = first( this.editor.document.selection.getSelectedBlocks() );
+		const listItem = first( this.editor.model.document.selection.getSelectedBlocks() );
 
 		// If selection is not in a list item, the command is disabled.
 		if ( !listItem || !listItem.is( 'listItem' ) ) {
@@ -149,13 +149,13 @@ export default class IndentCommand extends Command {
 
 // Fixes type of `item` element after it was indented/outdented. Looks for a sibling of `item` that has the same
 // indent and sets `item`'s type to the same as that sibling.
-function _fixType( item, batch ) {
+function _fixType( item, writer ) {
 	// Find a preceding sibling of `item` that is a list item of the same list as `item`.
 	const prev = _seekListItem( item, false );
 
 	// If found, fix type.
 	if ( prev ) {
-		batch.setAttribute( 'type', prev.getAttribute( 'type' ), item );
+		writer.setAttribute( 'type', prev.getAttribute( 'type' ), item );
 
 		return;
 	}
@@ -165,7 +165,7 @@ function _fixType( item, batch ) {
 
 	// If found, fix type.
 	if ( next ) {
-		batch.setAttribute( 'type', next.getAttribute( 'type' ), item );
+		writer.setAttribute( 'type', next.getAttribute( 'type' ), item );
 	}
 }
 

+ 2 - 2
packages/ckeditor5-list/src/list.js

@@ -50,7 +50,7 @@ export default class List extends Plugin {
 		// Overwrite default Enter key behavior.
 		// If Enter key is pressed with selection collapsed in empty list item, outdent it instead of breaking it.
 		this.listenTo( this.editor.editing.view, 'enter', ( evt, data ) => {
-			const doc = this.editor.document;
+			const doc = this.editor.model.document;
 			const positionParent = doc.selection.getLastPosition().parent;
 
 			if ( doc.selection.isCollapsed && positionParent.name == 'listItem' && positionParent.isEmpty ) {
@@ -69,7 +69,7 @@ export default class List extends Plugin {
 				return;
 			}
 
-			const selection = this.editor.document.selection;
+			const selection = this.editor.model.document.selection;
 
 			if ( !selection.isCollapsed ) {
 				return;

+ 13 - 17
packages/ckeditor5-list/src/listcommand.js

@@ -55,22 +55,18 @@ export default class ListCommand extends Command {
 	 * Executes the command.
 	 *
 	 * @protected
-	 * @param {Object} [options] Options for the executed command.
-	 * @param {module:engine/model/batch~Batch} [options.batch] A batch to collect all the change steps.
-	 * A new batch will be created if this option is not set.
 	 */
-	execute( options = {} ) {
-		const document = this.editor.document;
+	execute() {
+		const model = this.editor.model;
+		const document = model.document;
 		const blocks = Array.from( document.selection.getSelectedBlocks() )
-			.filter( block => checkCanBecomeListItem( block, document.schema ) );
+			.filter( block => checkCanBecomeListItem( block, model.schema ) );
 
 		// Whether we are turning off some items.
 		const turnOff = this.value === true;
 		// If we are turning off items, we are going to rename them to paragraphs.
 
-		document.enqueueChanges( () => {
-			const batch = options.batch || document.batch();
-
+		model.change( writer => {
 			// If part of a list got turned off, we need to handle (outdent) all of sub-items of the last turned-off item.
 			// To be sure that model is all the time in a good state, we first fix items below turned-off item.
 			if ( turnOff ) {
@@ -154,7 +150,7 @@ export default class ListCommand extends Command {
 				changes = changes.reverse();
 
 				for ( const item of changes ) {
-					batch.setAttribute( 'indent', item.indent, item.element );
+					writer.setAttribute( 'indent', item.indent, item.element );
 				}
 			}
 
@@ -204,16 +200,16 @@ export default class ListCommand extends Command {
 				if ( turnOff && element.name == 'listItem' ) {
 					// We are turning off and the element is a `listItem` - it should be converted to `paragraph`.
 					// List item specific attributes are removed by post fixer.
-					batch.rename( element, 'paragraph' );
+					writer.rename( element, 'paragraph' );
 				} 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.setAttributes( { type: this.type, indent: 0 }, element );
-					batch.rename( element, 'listItem' );
+					writer.setAttributes( { type: this.type, indent: 0 }, element );
+					writer.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( 'type', this.type, element );
+					writer.setAttribute( 'type', this.type, element );
 				}
 			}
 		} );
@@ -227,7 +223,7 @@ export default class ListCommand extends Command {
 	 */
 	_getValue() {
 		// Check whether closest `listItem` ancestor of the position has a correct type.
-		const listItem = first( this.editor.document.selection.getSelectedBlocks() );
+		const listItem = first( this.editor.model.document.selection.getSelectedBlocks() );
 
 		return !!listItem && listItem.is( 'listItem' ) && listItem.getAttribute( 'type' ) == this.type;
 	}
@@ -244,8 +240,8 @@ export default class ListCommand extends Command {
 			return true;
 		}
 
-		const selection = this.editor.document.selection;
-		const schema = this.editor.document.schema;
+		const selection = this.editor.model.document.selection;
+		const schema = this.editor.model.schema;
 
 		const firstBlock = first( selection.getSelectedBlocks() );
 

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

@@ -53,11 +53,12 @@ export default class ListEngine extends Plugin {
 		// Note: in case `$block` will be ever allowed in `listItem`, keep in mind that this feature
 		// uses `Selection#getSelectedBlocks()` without any additional processing to obtain all selected list items.
 		// If there are blocks allowed inside list item, algorithms using `getSelectedBlocks()` will have to be modified.
-		const schema = editor.document.schema;
+		const schema = editor.model.schema;
 		schema.registerItem( 'listItem', '$block' );
 		schema.allow( {
 			name: 'listItem',
 			inside: '$root',
+			coinside: '$root',
 			attributes: [ 'type', 'indent' ]
 		} );
 		schema.requireAttributes( 'listItem', [ 'type', 'indent' ] );
@@ -66,11 +67,11 @@ export default class ListEngine extends Plugin {
 		const data = editor.data;
 		const editing = editor.editing;
 
-		this.editor.document.on( 'change', modelChangePostFixer( this.editor.document ), { priority: 'high' } );
+		this.editor.model.document.on( 'change', modelChangePostFixer( this.editor.model ), { priority: 'high' } );
 
 		// Unbind all moved model elements before conversion happens. This is important for converters.
 		// TODO: fix this when changes are converted on `changesDone`.
-		this.editor.document.on( 'change', ( evt, type, changes ) => {
+		this.editor.model.document.on( 'change', ( evt, type, changes ) => {
 			if ( type == 'move' ) {
 				for ( const item of changes.range.getItems() ) {
 					if ( item.is( 'listItem' ) ) {