소스 검색

Get writer inside insertImage instead of taking from a parameter

Paweł Kwaśnik 5 년 전
부모
커밋
6b9c697248

+ 6 - 6
packages/ckeditor5-image/src/autoimage.js

@@ -117,18 +117,18 @@ export default class AutoImage extends Plugin {
 		const urlRange = new LiveRange( leftPosition, rightPosition );
 		const urlRange = new LiveRange( leftPosition, rightPosition );
 		const walker = urlRange.getWalker( { ignoreElementEnd: true } );
 		const walker = urlRange.getWalker( { ignoreElementEnd: true } );
 
 
-		let url = '';
+		let src = '';
 
 
 		for ( const node of walker ) {
 		for ( const node of walker ) {
 			if ( node.item.is( '$textProxy' ) ) {
 			if ( node.item.is( '$textProxy' ) ) {
-				url += node.item.data;
+				src += node.item.data;
 			}
 			}
 		}
 		}
 
 
-		url = url.trim();
+		src = src.trim();
 
 
 		// If the URL does not match to image URL regexp, let's skip that.
 		// If the URL does not match to image URL regexp, let's skip that.
-		if ( !url.match( IMAGE_URL_REGEXP ) ) {
+		if ( !src.match( IMAGE_URL_REGEXP ) ) {
 			urlRange.detach();
 			urlRange.detach();
 
 
 			return;
 			return;
@@ -160,10 +160,10 @@ export default class AutoImage extends Plugin {
 				// Check if position where the element should be inserted is still valid.
 				// Check if position where the element should be inserted is still valid.
 				// Otherwise leave it as undefined to use the logic of insertImage().
 				// Otherwise leave it as undefined to use the logic of insertImage().
 				if ( this._positionToInsert.root.rootName !== '$graveyard' ) {
 				if ( this._positionToInsert.root.rootName !== '$graveyard' ) {
-					insertionPosition = this._positionToInsert;
+					insertionPosition = this._positionToInsert.toPosition();
 				}
 				}
 
 
-				insertImage( writer, editor.model, { src: url }, insertionPosition );
+				insertImage( editor.model, { src }, insertionPosition );
 
 
 				this._positionToInsert.detach();
 				this._positionToInsert.detach();
 				this._positionToInsert = null;
 				this._positionToInsert = null;

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

@@ -54,7 +54,7 @@ export default class ImageInsertCommand extends Command {
 			const sources = Array.isArray( options.source ) ? options.source : [ options.source ];
 			const sources = Array.isArray( options.source ) ? options.source : [ options.source ];
 
 
 			for ( const src of sources ) {
 			for ( const src of sources ) {
-				insertImage( writer, model, { src } );
+				insertImage( model, { src } );
 			}
 			}
 		} );
 		} );
 	}
 	}

+ 13 - 13
packages/ckeditor5-image/src/image/utils.js

@@ -71,26 +71,26 @@ export function isImage( modelElement ) {
 /**
 /**
  * Handles inserting single file. This method unifies image insertion using {@link module:widget/utils~findOptimalInsertionPosition} method.
  * Handles inserting single file. This method unifies image insertion using {@link module:widget/utils~findOptimalInsertionPosition} method.
  *
  *
- *		model.change( writer => {
- *			insertImage( writer, model, { src: 'path/to/image.jpg' } );
- *		} );
+ *	insertImage( model, { src: 'path/to/image.jpg' } );
  *
  *
- * @param {module:engine/model/writer~Writer} writer
  * @param {module:engine/model/model~Model} model
  * @param {module:engine/model/model~Model} model
  * @param {Object} [attributes={}] Attributes of inserted image
  * @param {Object} [attributes={}] Attributes of inserted image
- * @param {module:engine/model/liveposition~LivePosition} insertPosition
+ * @param {module:engine/model/position~Position} [insertPosition] Position to insert the image. If not specified,
+ * the {@link module:widget/utils~findOptimalInsertionPosition} logic will be applied.
  */
  */
-export function insertImage( writer, model, attributes = {}, insertPosition = null ) {
-	const imageElement = writer.createElement( 'image', attributes );
+export function insertImage( model, attributes = {}, insertPosition = null ) {
+	model.change( writer => {
+		const imageElement = writer.createElement( 'image', attributes );
 
 
-	const insertAtSelection = insertPosition || findOptimalInsertionPosition( model.document.selection, model );
+		const insertAtSelection = insertPosition || findOptimalInsertionPosition( model.document.selection, model );
 
 
-	model.insertContent( imageElement, insertAtSelection );
+		model.insertContent( imageElement, insertAtSelection );
 
 
-	// Inserting an image might've failed due to schema regulations.
-	if ( imageElement.parent ) {
-		writer.setSelection( imageElement, 'on' );
-	}
+		// Inserting an image might've failed due to schema regulations.
+		if ( imageElement.parent ) {
+			writer.setSelection( imageElement, 'on' );
+		}
+	} );
 }
 }
 
 
 /**
 /**

+ 1 - 1
packages/ckeditor5-image/src/imageupload/imageuploadcommand.js

@@ -86,5 +86,5 @@ function uploadImage( writer, model, fileRepository, file ) {
 		return;
 		return;
 	}
 	}
 
 
-	insertImage( writer, model, { uploadId: loader.id } );
+	insertImage( model, { uploadId: loader.id } );
 }
 }

+ 3 - 9
packages/ckeditor5-image/tests/image/utils.js

@@ -240,9 +240,7 @@ describe( 'image widget utils', () => {
 		it( 'should insert image at selection position as other widgets', () => {
 		it( 'should insert image at selection position as other widgets', () => {
 			setModelData( model, '<paragraph>f[o]o</paragraph>' );
 			setModelData( model, '<paragraph>f[o]o</paragraph>' );
 
 
-			model.change( writer => {
-				insertImage( writer, model );
-			} );
+			insertImage( model );
 
 
 			expect( getModelData( model ) ).to.equal( '[<image></image>]<paragraph>foo</paragraph>' );
 			expect( getModelData( model ) ).to.equal( '[<image></image>]<paragraph>foo</paragraph>' );
 		} );
 		} );
@@ -250,9 +248,7 @@ describe( 'image widget utils', () => {
 		it( 'should insert image with given attributes', () => {
 		it( 'should insert image with given attributes', () => {
 			setModelData( model, '<paragraph>f[o]o</paragraph>' );
 			setModelData( model, '<paragraph>f[o]o</paragraph>' );
 
 
-			model.change( writer => {
-				insertImage( writer, model, { src: 'bar' } );
-			} );
+			insertImage( model, { src: 'bar' } );
 
 
 			expect( getModelData( model ) ).to.equal( '[<image src="bar"></image>]<paragraph>foo</paragraph>' );
 			expect( getModelData( model ) ).to.equal( '[<image src="bar"></image>]<paragraph>foo</paragraph>' );
 		} );
 		} );
@@ -268,9 +264,7 @@ describe( 'image widget utils', () => {
 
 
 			setModelData( model, '<other>[]</other>' );
 			setModelData( model, '<other>[]</other>' );
 
 
-			model.change( writer => {
-				insertImage( writer, model );
-			} );
+			insertImage( model );
 
 
 			expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
 			expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
 		} );
 		} );