8
0

utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 table/commands/utils
  7. */
  8. import { isObject } from 'lodash-es';
  9. /**
  10. * Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
  11. *
  12. * @param {String} parentName The name of the parent element to find.
  13. * @param {module:engine/model/position~Position|module:engine/model/position~Position} positionOrElement The position or
  14. * the parentElement to start searching.
  15. * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
  16. */
  17. export function findAncestor( parentName, positionOrElement ) {
  18. let parent = positionOrElement.parent;
  19. while ( parent ) {
  20. if ( parent.name === parentName ) {
  21. return parent;
  22. }
  23. parent = parent.parent;
  24. }
  25. }
  26. /**
  27. * A common method to update the numeric value. If a value is the default one, it will be unset.
  28. *
  29. * @param {String} key An attribute key.
  30. * @param {*} value The new attribute value.
  31. * @param {module:engine/model/item~Item} item A model item on which the attribute will be set.
  32. * @param {module:engine/model/writer~Writer} writer
  33. * @param {*} defaultValue The default attribute value. If a value is lower or equal, it will be unset.
  34. */
  35. export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
  36. if ( value > defaultValue ) {
  37. writer.setAttribute( key, value, item );
  38. } else {
  39. writer.removeAttribute( key, item );
  40. }
  41. }
  42. /**
  43. * A common method to create an empty table cell. It creates a proper model structure as a table cell must have at least one block inside.
  44. *
  45. * @param {module:engine/model/writer~Writer} writer The model writer.
  46. * @param {module:engine/model/position~Position} insertPosition The position at which the table cell should be inserted.
  47. * @param {Object} attributes The element attributes.
  48. */
  49. export function createEmptyTableCell( writer, insertPosition, attributes = {} ) {
  50. const tableCell = writer.createElement( 'tableCell', attributes );
  51. writer.insertElement( 'paragraph', tableCell );
  52. writer.insert( tableCell, insertPosition );
  53. }
  54. /**
  55. * Returns a string if all four values of box sides are equal.
  56. *
  57. * If a string is passed, it is treated as a single value (pass-through).
  58. *
  59. * // Returns 'foo':
  60. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
  61. * getSingleValue( 'foo' );
  62. *
  63. * // Returns undefined:
  64. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
  65. * getSingleValue( { top: 'foo', right: 'foo' } );
  66. *
  67. * @param objectOrString
  68. * @returns {module:engine/view/stylesmap~BoxSides|String}
  69. */
  70. export function getSingleValue( objectOrString ) {
  71. if ( !objectOrString || !isObject( objectOrString ) ) {
  72. return objectOrString;
  73. }
  74. const { top, right, bottom, left } = objectOrString;
  75. if ( top == right && right == bottom && bottom == left ) {
  76. return top;
  77. }
  78. }
  79. /**
  80. * Adds a unit to a value if the value is a number or a string representing a number.
  81. *
  82. * **Note**: It does nothing to non-numeric values.
  83. *
  84. * getSingleValue( 25, 'px' ); // '25px'
  85. * getSingleValue( 25, 'em' ); // '25em'
  86. * getSingleValue( '25em', 'px' ); // '25em'
  87. * getSingleValue( 'foo', 'px' ); // 'foo'
  88. *
  89. * @param {*} value
  90. * @param {String} defaultUnit A default unit added to a numeric value.
  91. * @returns {String|*}
  92. */
  93. export function addDefaultUnitToNumericValue( value, defaultUnit ) {
  94. const numericValue = parseFloat( value );
  95. if ( Number.isNaN( numericValue ) ) {
  96. return value;
  97. }
  98. if ( String( numericValue ) !== String( value ) ) {
  99. return value;
  100. }
  101. return `${ numericValue }${ defaultUnit }`;
  102. }