8
0

indentusingoffset.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/indentusingoffset
  7. */
  8. /**
  9. * @implements module:indent/indentblockcommand~IndentBehavior
  10. */
  11. export default class IndentUsingOffset {
  12. /**
  13. * Creates an instance of the behavior.
  14. *
  15. * @param {Object} config
  16. * @param {String} config.direction The direction of indentation.
  17. * @param {Number} config.offset Offset of next indentation step.
  18. * @param {String} config.unit Indentation unit.
  19. */
  20. constructor( config ) {
  21. /**
  22. * The direction of indentation.
  23. *
  24. * @type {Boolean}
  25. */
  26. this.isForward = config.direction === 'forward';
  27. /**
  28. * Offset of next indentation step.
  29. *
  30. * @type {Number}
  31. */
  32. this.offset = config.offset;
  33. /**
  34. * Indentation unit.
  35. *
  36. * @type {String}
  37. */
  38. this.unit = config.unit;
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. checkEnabled( indentAttributeValue ) {
  44. const currentOffset = parseFloat( indentAttributeValue || 0 );
  45. // The command is always enabled for forward indentation.
  46. return this.isForward || currentOffset > 0;
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. getNextIndent( indentAttributeValue ) {
  52. const currentOffset = parseFloat( indentAttributeValue || 0 );
  53. const isSameUnit = !indentAttributeValue || indentAttributeValue.endsWith( this.unit );
  54. if ( !isSameUnit ) {
  55. return this.isForward ? this.offset + this.unit : undefined;
  56. }
  57. const nextOffset = this.isForward ? this.offset : -this.offset;
  58. const offsetToSet = currentOffset + nextOffset;
  59. return offsetToSet > 0 ? offsetToSet + this.unit : undefined;
  60. }
  61. }