indentcommand.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module list/indentcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import first from '@ckeditor/ckeditor5-utils/src/first';
  10. /**
  11. * The list indent command. It is used by the {@link module:list/list~List list feature}.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class IndentCommand extends Command {
  16. /**
  17. * Creates an instance of the command.
  18. *
  19. * @param {module:core/editor/editor~Editor} editor The editor instance.
  20. * @param {'forward'|'backward'} indentDirection The direction of indent. If it is equal to `backward`, the command
  21. * will outdent a list item.
  22. */
  23. constructor( editor, indentDirection ) {
  24. super( editor );
  25. /**
  26. * Determines by how much the command will change the list item's indent attribute.
  27. *
  28. * @readonly
  29. * @private
  30. * @member {Number}
  31. */
  32. this._indentBy = indentDirection == 'forward' ? 1 : -1;
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. refresh() {
  38. this.isEnabled = this._checkEnabled();
  39. }
  40. /**
  41. * Indents or outdents (depends on the {@link #constructor}'s `indentDirection` parameter) selected list items.
  42. *
  43. * @fires execute
  44. */
  45. execute() {
  46. const model = this.editor.model;
  47. const doc = model.document;
  48. let itemsToChange = Array.from( doc.selection.getSelectedBlocks() );
  49. model.change( writer => {
  50. const lastItem = itemsToChange[ itemsToChange.length - 1 ];
  51. // Indenting a list item should also indent all the items that are already sub-items of indented item.
  52. let next = lastItem.nextSibling;
  53. // Check all items after last indented item, as long as their indent is bigger than indent of that item.
  54. while ( next && next.name == 'listItem' && next.getAttribute( 'indent' ) > lastItem.getAttribute( 'indent' ) ) {
  55. itemsToChange.push( next );
  56. next = next.nextSibling;
  57. }
  58. // We need to be sure to keep model in correct state after each small change, because converters
  59. // bases on that state and assumes that model is correct.
  60. // Because of that, if the command outdents items, we will outdent them starting from the last item, as
  61. // it is safer.
  62. if ( this._indentBy < 0 ) {
  63. itemsToChange = itemsToChange.reverse();
  64. }
  65. for ( const item of itemsToChange ) {
  66. const indent = item.getAttribute( 'indent' ) + this._indentBy;
  67. // If indent is lower than 0, it means that the item got outdented when it was not indented.
  68. // This means that we need to convert that list item to paragraph.
  69. if ( indent < 0 ) {
  70. // To keep the model as correct as possible, first rename listItem, then remove attributes,
  71. // as listItem without attributes is very incorrect and will cause problems in converters.
  72. // No need to remove attributes, will be removed by post fixer.
  73. writer.rename( item, 'paragraph' );
  74. }
  75. // If indent is >= 0, change the attribute value.
  76. else {
  77. writer.setAttribute( 'indent', indent, item );
  78. }
  79. }
  80. // Check whether some of changed list items' type should not be fixed.
  81. // But first, reverse `itemsToChange` again -- we always want to perform those fixes starting from first item (source-wise).
  82. if ( this._indentBy < 0 ) {
  83. itemsToChange = itemsToChange.reverse();
  84. }
  85. for ( const item of itemsToChange ) {
  86. _fixType( item, writer );
  87. }
  88. } );
  89. }
  90. /**
  91. * Checks whether the command can be enabled in the current context.
  92. *
  93. * @private
  94. * @returns {Boolean} Whether the command should be enabled.
  95. */
  96. _checkEnabled() {
  97. // Check whether any of position's ancestor is a list item.
  98. const listItem = first( this.editor.model.document.selection.getSelectedBlocks() );
  99. // If selection is not in a list item, the command is disabled.
  100. if ( !listItem || !listItem.is( 'listItem' ) ) {
  101. return false;
  102. }
  103. if ( this._indentBy > 0 ) {
  104. // Cannot indent first item in it's list. Check if before `listItem` is a list item that is in same list.
  105. // To be in the same list, the item has to have same attributes and cannot be "split" by an item with lower indent.
  106. const indent = listItem.getAttribute( 'indent' );
  107. const type = listItem.getAttribute( 'type' );
  108. let prev = listItem.previousSibling;
  109. while ( prev && prev.is( 'listItem' ) && prev.getAttribute( 'indent' ) >= indent ) {
  110. if ( prev.getAttribute( 'indent' ) == indent ) {
  111. // The item is on the same level.
  112. // If it has same type, it means that we found a preceding sibling from the same list.
  113. // If it does not have same type, it means that `listItem` is on different list (this can happen only
  114. // on top level lists, though).
  115. return prev.getAttribute( 'type' ) == type;
  116. }
  117. prev = prev.previousSibling;
  118. }
  119. // Could not find similar list item, this means that `listItem` is first in its list.
  120. return false;
  121. }
  122. // If we are outdenting it is enough to be in list item. Every list item can always be outdented.
  123. return true;
  124. }
  125. }
  126. // Fixes type of `item` element after it was indented/outdented. Looks for a sibling of `item` that has the same
  127. // indent and sets `item`'s type to the same as that sibling.
  128. function _fixType( item, writer ) {
  129. // Find a preceding sibling of `item` that is a list item of the same list as `item`.
  130. const prev = _seekListItem( item, false );
  131. // If found, fix type.
  132. if ( prev ) {
  133. writer.setAttribute( 'type', prev.getAttribute( 'type' ), item );
  134. return;
  135. }
  136. // If not found, find a following sibling of `item` that is a list item of the same list as `item`.
  137. const next = _seekListItem( item, true );
  138. // If found, fix type.
  139. if ( next ) {
  140. writer.setAttribute( 'type', next.getAttribute( 'type' ), item );
  141. }
  142. }
  143. // Seeks for a list item that has same indent as given `item`. May look through next siblings (`seekForward = true`) or
  144. // previous siblings (`seekForward = false`). Returns found list item or `null` if item has not been found.
  145. function _seekListItem( item, seekForward ) {
  146. let result = item[ seekForward ? 'nextSibling' : 'previousSibling' ];
  147. // Look for the previous/next sibling that has same indent and is before a list item element with lower indent.
  148. // If elements are split by an element with lower indent, they are on different lists.
  149. while ( result && result.is( 'listItem' ) && result.getAttribute( 'indent' ) >= item.getAttribute( 'indent' ) ) {
  150. if ( result.getAttribute( 'indent' ) == item.getAttribute( 'indent' ) ) {
  151. // We found sibling that is on the same list.
  152. return result;
  153. }
  154. result = result[ seekForward ? 'nextSibling' : 'previousSibling' ];
  155. }
  156. return null;
  157. }