Jelajahi Sumber

Used the InsertParagraphCommand in the WidgetTypeAround plugin.

Aleksander Nowodzinski 5 tahun lalu
induk
melakukan
71632e5bc0

+ 4 - 14
packages/ckeditor5-widget/src/widgettypearound/widgettypearound.js

@@ -105,21 +105,11 @@ export default class WidgetTypeAround extends Plugin {
 	_insertParagraph( widgetViewElement, position ) {
 		const editor = this.editor;
 		const editingView = editor.editing.view;
-		let viewPosition;
+		const widgetModelElement = editor.editing.mapper.toModelElement( widgetViewElement );
 
-		if ( position === 'before' ) {
-			viewPosition = editingView.createPositionBefore( widgetViewElement );
-		} else {
-			viewPosition = editingView.createPositionAfter( widgetViewElement );
-		}
-
-		const modelPosition = editor.editing.mapper.toModelPosition( viewPosition );
-
-		editor.model.change( writer => {
-			const paragraph = writer.createElement( 'paragraph' );
-
-			writer.insert( paragraph, modelPosition );
-			writer.setSelection( paragraph, 0 );
+		editor.execute( 'insertParagraph', {
+			element: widgetModelElement,
+			position
 		} );
 
 		editingView.focus();

+ 20 - 2
packages/ckeditor5-widget/tests/widgettypearound/widgettypearound.js

@@ -54,19 +54,37 @@ describe( 'WidgetTypeAround', () => {
 	} );
 
 	describe( '_insertParagraph()', () => {
-		it( 'should insert a paragraph with a selection before a widget when position is "before"', () => {
+		let executeSpy;
+
+		beforeEach( () => {
+			executeSpy = sinon.spy( editor, 'execute' );
+		} );
+
+		it( 'should execute the "insertParagraph" command when inserting a paragraph before the widget', () => {
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
 			plugin._insertParagraph( viewRoot.getChild( 0 ), 'before' );
 
+			sinon.assert.calledOnce( executeSpy );
+			sinon.assert.calledWithExactly( executeSpy, 'insertParagraph', {
+				position: 'before',
+				element: editor.model.document.getRoot().getChild( 1 )
+			} );
+
 			expect( getModelData( editor.model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );
 		} );
 
-		it( 'should insert a paragraph with a selection after a widget when position is "after"', () => {
+		it( 'should execute the "insertParagraph" command when inserting a paragraph after the widget', () => {
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
 			plugin._insertParagraph( viewRoot.getChild( 0 ), 'after' );
 
+			sinon.assert.calledOnce( executeSpy );
+			sinon.assert.calledWithExactly( executeSpy, 'insertParagraph', {
+				position: 'after',
+				element: editor.model.document.getRoot().getChild( 0 )
+			} );
+
 			expect( getModelData( editor.model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]</paragraph>' );
 		} );