8
0

indentblockcommand.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 selection is not in a list item, the command is disabled.
  52. if ( !block || !model.schema.checkAttribute( block, 'blockIndent' ) ) {
  53. this.isEnabled = false;
  54. return;
  55. }
  56. this.isEnabled = this._indentBehavior.checkEnabled( block.getAttribute( 'blockIndent' ) );
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. execute() {
  62. const model = this.editor.model;
  63. const doc = model.document;
  64. const itemsToChange = Array.from( doc.selection.getSelectedBlocks() );
  65. model.change( writer => {
  66. for ( const item of itemsToChange ) {
  67. const currentIndent = item.getAttribute( 'blockIndent' );
  68. const nextIndent = this._indentBehavior.getNextIndent( currentIndent );
  69. if ( nextIndent ) {
  70. writer.setAttribute( 'blockIndent', nextIndent, item );
  71. } else {
  72. writer.removeAttribute( 'blockIndent', item );
  73. }
  74. }
  75. } );
  76. }
  77. }
  78. /**
  79. * Provides indentation behavior to {@link module:indent/indentblockcommand~IndentBlockCommand}.
  80. *
  81. * @interface module:indent/indentblockcommand~IndentBehavior
  82. */
  83. /**
  84. * Performs check if command should be enabled.
  85. *
  86. * @method #checkEnabled
  87. * @param {String} indentAttributeValue Current indent attribute value.
  88. * @returns {Boolean}
  89. */
  90. /**
  91. * Returns a new indent attribute value based on the current indent. This method returns `undefined` when the indentation should be removed.
  92. *
  93. * @method #getNextIndent
  94. * @param {String} indentAttributeValue The current indent attribute value.
  95. * @returns {String|undefined}
  96. */