common.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/utils/common
  7. */
  8. /**
  9. * A common method to update the numeric value. If a value is the default one, it will be unset.
  10. *
  11. * @param {String} key An attribute key.
  12. * @param {*} value The new attribute value.
  13. * @param {module:engine/model/item~Item} item A model item on which the attribute will be set.
  14. * @param {module:engine/model/writer~Writer} writer
  15. * @param {*} defaultValue The default attribute value. If a value is lower or equal, it will be unset.
  16. */
  17. export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
  18. if ( value > defaultValue ) {
  19. writer.setAttribute( key, value, item );
  20. } else {
  21. writer.removeAttribute( key, item );
  22. }
  23. }
  24. /**
  25. * 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.
  26. *
  27. * @param {module:engine/model/writer~Writer} writer The model writer.
  28. * @param {module:engine/model/position~Position} insertPosition The position at which the table cell should be inserted.
  29. * @param {Object} attributes The element attributes.
  30. * @returns {module:engine/model/element~Element} Created table cell.
  31. */
  32. export function createEmptyTableCell( writer, insertPosition, attributes = {} ) {
  33. const tableCell = writer.createElement( 'tableCell', attributes );
  34. writer.insertElement( 'paragraph', tableCell );
  35. writer.insert( tableCell, insertPosition );
  36. return tableCell;
  37. }
  38. /**
  39. * Checks if a table cell belongs to the heading column section.
  40. *
  41. * @param {module:table/tableutils~TableUtils} tableUtils
  42. * @param {module:engine/model/element~Element} tableCell
  43. * @returns {Boolean}
  44. */
  45. export function isHeadingColumnCell( tableUtils, tableCell ) {
  46. const table = tableCell.parent.parent;
  47. const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  48. const { column } = tableUtils.getCellLocation( tableCell );
  49. return !!headingColumns && column < headingColumns;
  50. }