listcommand.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Command from '../core/command/command.js';
  6. import { getClosestListItem, getSelectedBlocks, getPositionBeforeBlock } from './utils.js';
  7. /**
  8. * The list command. It is used by the {@link list.List list feature}.
  9. *
  10. * @memberOf list
  11. * @extends core.command.Command
  12. */
  13. export default class ListCommand extends Command {
  14. /**
  15. * Creates an instance of the command.
  16. *
  17. * @param {core.editor.Editor} editor Editor instance.
  18. * @param {'numbered'|'bulleted'} type List type that will be handled by this command.
  19. */
  20. constructor( editor, type ) {
  21. super( editor );
  22. /**
  23. * The type of list created by the command.
  24. *
  25. * @readonly
  26. * @member {'numbered'|'bulleted'} list.ListCommand#type
  27. */
  28. this.type = type == 'bulleted' ? 'bulleted' : 'numbered';
  29. /**
  30. * Flag indicating whether the command is active, which means that selection starts in a list of the same type.
  31. *
  32. * @observable
  33. * @member {Boolean} list.ListCommand#value
  34. */
  35. this.set( 'value', false );
  36. const changeCallback = () => {
  37. this.refreshValue();
  38. this.refreshState();
  39. };
  40. // Listen on selection and document changes and set the current command's value.
  41. this.listenTo( editor.document.selection, 'change:range', changeCallback );
  42. this.listenTo( editor.document, 'changesDone', changeCallback );
  43. }
  44. /**
  45. * Sets command's value based on the document selection.
  46. */
  47. refreshValue() {
  48. const position = this.editor.document.selection.getFirstPosition();
  49. // Check whether closest `listItem` ancestor of the position has a correct type.
  50. const listItem = getClosestListItem( position );
  51. this.value = listItem !== null && listItem.getAttribute( 'type' ) == this.type;
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. _doExecute() {
  57. const document = this.editor.document;
  58. const blocks = getSelectedBlocks( document.selection, document.schema );
  59. // Whether we are turning off some items.
  60. const turnOff = this.value === true;
  61. // If we are turning off items, we are going to rename them to paragraphs.
  62. document.enqueueChanges( () => {
  63. const batch = document.batch();
  64. // If part of a list got turned off, we need to handle (outdent) all of sub-items of the last turned-off item.
  65. // To be sure that model is all the time in a good state, we first fix items below turned-off item.
  66. if ( turnOff ) {
  67. // Start from the model item that is just after the last turned-off item.
  68. let next = blocks[ blocks.length - 1 ].nextSibling;
  69. let currentIndent = Number.POSITIVE_INFINITY;
  70. let changes = [];
  71. // Correct indent of all items after the last turned off item.
  72. // Rules that should be followed:
  73. // 1. All direct sub-items of turned-off item should become indent 0, because the first item after it
  74. // will be the first item of a new list. Other items are at the same level, so should have same 0 index.
  75. // 2. All items with indent lower than indent of turned-off item should become indent 0, because they
  76. // should not end up as a child of any of list items that they were not children of before.
  77. // 3. All other items should have their indent changed relatively to it's parent.
  78. //
  79. // For example:
  80. // 1 * --------
  81. // 2 * --------
  82. // 3 * -------- <- this is turned off.
  83. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  84. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  85. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  86. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  87. // 8 * -------- <- this has to become indent = 0.
  88. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  89. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  90. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  91. // 12 * -------- <- this and all below are left unchanged.
  92. // 13 * --------
  93. // 14 * --------
  94. //
  95. // After turning off 3 the list becomes:
  96. //
  97. // 1 * --------
  98. // 2 * --------
  99. //
  100. // 3 --------
  101. //
  102. // 4 * --------
  103. // 5 * --------
  104. // 6 * --------
  105. // 7 * --------
  106. // 8 * --------
  107. // 9 * --------
  108. // 10 * --------
  109. // 11 * --------
  110. // 12 * --------
  111. // 13 * --------
  112. // 14 * --------
  113. //
  114. // Thanks to this algorithm no lists are mismatched and no items get unexpected children/parent, while
  115. // those parent-child connection which are possible to maintain are still maintained. It's worth noting
  116. // that this is the same effect that we would be get by multiple use of outdent command. However doing
  117. // it like this is much more efficient because it's less operation (less memory usage, easier OT) and
  118. // less conversion (faster).
  119. while ( next && next.name == 'listItem' && next.getAttribute( 'indent' ) !== 0 ) {
  120. // Check each next list item, as long as its indent is bigger than 0.
  121. // If the indent is 0 we are not going to change anything anyway.
  122. const indent = next.getAttribute( 'indent' );
  123. // We check if that's item indent is lower as current relative indent.
  124. if ( indent < currentIndent ) {
  125. // If it is, current relative indent becomes that indent.
  126. currentIndent = indent;
  127. }
  128. // Fix indent relatively to current relative indent.
  129. // Note, that if we just changed the current relative indent, the newIndent will be equal to 0.
  130. const newIndent = indent - currentIndent;
  131. // Save the entry in changes array. We do not apply it at the moment, because we will need to
  132. // reverse the changes so the last item is changed first.
  133. // This is to keep model in correct state all the time.
  134. changes.push( { element: next, indent: newIndent } );
  135. // Find next item.
  136. next = next.nextSibling;
  137. }
  138. changes = changes.reverse();
  139. for ( let item of changes ) {
  140. batch.setAttribute( item.element, 'indent', item.indent );
  141. }
  142. }
  143. // Phew! Now it will be easier :).
  144. // For each block element that was in the selection, we will either: turn it to list item,
  145. // turn it to paragraph, or change it's type. Or leave it as it is.
  146. for ( let element of blocks ) {
  147. if ( turnOff && element.name == 'listItem' ) {
  148. // We are turning off and the element is a `listItem` - it should be converted to `paragraph`.
  149. // The order is important to keep model in correct state.
  150. batch.rename( element, 'paragraph' ).removeAttribute( element, 'type' ).removeAttribute( element, 'indent' );
  151. } else if ( !turnOff && element.name != 'listItem' ) {
  152. // We are turning on and the element is not a `listItem` - it should be converted to `listItem`.
  153. // The order is important to keep model in correct state.
  154. batch.setAttribute( element, 'type', this.type ).setAttribute( element, 'indent', 0 ).rename( element, 'listItem' );
  155. } else if ( !turnOff && element.name == 'listItem' && element.getAttribute( 'type' ) != this.type ) {
  156. // We are turning on and the element is a `listItem` but has different type - change type.
  157. batch.setAttribute( element, 'type', this.type );
  158. }
  159. }
  160. } );
  161. }
  162. /**
  163. * @inheritDoc
  164. */
  165. _checkEnabled() {
  166. // If command is enabled it means that we are in list item, so the command should be enabled.
  167. if ( this.value ) {
  168. return true;
  169. }
  170. const selection = this.editor.document.selection;
  171. const schema = this.editor.document.schema;
  172. const position = getPositionBeforeBlock( selection.getFirstPosition(), schema );
  173. // Otherwise, check if list item can be inserted at the position start.
  174. return schema.check( { name: 'listItem', inside: position, attributes: [ 'type', 'indent' ] } );
  175. }
  176. }