Переглянути джерело

Selection is put over inserted image.

Szymon Kupś 8 роки тому
батько
коміт
dff7700df2

+ 9 - 8
packages/ckeditor5-upload/src/imageuploadcommand.js

@@ -45,17 +45,17 @@ export default class ImageUploadCommand extends Command {
 
 
 		doc.enqueueChanges( () => {
 		doc.enqueueChanges( () => {
 			let insertPosition;
 			let insertPosition;
-			const firstBlock = doc.selection.getSelectedBlocks().next().value;
+			const selectedElement = selection.getSelectedElement();
 
 
-			if ( firstBlock ) {
-				// If first block is found - insert image before it.
-				insertPosition = ModelPosition.createBefore( firstBlock );
+			// If selected element is placed directly in root - put image after it.
+			if ( selectedElement && selectedElement.parent.is( 'rootElement' ) ) {
+				insertPosition = ModelPosition.createAfter( selectedElement );
 			} else {
 			} else {
-				// If element placed in root is selected - insert after it.
-				const selectedElement = selection.getSelectedElement();
+				// If selection is inside some block - put image before it.
+				const firstBlock = doc.selection.getSelectedBlocks().next().value;
 
 
-				if ( selectedElement && selectedElement.parent.is( 'rootElement' ) ) {
-					insertPosition = ModelPosition.createAfter( selectedElement );
+				if ( firstBlock ) {
+					insertPosition = ModelPosition.createBefore( firstBlock );
 				}
 				}
 			}
 			}
 
 
@@ -73,6 +73,7 @@ export default class ImageUploadCommand extends Command {
 			insertSelection.setRanges( [ range ] );
 			insertSelection.setRanges( [ range ] );
 
 
 			editor.data.insertContent( documentFragment, insertSelection, batch );
 			editor.data.insertContent( documentFragment, insertSelection, batch );
+			selection.setRanges( [ ModelRange.createOn( imageElement ) ] );
 		} );
 		} );
 	}
 	}
 }
 }

+ 3 - 3
packages/ckeditor5-upload/tests/imageuploadcommand.js

@@ -45,7 +45,7 @@ describe( 'ImageUploadCommand', () => {
 			command._doExecute( { file } );
 			command._doExecute( { file } );
 
 
 			const id = fileRepository.getLoader( file ).id;
 			const id = fileRepository.getLoader( file ).id;
-			expect( getModelData( document ) ).to.equal( `<image uploadId="${ id }"></image><paragraph>foo[]</paragraph>` );
+			expect( getModelData( document ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
 		} );
 		} );
 
 
 		it( 'should insert image after other image', () => {
 		it( 'should insert image after other image', () => {
@@ -55,7 +55,7 @@ describe( 'ImageUploadCommand', () => {
 			command._doExecute( { file } );
 			command._doExecute( { file } );
 
 
 			const id = fileRepository.getLoader( file ).id;
 			const id = fileRepository.getLoader( file ).id;
-			expect( getModelData( document ) ).to.equal( `[<image src="image.png"></image>]<image uploadId="${ id }"></image>` );
+			expect( getModelData( document ) ).to.equal( `<image src="image.png"></image>[<image uploadId="${ id }"></image>]` );
 		} );
 		} );
 
 
 		it( 'should not insert image when proper insert position cannot be found', () => {
 		it( 'should not insert image when proper insert position cannot be found', () => {
@@ -92,7 +92,7 @@ describe( 'ImageUploadCommand', () => {
 			command._doExecute( { batch, file } );
 			command._doExecute( { batch, file } );
 			const id = fileRepository.getLoader( file ).id;
 			const id = fileRepository.getLoader( file ).id;
 
 
-			expect( getModelData( document ) ).to.equal( `<image uploadId="${ id }"></image><paragraph>foo[]</paragraph>` );
+			expect( getModelData( document ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
 			sinon.assert.calledOnce( spy );
 			sinon.assert.calledOnce( spy );
 		} );
 		} );
 	} );
 	} );

+ 5 - 5
packages/ckeditor5-upload/tests/imageuploadengine.js

@@ -71,7 +71,7 @@ describe( 'ImageUploadEngine', () => {
 
 
 		const id = fileRepository.getLoader( fileMock ).id;
 		const id = fileRepository.getLoader( fileMock ).id;
 		expect( getModelData( document ) ).to.equal(
 		expect( getModelData( document ) ).to.equal(
-			`<image uploadId="${ id }"></image><paragraph>foo bar baz[]</paragraph>`
+			`[<image uploadId="${ id }"></image>]<paragraph>foo bar baz</paragraph>`
 		);
 		);
 	} );
 	} );
 
 
@@ -120,10 +120,10 @@ describe( 'ImageUploadEngine', () => {
 
 
 		adapterMock.uploadStartedCallback = () => {
 		adapterMock.uploadStartedCallback = () => {
 			expect( getViewData( viewDocument ) ).to.equal(
 			expect( getViewData( viewDocument ) ).to.equal(
-				'<figure class="image ck-widget" contenteditable="false">' +
+				'[<figure class="image ck-widget" contenteditable="false">' +
 					`<img src="${ base64Sample }"></img>` +
 					`<img src="${ base64Sample }"></img>` +
-				'</figure>' +
-				'<p>{}foo bar</p>' );
+				'</figure>]' +
+				'<p>foo bar</p>' );
 			expect( loader.status ).to.equal( 'uploading' );
 			expect( loader.status ).to.equal( 'uploading' );
 			done();
 			done();
 		};
 		};
@@ -140,7 +140,7 @@ describe( 'ImageUploadEngine', () => {
 		adapterMock.uploadStartedCallback = () => {
 		adapterMock.uploadStartedCallback = () => {
 			document.once( 'changesDone', () => {
 			document.once( 'changesDone', () => {
 				expect( getViewData( viewDocument ) ).to.equal(
 				expect( getViewData( viewDocument ) ).to.equal(
-					'<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure><p>{}foo bar</p>'
+					'[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]<p>foo bar</p>'
 				);
 				);
 				expect( loader.status ).to.equal( 'idle' );
 				expect( loader.status ).to.equal( 'idle' );