@ckeditor/ckeditor5-table 36.0.0 → 37.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/build/table.js +1 -1
  2. package/package.json +34 -29
  3. package/src/commands/insertcolumncommand.d.ts +61 -0
  4. package/src/commands/insertcolumncommand.js +45 -60
  5. package/src/commands/insertrowcommand.d.ts +60 -0
  6. package/src/commands/insertrowcommand.js +44 -59
  7. package/src/commands/inserttablecommand.d.ts +50 -0
  8. package/src/commands/inserttablecommand.js +51 -68
  9. package/src/commands/mergecellcommand.d.ts +76 -0
  10. package/src/commands/mergecellcommand.js +169 -244
  11. package/src/commands/mergecellscommand.d.ts +33 -0
  12. package/src/commands/mergecellscommand.js +72 -101
  13. package/src/commands/removecolumncommand.d.ts +34 -0
  14. package/src/commands/removecolumncommand.js +88 -102
  15. package/src/commands/removerowcommand.d.ts +34 -0
  16. package/src/commands/removerowcommand.js +63 -80
  17. package/src/commands/selectcolumncommand.d.ts +38 -0
  18. package/src/commands/selectcolumncommand.js +41 -54
  19. package/src/commands/selectrowcommand.d.ts +38 -0
  20. package/src/commands/selectrowcommand.js +38 -48
  21. package/src/commands/setheadercolumncommand.d.ts +55 -0
  22. package/src/commands/setheadercolumncommand.js +48 -73
  23. package/src/commands/setheaderrowcommand.d.ts +58 -0
  24. package/src/commands/setheaderrowcommand.js +56 -85
  25. package/src/commands/splitcellcommand.d.ts +49 -0
  26. package/src/commands/splitcellcommand.js +35 -49
  27. package/src/converters/downcast.d.ts +63 -0
  28. package/src/converters/downcast.js +98 -130
  29. package/src/converters/table-caption-post-fixer.d.ts +20 -0
  30. package/src/converters/table-caption-post-fixer.js +36 -52
  31. package/src/converters/table-cell-paragraph-post-fixer.d.ts +32 -0
  32. package/src/converters/table-cell-paragraph-post-fixer.js +88 -119
  33. package/src/converters/table-cell-refresh-handler.d.ts +18 -0
  34. package/src/converters/table-cell-refresh-handler.js +29 -48
  35. package/src/converters/table-headings-refresh-handler.d.ts +17 -0
  36. package/src/converters/table-headings-refresh-handler.js +35 -54
  37. package/src/converters/table-layout-post-fixer.d.ts +226 -0
  38. package/src/converters/table-layout-post-fixer.js +276 -313
  39. package/src/converters/tableproperties.d.ts +54 -0
  40. package/src/converters/tableproperties.js +136 -168
  41. package/src/converters/upcasttable.d.ts +49 -0
  42. package/src/converters/upcasttable.js +196 -251
  43. package/src/index.d.ts +29 -0
  44. package/src/index.js +0 -2
  45. package/src/plaintableoutput.d.ts +30 -0
  46. package/src/plaintableoutput.js +107 -135
  47. package/src/table.d.ts +38 -0
  48. package/src/table.js +12 -88
  49. package/src/tablecaption/tablecaptionediting.d.ts +68 -0
  50. package/src/tablecaption/tablecaptionediting.js +104 -135
  51. package/src/tablecaption/tablecaptionui.d.ts +26 -0
  52. package/src/tablecaption/tablecaptionui.js +42 -58
  53. package/src/tablecaption/toggletablecaptioncommand.d.ts +73 -0
  54. package/src/tablecaption/toggletablecaptioncommand.js +77 -92
  55. package/src/tablecaption/utils.d.ts +42 -0
  56. package/src/tablecaption/utils.js +35 -61
  57. package/src/tablecaption.d.ts +27 -0
  58. package/src/tablecaption.js +12 -19
  59. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.d.ts +37 -0
  60. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.js +14 -20
  61. package/src/tablecellproperties/commands/tablecellbordercolorcommand.d.ts +42 -0
  62. package/src/tablecellproperties/commands/tablecellbordercolorcommand.js +27 -37
  63. package/src/tablecellproperties/commands/tablecellborderstylecommand.d.ts +42 -0
  64. package/src/tablecellproperties/commands/tablecellborderstylecommand.js +27 -37
  65. package/src/tablecellproperties/commands/tablecellborderwidthcommand.d.ts +56 -0
  66. package/src/tablecellproperties/commands/tablecellborderwidthcommand.js +42 -53
  67. package/src/tablecellproperties/commands/tablecellheightcommand.d.ts +51 -0
  68. package/src/tablecellproperties/commands/tablecellheightcommand.js +29 -36
  69. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.d.ts +37 -0
  70. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.js +14 -20
  71. package/src/tablecellproperties/commands/tablecellpaddingcommand.d.ts +56 -0
  72. package/src/tablecellproperties/commands/tablecellpaddingcommand.js +42 -53
  73. package/src/tablecellproperties/commands/tablecellpropertycommand.d.ts +62 -0
  74. package/src/tablecellproperties/commands/tablecellpropertycommand.js +77 -122
  75. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.d.ts +45 -0
  76. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.js +14 -20
  77. package/src/tablecellproperties/tablecellpropertiesediting.d.ts +47 -0
  78. package/src/tablecellproperties/tablecellpropertiesediting.js +194 -236
  79. package/src/tablecellproperties/tablecellpropertiesui.d.ts +117 -0
  80. package/src/tablecellproperties/tablecellpropertiesui.js +303 -456
  81. package/src/tablecellproperties/ui/tablecellpropertiesview.d.ts +227 -0
  82. package/src/tablecellproperties/ui/tablecellpropertiesview.js +509 -844
  83. package/src/tablecellproperties.d.ts +33 -0
  84. package/src/tablecellproperties.js +12 -98
  85. package/src/tablecellwidth/commands/tablecellwidthcommand.d.ts +51 -0
  86. package/src/tablecellwidth/commands/tablecellwidthcommand.js +29 -35
  87. package/src/tablecellwidth/tablecellwidthediting.d.ts +34 -0
  88. package/src/tablecellwidth/tablecellwidthediting.js +26 -38
  89. package/src/tableclipboard.d.ts +68 -0
  90. package/src/tableclipboard.js +429 -568
  91. package/src/tablecolumnresize/constants.d.ts +20 -0
  92. package/src/tablecolumnresize/constants.js +0 -10
  93. package/src/tablecolumnresize/converters.d.ts +18 -0
  94. package/src/tablecolumnresize/converters.js +35 -119
  95. package/src/tablecolumnresize/tablecolumnresizeediting.d.ts +142 -0
  96. package/src/tablecolumnresize/tablecolumnresizeediting.js +545 -711
  97. package/src/tablecolumnresize/tablewidthscommand.d.ts +38 -0
  98. package/src/tablecolumnresize/tablewidthscommand.js +61 -0
  99. package/src/tablecolumnresize/utils.d.ts +141 -0
  100. package/src/tablecolumnresize/utils.js +256 -233
  101. package/src/tablecolumnresize.d.ts +29 -0
  102. package/src/tablecolumnresize.js +12 -19
  103. package/src/tableconfig.d.ts +341 -0
  104. package/src/tableconfig.js +5 -0
  105. package/src/tableediting.d.ts +102 -0
  106. package/src/tableediting.js +157 -176
  107. package/src/tablekeyboard.d.ts +68 -0
  108. package/src/tablekeyboard.js +261 -344
  109. package/src/tablemouse/mouseeventsobserver.d.ts +62 -0
  110. package/src/tablemouse/mouseeventsobserver.js +12 -49
  111. package/src/tablemouse.d.ts +51 -0
  112. package/src/tablemouse.js +154 -202
  113. package/src/tableproperties/commands/tablealignmentcommand.d.ts +37 -0
  114. package/src/tableproperties/commands/tablealignmentcommand.js +14 -20
  115. package/src/tableproperties/commands/tablebackgroundcolorcommand.d.ts +37 -0
  116. package/src/tableproperties/commands/tablebackgroundcolorcommand.js +14 -20
  117. package/src/tableproperties/commands/tablebordercolorcommand.d.ts +42 -0
  118. package/src/tableproperties/commands/tablebordercolorcommand.js +27 -37
  119. package/src/tableproperties/commands/tableborderstylecommand.d.ts +42 -0
  120. package/src/tableproperties/commands/tableborderstylecommand.js +27 -37
  121. package/src/tableproperties/commands/tableborderwidthcommand.d.ts +56 -0
  122. package/src/tableproperties/commands/tableborderwidthcommand.js +42 -53
  123. package/src/tableproperties/commands/tableheightcommand.d.ts +51 -0
  124. package/src/tableproperties/commands/tableheightcommand.js +29 -33
  125. package/src/tableproperties/commands/tablepropertycommand.d.ts +61 -0
  126. package/src/tableproperties/commands/tablepropertycommand.js +68 -112
  127. package/src/tableproperties/commands/tablewidthcommand.d.ts +51 -0
  128. package/src/tableproperties/commands/tablewidthcommand.js +29 -33
  129. package/src/tableproperties/tablepropertiesediting.d.ts +45 -0
  130. package/src/tableproperties/tablepropertiesediting.js +164 -210
  131. package/src/tableproperties/tablepropertiesui.d.ts +119 -0
  132. package/src/tableproperties/tablepropertiesui.js +294 -439
  133. package/src/tableproperties/ui/tablepropertiesview.d.ts +203 -0
  134. package/src/tableproperties/ui/tablepropertiesview.js +427 -718
  135. package/src/tableproperties.d.ts +33 -0
  136. package/src/tableproperties.js +12 -95
  137. package/src/tableselection.d.ts +111 -0
  138. package/src/tableselection.js +279 -376
  139. package/src/tabletoolbar.d.ts +37 -0
  140. package/src/tabletoolbar.js +39 -92
  141. package/src/tableui.d.ts +58 -0
  142. package/src/tableui.js +281 -338
  143. package/src/tableutils.d.ts +453 -0
  144. package/src/tableutils.js +1015 -1229
  145. package/src/tablewalker.d.ts +323 -0
  146. package/src/tablewalker.js +308 -548
  147. package/src/ui/colorinputview.d.ts +143 -0
  148. package/src/ui/colorinputview.js +229 -366
  149. package/src/ui/formrowview.d.ts +61 -0
  150. package/src/ui/formrowview.js +38 -84
  151. package/src/ui/inserttableview.d.ts +77 -0
  152. package/src/ui/inserttableview.js +152 -242
  153. package/src/utils/common.d.ts +42 -0
  154. package/src/utils/common.js +33 -57
  155. package/src/utils/structure.d.ts +245 -0
  156. package/src/utils/structure.js +261 -379
  157. package/src/utils/table-properties.d.ts +67 -0
  158. package/src/utils/table-properties.js +60 -81
  159. package/src/utils/ui/contextualballoon.d.ts +34 -0
  160. package/src/utils/ui/contextualballoon.js +70 -89
  161. package/src/utils/ui/table-properties.d.ts +193 -0
  162. package/src/utils/ui/table-properties.js +259 -319
  163. package/src/utils/ui/widget.d.ts +16 -0
  164. package/src/utils/ui/widget.js +24 -46
  165. package/src/tablecolumnresize/tablecolumnwidthscommand.js +0 -55
  166. package/src/tablecolumnresize/tablewidthresizecommand.js +0 -65
