Forráskód Böngészése

Aligned code with engine changes.

Oskar Wróbel 8 éve
szülő
commit
60e2c9bf6a

+ 1 - 1
packages/ckeditor5-image/src/image/converters.js

@@ -55,7 +55,7 @@ export function viewFigureToModel() {
 		data.context.pop();
 
 		// Add converted children to model image.
-		conversionApi.batch.insert( modelChildren, modelImage );
+		conversionApi.writer.insert( modelChildren, modelImage );
 
 		// Set model image as conversion result.
 		data.output = modelImage;

+ 1 - 2
packages/ckeditor5-image/src/image/imageengine.js

@@ -35,8 +35,7 @@ export default class ImageEngine extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
-		const doc = editor.document;
-		const schema = doc.schema;
+		const schema = editor.model.schema;
 		const data = editor.data;
 		const editing = editor.editing;
 		const t = editor.t;

+ 39 - 35
packages/ckeditor5-image/src/imagecaption/imagecaptionengine.js

@@ -35,9 +35,9 @@ export default class ImageCaptionEngine extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
-		const document = editor.document;
+		const document = editor.model.document;
 		const viewDocument = editor.editing.view;
-		const schema = document.schema;
+		const schema = editor.model.schema;
 		const data = editor.data;
 		const editing = editor.editing;
 		const t = editor.t;
@@ -65,7 +65,7 @@ export default class ImageCaptionEngine extends Plugin {
 		schema.limits.add( 'caption' );
 
 		// Add caption element to each image inserted without it.
-		document.on( 'change', insertMissingModelCaptionElement );
+		document.on( 'change', ( evt, type, data, batch ) => this._insertMissingModelCaptionElement( type, data, batch ) );
 
 		// View to model converter for the data pipeline.
 		buildViewConverter()
@@ -105,7 +105,7 @@ export default class ImageCaptionEngine extends Plugin {
 		}
 
 		// If whole image is selected.
-		const modelSelection = this.editor.document.selection;
+		const modelSelection = this.editor.model.document.selection;
 		const selectedElement = modelSelection.getSelectedElement();
 
 		if ( selectedElement && selectedElement.is( 'image' ) ) {
@@ -150,33 +150,38 @@ export default class ImageCaptionEngine extends Plugin {
 			}
 		}
 	}
-}
-
-// Checks whether data inserted to the model document have image element that has no caption element inside it.
-// If there is none - adds it to the image element.
-//
-// @private
-function insertMissingModelCaptionElement( evt, changeType, data, batch ) {
-	if ( changeType !== 'insert' ) {
-		return;
-	}
 
-	const walker = new ModelTreeWalker( {
-		boundaries: data.range,
-		ignoreElementEnd: true
-	} );
-
-	for ( const value of walker ) {
-		const item = value.item;
+	/**
+	 * Checks whether data inserted to the model document have image element that has no caption element inside it.
+	 * If there is none - adds it to the image element.
+	 *
+	 * @private
+	 * @param {String} changeType Type of change.
+	 * @param {Object} data Change data.
+	 * @param {module:engine/model/batch} batch Bath to with changed will be added.
+	 */
+	_insertMissingModelCaptionElement( changeType, data, batch ) {
+		if ( changeType !== 'insert' ) {
+			return;
+		}
 
-		if ( value.type == 'elementStart' && isImage( item ) && !getCaptionFromImage( item ) ) {
-			batch.document.enqueueChanges( () => {
-				// Make sure that the image does not have caption already.
-				// https://github.com/ckeditor/ckeditor5-image/issues/78
-				if ( !getCaptionFromImage( item ) ) {
-					batch.appendElement( 'caption', item );
-				}
-			} );
+		const walker = new ModelTreeWalker( {
+			boundaries: data.range,
+			ignoreElementEnd: true
+		} );
+
+		for ( const value of walker ) {
+			const item = value.item;
+
+			if ( value.type == 'elementStart' && isImage( item ) && !getCaptionFromImage( item ) ) {
+				this.editor.model.enqueueChange( batch, writer => {
+					// Make sure that the image does not have caption already.
+					// https://github.com/ckeditor/ckeditor5-image/issues/78
+					if ( !getCaptionFromImage( item ) ) {
+						writer.appendElement( 'caption', item );
+					}
+				} );
+			}
 		}
 	}
 }
@@ -230,12 +235,11 @@ function insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper
 	mapper.bindElements( modelCaption, viewCaption );
 }
 
-/**
- * Checks if the provided node or one of its ancestors is a caption element, and returns it.
- *
- * @param {module:engine/model/node~Node} node
- * @returns {module:engine/model/element~Element|null}
- */
+// Checks if the provided node or one of its ancestors is a caption element, and returns it.
+//
+// @private
+// @param {module:engine/model/node~Node} node
+// @returns {module:engine/model/element~Element|null}
 function getParentCaption( node ) {
 	const ancestors = node.getAncestors( { includeSelf: true } );
 	const caption = ancestors.find( ancestor => ancestor.name == 'caption' );

+ 7 - 12
packages/ckeditor5-image/src/imagestyle/imagestylecommand.js

@@ -47,7 +47,7 @@ export default class ImageStyleCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const element = this.editor.document.selection.getSelectedElement();
+		const element = this.editor.model.document.selection.getSelectedElement();
 
 		this.isEnabled = isImage( element );
 
@@ -64,27 +64,22 @@ export default class ImageStyleCommand extends Command {
 	 * Executes the command.
 	 *
 	 * @fires execute
-	 * @param {Object} options
-	 * @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 = {} ) {
+	execute() {
 		if ( this.value ) {
 			return;
 		}
 
-		const doc = this.editor.document;
-		const imageElement = doc.selection.getSelectedElement();
-
-		doc.enqueueChanges( () => {
-			const batch = options.batch || doc.batch();
+		const model = this.editor.model;
+		const imageElement = model.document.selection.getSelectedElement();
 
+		model.change( writer => {
 			// Default style means that there is no `imageStyle` attribute in the model.
 			// https://github.com/ckeditor/ckeditor5-image/issues/147
 			if ( this.style.isDefault ) {
-				batch.removeAttribute( 'imageStyle', imageElement );
+				writer.removeAttribute( 'imageStyle', imageElement );
 			} else {
-				batch.setAttribute( 'imageStyle', this.style.name, imageElement );
+				writer.setAttribute( 'imageStyle', this.style.name, imageElement );
 			}
 		} );
 	}

+ 1 - 2
packages/ckeditor5-image/src/imagestyle/imagestyleengine.js

@@ -44,8 +44,7 @@ export default class ImageStyleEngine extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
-		const doc = editor.document;
-		const schema = doc.schema;
+		const schema = editor.model.schema;
 		const data = editor.data;
 		const editing = editor.editing;
 

+ 5 - 9
packages/ckeditor5-image/src/imagetextalternative/imagetextalternativecommand.js

@@ -28,7 +28,7 @@ export default class ImageTextAlternativeCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const element = this.editor.document.selection.getSelectedElement();
+		const element = this.editor.model.document.selection.getSelectedElement();
 
 		this.isEnabled = isImage( element );
 
@@ -45,17 +45,13 @@ export default class ImageTextAlternativeCommand extends Command {
 	 * @fires execute
 	 * @param {Object} options
 	 * @param {String} options.newValue The new value of the `alt` attribute to set.
-	 * @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 doc = this.editor.document;
-		const imageElement = doc.selection.getSelectedElement();
+		const model = this.editor.model;
+		const imageElement = model.document.selection.getSelectedElement();
 
-		doc.enqueueChanges( () => {
-			const batch = options.batch || doc.batch();
-
-			batch.setAttribute( 'alt', options.newValue, imageElement );
+		model.change( writer => {
+			writer.setAttribute( 'alt', options.newValue, imageElement );
 		} );
 	}
 }