utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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
  7. */
  8. import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  9. import { findAncestor } from './commands/utils';
  10. import TableWalker from './tablewalker';
  11. /**
  12. * Converts a given {@link module:engine/view/element~Element} to a table widget:
  13. * * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the table widget element.
  14. * * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
  15. *
  16. * @param {module:engine/view/element~Element} viewElement
  17. * @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
  18. * @param {String} label The element's label. It will be concatenated with the table `alt` attribute if one is present.
  19. * @returns {module:engine/view/element~Element}
  20. */
  21. export function toTableWidget( viewElement, writer ) {
  22. writer.setCustomProperty( 'table', true, viewElement );
  23. return toWidget( viewElement, writer, { hasSelectionHandle: true } );
  24. }
  25. /**
  26. * Checks if a given view element is a table widget.
  27. *
  28. * @param {module:engine/view/element~Element} viewElement
  29. * @returns {Boolean}
  30. */
  31. export function isTableWidget( viewElement ) {
  32. return !!viewElement.getCustomProperty( 'table' ) && isWidget( viewElement );
  33. }
  34. /**
  35. * Returns a table widget editing view element if one is selected.
  36. *
  37. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  38. * @returns {module:engine/view/element~Element|null}
  39. */
  40. export function getSelectedTableWidget( selection ) {
  41. const viewElement = selection.getSelectedElement();
  42. if ( viewElement && isTableWidget( viewElement ) ) {
  43. return viewElement;
  44. }
  45. return null;
  46. }
  47. /**
  48. * Returns a table widget editing view element if one is among the selection's ancestors.
  49. *
  50. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
  51. * @returns {module:engine/view/element~Element|null}
  52. */
  53. export function getTableWidgetAncestor( selection ) {
  54. const parentTable = findAncestor( 'table', selection.getFirstPosition() );
  55. if ( parentTable && isTableWidget( parentTable.parent ) ) {
  56. return parentTable.parent;
  57. }
  58. return null;
  59. }
  60. /**
  61. * Returns all model table cells that are fully selected (from the outside)
  62. * within the provided model selection's ranges.
  63. *
  64. * To obtain the cells selected from the inside, use
  65. * {@link module:table/utils~getTableCellsContainingSelection}.
  66. *
  67. * @param {module:engine/model/selection~Selection} selection
  68. * @returns {Array.<module:engine/model/element~Element>}
  69. */
  70. export function getSelectedTableCells( selection ) {
  71. const cells = [];
  72. for ( const range of sortRanges( selection.getRanges() ) ) {
  73. const element = range.getContainedElement();
  74. if ( element && element.is( 'tableCell' ) ) {
  75. cells.push( element );
  76. }
  77. }
  78. return cells;
  79. }
  80. /**
  81. * Returns all model table cells that the provided model selection's ranges
  82. * {@link module:engine/model/range~Range#start} inside.
  83. *
  84. * To obtain the cells selected from the outside, use
  85. * {@link module:table/utils~getSelectedTableCells}.
  86. *
  87. * @param {module:engine/model/selection~Selection} selection
  88. * @returns {Array.<module:engine/model/element~Element>}
  89. */
  90. export function getTableCellsContainingSelection( selection ) {
  91. const cells = [];
  92. for ( const range of selection.getRanges() ) {
  93. const cellWithSelection = findAncestor( 'tableCell', range.start );
  94. if ( cellWithSelection ) {
  95. cells.push( cellWithSelection );
  96. }
  97. }
  98. return cells;
  99. }
  100. /**
  101. * Returns all model table cells that are either completely selected
  102. * by selection ranges or host selection range
  103. * {@link module:engine/model/range~Range#start start positions} inside them.
  104. *
  105. * Combines {@link module:table/utils~getTableCellsContainingSelection} and
  106. * {@link module:table/utils~getSelectedTableCells}.
  107. *
  108. * @param {module:engine/model/selection~Selection} selection
  109. * @returns {Array.<module:engine/model/element~Element>}
  110. */
  111. export function getSelectionAffectedTableCells( selection ) {
  112. const selectedCells = getSelectedTableCells( selection );
  113. if ( selectedCells.length ) {
  114. return selectedCells;
  115. }
  116. return getTableCellsContainingSelection( selection );
  117. }
  118. /**
  119. * Returns an object with the `first` and `last` row index contained in the given `tableCells`.
  120. *
  121. * const selectedTableCells = getSelectedTableCells( editor.model.document.selection );
  122. *
  123. * const { first, last } = getRowIndexes( selectedTableCells );
  124. *
  125. * console.log( `Selected rows: ${ first } to ${ last }` );
  126. *
  127. * @param {Array.<module:engine/model/element~Element>} tableCells
  128. * @returns {Object} Returns an object with the `first` and `last` table row indexes.
  129. */
  130. export function getRowIndexes( tableCells ) {
  131. const indexes = tableCells.map( cell => cell.parent.index );
  132. return getFirstLastIndexesObject( indexes );
  133. }
  134. /**
  135. * Returns an object with the `first` and `last` column index contained in the given `tableCells`.
  136. *
  137. * const selectedTableCells = getSelectedTableCells( editor.model.document.selection );
  138. *
  139. * const { first, last } = getColumnIndexes( selectedTableCells );
  140. *
  141. * console.log( `Selected columns: ${ first } to ${ last }` );
  142. *
  143. * @param {Array.<module:engine/model/element~Element>} tableCells
  144. * @returns {Object} Returns an object with the `first` and `last` table column indexes.
  145. */
  146. export function getColumnIndexes( tableCells ) {
  147. const table = findAncestor( 'table', tableCells[ 0 ] );
  148. const tableMap = [ ...new TableWalker( table ) ];
  149. const indexes = tableMap
  150. .filter( entry => tableCells.includes( entry.cell ) )
  151. .map( entry => entry.column );
  152. return getFirstLastIndexesObject( indexes );
  153. }
  154. /**
  155. * Checks if the selection contains cells that do not exceed rectangular selection.
  156. *
  157. * In a table below:
  158. *
  159. * ┌───┬───┬───┬───┐
  160. * │ a │ b │ c │ d │
  161. * ├───┴───┼───┤ │
  162. * │ e │ f │ │
  163. * ├ ├───┼───┤
  164. * │ │ g │ h │
  165. * └───────┴───┴───┘
  166. *
  167. * Valid selections are these which create a solid rectangle (without gaps), such as:
  168. * - a, b (two horizontal cells)
  169. * - c, f (two vertical cells)
  170. * - a, b, e (cell "e" spans over four cells)
  171. * - c, d, f (cell d spans over a cell in the row below)
  172. *
  173. * While an invalid selection would be:
  174. * - a, c (the unselected cell "b" creates a gap)
  175. * - f, g, h (cell "d" spans over a cell from the row of "f" cell - thus creates a gap)
  176. *
  177. * @param {Array.<module:engine/model/element~Element>} selectedTableCells
  178. * @param {module:table/tableutils~TableUtils} tableUtils
  179. * @returns {Boolean}
  180. */
  181. export function isSelectionRectangular( selectedTableCells, tableUtils ) {
  182. if ( selectedTableCells.length < 2 || !areCellInTheSameTableSection( selectedTableCells ) ) {
  183. return false;
  184. }
  185. // A valid selection is a fully occupied rectangle composed of table cells.
  186. // Below we will calculate the area of a selected table cells and the area of valid selection.
  187. // The area of a valid selection is defined by top-left and bottom-right cells.
  188. const rows = new Set();
  189. const columns = new Set();
  190. let areaOfSelectedCells = 0;
  191. for ( const tableCell of selectedTableCells ) {
  192. const { row, column } = tableUtils.getCellLocation( tableCell );
  193. const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
  194. const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
  195. // Record row & column indexes of current cell.
  196. rows.add( row );
  197. columns.add( column );
  198. // For cells that spans over multiple rows add also the last row that this cell spans over.
  199. if ( rowspan > 1 ) {
  200. rows.add( row + rowspan - 1 );
  201. }
  202. // For cells that spans over multiple columns add also the last column that this cell spans over.
  203. if ( colspan > 1 ) {
  204. columns.add( column + colspan - 1 );
  205. }
  206. areaOfSelectedCells += ( rowspan * colspan );
  207. }
  208. // We can only merge table cells that are in adjacent rows...
  209. const areaOfValidSelection = getBiggestRectangleArea( rows, columns );
  210. return areaOfValidSelection == areaOfSelectedCells;
  211. }
  212. // Helper method to get an object with `first` and `last` indexes from an unsorted array of indexes.
  213. function getFirstLastIndexesObject( indexes ) {
  214. const allIndexesSorted = indexes.sort( ( indexA, indexB ) => indexA - indexB );
  215. const first = allIndexesSorted[ 0 ];
  216. const last = allIndexesSorted[ allIndexesSorted.length - 1 ];
  217. return { first, last };
  218. }
  219. function sortRanges( rangesIterator ) {
  220. return Array.from( rangesIterator ).sort( compareRangeOrder );
  221. }
  222. function compareRangeOrder( rangeA, rangeB ) {
  223. // Since table cell ranges are disjoint, it's enough to check their start positions.
  224. const posA = rangeA.start;
  225. const posB = rangeB.start;
  226. // Checking for equal position (returning 0) is not needed because this would be either:
  227. // a. Intersecting range (not allowed by model)
  228. // b. Collapsed range on the same position (allowed by model but should not happen).
  229. return posA.isBefore( posB ) ? -1 : 1;
  230. }
  231. // Calculates the area of a maximum rectangle that can span over the provided row & column indexes.
  232. //
  233. // @param {Array.<Number>} rows
  234. // @param {Array.<Number>} columns
  235. // @returns {Number}
  236. function getBiggestRectangleArea( rows, columns ) {
  237. const rowsIndexes = Array.from( rows.values() );
  238. const columnIndexes = Array.from( columns.values() );
  239. const lastRow = Math.max( ...rowsIndexes );
  240. const firstRow = Math.min( ...rowsIndexes );
  241. const lastColumn = Math.max( ...columnIndexes );
  242. const firstColumn = Math.min( ...columnIndexes );
  243. return ( lastRow - firstRow + 1 ) * ( lastColumn - firstColumn + 1 );
  244. }
  245. // Checks if the selection does not mix a header (column or row) with other cells.
  246. //
  247. // For instance, in the table below valid selections consist of cells with the same letter only.
  248. // So, a-a (same heading row and column) or d-d (body cells) are valid while c-d or a-b are not.
  249. //
  250. // header columns
  251. // ↓ ↓
  252. // ┌───┬───┬───┬───┐
  253. // │ a │ a │ b │ b │ ← header row
  254. // ├───┼───┼───┼───┤
  255. // │ c │ c │ d │ d │
  256. // ├───┼───┼───┼───┤
  257. // │ c │ c │ d │ d │
  258. // └───┴───┴───┴───┘
  259. //
  260. function areCellInTheSameTableSection( tableCells ) {
  261. const table = findAncestor( 'table', tableCells[ 0 ] );
  262. const rowIndexes = getRowIndexes( tableCells );
  263. const headingRows = parseInt( table.getAttribute( 'headingRows' ) || 0 );
  264. // Calculating row indexes is a bit cheaper so if this check fails we can't merge.
  265. if ( !areIndexesInSameSection( rowIndexes, headingRows ) ) {
  266. return false;
  267. }
  268. const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  269. const columnIndexes = getColumnIndexes( tableCells );
  270. // Similarly cells must be in same column section.
  271. return areIndexesInSameSection( columnIndexes, headingColumns );
  272. }
  273. // Unified check if table rows/columns indexes are in the same heading/body section.
  274. function areIndexesInSameSection( { first, last }, headingSectionSize ) {
  275. const firstCellIsInHeading = first < headingSectionSize;
  276. const lastCellIsInHeading = last < headingSectionSize;
  277. return firstCellIsInHeading === lastCellIsInHeading;
  278. }