8
0

common.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
  10. *
  11. * @param {String} parentName The name of the parent element to find.
  12. * @param {module:engine/model/position~Position|module:engine/model/position~Position} positionOrElement The position or
  13. * the parentElement to start searching.
  14. * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
  15. */
  16. export function findAncestor( parentName, positionOrElement ) {
  17. let parent = positionOrElement.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 An attribute key.
  29. * @param {*} value The new attribute value.
  30. * @param {module:engine/model/item~Item} item A model item on which the attribute will be set.
  31. * @param {module:engine/model/writer~Writer} writer
  32. * @param {*} defaultValue The 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. * 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.
  43. *
  44. * @param {module:engine/model/writer~Writer} writer The model writer.
  45. * @param {module:engine/model/position~Position} insertPosition The position at which the table cell should be inserted.
  46. * @param {Object} attributes The element 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. * Checks if a table cell belongs to the heading column section.
  55. *
  56. * @param {module:table/tableutils~TableUtils} tableUtils
  57. * @param {module:engine/model/element~Element} tableCell
  58. * @returns {Boolean}
  59. */
  60. export function isHeadingColumnCell( tableUtils, tableCell ) {
  61. const table = tableCell.parent.parent;
  62. const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  63. const { column } = tableUtils.getCellLocation( tableCell );
  64. return !!headingColumns && column < headingColumns;
  65. }