paragraphcommand.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module paragraph/paragraphcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  10. import first from '@ckeditor/ckeditor5-utils/src/first';
  11. /**
  12. * The paragraph command.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class ParagraphCommand extends Command {
  17. /**
  18. * The value of the command. Indicates whether the selection's start is placed in a paragraph.
  19. *
  20. * @readonly
  21. * @observable
  22. * @member {Boolean} #value
  23. */
  24. /**
  25. * @inheritDoc
  26. */
  27. refresh() {
  28. const block = first( this.editor.document.selection.getSelectedBlocks() );
  29. this.value = !!block && block.is( 'paragraph' );
  30. this.isEnabled = !!block && this.editor.document.schema.check( {
  31. name: 'paragraph',
  32. inside: Position.createBefore( block )
  33. } );
  34. }
  35. /**
  36. * Executes the command. All the blocks (see {@link module:engine/model/schema~Schema}) in the selection
  37. * will be turned to paragraphs.
  38. *
  39. * @fires execute
  40. * @param {Object} [options] Options for executed command.
  41. * @param {module:engine/model/batch~Batch} [options.batch] Batch to collect all the change steps.
  42. * New batch will be created if this option is not set.
  43. * @param {module:engine/model/selection~Selection} [options.selection] Selection the command should be applied to.
  44. * By default, if not provided, the command is applied to {@link module:engine/model/document~Document#selection}.
  45. */
  46. execute( options = {} ) {
  47. const document = this.editor.document;
  48. document.enqueueChanges( () => {
  49. const batch = options.batch || document.batch();
  50. const blocks = ( options.selection || document.selection ).getSelectedBlocks();
  51. for ( const block of blocks ) {
  52. if ( !block.is( 'paragraph' ) ) {
  53. batch.rename( block, 'paragraph' );
  54. }
  55. }
  56. } );
  57. }
  58. }