insertrowcommand.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/insertrowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TableIterator from './tableiterator';
  10. /**
  11. * The insert row command.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class InsertRowCommand extends Command {
  16. /**
  17. * @inheritDoc
  18. */
  19. refresh() {
  20. const model = this.editor.model;
  21. const doc = model.document;
  22. const tableParent = getValidParent( doc.selection.getFirstPosition() );
  23. this.isEnabled = !!tableParent;
  24. }
  25. /**
  26. * Executes the command.
  27. *
  28. * @param {Object} [options] Options for the executed command.
  29. * @param {Number} [options.rows=1] Number of rows to insert.
  30. * @param {Number} [options.at=0] Row index to insert at.
  31. *
  32. * @fires execute
  33. */
  34. execute( options = {} ) {
  35. const model = this.editor.model;
  36. const document = model.document;
  37. const selection = document.selection;
  38. const rows = parseInt( options.rows ) || 1;
  39. const insertAt = parseInt( options.at ) || 0;
  40. const table = getValidParent( selection.getFirstPosition() );
  41. const headingRows = table.getAttribute( 'headingRows' ) || 0;
  42. const columns = getColumns( table );
  43. model.change( writer => {
  44. if ( headingRows > insertAt ) {
  45. writer.setAttribute( 'headingRows', headingRows + rows, table );
  46. }
  47. const tableIterator = new TableIterator( table );
  48. let tableCellToInsert = 0;
  49. for ( const tableCellInfo of tableIterator.iterateOverRows( 0, insertAt + 1 ) ) {
  50. const { row, rowspan, colspan, cell } = tableCellInfo;
  51. if ( row < insertAt ) {
  52. if ( rowspan > 1 ) {
  53. // check whether rowspan overlaps inserts:
  54. if ( row < insertAt && row + rowspan > insertAt ) {
  55. writer.setAttribute( 'rowspan', rowspan + rows, cell );
  56. }
  57. }
  58. } else if ( row === insertAt ) {
  59. tableCellToInsert += colspan;
  60. }
  61. }
  62. if ( insertAt >= table.childCount ) {
  63. tableCellToInsert = columns;
  64. }
  65. for ( let i = 0; i < rows; i++ ) {
  66. const tableRow = writer.createElement( 'tableRow' );
  67. writer.insert( tableRow, table, insertAt );
  68. for ( let columnIndex = 0; columnIndex < tableCellToInsert; columnIndex++ ) {
  69. const cell = writer.createElement( 'tableCell' );
  70. writer.insert( cell, tableRow, 'end' );
  71. }
  72. }
  73. } );
  74. }
  75. }
  76. function getValidParent( firstPosition ) {
  77. let parent = firstPosition.parent;
  78. while ( parent ) {
  79. if ( parent.name === 'table' ) {
  80. return parent;
  81. }
  82. parent = parent.parent;
  83. }
  84. }
  85. function getColumns( table ) {
  86. const row = table.getChild( 0 );
  87. return [ ...row.getChildren() ].reduce( ( columns, row ) => {
  88. const columnWidth = parseInt( row.getAttribute( 'colspan' ) ) || 1;
  89. return columns + ( columnWidth );
  90. }, 0 );
  91. }