8
0
Prechádzať zdrojové kódy

Made the InsertParagraphCommand use a model position instead of element and before/after.

Aleksander Nowodzinski 5 rokov pred
rodič
commit
b7f0fffbec

+ 9 - 20
packages/ckeditor5-paragraph/src/insertparagraphcommand.js

@@ -10,12 +10,12 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 /**
- * The insert paragraph command. It insert a new paragraph next to the provided model element.
+ * The insert paragraph command. It inserts a new paragraph at a specific
+ * {@link module:engine/model/position~Position document position}.
  *
- *		// Insert a new paragraph before the first element in the document root.
+ *		// Insert a new paragraph before an element in the document.
  *		editor.execute( 'insertParagraph', {
- *			element: editor.model.document.getRoot().getChild( 0 ),
- *			position: 'before'
+ *			position: editor.model.createPositionBefore( element )
  *		} );
  *
  * **Note**: This command moves the selection to the inserted paragraph.
@@ -27,34 +27,23 @@ export default class InsertParagraphCommand extends Command {
 	 * Executes the command.
 	 *
 	 * @param {Object} options Options for the executed command.
-	 * @param {module:engine/model/element~Element} options.element The model element next to which
-	 * the new paragraph will be inserted.
-	 * @param {'before'|'after'} options.position The position relative to the passed `element` where
+	 * @param {module:engine/model/position~Position} options.position The model position at which
 	 * the new paragraph will be inserted.
 	 * @fires execute
 	 */
 	execute( options ) {
-		const editor = this.editor;
 		const model = this.editor.model;
-		let modelPosition;
-
-		if ( options.position === 'before' ) {
-			modelPosition = model.createPositionBefore( options.element );
-		} else {
-			modelPosition = model.createPositionAfter( options.element );
-		}
 
-		if ( !model.schema.checkChild( modelPosition, 'paragraph' ) ) {
+		if ( !model.schema.checkChild( options.position, 'paragraph' ) ) {
 			return;
 		}
 
-		editor.model.change( writer => {
+		model.change( writer => {
 			const paragraph = writer.createElement( 'paragraph' );
-			const selection = writer.createSelection( paragraph, 'in' );
 
-			editor.model.insertContent( paragraph, modelPosition );
+			model.insertContent( paragraph, options.position );
 
-			writer.setSelection( selection );
+			writer.setSelection( paragraph, 'in' );
 		} );
 	}
 }

+ 11 - 29
packages/ckeditor5-paragraph/tests/insertparagraphcommand.js

@@ -32,39 +32,25 @@ describe( 'InsertParagraphCommand', () => {
 	} );
 
 	describe( 'execute()', () => {
-		it( 'should insert a paragraph before the provided model element and anchor the selection inside of it', () => {
+		it( 'should insert a paragraph at a specific document position and anchor the selection inside of it', () => {
 			setData( model, '<heading1>foo[]</heading1>' );
 
 			command.execute( {
-				element: root.getChild( 0 ),
-				position: 'before'
+				position: model.createPositionBefore( root.getChild( 0 ) )
 			} );
 
 			expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><heading1>foo</heading1>' );
 		} );
 
-		it( 'should insert a paragraph after the provided model element and anchor the selection inside of it', () => {
-			setData( model, '<heading1>foo[]</heading1>' );
-
-			command.execute( {
-				element: root.getChild( 0 ),
-				position: 'after'
-			} );
-
-			expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph>' );
-		} );
-
-		it( 'should do nothing if the paragraph is not allowed in the provided context', () => {
+		it( 'should do nothing if the paragraph is not allowed at the provided position', () => {
 			setData( model, '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
 
 			command.execute( {
-				element: root.getChild( 0 ).getChild( 0 ),
-				position: 'before'
+				position: model.createPositionBefore( root.getChild( 0 ).getChild( 0 ) )
 			} );
 
 			command.execute( {
-				element: root.getChild( 0 ).getChild( 0 ),
-				position: 'after'
+				position: model.createPositionAfter( root.getChild( 0 ).getChild( 0 ) )
 			} );
 
 			expect( getData( model ) ).to.equal( '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
@@ -75,8 +61,7 @@ describe( 'InsertParagraphCommand', () => {
 				setData( model, '<paragraph>foo[]</paragraph>' );
 
 				command.execute( {
-					element: root.getChild( 0 ),
-					position: 'before'
+					position: model.createPositionBefore( root.getChild( 0 ) )
 				} );
 
 				expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' );
@@ -86,30 +71,27 @@ describe( 'InsertParagraphCommand', () => {
 				setData( model, '<paragraph>foo[]</paragraph>' );
 
 				command.execute( {
-					element: root.getChild( 0 ),
-					position: 'after'
+					position: model.createPositionAfter( root.getChild( 0 ) )
 				} );
 
 				expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );
 			} );
 
-			it( 'should not merge with a paragraph that precedes the model element before which a new paragraph is inserted', () => {
+			it( 'should not merge with a paragraph that precedes the position at which a new paragraph is inserted', () => {
 				setData( model, '<paragraph>bar</paragraph><heading1>foo[]</heading1>' );
 
 				command.execute( {
-					element: root.getChild( 1 ),
-					position: 'before'
+					position: model.createPositionBefore( root.getChild( 1 ) )
 				} );
 
 				expect( getData( model ) ).to.equal( '<paragraph>bar</paragraph><paragraph>[]</paragraph><heading1>foo</heading1>' );
 			} );
 
-			it( 'should not merge with a paragraph that follows the model element before which a new paragraph is inserted', () => {
+			it( 'should not merge with a paragraph that follows the position at which a new paragraph is inserted', () => {
 				setData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' );
 
 				command.execute( {
-					element: root.getChild( 0 ),
-					position: 'after'
+					position: model.createPositionAfter( root.getChild( 0 ) )
 				} );
 
 				expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph><paragraph>bar</paragraph>' );