8
0

tableclipboard.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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/tableclipboard
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import TableSelection from './tableselection';
  10. import TableWalker from './tablewalker';
  11. import TableUtils from './tableutils';
  12. import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular, sortRanges } from './utils/selection';
  13. import {
  14. cropTableToDimensions,
  15. getHorizontallyOverlappingCells,
  16. getVerticallyOverlappingCells,
  17. removeEmptyRowsColumns,
  18. splitHorizontally,
  19. splitVertically,
  20. trimTableCellIfNeeded,
  21. adjustLastRowIndex,
  22. adjustLastColumnIndex
  23. } from './utils/structure';
  24. /**
  25. * This plugin adds support for copying/cutting/pasting fragments of tables.
  26. * It is loaded automatically by the {@link module:table/table~Table} plugin.
  27. *
  28. * @extends module:core/plugin~Plugin
  29. */
  30. export default class TableClipboard extends Plugin {
  31. /**
  32. * @inheritDoc
  33. */
  34. static get pluginName() {
  35. return 'TableClipboard';
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. static get requires() {
  41. return [ TableSelection, TableUtils ];
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. init() {
  47. const editor = this.editor;
  48. const viewDocument = editor.editing.view.document;
  49. this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopyCut( evt, data ) );
  50. this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCopyCut( evt, data ) );
  51. this.listenTo( editor.model, 'insertContent', ( evt, args ) => this._onInsertContent( evt, ...args ), { priority: 'high' } );
  52. this.decorate( '_replaceTableSlotCell' );
  53. }
  54. /**
  55. * Copies table content to a clipboard on "copy" & "cut" events.
  56. *
  57. * @private
  58. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
  59. * @param {Object} data Clipboard event data.
  60. */
  61. _onCopyCut( evt, data ) {
  62. const tableSelection = this.editor.plugins.get( TableSelection );
  63. if ( !tableSelection.getSelectedTableCells() ) {
  64. return;
  65. }
  66. if ( evt.name == 'cut' && this.editor.isReadOnly ) {
  67. return;
  68. }
  69. data.preventDefault();
  70. evt.stop();
  71. const dataController = this.editor.data;
  72. const viewDocument = this.editor.editing.view.document;
  73. const content = dataController.toView( tableSelection.getSelectionAsFragment() );
  74. viewDocument.fire( 'clipboardOutput', {
  75. dataTransfer: data.dataTransfer,
  76. content,
  77. method: evt.name
  78. } );
  79. }
  80. /**
  81. * Overrides default {@link module:engine/model/model~Model#insertContent `model.insertContent()`} method to handle pasting table inside
  82. * selected table fragment.
  83. *
  84. * Depending on selected table fragment:
  85. * - If a selected table fragment is smaller than paste table it will crop pasted table to match dimensions.
  86. * - If dimensions are equal it will replace selected table fragment with a pasted table contents.
  87. *
  88. * @private
  89. * @param evt
  90. * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
  91. * @param {module:engine/model/selection~Selectable} [selectable=model.document.selection]
  92. * The selection into which the content should be inserted. If not provided the current model document selection will be used.
  93. */
  94. _onInsertContent( evt, content, selectable ) {
  95. if ( selectable && !selectable.is( 'documentSelection' ) ) {
  96. return;
  97. }
  98. const model = this.editor.model;
  99. const tableUtils = this.editor.plugins.get( TableUtils );
  100. // We might need to crop table before inserting so reference might change.
  101. let pastedTable = getTableIfOnlyTableInContent( content, model );
  102. if ( !pastedTable ) {
  103. return;
  104. }
  105. const selectedTableCells = getSelectionAffectedTableCells( model.document.selection );
  106. if ( !selectedTableCells.length ) {
  107. removeEmptyRowsColumns( pastedTable, tableUtils );
  108. return;
  109. }
  110. // Override default model.insertContent() handling at this point.
  111. evt.stop();
  112. model.change( writer => {
  113. const pastedDimensions = {
  114. width: tableUtils.getColumns( pastedTable ),
  115. height: tableUtils.getRows( pastedTable )
  116. };
  117. // Prepare the table for pasting.
  118. const selection = prepareTableForPasting( selectedTableCells, pastedDimensions, writer, tableUtils );
  119. // Beyond this point we operate on a fixed content table with rectangular selection and proper last row/column values.
  120. const selectionHeight = selection.lastRow - selection.firstRow + 1;
  121. const selectionWidth = selection.lastColumn - selection.firstColumn + 1;
  122. // Crop pasted table if:
  123. // - Pasted table dimensions exceeds selection area.
  124. // - Pasted table has broken layout (ie some cells sticks out by the table dimensions established by the first and last row).
  125. //
  126. // Note: The table dimensions are established by the width of the first row and the total number of rows.
  127. // It is possible to programmatically create a table that has rows which would have cells anchored beyond first row width but
  128. // such table will not be created by other editing solutions.
  129. const cropDimensions = {
  130. startRow: 0,
  131. startColumn: 0,
  132. endRow: Math.min( selectionHeight, pastedDimensions.height ) - 1,
  133. endColumn: Math.min( selectionWidth, pastedDimensions.width ) - 1
  134. };
  135. pastedTable = cropTableToDimensions( pastedTable, cropDimensions, writer );
  136. // Content table to which we insert a pasted table.
  137. const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );
  138. const cellsToSelect = this._replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer );
  139. if ( this.editor.plugins.get( 'TableSelection' ).isEnabled ) {
  140. // Selection ranges must be sorted because the first and last selection ranges are considered
  141. // as anchor/focus cell ranges for multi-cell selection.
  142. const selectionRanges = sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );
  143. writer.setSelection( selectionRanges );
  144. } else {
  145. // Set selection inside first cell if multi-cell selection is disabled.
  146. writer.setSelection( cellsToSelect[ 0 ], 0 );
  147. }
  148. } );
  149. }
  150. /**
  151. * Replaces the part of selectedTable with pastedTable.
  152. *
  153. * @private
  154. * @param {module:engine/model/element~Element} pastedTable
  155. * @param {Object} pastedDimensions
  156. * @param {Number} pastedDimensions.height
  157. * @param {Number} pastedDimensions.width
  158. * @param {module:engine/model/element~Element} selectedTable
  159. * @param {Object} selection
  160. * @param {Number} selection.firstColumn
  161. * @param {Number} selection.firstRow
  162. * @param {Number} selection.lastColumn
  163. * @param {Number} selection.lastRow
  164. * @param {module:engine/model/writer~Writer} writer
  165. * @returns {Array.<module:engine/model/element~Element>}
  166. */
  167. _replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer ) {
  168. const { width: pastedWidth, height: pastedHeight } = pastedDimensions;
  169. // Holds two-dimensional array that is addressed by [ row ][ column ] that stores cells anchored at given location.
  170. const pastedTableLocationMap = createLocationMap( pastedTable, pastedWidth, pastedHeight );
  171. const selectedTableMap = [ ...new TableWalker( selectedTable, {
  172. startRow: selection.firstRow,
  173. endRow: selection.lastRow,
  174. startColumn: selection.firstColumn,
  175. endColumn: selection.lastColumn,
  176. includeAllSlots: true
  177. } ) ];
  178. // Selection must be set to pasted cells (some might be removed or new created).
  179. const cellsToSelect = [];
  180. // Store next cell insert position.
  181. let insertPosition;
  182. // Content table replace cells algorithm iterates over a selected table fragment and:
  183. //
  184. // - Removes existing table cells at current slot (location).
  185. // - Inserts cell from a pasted table for a matched slots.
  186. //
  187. // This ensures proper table geometry after the paste
  188. for ( const tableSlot of selectedTableMap ) {
  189. const { row, column } = tableSlot;
  190. // Save the insert position for current row start.
  191. if ( column === selection.firstColumn ) {
  192. insertPosition = tableSlot.getPositionBefore();
  193. }
  194. // Map current table slot location to an pasted table slot location.
  195. const pastedRow = row - selection.firstRow;
  196. const pastedColumn = column - selection.firstColumn;
  197. const pastedCell = pastedTableLocationMap[ pastedRow % pastedHeight ][ pastedColumn % pastedWidth ];
  198. // Clone cell to insert (to duplicate its attributes and children).
  199. // Cloning is required to support repeating pasted table content when inserting to a bigger selection.
  200. const cellToInsert = pastedCell ? writer.cloneElement( pastedCell ) : null;
  201. // Replace the cell from the current slot with new table cell.
  202. const newTableCell = this._replaceTableSlotCell( tableSlot, cellToInsert, insertPosition, writer );
  203. // The cell was only removed.
  204. if ( !newTableCell ) {
  205. continue;
  206. }
  207. // Trim the cell if it's row/col-spans would exceed selection area.
  208. trimTableCellIfNeeded( newTableCell, row, column, selection.lastRow, selection.lastColumn, writer );
  209. cellsToSelect.push( newTableCell );
  210. insertPosition = writer.createPositionAfter( newTableCell );
  211. }
  212. // If there are any headings, all the cells that overlap from heading must be splitted.
  213. const headingRows = parseInt( selectedTable.getAttribute( 'headingRows' ) || 0 );
  214. const headingColumns = parseInt( selectedTable.getAttribute( 'headingColumns' ) || 0 );
  215. const areHeadingRowsIntersectingSelection = selection.firstRow < headingRows && headingRows <= selection.lastRow;
  216. const areHeadingColumnsIntersectingSelection = selection.firstColumn < headingColumns && headingColumns <= selection.lastColumn;
  217. if ( areHeadingRowsIntersectingSelection ) {
  218. const columnsLimit = { first: selection.firstColumn, last: selection.lastColumn };
  219. const newCells = doHorizontalSplit( selectedTable, headingRows, columnsLimit, writer, selection.firstRow );
  220. cellsToSelect.push( ...newCells );
  221. }
  222. if ( areHeadingColumnsIntersectingSelection ) {
  223. const rowsLimit = { first: selection.firstRow, last: selection.lastRow };
  224. const newCells = doVerticalSplit( selectedTable, headingColumns, rowsLimit, writer );
  225. cellsToSelect.push( ...newCells );
  226. }
  227. return cellsToSelect;
  228. }
  229. /**
  230. * Replaces a single table slot.
  231. *
  232. * @private
  233. * @param {module:table/tablewalker~TableSlot} tableSlot
  234. * @param {module:engine/model/element~Element} cellToInsert
  235. * @param {module:engine/model/position~Position} insertPosition
  236. * @param {module:engine/model/writer~Writer} writer
  237. * @returns {module:engine/model/element~Element|null} Inserted table cell or null if slot should remain empty.
  238. */
  239. _replaceTableSlotCell( tableSlot, cellToInsert, insertPosition, writer ) {
  240. const { cell, isAnchor } = tableSlot;
  241. // If the slot is occupied by a cell in a selected table - remove it.
  242. // The slot of this cell will be either:
  243. // - Replaced by a pasted table cell.
  244. // - Spanned by a previously pasted table cell.
  245. if ( isAnchor ) {
  246. writer.remove( cell );
  247. }
  248. // There is no cell to insert (might be spanned by other cell in a pasted table) - advance to the next content table slot.
  249. if ( !cellToInsert ) {
  250. return null;
  251. }
  252. writer.insert( cellToInsert, insertPosition );
  253. return cellToInsert;
  254. }
  255. }
  256. /**
  257. * Extract table for pasting into table.
  258. *
  259. * @private
  260. * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
  261. * @param {module:engine/model/model~Model} model The editor model.
  262. * @returns {module:engine/model/element~Element|null}
  263. */
  264. export function getTableIfOnlyTableInContent( content, model ) {
  265. if ( !content.is( 'documentFragment' ) && !content.is( 'element' ) ) {
  266. return null;
  267. }
  268. // Table passed directly.
  269. if ( content.is( 'element', 'table' ) ) {
  270. return content;
  271. }
  272. // We do not support mixed content when pasting table into table.
  273. // See: https://github.com/ckeditor/ckeditor5/issues/6817.
  274. if ( content.childCount == 1 && content.getChild( 0 ).is( 'element', 'table' ) ) {
  275. return content.getChild( 0 );
  276. }
  277. // If there are only whitespaces around a table then use that table for pasting.
  278. const contentRange = model.createRangeIn( content );
  279. for ( const element of contentRange.getItems() ) {
  280. if ( element.is( 'element', 'table' ) ) {
  281. // Stop checking if there is some content before table.
  282. const rangeBefore = model.createRange( contentRange.start, model.createPositionBefore( element ) );
  283. if ( model.hasContent( rangeBefore, { ignoreWhitespaces: true } ) ) {
  284. return null;
  285. }
  286. // Stop checking if there is some content after table.
  287. const rangeAfter = model.createRange( model.createPositionAfter( element ), contentRange.end );
  288. if ( model.hasContent( rangeAfter, { ignoreWhitespaces: true } ) ) {
  289. return null;
  290. }
  291. // There wasn't any content neither before nor after.
  292. return element;
  293. }
  294. }
  295. return null;
  296. }
  297. // Prepares a table for pasting and returns adjusted selection dimensions.
  298. //
  299. // @param {Array.<module:engine/model/element~Element>} selectedTableCells
  300. // @param {Object} pastedDimensions
  301. // @param {Number} pastedDimensions.height
  302. // @param {Number} pastedDimensions.width
  303. // @param {module:engine/model/writer~Writer} writer
  304. // @param {module:table/tableutils~TableUtils} tableUtils
  305. // @returns {Object} selection
  306. // @returns {Number} selection.firstColumn
  307. // @returns {Number} selection.firstRow
  308. // @returns {Number} selection.lastColumn
  309. // @returns {Number} selection.lastRow
  310. function prepareTableForPasting( selectedTableCells, pastedDimensions, writer, tableUtils ) {
  311. const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );
  312. const columnIndexes = getColumnIndexes( selectedTableCells );
  313. const rowIndexes = getRowIndexes( selectedTableCells );
  314. const selection = {
  315. firstColumn: columnIndexes.first,
  316. lastColumn: columnIndexes.last,
  317. firstRow: rowIndexes.first,
  318. lastRow: rowIndexes.last
  319. };
  320. // Single cell selected - expand selection to pasted table dimensions.
  321. const shouldExpandSelection = selectedTableCells.length === 1;
  322. if ( shouldExpandSelection ) {
  323. selection.lastRow += pastedDimensions.height - 1;
  324. selection.lastColumn += pastedDimensions.width - 1;
  325. expandTableSize( selectedTable, selection.lastRow + 1, selection.lastColumn + 1, tableUtils );
  326. }
  327. // In case of expanding selection we do not reset the selection so in this case we will always try to fix selection
  328. // like in the case of a non-rectangular area. This might be fixed by re-setting selected cells array but this shortcut is safe.
  329. if ( shouldExpandSelection || !isSelectionRectangular( selectedTableCells, tableUtils ) ) {
  330. // For a non-rectangular selection (ie in which some cells sticks out from a virtual selection rectangle) we need to create
  331. // a table layout that has a rectangular selection. This will split cells so the selection become rectangular.
  332. // Beyond this point we will operate on fixed content table.
  333. splitCellsToRectangularSelection( selectedTable, selection, writer );
  334. }
  335. // However a selected table fragment might be invalid if examined alone. Ie such table fragment:
  336. //
  337. // +---+---+---+---+
  338. // 0 | a | b | c | d |
  339. // + + +---+---+
  340. // 1 | | e | f | g |
  341. // + +---+ +---+
  342. // 2 | | h | | i | <- last row, each cell has rowspan = 2,
  343. // + + + + + so we need to return 3, not 2
  344. // 3 | | | | |
  345. // +---+---+---+---+
  346. //
  347. // is invalid as the cells "h" and "i" have rowspans.
  348. // This case needs only adjusting the selection dimension as the rest of the algorithm operates on empty slots also.
  349. else {
  350. selection.lastRow = adjustLastRowIndex( selectedTable, selection );
  351. selection.lastColumn = adjustLastColumnIndex( selectedTable, selection );
  352. }
  353. return selection;
  354. }
  355. // Expand table (in place) to expected size.
  356. function expandTableSize( table, expectedHeight, expectedWidth, tableUtils ) {
  357. const tableWidth = tableUtils.getColumns( table );
  358. const tableHeight = tableUtils.getRows( table );
  359. if ( expectedWidth > tableWidth ) {
  360. tableUtils.insertColumns( table, {
  361. at: tableWidth,
  362. columns: expectedWidth - tableWidth
  363. } );
  364. }
  365. if ( expectedHeight > tableHeight ) {
  366. tableUtils.insertRows( table, {
  367. at: tableHeight,
  368. rows: expectedHeight - tableHeight
  369. } );
  370. }
  371. }
  372. // Returns two-dimensional array that is addressed by [ row ][ column ] that stores cells anchored at given location.
  373. //
  374. // At given row & column location it might be one of:
  375. //
  376. // * cell - cell from pasted table anchored at this location.
  377. // * null - if no cell is anchored at this location.
  378. //
  379. // For instance, from a table below:
  380. //
  381. // +----+----+----+----+
  382. // | 00 | 01 | 02 | 03 |
  383. // + +----+----+----+
  384. // | | 11 | 13 |
  385. // +----+ +----+
  386. // | 20 | | 23 |
  387. // +----+----+----+----+
  388. //
  389. // The method will return an array (numbers represents cell element):
  390. //
  391. // const map = [
  392. // [ '00', '01', '02', '03' ],
  393. // [ null, '11', null, '13' ],
  394. // [ '20', null, null, '23' ]
  395. // ]
  396. //
  397. // This allows for a quick access to table at give row & column. For instance to access table cell "13" from pasted table call:
  398. //
  399. // const cell = map[ 1 ][ 3 ]
  400. //
  401. function createLocationMap( table, width, height ) {
  402. // Create height x width (row x column) two-dimensional table to store cells.
  403. const map = new Array( height ).fill( null )
  404. .map( () => new Array( width ).fill( null ) );
  405. for ( const { column, row, cell } of new TableWalker( table ) ) {
  406. map[ row ][ column ] = cell;
  407. }
  408. return map;
  409. }
  410. // Make selected cells rectangular by splitting the cells that stand out from a rectangular selection.
  411. //
  412. // In the table below a selection is shown with "::" and slots with anchor cells are named.
  413. //
  414. // +----+----+----+----+----+ +----+----+----+----+----+
  415. // | 00 | 01 | 02 | 03 | | 00 | 01 | 02 | 03 |
  416. // + +----+ +----+----+ | ::::::::::::::::----+
  417. // | | 11 | | 13 | 14 | | ::11 | | 13:: 14 | <- first row
  418. // +----+----+ + +----+ +----::---| | ::----+
  419. // | 20 | 21 | | | 24 | select cells: | 20 ::21 | | :: 24 |
  420. // +----+----+ +----+----+ 11 -> 33 +----::---| |---::----+
  421. // | 30 | | 33 | 34 | | 30 :: | | 33:: 34 | <- last row
  422. // + + +----+ + | :::::::::::::::: +
  423. // | | | 43 | | | | | 43 | |
  424. // +----+----+----+----+----+ +----+----+----+----+----+
  425. // ^ ^
  426. // first & last columns
  427. //
  428. // Will update table to:
  429. //
  430. // +----+----+----+----+----+
  431. // | 00 | 01 | 02 | 03 |
  432. // + +----+----+----+----+
  433. // | | 11 | | 13 | 14 |
  434. // +----+----+ + +----+
  435. // | 20 | 21 | | | 24 |
  436. // +----+----+ +----+----+
  437. // | 30 | | | 33 | 34 |
  438. // + +----+----+----+ +
  439. // | | | | 43 | |
  440. // +----+----+----+----+----+
  441. //
  442. // In th example above:
  443. // - Cell "02" which have `rowspan = 4` must be trimmed at first and at after last row.
  444. // - Cell "03" which have `rowspan = 2` and `colspan = 2` must be trimmed at first column and after last row.
  445. // - Cells "00", "03" & "30" which cannot be cut by this algorithm as they are outside the trimmed area.
  446. // - Cell "13" cannot be cut as it is inside the trimmed area.
  447. function splitCellsToRectangularSelection( table, dimensions, writer ) {
  448. const { firstRow, lastRow, firstColumn, lastColumn } = dimensions;
  449. const rowIndexes = { first: firstRow, last: lastRow };
  450. const columnIndexes = { first: firstColumn, last: lastColumn };
  451. // 1. Split cells vertically in two steps as first step might create cells that needs to split again.
  452. doVerticalSplit( table, firstColumn, rowIndexes, writer );
  453. doVerticalSplit( table, lastColumn + 1, rowIndexes, writer );
  454. // 2. Split cells horizontally in two steps as first step might create cells that needs to split again.
  455. doHorizontalSplit( table, firstRow, columnIndexes, writer );
  456. doHorizontalSplit( table, lastRow + 1, columnIndexes, writer, firstRow );
  457. }
  458. function doHorizontalSplit( table, splitRow, limitColumns, writer, startRow = 0 ) {
  459. // If selection starts at first row then no split is needed.
  460. if ( splitRow < 1 ) {
  461. return;
  462. }
  463. const overlappingCells = getVerticallyOverlappingCells( table, splitRow, startRow );
  464. // Filter out cells that are not touching insides of the rectangular selection.
  465. const cellsToSplit = overlappingCells.filter( ( { column, cellWidth } ) => isAffectedBySelection( column, cellWidth, limitColumns ) );
  466. return cellsToSplit.map( ( { cell } ) => splitHorizontally( cell, splitRow, writer ) );
  467. }
  468. function doVerticalSplit( table, splitColumn, limitRows, writer ) {
  469. // If selection starts at first column then no split is needed.
  470. if ( splitColumn < 1 ) {
  471. return;
  472. }
  473. const overlappingCells = getHorizontallyOverlappingCells( table, splitColumn );
  474. // Filter out cells that are not touching insides of the rectangular selection.
  475. const cellsToSplit = overlappingCells.filter( ( { row, cellHeight } ) => isAffectedBySelection( row, cellHeight, limitRows ) );
  476. return cellsToSplit.map( ( { cell, column } ) => splitVertically( cell, column, splitColumn, writer ) );
  477. }
  478. // Checks if cell at given row (column) is affected by a rectangular selection defined by first/last column (row).
  479. //
  480. // The same check is used for row as for column.
  481. function isAffectedBySelection( index, span, limit ) {
  482. const endIndex = index + span - 1;
  483. const { first, last } = limit;
  484. const isInsideSelection = index >= first && index <= last;
  485. const overlapsSelectionFromOutside = index < first && endIndex >= first;
  486. return isInsideSelection || overlapsSelectionFromOutside;
  487. }