Przeglądaj źródła

Aligned code with engine changes.

Oskar Wróbel 8 lat temu
rodzic
commit
4cbfe4c237

+ 15 - 15
packages/ckeditor5-paragraph/src/paragraph.js

@@ -10,7 +10,7 @@
 import ParagraphCommand from './paragraphcommand';
 import ParagraphCommand from './paragraphcommand';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
 
-import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
+import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
 
 import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
 import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
 import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
 import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
@@ -34,14 +34,15 @@ export default class Paragraph extends Plugin {
 	 */
 	 */
 	init() {
 	init() {
 		const editor = this.editor;
 		const editor = this.editor;
-		const doc = editor.document;
+		const model = editor.model;
+		const doc = model.document;
 		const data = editor.data;
 		const data = editor.data;
 		const editing = editor.editing;
 		const editing = editor.editing;
 
 
 		editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
 		editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
 
 
 		// Schema.
 		// Schema.
-		doc.schema.registerItem( 'paragraph', '$block' );
+		model.schema.registerItem( 'paragraph', '$block' );
 
 
 		// Build converter from model to view for data and editing pipelines.
 		// Build converter from model to view for data and editing pipelines.
 		buildModelConverter().for( data.modelToView, editing.modelToView )
 		buildModelConverter().for( data.modelToView, editing.modelToView )
@@ -85,10 +86,10 @@ export default class Paragraph extends Plugin {
 
 
 			findEmptyRoots( doc, batch );
 			findEmptyRoots( doc, batch );
 		} );
 		} );
-		doc.on( 'changesDone', autoparagraphEmptyRoots, { priority: 'lowest' } );
+		doc.on( 'changesDone', () => autoparagraphEmptyRoots( model ), { priority: 'lowest' } );
 		editor.on( 'dataReady', () => {
 		editor.on( 'dataReady', () => {
-			findEmptyRoots( doc, doc.batch( 'transparent' ) );
-			autoparagraphEmptyRoots();
+			findEmptyRoots( doc, new Batch( 'transparent' ) );
+			autoparagraphEmptyRoots( model );
 		}, { priority: 'lowest' } );
 		}, { priority: 'lowest' } );
 	}
 	}
 }
 }
