indentusingclasses.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/indentcommandbehavior/indentusingclasses
  7. */
  8. /**
  9. * The block indentation behavior that uses classes to set indentation.
  10. *
  11. * @implements module:indent/indentblockcommand~IndentBehavior
  12. */
  13. export default class IndentUsingClasses {
  14. /**
  15. * Creates an instance of the command.
  16. *
  17. * @param {Object} config
  18. * @param {String} config.direction The direction of indentation.
  19. * @param {Array.<String>} config.classes A list of classes used for indentation.
  20. */
  21. constructor( config ) {
  22. /**
  23. * The direction of indentation.
  24. *
  25. * @type {Boolean}
  26. */
  27. this.isForward = config.direction === 'forward';
  28. /**
  29. * A list of classes used for indentation.
  30. *
  31. * @type {Array.<String>}
  32. */
  33. this.classes = config.classes;
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. checkEnabled( indentAttributeValue ) {
  39. const currentIndex = this.classes.indexOf( indentAttributeValue );
  40. if ( this.isForward ) {
  41. return currentIndex < this.classes.length - 1;
  42. } else {
  43. return currentIndex >= 0;
  44. }
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. getNextIndent( indentAttributeValue ) {
  50. const currentIndex = this.classes.indexOf( indentAttributeValue );
  51. const indexStep = this.isForward ? 1 : -1;
  52. return this.classes[ currentIndex + indexStep ];
  53. }
  54. }