@@ -2,318 +2,241 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module table/utils/structure
8
- */
9
-
10
- import TableWalker from '../tablewalker';
5
+ import { default as TableWalker } from '../tablewalker';
11
6
  import { createEmptyTableCell, updateNumericAttribute } from './common';
12
-
13
7
  /**
14
8
  * Returns a cropped table according to given dimensions.
15
9
 
16
10
  * To return a cropped table that starts at first row and first column and end in third row and column:
17
11
  *
18
- * const croppedTable = cropTableToDimensions( table, {
19
- * startRow: 1,
20
- * endRow: 3,
21
- * startColumn: 1,
22
- * endColumn: 3
23
- * }, writer );
12
+ * ```ts
13
+ * const croppedTable = cropTableToDimensions( table, {
14
+ * startRow: 1,
15
+ * endRow: 3,
16
+ * startColumn: 1,
17
+ * endColumn: 3
18
+ * }, writer );
19
+ * ```
24
20
  *
25
21
  * Calling the code above for the table below:
26
22
  *
27
- * 0 1 2 3 4 0 1 2
28
- * ┌───┬───┬───┬───┬───┐
29
- * 0 │ a │ b │ c │ d │ e │
30
- * ├───┴───┤ ├───┴───┤ ┌───┬───┬───┐
31
- * 1 │ f │ │ g │ │ │ │ g │ 0
32
- * ├───┬───┴───┼───┬───┤ will return: ├───┴───┼───┤
33
- * 2 │ h │ i │ j │ k │ │ i │ j │ 1
34
- * ├───┤ ├───┤ │ │ ├───┤
35
- * 3 │ l │ │ m │ │ │ │ m │ 2
36
- * ├───┼───┬───┤ ├───┤ └───────┴───┘
37
- * 4 │ n │ o │ p │ │ q │
38
- * └───┴───┴───┴───┴───┘
39
- *
40
- * @param {module:engine/model/element~Element} sourceTable
41
- * @param {Object} cropDimensions
42
- * @param {Number} cropDimensions.startRow
43
- * @param {Number} cropDimensions.startColumn
44
- * @param {Number} cropDimensions.endRow
45
- * @param {Number} cropDimensions.endColumn
46
- * @param {module:engine/model/writer~Writer} writer
47
- * @returns {module:engine/model/element~Element}
23
+ * 0 1 2 3 4 0 1 2
24
+ * ┌───┬───┬───┬───┬───┐
25
+ * 0 │ a │ b │ c │ d │ e │
26
+ * ├───┴───┤ ├───┴───┤ ┌───┬───┬───┐
27
+ * 1 │ f │ │ g │ │ │ │ g │ 0
28
+ * ├───┬───┴───┼───┬───┤ will return: ├───┴───┼───┤
29
+ * 2 │ h │ i │ j │ k │ │ i │ j │ 1
30
+ * ├───┤ ├───┤ │ │ ├───┤
31
+ * 3 │ l │ │ m │ │ │ │ m │ 2
32
+ * ├───┼───┬───┤ ├───┤ └───────┴───┘
33
+ * 4 │ n │ o │ p │ │ q │
34
+ * └───┴───┴───┴───┴───┘
48
35
  */
