8
0

liststylecommand.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 { getSiblingNodes } from './utils';
  10. /**
  11. * The list style command. It is used by the {@link module:list/liststyle~ListStyle list style 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 style, e.g. `'disc'` or `'square'`. If `null` is 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. }