insertcolumncommand.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/insertcolumncommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { CellSpans } from './converters/downcasttable';
  10. import Position from '../../ckeditor5-engine/src/model/position';
  11. /**
  12. * The insert column command.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class InsertColumnCommand extends Command {
  17. /**
  18. * @inheritDoc
  19. */
  20. refresh() {
  21. const model = this.editor.model;
  22. const doc = model.document;
  23. const tableParent = getValidParent( doc.selection.getFirstPosition() );
  24. this.isEnabled = !!tableParent;
  25. }
  26. /**
  27. * Executes the command.
  28. *
  29. * @param {Object} [options] Options for the executed command.
  30. * @param {Number} [options.columns=1] Number of rows to insert.
  31. * @param {Number} [options.at=0] Row index to insert at.
  32. *
  33. * @fires execute
  34. */
  35. execute( options = {} ) {
  36. const model = this.editor.model;
  37. const document = model.document;
  38. const selection = document.selection;
  39. const columns = parseInt( options.columns ) || 1;
  40. const insertAt = parseInt( options.at ) || 0;
  41. const table = getValidParent( selection.getFirstPosition() );
  42. const cellSpans = new CellSpans();
  43. model.change( writer => {
  44. let rowIndex = 0;
  45. const headingColumns = table.getAttribute( 'headingColumns' );
  46. if ( insertAt < headingColumns ) {
  47. writer.setAttribute( 'headingColumns', headingColumns + columns, table );
  48. }
  49. for ( const row of table.getChildren() ) {
  50. // TODO: what to do with max columns?
  51. let columnIndex = 0;
  52. // Cache original children.
  53. const children = [ ...row.getChildren() ];
  54. for ( const tableCell of children ) {
  55. let colspan = tableCell.hasAttribute( 'colspan' ) ? parseInt( tableCell.getAttribute( 'colspan' ) ) : 1;
  56. const rowspan = tableCell.hasAttribute( 'rowspan' ) ? parseInt( tableCell.getAttribute( 'rowspan' ) ) : 1;
  57. columnIndex = cellSpans.getNextFreeColumnIndex( rowIndex, columnIndex );
  58. // TODO: this is not cool:
  59. const shouldExpandSpan = colspan > 1 &&
  60. ( columnIndex !== insertAt ) &&
  61. ( columnIndex <= insertAt ) &&
  62. ( columnIndex <= insertAt + columns ) &&
  63. ( columnIndex + colspan > insertAt );
  64. if ( shouldExpandSpan ) {
  65. colspan += columns;
  66. writer.setAttribute( 'colspan', colspan, tableCell );
  67. }
  68. while ( columnIndex >= insertAt && columnIndex < insertAt + columns ) {
  69. const cell = writer.createElement( 'tableCell' );
  70. writer.insert( cell, Position.createBefore( tableCell ) );
  71. columnIndex++;
  72. }
  73. cellSpans.recordSpans( rowIndex, columnIndex, rowspan, colspan );
  74. columnIndex += colspan;
  75. }
  76. // Insert at the end of column
  77. while ( columnIndex >= insertAt && columnIndex < insertAt + columns ) {
  78. const cell = writer.createElement( 'tableCell' );
  79. writer.insert( cell, row, 'end' );
  80. columnIndex++;
  81. }
  82. rowIndex++;
  83. }
  84. } );
  85. }
  86. }
  87. function getValidParent( firstPosition ) {
  88. let parent = firstPosition.parent;
  89. while ( parent ) {
  90. if ( parent.name === 'table' ) {
  91. return parent;
  92. }
  93. parent = parent.parent;
  94. }
  95. }