8
0

utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 given name. Returns undefined if position is not inside desired parent.
  11. *
  12. * @param {String} parentName Name of parent element to find.
  13. * @param {module:engine/model/position~Position|module:engine/model/position~Position} position Position to start searching.
  14. * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
  15. */
  16. export function findAncestor( parentName, position ) {
  17. let parent = position.parent;
  18. while ( parent ) {
  19. if ( parent.name === parentName ) {
  20. return parent;
  21. }
  22. parent = parent.parent;
  23. }
  24. }
  25. /**
  26. * A common method to update the numeric value. If a value is the default one, it will be unset.
  27. *
  28. * @param {String} key Attribute key.
  29. * @param {*} value The new attribute value.
  30. * @param {module:engine/model/item~Item} item Model item on which the attribute will be set.
  31. * @param {module:engine/model/writer~Writer} writer
  32. * @param {*} defaultValue Default attribute value. If a value is lower or equal, it will be unset.
  33. */
  34. export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
  35. if ( value > defaultValue ) {
  36. writer.setAttribute( key, value, item );
  37. } else {
  38. writer.removeAttribute( key, item );
  39. }
  40. }
  41. /**
  42. * Common method to create empty table cell - it will create proper model structure as table cell must have at least one block inside.
  43. *
  44. * @param {module:engine/model/writer~Writer} writer Model writer.
  45. * @param {module:engine/model/position~Position} insertPosition Position at which table cell should be inserted.
  46. * @param {Object} attributes Element's attributes.
  47. */
  48. export function createEmptyTableCell( writer, insertPosition, attributes = {} ) {
  49. const tableCell = writer.createElement( 'tableCell', attributes );
  50. writer.insertElement( 'paragraph', tableCell );
  51. writer.insert( tableCell, insertPosition );
  52. }
  53. /**
  54. * Returns a string if all four values of box sides are equal.
  55. *
  56. * If a string is passed, it is treated as a single value (pass-through).
  57. *
  58. * // returns 'foo':
  59. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
  60. * getSingleValue( 'foo' );
  61. *
  62. * // Returns undefined:
  63. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
  64. * getSingleValue( { top: 'foo', right: 'foo' } );
  65. *
  66. * @param objectOrString
  67. * @returns {module:engine/view/stylesmap~BoxSides|String}
  68. */
  69. export function getSingleValue( objectOrString ) {
  70. if ( !objectOrString || !isObject( objectOrString ) ) {
  71. return objectOrString;
  72. }
  73. const { top, right, bottom, left } = objectOrString;
  74. if ( top == right && right == bottom && bottom == left ) {
  75. return top;
  76. }
  77. }