瀏覽代碼

Update API usage after refactoring.

Piotr Jasiun 8 年之前
父節點
當前提交
6d3a804700

+ 2 - 4
packages/ckeditor5-autoformat/src/autoformat.js

@@ -117,10 +117,8 @@ export default class Autoformat extends Plugin {
 				const pattern = new RegExp( `^(#{${ level }})\\s$` );
 
 				// eslint-disable-next-line no-new
-				new BlockAutoformatEngine( this.editor, pattern, context => {
-					const { batch } = context;
-
-					this.editor.execute( commandName, { batch } );
+				new BlockAutoformatEngine( this.editor, pattern, () => {
+					this.editor.execute( commandName );
 				} );
 			} );
 	}

+ 9 - 16
packages/ckeditor5-autoformat/src/blockautoformatengine.js

@@ -36,11 +36,10 @@ export default class BlockAutoformatEngine {
 	 * To convert a paragraph to heading 1 when `- ` is typed, using just the callback:
 	 *
 	 *		new BlockAutoformatEngine( editor, /^\- $/, ( context ) => {
-	 *			const { batch, match } = context;
+	 *			const { match } = context;
 	 *			const headingLevel = match[ 1 ].length;
 	 *
 	 *			editor.execute( 'heading', {
-	 *				batch,
 	 *				formatId: `heading${ headingLevel }`
 	 *			} );
 	 * 		} );
@@ -48,8 +47,7 @@ export default class BlockAutoformatEngine {
 	 * @param {module:core/editor/editor~Editor} editor The editor instance.
 	 * @param {RegExp} pattern The regular expression to execute on just inserted text.
 	 * @param {Function|String} callbackOrCommand The callback to execute or the command to run when the text is matched.
-	 * In case of providing the callback, it receives the following parameters:
-	 * * {module:engine/model/batch~Batch} batch Newly created batch for autoformat changes.
+	 * In case of providing the callback, it receives the following parameter:
 	 * * {Object} match RegExp.exec() result of matching the pattern to inserted text.
 	 */
 	constructor( editor, pattern, callbackOrCommand ) {
@@ -61,15 +59,12 @@ export default class BlockAutoformatEngine {
 			// We assume that the actual command name was provided.
 			const command = callbackOrCommand;
 
-			callback = context => {
-				const { batch } = context;
-
-				// Create new batch for removal and command execution.
-				editor.execute( command, { batch } );
+			callback = () => {
+				editor.execute( command );
 			};
 		}
 
-		editor.document.on( 'change', ( event, type, changes, batch ) => {
+		editor.model.document.on( 'change', ( event, type, changes, batch ) => {
 			if ( batch.type == 'transparent' ) {
 				return;
 			}
@@ -100,17 +95,15 @@ export default class BlockAutoformatEngine {
 				return;
 			}
 
-			editor.document.enqueueChanges( () => {
-				// Create new batch to separate typing batch from the Autoformat changes.
-				const fixBatch = editor.document.batch();
-
+			// Use enqueueChange to create new batch to separate typing batch from the autoformat changes.
+			editor.model.enqueueChange( writer => {
 				// Matched range.
 				const range = Range.createFromParentsAndOffsets( textNode.parent, 0, textNode.parent, match[ 0 ].length );
 
 				// Remove matched text.
-				fixBatch.remove( range );
+				writer.remove( range );
 
-				callback( { fixBatch, match } );
+				callback( { match } );
 			} );
 		} );
 	}

+ 11 - 13
packages/ckeditor5-autoformat/src/inlineautoformatengine.js

@@ -60,9 +60,9 @@ export default class InlineAutoformatEngine {
 	 *		new InlineAutoformatEngine( editor, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
 	 *
 	 *		// Use formatting callback:
-	 *		new InlineAutoformatEngine( editor, /(\*\*)([^\*]+?)(\*\*)$/g, ( batch, validRanges ) => {
+	 *		new InlineAutoformatEngine( editor, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, validRanges ) => {
 	 *			for ( let range of validRanges ) {
-	 *				batch.setAttribute( command, true, range );
+	 *				writer.setAttribute( command, true, range );
 	 *			}
 	 *		} );
 	 */
@@ -130,13 +130,13 @@ export default class InlineAutoformatEngine {
 		} );
 
 		// A format callback run on matched text.
-		formatCallback = formatCallback || ( ( batch, validRanges ) => {
+		formatCallback = formatCallback || ( ( writer, validRanges ) => {
 			for ( const range of validRanges ) {
-				batch.setAttribute( command, true, range );
+				writer.setAttribute( command, true, range );
 			}
 		} );
 
-		editor.document.on( 'change', ( evt, type, changes, batch ) => {
+		editor.model.document.on( 'change', ( evt, type, changes, batch ) => {
 			if ( batch.type == 'transparent' ) {
 				return;
 			}
@@ -145,7 +145,7 @@ export default class InlineAutoformatEngine {
 				return;
 			}
 
-			const selection = editor.document.selection;
+			const selection = editor.model.document.selection;
 
 			if ( !selection.isCollapsed || !selection.focus || !selection.focus.parent ) {
 				return;
@@ -186,21 +186,19 @@ export default class InlineAutoformatEngine {
 				return;
 			}
 
-			editor.document.enqueueChanges( () => {
-				// Create new batch to separate typing batch from the Autoformat changes.
-				const fixBatch = editor.document.batch();
-
-				const validRanges = editor.document.schema.getValidRanges( rangesToFormat, command );
+			// Use enqueueChange to create new batch to separate typing batch from the autoformat changes.
+			editor.model.enqueueChange( writer => {
+				const validRanges = editor.model.schema.getValidRanges( rangesToFormat, command );
 
 				// Apply format.
-				formatCallback( fixBatch, validRanges );
+				formatCallback( writer, validRanges );
 
 				// Detach ranges used to apply Autoformat. Prevents memory leaks. #39
 				rangesToFormat.forEach( range => range.detach() );
 
 				// Remove delimiters.
 				for ( const range of rangesToRemove ) {
-					fixBatch.remove( range );
+					writer.remove( range );
 
 					// Prevents memory leaks.
 					// https://github.com/ckeditor/ckeditor5-autoformat/issues/39