49
- export function cropTableToDimensions( sourceTable, cropDimensions, writer ) {
50
- const { startRow, startColumn, endRow, endColumn } = cropDimensions;
51
-
52
- // Create empty table with empty rows equal to crop height.
53
- const croppedTable = writer.createElement( 'table' );
54
- const cropHeight = endRow - startRow + 1;
55
-
56
- for ( let i = 0; i < cropHeight; i++ ) {
57
- writer.insertElement( 'tableRow', croppedTable, 'end' );
58
- }
59
-
60
- const tableMap = [ ...new TableWalker( sourceTable, { startRow, endRow, startColumn, endColumn, includeAllSlots: true } ) ];
61
-
62
- // Iterate over source table slots (including empty - spanned - ones).
63
- for ( const { row: sourceRow, column: sourceColumn, cell: tableCell, isAnchor, cellAnchorRow, cellAnchorColumn } of tableMap ) {
64
- // Row index in cropped table.
65
- const rowInCroppedTable = sourceRow - startRow;
66
- const row = croppedTable.getChild( rowInCroppedTable );
67
-
68
- // For empty slots: fill the gap with empty table cell.
69
- if ( !isAnchor ) {
70
- // But fill the gap only if the spanning cell is anchored outside cropped area.
71
- // In the table from method jsdoc those cells are: "c" & "f".
72
- if ( cellAnchorRow < startRow || cellAnchorColumn < startColumn ) {
73
- createEmptyTableCell( writer, writer.createPositionAt( row, 'end' ) );
74
- }
75
- }
76
- // Otherwise clone the cell with all children and trim if it exceeds cropped area.
77
- else {
78
- const tableCellCopy = writer.cloneElement( tableCell );
79
-
80
- writer.append( tableCellCopy, row );
81
-
82
- // Trim table if it exceeds cropped area.
83
- // In the table from method jsdoc those cells are: "g" & "m".
84
- trimTableCellIfNeeded( tableCellCopy, sourceRow, sourceColumn, endRow, endColumn, writer );
85
- }
86
- }
87
-
88
- // Adjust heading rows & columns in cropped table if crop selection includes headings parts.
89
- addHeadingsToCroppedTable( croppedTable, sourceTable, startRow, startColumn, writer );
90
-
91
- return croppedTable;
36
+ export function cropTableToDimensions(sourceTable, cropDimensions, writer) {
37
+ const { startRow, startColumn, endRow, endColumn } = cropDimensions;
38
+ // Create empty table with empty rows equal to crop height.
39
+ const croppedTable = writer.createElement('table');
40
+ const cropHeight = endRow - startRow + 1;
41
+ for (let i = 0; i < cropHeight; i++) {
42
+ writer.insertElement('tableRow', croppedTable, 'end');
43
+ }
44
+ const tableMap = [...new TableWalker(sourceTable, { startRow, endRow, startColumn, endColumn, includeAllSlots: true })];
45
+ // Iterate over source table slots (including empty - spanned - ones).
46
+ for (const { row: sourceRow, column: sourceColumn, cell: tableCell, isAnchor, cellAnchorRow, cellAnchorColumn } of tableMap) {
47
+ // Row index in cropped table.
48
+ const rowInCroppedTable = sourceRow - startRow;
49
+ const row = croppedTable.getChild(rowInCroppedTable);
50
+ // For empty slots: fill the gap with empty table cell.
51
+ if (!isAnchor) {
52
+ // But fill the gap only if the spanning cell is anchored outside cropped area.
53
+ // In the table from method jsdoc those cells are: "c" & "f".
54
+ if (cellAnchorRow < startRow || cellAnchorColumn < startColumn) {
55
+ createEmptyTableCell(writer, writer.createPositionAt(row, 'end'));
56
+ }
57
+ }
58
+ // Otherwise clone the cell with all children and trim if it exceeds cropped area.
59
+ else {
60
+ const tableCellCopy = writer.cloneElement(tableCell);
61
+ writer.append(tableCellCopy, row);
62
+ // Trim table if it exceeds cropped area.
63
+ // In the table from method jsdoc those cells are: "g" & "m".
64
+ trimTableCellIfNeeded(tableCellCopy, sourceRow, sourceColumn, endRow, endColumn, writer);
65
+ }
66
+ }
67
+ // Adjust heading rows & columns in cropped table if crop selection includes headings parts.
68
+ addHeadingsToCroppedTable(croppedTable, sourceTable, startRow, startColumn, writer);
69
+ return croppedTable;
92
70
  }
93
-
94
71
  /**
95
72
  * Returns slot info of cells that starts above and overlaps a given row.
96
73
  *
97
74
  * In a table below, passing `overlapRow = 3`
98
75
  *
99
- * ┌───┬───┬───┬───┬───┐
100
- * 0 │ a │ b │ c │ d │ e │
101
- * │ ├───┼───┼───┼───┤
102
- * 1 │ │ f │ g │ h │ i │
103
- * ├───┤ ├───┼───┤ │
104
- * 2 │ j │ │ k │ l │ │
105
- * │ │ │ ├───┼───┤
106
- * 3 │ │ │ │ m │ n │ <- overlap row to check
107
- * ├───┼───┤ │ ├───│
108
- * 4 │ o │ p │ │ │ q │
109
- * └───┴───┴───┴───┴───┘
76
+ * ┌───┬───┬───┬───┬───┐
77
+ * 0 │ a │ b │ c │ d │ e │
78
+ * │ ├───┼───┼───┼───┤
79
+ * 1 │ │ f │ g │ h │ i │
80
+ * ├───┤ ├───┼───┤ │
81
+ * 2 │ j │ │ k │ l │ │
82
+ * │ │ │ ├───┼───┤
83
+ * 3 │ │ │ │ m │ n │ <- overlap row to check
84
+ * ├───┼───┤ │ ├───│
85
+ * 4 │ o │ p │ │ │ q │
86
+ * └───┴───┴───┴───┴───┘
110
87
  *
111
88
  * will return slot info for cells: "j", "f", "k".
112
89
  *
113
- * @param {module:engine/model/element~Element} table The table to check.
114
- * @param {Number} overlapRow The index of the row to check.
115
- * @param {Number} [startRow=0] A row to start analysis. Use it when it is known that the cells above that row will not overlap.
116
- * @returns {Array.<module:table/tablewalker~TableSlot>}
90
+ * @param table The table to check.
91
+ * @param overlapRow The index of the row to check.
92
+ * @param startRow row to start analysis. Use it when it is known that the cells above that row will not overlap. Default value is 0.
117
93
  */
118
- export function getVerticallyOverlappingCells( table, overlapRow, startRow = 0 ) {
119
- const cells = [];
120
-
121
- const tableWalker = new TableWalker( table, { startRow, endRow: overlapRow - 1 } );
122
-
123
- for ( const slotInfo of tableWalker ) {
124
- const { row, cellHeight } = slotInfo;
125
- const cellEndRow = row + cellHeight - 1;
126
-
127
- if ( row < overlapRow && overlapRow <= cellEndRow ) {
128
- cells.push( slotInfo );
129
- }
130
- }
131
-
132
- return cells;
94
+ export function getVerticallyOverlappingCells(table, overlapRow, startRow = 0) {
95
+ const cells = [];
96
+ const tableWalker = new TableWalker(table, { startRow, endRow: overlapRow - 1 });
97
+ for (const slotInfo of tableWalker) {
98
+ const { row, cellHeight } = slotInfo;
99
+ const cellEndRow = row + cellHeight - 1;
100
+ if (row < overlapRow && overlapRow <= cellEndRow) {
101
+ cells.push(slotInfo);
102
+ }
103
+ }
104
+ return cells;
133
105
  }
134
-
135
106
  /**
136
107
  * Splits the table cell horizontally.
137
108
  *
138
- * @param {module:engine/model/element~Element} tableCell
139
- * @param {Number} splitRow
140
- * @param {module:engine/model/writer~Writer} writer
141
- * @returns {module:engine/model/element~Element} Created table cell.
109
+ * @returns Created table cell, if any were created.
142
110
  */
143
- export function splitHorizontally( tableCell, splitRow, writer ) {
144
- const tableRow = tableCell.parent;
145
- const table = tableRow.parent;
146
- const rowIndex = tableRow.index;
147
-
148
- const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) );
149
- const newRowspan = splitRow - rowIndex;
150
-
151
- const newCellAttributes = {};
152
- const newCellRowSpan = rowspan - newRowspan;
153
-
154
- if ( newCellRowSpan > 1 ) {
155
- newCellAttributes.rowspan = newCellRowSpan;
156
- }
157
-
158
- const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
159
-
160
- if ( colspan > 1 ) {
161
- newCellAttributes.colspan = colspan;
162
- }
163
-
164
- const startRow = rowIndex;
165
- const endRow = startRow + newRowspan;
166
- const tableMap = [ ...new TableWalker( table, { startRow, endRow, includeAllSlots: true } ) ];
167
-
168
- let newCell = null;
169
- let columnIndex;
170
-
171
- for ( const tableSlot of tableMap ) {
172
- const { row, column, cell } = tableSlot;
173
-
174
- if ( cell === tableCell && columnIndex === undefined ) {
175
- columnIndex = column;
176
- }
177
-
178
- if ( columnIndex !== undefined && columnIndex === column && row === endRow ) {
179
- newCell = createEmptyTableCell( writer, tableSlot.getPositionBefore(), newCellAttributes );
180
- }
181
- }
182
-
183
- // Update the rowspan attribute after updating table.
184
- updateNumericAttribute( 'rowspan', newRowspan, tableCell, writer );
185
-
186
- return newCell;
111
+ export function splitHorizontally(tableCell, splitRow, writer) {
112
+ const tableRow = tableCell.parent;
113
+ const table = tableRow.parent;
114
+ const rowIndex = tableRow.index;
115
+ const rowspan = parseInt(tableCell.getAttribute('rowspan'));
116
+ const newRowspan = splitRow - rowIndex;
117
+ const newCellAttributes = {};
118
+ const newCellRowSpan = rowspan - newRowspan;
119
+ if (newCellRowSpan > 1) {
120
+ newCellAttributes.rowspan = newCellRowSpan;
121
+ }
122
+ const colspan = parseInt(tableCell.getAttribute('colspan') || '1');
123
+ if (colspan > 1) {
124
+ newCellAttributes.colspan = colspan;
125
+ }
126
+ const startRow = rowIndex;
127
+ const endRow = startRow + newRowspan;
128
+ const tableMap = [...new TableWalker(table, { startRow, endRow, includeAllSlots: true })];
129
+ let newCell = null;
130
+ let columnIndex;
131
+ for (const tableSlot of tableMap) {
132
+ const { row, column, cell } = tableSlot;
133
+ if (cell === tableCell && columnIndex === undefined) {
134
+ columnIndex = column;
135
+ }
136
+ if (columnIndex !== undefined && columnIndex === column && row === endRow) {
137
+ newCell = createEmptyTableCell(writer, tableSlot.getPositionBefore(), newCellAttributes);
138
+ }
139
+ }
140
+ // Update the rowspan attribute after updating table.
141
+ updateNumericAttribute('rowspan', newRowspan, tableCell, writer);
142
+ return newCell;
187
143
  }
188
-
189
144
  /**
190
145
  * Returns slot info of cells that starts before and overlaps a given column.
191
146
  *
192
147
  * In a table below, passing `overlapColumn = 3`
193
148
  *
194
- * 0 1 2 3 4
195
- * ┌───────┬───────┬───┐
196
- * │ a │ b │ c │
197
- * │───┬───┴───────┼───┤
198
- * │ d │ e │ f │
199
- * ├───┼───┬───────┴───┤
200
- * │ g │ h │ i │
201
- * ├───┼───┼───┬───────┤
202
- * │ j │ k │ l │ m │
203
- * ├───┼───┴───┼───┬───┤
204
- * │ n │ o │ p │ q │
205
- * └───┴───────┴───┴───┘
206
- * ^
207
- * Overlap column to check
149
+ * 0 1 2 3 4
150
+ * ┌───────┬───────┬───┐
151
+ * │ a │ b │ c │
152
+ * │───┬───┴───────┼───┤
153
+ * │ d │ e │ f │
154
+ * ├───┼───┬───────┴───┤
155
+ * │ g │ h │ i │
156
+ * ├───┼───┼───┬───────┤
157
+ * │ j │ k │ l │ m │
158
+ * ├───┼───┴───┼───┬───┤
159
+ * │ n │ o │ p │ q │
160
+ * └───┴───────┴───┴───┘
161
+ * ^
162
+ * Overlap column to check
208
163
  *
209
164
  * will return slot info for cells: "b", "e", "i".
210
165
  *
211
- * @param {module:engine/model/element~Element} table The table to check.
212
- * @param {Number} overlapColumn The index of the column to check.
213
- * @returns {Array.<module:table/tablewalker~TableSlot>}
166
+ * @param table The table to check.
167
+ * @param overlapColumn The index of the column to check.
214
168
  */
215
- export function getHorizontallyOverlappingCells( table, overlapColumn ) {
216
- const cellsToSplit = [];
217
-
218
- const tableWalker = new TableWalker( table );
219
-
220
- for ( const slotInfo of tableWalker ) {
221
- const { column, cellWidth } = slotInfo;
222
- const cellEndColumn = column + cellWidth - 1;
223
-
224
- if ( column < overlapColumn && overlapColumn <= cellEndColumn ) {
225
- cellsToSplit.push( slotInfo );
226
- }
227
- }
228
-
229
- return cellsToSplit;
169
+ export function getHorizontallyOverlappingCells(table, overlapColumn) {
170
+ const cellsToSplit = [];
171
+ const tableWalker = new TableWalker(table);
172
+ for (const slotInfo of tableWalker) {
173
+ const { column, cellWidth } = slotInfo;
174
+ const cellEndColumn = column + cellWidth - 1;
175
+ if (column < overlapColumn && overlapColumn <= cellEndColumn) {
176
+ cellsToSplit.push(slotInfo);
177
+ }
178
+ }
179
+ return cellsToSplit;
230
180
  }
231
-
232
181
  /**
233
182
  * Splits the table cell vertically.
234
183
  *
235
- * @param {module:engine/model/element~Element} tableCell
236
- * @param {Number} columnIndex The table cell column index.
237
- * @param {Number} splitColumn The index of column to split cell on.
238
- * @param {module:engine/model/writer~Writer} writer
239
- * @returns {module:engine/model/element~Element} Created table cell.
184
+ * @param columnIndex The table cell column index.
185
+ * @param splitColumn The index of column to split cell on.
186
+ * @returns Created table cell.
240
187
  */
241
- export function splitVertically( tableCell, columnIndex, splitColumn, writer ) {
242
- const colspan = parseInt( tableCell.getAttribute( 'colspan' ) );
243
- const newColspan = splitColumn - columnIndex;
244
-
245
- const newCellAttributes = {};
246
- const newCellColSpan = colspan - newColspan;
247
-
248
- if ( newCellColSpan > 1 ) {
249
- newCellAttributes.colspan = newCellColSpan;
250
- }
251
-
252
- const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
253
-
254
- if ( rowspan > 1 ) {
255
- newCellAttributes.rowspan = rowspan;
256
- }
257
-
258
- const newCell = createEmptyTableCell( writer, writer.createPositionAfter( tableCell ), newCellAttributes );
259
-
260
- // Update the colspan attribute after updating table.
261
- updateNumericAttribute( 'colspan', newColspan, tableCell, writer );
262
-
263
- return newCell;
188
+ export function splitVertically(tableCell, columnIndex, splitColumn, writer) {
189
+ const colspan = parseInt(tableCell.getAttribute('colspan'));
190
+ const newColspan = splitColumn - columnIndex;
191
+ const newCellAttributes = {};
192
+ const newCellColSpan = colspan - newColspan;
193
+ if (newCellColSpan > 1) {
194
+ newCellAttributes.colspan = newCellColSpan;
195
+ }
196
+ const rowspan = parseInt(tableCell.getAttribute('rowspan') || '1');
197
+ if (rowspan > 1) {
198
+ newCellAttributes.rowspan = rowspan;
199
+ }
200
+ const newCell = createEmptyTableCell(writer, writer.createPositionAfter(tableCell), newCellAttributes);
201
+ // Update the colspan attribute after updating table.
202
+ updateNumericAttribute('colspan', newColspan, tableCell, writer);
203
+ return newCell;
264
204
  }
265
-
266
205
  /**
267
206
  * Adjusts table cell dimensions to not exceed limit row and column.
268
207
  *
269
208
  * If table cell width (or height) covers a column (or row) that is after a limit column (or row)
270
209
  * this method will trim "colspan" (or "rowspan") attribute so the table cell will fit in a defined limits.
271
- *
272
- * @param {module:engine/model/element~Element} tableCell
273
- * @param {Number} cellRow
274
- * @param {Number} cellColumn
275
- * @param {Number} limitRow
276
- * @param {Number} limitColumn
277
- * @param {module:engine/model/writer~Writer} writer
278
210
  */
279
- export function trimTableCellIfNeeded( tableCell, cellRow, cellColumn, limitRow, limitColumn, writer ) {
280
- const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
281
- const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
282
-
283
- const endColumn = cellColumn + colspan - 1;
284
-
285
- if ( endColumn > limitColumn ) {
286
- const trimmedSpan = limitColumn - cellColumn + 1;
287
-
288
- updateNumericAttribute( 'colspan', trimmedSpan, tableCell, writer, 1 );
289
- }
290
-
291
- const endRow = cellRow + rowspan - 1;
292
-
293
- if ( endRow > limitRow ) {
294
- const trimmedSpan = limitRow - cellRow + 1;
295
-
296
- updateNumericAttribute( 'rowspan', trimmedSpan, tableCell, writer, 1 );
297
- }
211
+ export function trimTableCellIfNeeded(tableCell, cellRow, cellColumn, limitRow, limitColumn, writer) {
212
+ const colspan = parseInt(tableCell.getAttribute('colspan') || '1');
213
+ const rowspan = parseInt(tableCell.getAttribute('rowspan') || '1');
214
+ const endColumn = cellColumn + colspan - 1;
215
+ if (endColumn > limitColumn) {
216
+ const trimmedSpan = limitColumn - cellColumn + 1;
217
+ updateNumericAttribute('colspan', trimmedSpan, tableCell, writer, 1);
218
+ }
219
+ const endRow = cellRow + rowspan - 1;
220
+ if (endRow > limitRow) {
221
+ const trimmedSpan = limitRow - cellRow + 1;
222
+ updateNumericAttribute('rowspan', trimmedSpan, tableCell, writer, 1);
223
+ }
298
224
  }
299
-
300
- // Sets proper heading attributes to a cropped table.
301
- function addHeadingsToCroppedTable( croppedTable, sourceTable, startRow, startColumn, writer ) {
302
- const headingRows = parseInt( sourceTable.getAttribute( 'headingRows' ) || 0 );
303
-
304
- if ( headingRows > 0 ) {
305
- const headingRowsInCrop = headingRows - startRow;
306
- updateNumericAttribute( 'headingRows', headingRowsInCrop, croppedTable, writer, 0 );
307
- }
308
-
309
- const headingColumns = parseInt( sourceTable.getAttribute( 'headingColumns' ) || 0 );
310
-
311
- if ( headingColumns > 0 ) {
312
- const headingColumnsInCrop = headingColumns - startColumn;
313
- updateNumericAttribute( 'headingColumns', headingColumnsInCrop, croppedTable, writer, 0 );
314
- }
225
+ /**
226
+ * Sets proper heading attributes to a cropped table.
227
+ */
228
+ function addHeadingsToCroppedTable(croppedTable, sourceTable, startRow, startColumn, writer) {
229
+ const headingRows = parseInt(sourceTable.getAttribute('headingRows') || '0');
230
+ if (headingRows > 0) {
231
+ const headingRowsInCrop = headingRows - startRow;
232
+ updateNumericAttribute('headingRows', headingRowsInCrop, croppedTable, writer, 0);
233
+ }
234
+ const headingColumns = parseInt(sourceTable.getAttribute('headingColumns') || '0');
235
+ if (headingColumns > 0) {
236
+ const headingColumnsInCrop = headingColumns - startColumn;
237
+ updateNumericAttribute('headingColumns', headingColumnsInCrop, croppedTable, writer, 0);
238
+ }
315
239
  }
316
-
317
240
  /**
318
241
  * Removes columns that have no cells anchored.
319
242
  *
@@ -333,36 +256,27 @@ function addHeadingsToCroppedTable( croppedTable, sourceTable, startRow, startCo
333
256
  * **Note:** This is a low-level helper method for clearing invalid model state when doing table modifications.
334
257
  * To remove a column from a table use {@link module:table/tableutils~TableUtils#removeColumns `TableUtils.removeColumns()`}.
335
258
  *
336
- * @protected
337
- * @param {module:engine/model/element~Element} table
338
- * @param {module:table/tableutils~TableUtils} tableUtils
339
- * @returns {Boolean} True if removed some columns.
259
+ * @internal
260
+ * @returns True if removed some columns.
340
261
  */
341
- export function removeEmptyColumns( table, tableUtils ) {
342
- const width = tableUtils.getColumns( table );
343
- const columnsMap = new Array( width ).fill( 0 );
344
-
345
- for ( const { column } of new TableWalker( table ) ) {
346
- columnsMap[ column ]++;
347
- }
348
-
349
- const emptyColumns = columnsMap.reduce( ( result, cellsCount, column ) => {
350
- return cellsCount ? result : [ ...result, column ];
351
- }, [] );
352
-
353
- if ( emptyColumns.length > 0 ) {
354
- // Remove only last empty column because it will recurrently trigger removing empty rows.
355
- const emptyColumn = emptyColumns[ emptyColumns.length - 1 ];
356
-
357
- // @if CK_DEBUG_TABLE // console.log( `Removing empty column: ${ emptyColumn }.` );
358
- tableUtils.removeColumns( table, { at: emptyColumn } );
359
-
360
- return true;
361
- }
362
-
363
- return false;
262
+ export function removeEmptyColumns(table, tableUtils) {
263
+ const width = tableUtils.getColumns(table);
264
+ const columnsMap = new Array(width).fill(0);
265
+ for (const { column } of new TableWalker(table)) {
266
+ columnsMap[column]++;
267
+ }
268
+ const emptyColumns = columnsMap.reduce((result, cellsCount, column) => {
269
+ return cellsCount ? result : [...result, column];
270
+ }, []);
271
+ if (emptyColumns.length > 0) {
272
+ // Remove only last empty column because it will recurrently trigger removing empty rows.
273
+ const emptyColumn = emptyColumns[emptyColumns.length - 1];
274
+ // @if CK_DEBUG_TABLE // console.log( `Removing empty column: ${ emptyColumn }.` );
275
+ tableUtils.removeColumns(table, { at: emptyColumn });
276
+ return true;
277
+ }
278
+ return false;
364
279
  }
365
-
366
280
  /**
367
281
  * Removes rows that have no cells anchored.
368
282
  *
@@ -389,36 +303,27 @@ export function removeEmptyColumns( table, tableUtils ) {
389
303
  * **Note:** This is a low-level helper method for clearing invalid model state when doing table modifications.
390
304
  * To remove a row from a table use {@link module:table/tableutils~TableUtils#removeRows `TableUtils.removeRows()`}.
391
305
  *
392
- * @protected
393
- * @param {module:engine/model/element~Element} table
394
- * @param {module:table/tableutils~TableUtils} tableUtils
395
- * @returns {Boolean} True if removed some rows.
306
+ * @internal
307
+ * @returns True if removed some rows.
396
308
  */
397
- export function removeEmptyRows( table, tableUtils ) {
398
- const emptyRows = [];
399
- const tableRowCount = tableUtils.getRows( table );
400
-
401
- for ( let rowIndex = 0; rowIndex < tableRowCount; rowIndex++ ) {
402
- const tableRow = table.getChild( rowIndex );
403
-
404
- if ( tableRow.isEmpty ) {
405
- emptyRows.push( rowIndex );
406
- }
407
- }
408
-
409
- if ( emptyRows.length > 0 ) {
410
- // Remove only last empty row because it will recurrently trigger removing empty columns.
411
- const emptyRow = emptyRows[ emptyRows.length - 1 ];
412
-
413
- // @if CK_DEBUG_TABLE // console.log( `Removing empty row: ${ emptyRow }.` );
414
- tableUtils.removeRows( table, { at: emptyRow } );
415
-
416
- return true;
417
- }
418
-
419
- return false;
309
+ export function removeEmptyRows(table, tableUtils) {
310
+ const emptyRows = [];
311
+ const tableRowCount = tableUtils.getRows(table);
312
+ for (let rowIndex = 0; rowIndex < tableRowCount; rowIndex++) {
313
+ const tableRow = table.getChild(rowIndex);
314
+ if (tableRow.isEmpty) {
315
+ emptyRows.push(rowIndex);
316
+ }
317
+ }
318
+ if (emptyRows.length > 0) {
319
+ // Remove only last empty row because it will recurrently trigger removing empty columns.
320
+ const emptyRow = emptyRows[emptyRows.length - 1];
321
+ // @if CK_DEBUG_TABLE // console.log( `Removing empty row: ${ emptyRow }.` );
322
+ tableUtils.removeRows(table, { at: emptyRow });
323
+ return true;
324
+ }
325
+ return false;
420
326
  }
421
-
422
327
  /**
423
328
  * Removes rows and columns that have no cells anchored.
424
329
  *
@@ -441,19 +346,15 @@ export function removeEmptyRows( table, tableUtils ) {
441
346
  * To remove a rows from a table use {@link module:table/tableutils~TableUtils#removeRows `TableUtils.removeRows()`} and
442
347
  * {@link module:table/tableutils~TableUtils#removeColumns `TableUtils.removeColumns()`} to remove a column.
443
348
  *
444
- * @protected
445
- * @param {module:engine/model/element~Element} table
446
- * @param {module:table/tableutils~TableUtils} tableUtils
349
+ * @internal
447
350
  */
448
- export function removeEmptyRowsColumns( table, tableUtils ) {
449
- const removedColumns = removeEmptyColumns( table, tableUtils );
450
-
451
- // If there was some columns removed then cleaning empty rows was already triggered.
452
- if ( !removedColumns ) {
453
- removeEmptyRows( table, tableUtils );
454
- }
351
+ export function removeEmptyRowsColumns(table, tableUtils) {
352
+ const removedColumns = removeEmptyColumns(table, tableUtils);
353
+ // If there was some columns removed then cleaning empty rows was already triggered.
354
+ if (!removedColumns) {
355
+ removeEmptyRows(table, tableUtils);
356
+ }
455
357
  }
456
-
457
358
  /**
458
359
  * Returns adjusted last row index if selection covers part of a row with empty slots (spanned by other cells).
459
360
  * The `dimensions.lastRow` is equal to last row index but selection might be bigger.
@@ -470,33 +371,23 @@ export function removeEmptyRowsColumns( table, tableUtils ) {
470
371
  * 3 | | | | |
471
372
  * +---+---+---+---+
472
373
  *
473
- * @param {module:engine/model/element~Element} table
474
- * @param {Object} dimensions
475
- * @param {Number} dimensions.firstRow
476
- * @param {Number} dimensions.firstColumn
477
- * @param {Number} dimensions.lastRow
478
- * @param {Number} dimensions.lastColumn
479
- * @returns {Number} Adjusted last row index.
374
+ * @returns Adjusted last row index.
480
375
  */
481
- export function adjustLastRowIndex( table, dimensions ) {
482
- const lastRowMap = Array.from( new TableWalker( table, {
483
- startColumn: dimensions.firstColumn,
484
- endColumn: dimensions.lastColumn,
485
- row: dimensions.lastRow
486
- } ) );
487
-
488
- const everyCellHasSingleRowspan = lastRowMap.every( ( { cellHeight } ) => cellHeight === 1 );
489
-
490
- // It is a "flat" row, so the last row index is OK.
491
- if ( everyCellHasSingleRowspan ) {
492
- return dimensions.lastRow;
493
- }
494
-
495
- // Otherwise get any cell's rowspan and adjust the last row index.
496
- const rowspanAdjustment = lastRowMap[ 0 ].cellHeight - 1;
497
- return dimensions.lastRow + rowspanAdjustment;
376
+ export function adjustLastRowIndex(table, dimensions) {
377
+ const lastRowMap = Array.from(new TableWalker(table, {
378
+ startColumn: dimensions.firstColumn,
379
+ endColumn: dimensions.lastColumn,
380
+ row: dimensions.lastRow
381
+ }));
382
+ const everyCellHasSingleRowspan = lastRowMap.every(({ cellHeight }) => cellHeight === 1);
383
+ // It is a "flat" row, so the last row index is OK.
384
+ if (everyCellHasSingleRowspan) {
385
+ return dimensions.lastRow;
386
+ }
387
+ // Otherwise get any cell's rowspan and adjust the last row index.
388
+ const rowspanAdjustment = lastRowMap[0].cellHeight - 1;
389
+ return dimensions.lastRow + rowspanAdjustment;
498
390
  }
499
-
500
391
  /**
501
392
  * Returns adjusted last column index if selection covers part of a column with empty slots (spanned by other cells).
502
393
  * The `dimensions.lastColumn` is equal to last column index but selection might be bigger.
@@ -516,29 +407,20 @@ export function adjustLastRowIndex( table, dimensions ) {
516
407
  * ^
517
408
  * last column, each cell has colspan = 2, so we need to return 3, not 2
518
409
  *
519
- * @param {module:engine/model/element~Element} table
520
- * @param {Object} dimensions
521
- * @param {Number} dimensions.firstRow
522
- * @param {Number} dimensions.firstColumn
523
- * @param {Number} dimensions.lastRow
524
- * @param {Number} dimensions.lastColumn
525
- * @returns {Number} Adjusted last column index.
410
+ * @returns Adjusted last column index.
526
411
  */
527
- export function adjustLastColumnIndex( table, dimensions ) {
528
- const lastColumnMap = Array.from( new TableWalker( table, {
529
- startRow: dimensions.firstRow,
530
- endRow: dimensions.lastRow,
531
- column: dimensions.lastColumn
532
- } ) );
533
-
534
- const everyCellHasSingleColspan = lastColumnMap.every( ( { cellWidth } ) => cellWidth === 1 );
535
-
536
- // It is a "flat" column, so the last column index is OK.
537
- if ( everyCellHasSingleColspan ) {
538
- return dimensions.lastColumn;
539
- }
540
-
541
- // Otherwise get any cell's colspan and adjust the last column index.
542
- const colspanAdjustment = lastColumnMap[ 0 ].cellWidth - 1;
543
- return dimensions.lastColumn + colspanAdjustment;
412
+ export function adjustLastColumnIndex(table, dimensions) {
413
+ const lastColumnMap = Array.from(new TableWalker(table, {
414
+ startRow: dimensions.firstRow,
415
+ endRow: dimensions.lastRow,
416
+ column: dimensions.lastColumn
417
+ }));
418
+ const everyCellHasSingleColspan = lastColumnMap.every(({ cellWidth }) => cellWidth === 1);
419
+ // It is a "flat" column, so the last column index is OK.
420
+ if (everyCellHasSingleColspan) {
421
+ return dimensions.lastColumn;
422
+ }
423
+ // Otherwise get any cell's colspan and adjust the last column index.
424
+ const colspanAdjustment = lastColumnMap[0].cellWidth - 1;
425
+ return dimensions.lastColumn + colspanAdjustment;
544
426
  }