8
0

paragraphcommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 first from '@ckeditor/ckeditor5-utils/src/first';
  10. /**
  11. * The paragraph command.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class ParagraphCommand extends Command {
  16. /**
  17. * The value of the command. Indicates whether the selection start is placed in a paragraph.
  18. *
  19. * @readonly
  20. * @observable
  21. * @member {Boolean} #value
  22. */
  23. /**
  24. * @inheritDoc
  25. */
  26. refresh() {
  27. const model = this.editor.model;
  28. const document = model.document;
  29. const block = first( document.selection.getSelectedBlocks() );
  30. this.value = !!block && block.is( 'paragraph' );
  31. this.isEnabled = !!block && checkCanBecomeParagraph( block, model.schema );
  32. }
  33. /**
  34. * Executes the command. All the blocks (see {@link module:engine/model/schema~Schema}) in the selection
  35. * will be turned to paragraphs.
  36. *
  37. * @fires execute
  38. * @param {Object} [options] Options for the executed command.
  39. * @param {module:engine/model/selection~Selection} [options.selection] The selection that the command should be applied to.
  40. * By default, if not provided, the command is applied to the {@link module:engine/model/document~Document#selection}.
  41. */
  42. execute( options = {} ) {
  43. const model = this.editor.model;
  44. const document = model.document;
  45. model.change( writer => {
  46. const blocks = ( options.selection || document.selection ).getSelectedBlocks();
  47. for ( const block of blocks ) {
  48. if ( !block.is( 'paragraph' ) && checkCanBecomeParagraph( block, model.schema ) ) {
  49. writer.rename( block, 'paragraph' );
  50. }
  51. }
  52. } );
  53. }
  54. }
  55. // Checks whether the given block can be replaced by a paragraph.
  56. //
  57. // @private
  58. // @param {module:engine/model/element~Element} block A block to be tested.
  59. // @param {module:engine/model/schema~Schema} schema The schema of the document.
  60. // @returns {Boolean}
  61. function checkCanBecomeParagraph( block, schema ) {
  62. return schema.checkChild( block.parent, 'paragraph' );
  63. }