indentblockcommand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module indent/indentblockcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import first from '@ckeditor/ckeditor5-utils/src/first';
  10. /**
  11. * The indent block command.
  12. *
  13. * The command is registered by the {@link module:indent/indentblock~IndentBlock} as `'indentBlock'` - for indenting blocks and
  14. * `'outdentBlock'` - for outdenting blocks.
  15. *
  16. * To increase block indentation at current selection execute the command:
  17. *
  18. * editor.execute( 'indentBlock' );
  19. *
  20. * To decrease block indentation at current selection execute the command:
  21. *
  22. * editor.execute( 'outdentBlock' );
  23. *
  24. * @extends module:core/command~Command
  25. */
  26. export default class IndentBlockCommand extends Command {
  27. /**
  28. * Creates an instance of the command.
  29. *
  30. * @param {module:core/editor/editor~Editor} editor Editor instance.
  31. * @param {module:indent/indentblockcommand~IndentBehavior} indentBehavior
  32. */
  33. constructor( editor, indentBehavior ) {
  34. super( editor );
  35. /**
  36. * Command's indentation behavior.
  37. *
  38. * @type {module:indent/indentblockcommand~IndentBehavior}
  39. * @private
  40. */
  41. this._indentBehavior = indentBehavior;
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. refresh() {
  47. // Check whether any of position's ancestor is a list item.
  48. const editor = this.editor;
  49. const model = editor.model;
  50. const block = first( model.document.selection.getSelectedBlocks() );
  51. if ( !block || !model.schema.checkAttribute( block, 'blockIndent' ) ) {
  52. this.isEnabled = false;
  53. return;
  54. }
  55. this.isEnabled = this._indentBehavior.checkEnabled( block.getAttribute( 'blockIndent' ) );
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. execute() {
  61. const model = this.editor.model;
  62. const blocksToChange = getBlocksToChange( model );
  63. model.change( writer => {
  64. for ( const block of blocksToChange ) {
  65. const currentIndent = block.getAttribute( 'blockIndent' );
  66. const nextIndent = this._indentBehavior.getNextIndent( currentIndent );
  67. if ( nextIndent ) {
  68. writer.setAttribute( 'blockIndent', nextIndent, block );
  69. } else {
  70. writer.removeAttribute( 'blockIndent', block );
  71. }
  72. }
  73. } );
  74. }
  75. }
  76. // Returns blocks from selection that should have blockIndent selection set.
  77. //
  78. // @param {module:engine/model/model~model} model A model.
  79. function getBlocksToChange( model ) {
  80. const selection = model.document.selection;
  81. const schema = model.schema;
  82. const blocksInSelection = Array.from( selection.getSelectedBlocks() );
  83. return blocksInSelection.filter( block => schema.checkAttribute( block, 'blockIndent' ) );
  84. }
  85. /**
  86. * Provides indentation behavior to {@link module:indent/indentblockcommand~IndentBlockCommand}.
  87. *
  88. * @interface module:indent/indentblockcommand~IndentBehavior
  89. */
  90. /**
  91. * Performs check if command should be enabled.
  92. *
  93. * @method #checkEnabled
  94. * @param {String} indentAttributeValue Current indent attribute value.
  95. * @returns {Boolean}
  96. */
  97. /**
  98. * Returns a new indent attribute value based on the current indent. This method returns `undefined` when the indentation should be removed.
  99. *
  100. * @method #getNextIndent
  101. * @param {String} indentAttributeValue The current indent attribute value.
  102. * @returns {String|undefined}
  103. */