8
0

liststylecommand.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module list/liststylecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TreeWalker from '@ckeditor/ckeditor5-engine/src/model/treewalker';
  10. /**
  11. * The list style command. It is used by the {@link module:list/liststyle~ListStyle list styles feature}.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class ListStyleCommand extends Command {
  16. /**
  17. * Creates an instance of the command.
  18. *
  19. * @param {module:core/editor/editor~Editor} editor The editor instance.
  20. * @param {String} defaultType The list type that will be used by default if the value was not specified during
  21. * the command execution.
  22. */
  23. constructor( editor, defaultType ) {
  24. super( editor );
  25. /**
  26. * The default type of the list style.
  27. *
  28. * @protected
  29. * @member {String}
  30. */
  31. this._defaultType = defaultType;
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. refresh() {
  37. this.value = this._getValue();
  38. this.isEnabled = this._checkEnabled();
  39. }
  40. /**
  41. * Executes the command.
  42. *
  43. * @param {Object} options
  44. * @param {String|null} options.type The type of the list styles, e.g. 'disc' or 'square'. If `null` specified, the default
  45. * style will be applied.
  46. * @protected
  47. */
  48. execute( options = {} ) {
  49. const model = this.editor.model;
  50. const document = model.document;
  51. // For all selected blocks find all list items that are being selected
  52. // and update the `listStyle` attribute in those lists.
  53. let listItems = [ ...document.selection.getSelectedBlocks() ]
  54. .filter( element => element.is( 'element', 'listItem' ) )
  55. .map( element => {
  56. const position = model.change( writer => writer.createPositionAt( element, 0 ) );
  57. return [
  58. ...getSiblingNodes( position, 'backward' ),
  59. ...getSiblingNodes( position, 'forward' )
  60. ];
  61. } )
  62. .flat();
  63. // Since `getSelectedBlocks()` can return items that belong to the same list, and
  64. // `getSiblingNodes()` returns the entire list, we need to remove duplicated items.
  65. listItems = [ ...new Set( listItems ) ];
  66. if ( !listItems.length ) {
  67. return;
  68. }
  69. model.change( writer => {
  70. for ( const item of listItems ) {
  71. writer.setAttribute( 'listStyle', options.type || this._defaultType, item );
  72. }
  73. } );
  74. }
  75. /**
  76. * Checks the command's {@link #value}.
  77. *
  78. * @private
  79. * @returns {String|null} The current value.
  80. */
  81. _getValue() {
  82. const listItem = this.editor.model.document.selection.getFirstPosition().parent;
  83. if ( listItem && listItem.is( 'element', 'listItem' ) ) {
  84. return listItem.getAttribute( 'listStyle' );
  85. }
  86. return null;
  87. }
  88. /**
  89. * Checks whether the command can be enabled in the current context.
  90. *
  91. * @private
  92. * @returns {Boolean} Whether the command should be enabled.
  93. */
  94. _checkEnabled() {
  95. const editor = this.editor;
  96. const numberedList = editor.commands.get( 'numberedList' );
  97. const bulletedList = editor.commands.get( 'bulletedList' );
  98. return numberedList.isEnabled || bulletedList.isEnabled;
  99. }
  100. }
  101. // Returns an array with all `listItem` elements that represents the same list.
  102. //
  103. // It means that values for `listIndent`, `listType`, and `listStyle` for all items
  104. // are equal.
  105. //
  106. // @param {module:engine/model/position~Position} position Starting position.
  107. // @param {'forward'|'backward'} direction Walking direction.
  108. // @returns {Array.<module:engine/model/element~Element>
  109. function getSiblingNodes( position, direction ) {
  110. const items = [];
  111. const listItem = position.parent;
  112. const walkerOptions = {
  113. ignoreElementEnd: true,
  114. startPosition: position,
  115. shallow: true,
  116. direction
  117. };
  118. const limitIndent = listItem.getAttribute( 'listIndent' );
  119. const nodes = [ ...new TreeWalker( walkerOptions ) ]
  120. .filter( value => value.item.is( 'element' ) )
  121. .map( value => value.item );
  122. for ( const element of nodes ) {
  123. // If found something else than `listItem`, we're out of the list scope.
  124. if ( !element.is( 'element', 'listItem' ) ) {
  125. break;
  126. }
  127. // If current parsed item has lower indent that element that the element that was a starting point,
  128. // it means we left a nested list. Abort searching items.
  129. //
  130. // ■ List item 1. [listIndent=0]
  131. // ○ List item 2.[] [listIndent=1], limitIndent = 1,
  132. // ○ List item 3. [listIndent=1]
  133. // ■ List item 4. [listIndent=0]
  134. //
  135. // Abort searching when leave nested list.
  136. if ( element.getAttribute( 'listIndent' ) < limitIndent ) {
  137. break;
  138. }
  139. // ■ List item 1.[] [listIndent=0] limitIndent = 0,
  140. // ○ List item 2. [listIndent=1]
  141. // ○ List item 3. [listIndent=1]
  142. // ■ List item 4. [listIndent=0]
  143. //
  144. // Ignore nested lists.
  145. if ( element.getAttribute( 'listIndent' ) > limitIndent ) {
  146. continue;
  147. }
  148. // ■ List item 1.[] [listType=bulleted]
  149. // 1. List item 2. [listType=numbered]
  150. // 2.List item 3. [listType=numbered]
  151. //
  152. // Abort searching when found a different kind of a list.
  153. if ( element.getAttribute( 'listType' ) !== listItem.getAttribute( 'listType' ) ) {
  154. break;
  155. }
  156. // ■ List item 1.[] [listType=bulleted]
  157. // ■ List item 2. [listType=bulleted]
  158. // ○ List item 3. [listType=bulleted]
  159. // ○ List item 4. [listType=bulleted]
  160. //
  161. // Abort searching when found a different list style.
  162. if ( element.getAttribute( 'listStyle' ) !== listItem.getAttribute( 'listStyle' ) ) {
  163. break;
  164. }
  165. if ( direction === 'backward' ) {
  166. items.unshift( element );
  167. } else {
  168. items.push( element );
  169. }
  170. }
  171. return items;
  172. }