瀏覽代碼

Code refactoring: Made WidgetTypeAround#_insertParagraph accept a model element instead of a view element.

Aleksander Nowodzinski 5 年之前
父節點
當前提交
3a531dd321

+ 9 - 9
packages/ckeditor5-widget/src/widgettypearound/widgettypearound.js

@@ -103,13 +103,12 @@ export default class WidgetTypeAround extends Plugin {
 	 * the viewport to the selection in the inserted paragraph.
 	 *
 	 * @protected
-	 * @param {module:engine/view/element~Element} widgetViewElement The view widget element next to which a paragraph is inserted.
+	 * @param {module:engine/model/element~Element} widgetModelElement The model widget element next to which a paragraph is inserted.
 	 * @param {'before'|'after'} position The position where the paragraph is inserted. Either `'before'` or `'after'` the widget.
 	 */
-	_insertParagraph( widgetViewElement, position ) {
+	_insertParagraph( widgetModelElement, position ) {
 		const editor = this.editor;
 		const editingView = editor.editing.view;
-		const widgetModelElement = editor.editing.mapper.toModelElement( widgetViewElement );
 		let modelPosition;
 
 		if ( position === 'before' ) {
@@ -141,16 +140,16 @@ export default class WidgetTypeAround extends Plugin {
 	_insertParagraphAccordingToSelectionAttribute() {
 		const editor = this.editor;
 		const model = editor.model;
-		const editingView = editor.editing.view;
-		const typeAroundSelectionAttributeValue = model.document.selection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
+		const modelSelection = model.document.selection;
+		const typeAroundSelectionAttributeValue = modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
 
 		if ( !typeAroundSelectionAttributeValue ) {
 			return false;
 		}
 
-		const selectedViewElement = editingView.document.selection.getSelectedElement();
+		const selectedModelElement = modelSelection.getSelectedElement();
 
-		this._insertParagraph( selectedViewElement, typeAroundSelectionAttributeValue );
+		this._insertParagraph( selectedModelElement, typeAroundSelectionAttributeValue );
 
 		return true;
 	}
@@ -461,8 +460,9 @@ export default class WidgetTypeAround extends Plugin {
 
 			const buttonPosition = getTypeAroundButtonPosition( button );
 			const widgetViewElement = getClosestWidgetViewElement( button, editingView.domConverter );
+			const widgetModelElement = editor.editing.mapper.toModelElement( widgetViewElement );
 
-			this._insertParagraph( widgetViewElement, buttonPosition );
+			this._insertParagraph( widgetModelElement, buttonPosition );
 
 			domEventData.preventDefault();
 			evt.stop();
@@ -502,7 +502,7 @@ export default class WidgetTypeAround extends Plugin {
 			// Then, if there is no selection attribute associated with the "fake caret", check if the widget
 			// simply is selected and create a new paragraph according to the keystroke (Shift+)Enter.
 			else if ( isTypeAroundWidget( selectedViewElement, selectedModelElement, schema ) ) {
-				this._insertParagraph( selectedViewElement, domEventData.isSoft ? 'before' : 'after' );
+				this._insertParagraph( selectedModelElement, domEventData.isSoft ? 'before' : 'after' );
 
 				wasHandled = true;
 			}

+ 10 - 9
packages/ckeditor5-widget/tests/widgettypearound/widgettypearound.js

@@ -18,7 +18,7 @@ import { setData as setModelData, getData as getModelData } from '@ckeditor/cked
 import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
 
 describe( 'WidgetTypeAround', () => {
-	let element, plugin, editor, editingView, viewDocument, viewRoot;
+	let element, plugin, editor, editingView, viewDocument, modelRoot, viewRoot;
 
 	beforeEach( async () => {
 		element = global.document.createElement( 'div' );
@@ -35,6 +35,7 @@ describe( 'WidgetTypeAround', () => {
 		editingView = editor.editing.view;
 		viewDocument = editingView.document;
 		viewRoot = viewDocument.getRoot();
+		modelRoot = editor.model.document.getRoot();
 		plugin = editor.plugins.get( WidgetTypeAround );
 	} );
 
@@ -64,10 +65,10 @@ describe( 'WidgetTypeAround', () => {
 		it( 'should execute the "insertParagraph" command when inserting a paragraph before the widget', () => {
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
-			plugin._insertParagraph( viewRoot.getChild( 0 ), 'before' );
+			plugin._insertParagraph( modelRoot.getChild( 0 ), 'before' );
 
 			const spyExecutePosition = executeSpy.firstCall.args[ 1 ].position;
-			const positionBeforeWidget = editor.model.createPositionBefore( editor.model.document.getRoot().getChild( 0 ) );
+			const positionBeforeWidget = editor.model.createPositionBefore( modelRoot.getChild( 0 ) );
 
 			sinon.assert.calledOnce( executeSpy );
 			sinon.assert.calledWith( executeSpy, 'insertParagraph' );
@@ -80,10 +81,10 @@ describe( 'WidgetTypeAround', () => {
 		it( 'should execute the "insertParagraph" command when inserting a paragraph after the widget', () => {
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
-			plugin._insertParagraph( viewRoot.getChild( 0 ), 'after' );
+			plugin._insertParagraph( modelRoot.getChild( 0 ), 'after' );
 
 			const spyExecutePosition = executeSpy.firstCall.args[ 1 ].position;
-			const positionAfterWidget = editor.model.createPositionAfter( editor.model.document.getRoot().getChild( 0 ) );
+			const positionAfterWidget = editor.model.createPositionAfter( modelRoot.getChild( 0 ) );
 
 			sinon.assert.calledOnce( executeSpy );
 			sinon.assert.calledWith( executeSpy, 'insertParagraph' );
@@ -98,7 +99,7 @@ describe( 'WidgetTypeAround', () => {
 
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
-			plugin._insertParagraph( viewRoot.getChild( 0 ), 'after' );
+			plugin._insertParagraph( modelRoot.getChild( 0 ), 'after' );
 
 			sinon.assert.calledOnce( spy );
 		} );
@@ -108,7 +109,7 @@ describe( 'WidgetTypeAround', () => {
 
 			setModelData( editor.model, '<blockWidget></blockWidget>' );
 
-			plugin._insertParagraph( viewRoot.getChild( 0 ), 'after' );
+			plugin._insertParagraph( modelRoot.getChild( 0 ), 'after' );
 
 			sinon.assert.calledOnce( spy );
 		} );
@@ -222,7 +223,7 @@ describe( 'WidgetTypeAround', () => {
 					viewDocument.fire( eventInfo, domEventDataMock );
 
 					sinon.assert.calledOnce( typeAroundSpy );
-					sinon.assert.calledWithExactly( typeAroundSpy, viewRoot.getChild( 1 ), 'before' );
+					sinon.assert.calledWithExactly( typeAroundSpy, modelRoot.getChild( 1 ), 'before' );
 					sinon.assert.calledOnce( preventDefaultSpy );
 					sinon.assert.calledOnce( stopSpy );
 				} );
@@ -265,7 +266,7 @@ describe( 'WidgetTypeAround', () => {
 					viewDocument.fire( eventInfo, domEventDataMock );
 
 					sinon.assert.calledOnce( typeAroundSpy );
-					sinon.assert.calledWithExactly( typeAroundSpy, viewRoot.getChild( 0 ), 'after' );
+					sinon.assert.calledWithExactly( typeAroundSpy, modelRoot.getChild( 0 ), 'after' );
 					sinon.assert.calledOnce( preventDefaultSpy );
 					sinon.assert.calledOnce( stopSpy );
 				} );