@@ -215,8 +216,9 @@ function autoparagraphItems( evt, data, consumable, conversionApi ) {
 		if ( isParagraphable( child, data.context, conversionApi.schema, isParagraphLike ) ) {
 		if ( isParagraphable( child, data.context, conversionApi.schema, isParagraphLike ) ) {
 			// If there is no wrapping `paragraph` element, create it.
 			// If there is no wrapping `paragraph` element, create it.
 			if ( !autoParagraph ) {
 			if ( !autoParagraph ) {
-				autoParagraph = new ModelElement( 'paragraph' );
-				data.output.insertChildren( child.index, autoParagraph );
+				conversionApi.writer.insertElement( 'paragraph', data.output, child.index );
+				autoParagraph = conversionApi.writer.createElement( 'paragraph' );
+				conversionApi.writer.insert( autoParagraph, data.output, child.index );
 			}
 			}
 			// Otherwise, use existing `paragraph` and just fix iterator.
 			// Otherwise, use existing `paragraph` and just fix iterator.
 			// Thanks to reusing `paragraph` element, multiple siblings ends up in same container.
 			// Thanks to reusing `paragraph` element, multiple siblings ends up in same container.
@@ -224,8 +226,7 @@ function autoparagraphItems( evt, data, consumable, conversionApi ) {
 				i--;
 				i--;
 			}
 			}
 
 
-			child.remove();
-			autoParagraph.appendChildren( child );
+			conversionApi.writer.append( child, autoParagraph );
 		} else {
 		} else {
 			// That was not a paragraphable children, reset `paragraph` wrapper - following auto-paragraphable children
 			// That was not a paragraphable children, reset `paragraph` wrapper - following auto-paragraphable children
 			// need to be placed in a new `paragraph` element.
 			// need to be placed in a new `paragraph` element.
@@ -278,23 +279,22 @@ function findEmptyRoots( doc, batch ) {
 }
 }
 
 
 // Fixes all empty roots.
 // Fixes all empty roots.
-function autoparagraphEmptyRoots() {
+function autoparagraphEmptyRoots( model ) {
 	for ( const [ root, batch ] of rootsToFix ) {
 	for ( const [ root, batch ] of rootsToFix ) {
 		// Only empty roots are in `rootsToFix`. Even if root got content during `changesDone` event (because of, for example
 		// Only empty roots are in `rootsToFix`. Even if root got content during `changesDone` event (because of, for example
 		// other feature), this will fire `findEmptyRoots` and remove that root from `rootsToFix`. So we are guaranteed
 		// other feature), this will fire `findEmptyRoots` and remove that root from `rootsToFix`. So we are guaranteed
 		// to have only empty roots here.
 		// to have only empty roots here.
 		const query = { name: 'paragraph', inside: [ root ] };
 		const query = { name: 'paragraph', inside: [ root ] };
-		const doc = batch.document;
-		const schema = doc.schema;
+		const schema = model.schema;
 
 
 		// If paragraph element is allowed in the root, create paragraph element.
 		// If paragraph element is allowed in the root, create paragraph element.
 		if ( schema.check( query ) ) {
 		if ( schema.check( query ) ) {
-			doc.enqueueChanges( () => {
+			model.enqueueChange( batch, writer => {
 				// Remove root from `rootsToFix` here, before executing batch, to prevent infinite loops.
 				// Remove root from `rootsToFix` here, before executing batch, to prevent infinite loops.
 				rootsToFix.delete( root );
 				rootsToFix.delete( root );
 
 
 				// Fix empty root.
 				// Fix empty root.
-				batch.insertElement( 'paragraph', root );
+				writer.insertElement( 'paragraph', root );
 			} );
 			} );
 		}
 		}
 	}
 	}

+ 8 - 7
packages/ckeditor5-paragraph/src/paragraphcommand.js

@@ -29,11 +29,12 @@ export default class ParagraphCommand extends Command {
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	refresh() {
 	refresh() {
-		const document = this.editor.document;
+		const model = this.editor.model;
+		const document = model.document;
 		const block = first( document.selection.getSelectedBlocks() );
 		const block = first( document.selection.getSelectedBlocks() );
 
 
 		this.value = !!block && block.is( 'paragraph' );
 		this.value = !!block && block.is( 'paragraph' );
-		this.isEnabled = !!block && checkCanBecomeParagraph( block, document.schema );
+		this.isEnabled = !!block && checkCanBecomeParagraph( block, model.schema );
 	}
 	}
 
 
 	/**
 	/**
@@ -48,15 +49,15 @@ export default class ParagraphCommand extends Command {
 	 * By default, if not provided, the command is applied to the {@link module:engine/model/document~Document#selection}.
 	 * By default, if not provided, the command is applied to the {@link module:engine/model/document~Document#selection}.
 	 */
 	 */
 	execute( options = {} ) {
 	execute( options = {} ) {
-		const document = this.editor.document;
+		const model = this.editor.model;
+		const document = model.document;
 
 
-		document.enqueueChanges( () => {
-			const batch = options.batch || document.batch();
+		model.enqueueChange( writer => {
 			const blocks = ( options.selection || document.selection ).getSelectedBlocks();
 			const blocks = ( options.selection || document.selection ).getSelectedBlocks();
 
 
 			for ( const block of blocks ) {
 			for ( const block of blocks ) {
-				if ( !block.is( 'paragraph' ) && checkCanBecomeParagraph( block, document.schema ) ) {
-					batch.rename( block, 'paragraph' );
+				if ( !block.is( 'paragraph' ) && checkCanBecomeParagraph( block, model.schema ) ) {
+					writer.rename( block, 'paragraph' );
 				}
 				}
 			}
 			}
 		} );
 		} );