insertparagraphcommand.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module paragraph/insertparagraphcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. /**
  10. * The insert paragraph command. It inserts a new paragraph at a specific
  11. * {@link module:engine/model/position~Position document position}.
  12. *
  13. * // Insert a new paragraph before an element in the document.
  14. * editor.execute( 'insertParagraph', {
  15. * position: editor.model.createPositionBefore( element )
  16. * } );
  17. *
  18. * **Note**: This command moves the selection to the inserted paragraph.
  19. *
  20. * @extends module:core/command~Command
  21. */
  22. export default class InsertParagraphCommand extends Command {
  23. /**
  24. * Executes the command.
  25. *
  26. * @param {Object} options Options for the executed command.
  27. * @param {module:engine/model/position~Position} options.position The model position at which
  28. * the new paragraph will be inserted.
  29. * @fires execute
  30. */
  31. execute( options ) {
  32. const model = this.editor.model;
  33. if ( !model.schema.checkChild( options.position, 'paragraph' ) ) {
  34. return;
  35. }
  36. model.change( writer => {
  37. const paragraph = writer.createElement( 'paragraph' );
  38. model.insertContent( paragraph, options.position );
  39. writer.setSelection( paragraph, 'in' );
  40. } );
  41. }
  42. }