@ckeditor/ckeditor5-table 36.0.1 → 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,572 +2,332 @@
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/tablewalker
8
- */
9
-
10
- // @if CK_DEBUG // const { CKEditorError } = require( 'ckeditor5/src/utils' );
11
-
12
5
  /**
13
6
  * The table iterator class. It allows to iterate over table cells. For each cell the iterator yields
14
7
  * {@link module:table/tablewalker~TableSlot} with proper table cell attributes.
15
8
  */
16
9
  export default class TableWalker {
17
- /**
18
- * Creates an instance of the table walker.
19
- *
20
- * The table walker iterates internally by traversing the table from row index = 0 and column index = 0.
21
- * It walks row by row and column by column in order to output values defined in the constructor.
22
- * By default it will output only the locations that are occupied by a cell. To include also spanned rows and columns,
23
- * pass the `includeAllSlots` option to the constructor.
24
- *
25
- * The most important values of the iterator are column and row indexes of a cell.
26
- *
27
- * See {@link module:table/tablewalker~TableSlot} what values are returned by the table walker.
28
- *
29
- * To iterate over a given row:
30
- *
31
- * const tableWalker = new TableWalker( table, { startRow: 1, endRow: 2 } );
32
- *
33
- * for ( const tableSlot of tableWalker ) {
34
- * console.log( 'A cell at row', tableSlot.row, 'and column', tableSlot.column );
35
- * }
36
- *
37
- * For instance the code above for the following table:
38
- *
39
- * +----+----+----+----+----+----+
40
- * | 00 | 02 | 03 | 04 | 05 |
41
- * | +----+----+----+----+
42
- * | | 12 | 14 | 15 |
43
- * | +----+----+----+ +
44
- * | | 22 | |
45
- * |----+----+----+----+----+ +
46
- * | 30 | 31 | 32 | 33 | 34 | |
47
- * +----+----+----+----+----+----+
48
- *
49
- * will log in the console:
50
- *
51
- * 'A cell at row 1 and column 2'
52
- * 'A cell at row 1 and column 4'
53
- * 'A cell at row 1 and column 5'
54
- * 'A cell at row 2 and column 2'
55
- *
56
- * To also iterate over spanned cells:
57
- *
58
- * const tableWalker = new TableWalker( table, { row: 1, includeAllSlots: true } );
59
- *
60
- * for ( const tableSlot of tableWalker ) {
61
- * console.log( 'Slot at', tableSlot.row, 'x', tableSlot.column, ':', tableSlot.isAnchor ? 'is anchored' : 'is spanned' );
62
- * }
63
- *
64
- * will log in the console for the table from the previous example:
65
- *
66
- * 'Cell at 1 x 0 : is spanned'
67
- * 'Cell at 1 x 1 : is spanned'
68
- * 'Cell at 1 x 2 : is anchored'
69
- * 'Cell at 1 x 3 : is spanned'
70
- * 'Cell at 1 x 4 : is anchored'
71
- * 'Cell at 1 x 5 : is anchored'
72
- *
73
- * **Note**: Option `row` is a shortcut that sets both `startRow` and `endRow` to the same row.
74
- * (Use either `row` or `startRow` and `endRow` but never together). Similarly the `column` option sets both `startColumn`
75
- * and `endColumn` to the same column (Use either `column` or `startColumn` and `endColumn` but never together).
76
- *
77
- * @constructor
78
- * @param {module:engine/model/element~Element} table A table over which the walker iterates.
79
- * @param {Object} [options={}] An object with configuration.
80
- * @param {Number} [options.row] A row index for which this iterator will output cells.
81
- * Can't be used together with `startRow` and `endRow`.
82
- * @param {Number} [options.startRow=0] A row index from which this iterator should start. Can't be used together with `row`.
83
- * @param {Number} [options.endRow] A row index at which this iterator should end. Can't be used together with `row`.
84
- * @param {Number} [options.column] A column index for which this iterator will output cells.
85
- * Can't be used together with `startColumn` and `endColumn`.
86
- * @param {Number} [options.startColumn=0] A column index from which this iterator should start. Can't be used together with `column`.
87
- * @param {Number} [options.endColumn] A column index at which this iterator should end. Can't be used together with `column`.
88
- * @param {Boolean} [options.includeAllSlots=false] Also return values for spanned cells.
89
- */
90
- constructor( table, options = {} ) {
91
- /**
92
- * The walker's table element.
93
- *
94
- * @readonly
95
- * @member {module:engine/model/element~Element}
96
- * @protected
97
- */
98
- this._table = table;
99
-
100
- /**
101
- * A row index from which this iterator will start.
102
- *
103
- * @readonly
104
- * @member {Number}
105
- * @private
106
- */
107
- this._startRow = options.row !== undefined ? options.row : options.startRow || 0;
108
-
109
- /**
110
- * A row index at which this iterator will end.
111
- *
112
- * @readonly
113
- * @member {Number}
114
- * @private
115
- */
116
- this._endRow = options.row !== undefined ? options.row : options.endRow;
117
-
118
- /**
119
- * If set, the table walker will only output cells from a given column and following ones or cells that overlap them.
120
- *
121
- * @readonly
122
- * @member {Number}
123
- * @private
124
- */
125
- this._startColumn = options.column !== undefined ? options.column : options.startColumn || 0;
126
-
127
- /**
128
- * If set, the table walker will only output cells up to a given column.
129
- *
130
- * @readonly
131
- * @member {Number}
132
- * @private
133
- */
134
- this._endColumn = options.column !== undefined ? options.column : options.endColumn;
135
-
136
- /**
137
- * Enables output of spanned cells that are normally not yielded.
138
- *
139
- * @readonly
140
- * @member {Boolean}
141
- * @private
142
- */
143
- this._includeAllSlots = !!options.includeAllSlots;
144
-
145
- /**
146
- * Row indexes to skip from the iteration.
147
- *
148
- * @readonly
149
- * @member {Set<Number>}
150
- * @private
151
- */
152
- this._skipRows = new Set();
153
-
154
- /**
155
- * The current row index.
156
- *
157
- * @member {Number}
158
- * @protected
159
- */
160
- this._row = 0;
161
-
162
- /**
163
- * The index of the current row element in the table.
164
- *
165
- * @type {Number}
166
- * @protected
167
- */
168
- this._rowIndex = 0;
169
-
170
- /**
171
- * The current column index.
172
- *
173
- * @member {Number}
174
- * @protected
175
- */
176
- this._column = 0;
177
-
178
- /**
179
- * The cell index in a parent row. For spanned cells when {@link #_includeAllSlots} is set to `true`,
180
- * this represents the index of the next table cell.
181
- *
182
- * @member {Number}
183
- * @protected
184
- */
185
- this._cellIndex = 0;
186
-
187
- /**
188
- * Holds a map of spanned cells in a table.
189
- *
190
- * @readonly
191
- * @member {Map.<Number, Map.<Number, Object>>}
192
- * @private
193
- */
194
- this._spannedCells = new Map();
195
-
196
- /**
197
- * Index of the next column where a cell is anchored.
198
- *
199
- * @member {Number}
200
- * @private
201
- */
202
- this._nextCellAtColumn = -1;
203
- }
204
-
205
- /**
206
- * Iterable interface.
207
- *
208
- * @returns {Iterable.<module:table/tablewalker~TableSlot>}
209
- */
210
- [ Symbol.iterator ]() {
211
- return this;
212
- }
213
-
214
- /**
215
- * Gets the next table walker's value.
216
- *
217
- * @returns {module:table/tablewalker~TableSlot} The next table walker's value.
218
- */
219
- next() {
220
- const row = this._table.getChild( this._rowIndex );
221
-
222
- // Iterator is done when there's no row (table ended) or the row is after `endRow` limit.
223
- if ( !row || this._isOverEndRow() ) {
224
- return { done: true };
225
- }
226
-
227
- // We step over current element when it is not a tableRow instance.
228
- if ( !row.is( 'element', 'tableRow' ) ) {
229
- this._rowIndex++;
230
-
231
- return this.next();
232
- }
233
-
234
- if ( this._isOverEndColumn() ) {
235
- return this._advanceToNextRow();
236
- }
237
-
238
- let outValue = null;
239
-
240
- const spanData = this._getSpanned();
241
-
242
- if ( spanData ) {
243
- if ( this._includeAllSlots && !this._shouldSkipSlot() ) {
244
- outValue = this._formatOutValue( spanData.cell, spanData.row, spanData.column );
245
- }
246
- } else {
247
- const cell = row.getChild( this._cellIndex );
248
-
249
- if ( !cell ) {
250
- // If there are no more cells left in row advance to the next row.
251
- return this._advanceToNextRow();
252
- }
253
-
254
- const colspan = parseInt( cell.getAttribute( 'colspan' ) || 1 );
255
- const rowspan = parseInt( cell.getAttribute( 'rowspan' ) || 1 );
256
-
257
- // Record this cell spans if it's not 1x1 cell.
258
- if ( colspan > 1 || rowspan > 1 ) {
259
- this._recordSpans( cell, rowspan, colspan );
260
- }
261
-
262
- if ( !this._shouldSkipSlot() ) {
263
- outValue = this._formatOutValue( cell );
264
- }
265
-
266
- this._nextCellAtColumn = this._column + colspan;
267
- }
268
-
269
- // Advance to the next column before returning value.
270
- this._column++;
271
-
272
- if ( this._column == this._nextCellAtColumn ) {
273
- this._cellIndex++;
274
- }
275
-
276
- // The current value will be returned only if current row and column are not skipped.
277
- return outValue || this.next();
278
- }
279
-
280
- /**
281
- * Marks a row to skip in the next iteration. It will also skip cells from the current row if there are any cells from the current row
282
- * to output.
283
- *
284
- * @param {Number} row The row index to skip.
285
- */
286
- skipRow( row ) {
287
- this._skipRows.add( row );
288
- }
289
-
290
- /**
291
- * Advances internal cursor to the next row.
292
- *
293
- * @private
294
- * @returns {module:table/tablewalker~TableSlot}
295
- */
296
- _advanceToNextRow() {
297
- this._row++;
298
- this._rowIndex++;
299
- this._column = 0;
300
- this._cellIndex = 0;
301
- this._nextCellAtColumn = -1;
302
-
303
- return this.next();
304
- }
305
-
306
- /**
307
- * Checks if the current row is over {@link #_endRow}.
308
- *
309
- * @private
310
- * @returns {Boolean}
311
- */
312
- _isOverEndRow() {
313
- // If #_endRow is defined skip all rows after it.
314
- return this._endRow !== undefined && this._row > this._endRow;
315
- }
316
-
317
- /**
318
- * Checks if the current cell is over {@link #_endColumn}
319
- *
320
- * @private
321
- * @returns {Boolean}
322
- */
323
- _isOverEndColumn() {
324
- // If #_endColumn is defined skip all cells after it.
325
- return this._endColumn !== undefined && this._column > this._endColumn;
326
- }
327
-
328
- /**
329
- * A common method for formatting the iterator's output value.
330
- *
331
- * @private
332
- * @param {module:engine/model/element~Element} cell The table cell to output.
333
- * @param {Number} [anchorRow] The row index of a cell anchor slot.
334
- * @param {Number} [anchorColumn] The column index of a cell anchor slot.
335
- * @returns {{done: Boolean, value: {cell: *, row: Number, column: *, rowspan: *, colspan: *, cellIndex: Number}}}
336
- */
337
- _formatOutValue( cell, anchorRow = this._row, anchorColumn = this._column ) {
338
- return {
339
- done: false,
340
- value: new TableSlot( this, cell, anchorRow, anchorColumn )
341
- };
342
- }
343
-
344
- /**
345
- * Checks if the current slot should be skipped.
346
- *
347
- * @private
348
- * @returns {Boolean}
349
- */
350
- _shouldSkipSlot() {
351
- const rowIsMarkedAsSkipped = this._skipRows.has( this._row );
352
- const rowIsBeforeStartRow = this._row < this._startRow;
353
-
354
- const columnIsBeforeStartColumn = this._column < this._startColumn;
355
- const columnIsAfterEndColumn = this._endColumn !== undefined && this._column > this._endColumn;
356
-
357
- return rowIsMarkedAsSkipped || rowIsBeforeStartRow || columnIsBeforeStartColumn || columnIsAfterEndColumn;
358
- }
359
-
360
- /**
361
- * Returns the cell element that is spanned over the current cell location.
362
- *
363
- * @private
364
- * @returns {module:engine/model/element~Element}
365
- */
366
- _getSpanned() {
367
- const rowMap = this._spannedCells.get( this._row );
368
-
369
- // No spans for given row.
370
- if ( !rowMap ) {
371
- return null;
372
- }
373
-
374
- // If spans for given rows has entry for column it means that this location if spanned by other cell.
375
- return rowMap.get( this._column ) || null;
376
- }
377
-
378
- /**
379
- * Updates spanned cells map relative to the current cell location and its span dimensions.
380
- *
381
- * @private
382
- * @param {module:engine/model/element~Element} cell A cell that is spanned.
383
- * @param {Number} rowspan Cell height.
384
- * @param {Number} colspan Cell width.
385
- */
386
- _recordSpans( cell, rowspan, colspan ) {
387
- const data = {
388
- cell,
389
- row: this._row,
390
- column: this._column
391
- };
392
-
393
- for ( let rowToUpdate = this._row; rowToUpdate < this._row + rowspan; rowToUpdate++ ) {
394
- for ( let columnToUpdate = this._column; columnToUpdate < this._column + colspan; columnToUpdate++ ) {
395
- if ( rowToUpdate != this._row || columnToUpdate != this._column ) {
396
- this._markSpannedCell( rowToUpdate, columnToUpdate, data );
397
- }
398
- }
399
- }
400
- }
401
-
402
- /**
403
- * Marks the cell location as spanned by another cell.
404
- *
405
- * @private
406
- * @param {Number} row The row index of the cell location.
407
- * @param {Number} column The column index of the cell location.
408
- * @param {Object} data A spanned cell details (cell element, anchor row and column).
409
- */
410
- _markSpannedCell( row, column, data ) {
411
- if ( !this._spannedCells.has( row ) ) {
412
- this._spannedCells.set( row, new Map() );
413
- }
414
-
415
- const rowSpans = this._spannedCells.get( row );
416
-
417
- rowSpans.set( column, data );
418
- }
10
+ /**
11
+ * Creates an instance of the table walker.
12
+ *
13
+ * The table walker iterates internally by traversing the table from row index = 0 and column index = 0.
14
+ * It walks row by row and column by column in order to output values defined in the constructor.
15
+ * By default it will output only the locations that are occupied by a cell. To include also spanned rows and columns,
16
+ * pass the `includeAllSlots` option to the constructor.
17
+ *
18
+ * The most important values of the iterator are column and row indexes of a cell.
19
+ *
20
+ * See {@link module:table/tablewalker~TableSlot} what values are returned by the table walker.
21
+ *
22
+ * To iterate over a given row:
23
+ *
24
+ * ```ts
25
+ * const tableWalker = new TableWalker( table, { startRow: 1, endRow: 2 } );
26
+ *
27
+ * for ( const tableSlot of tableWalker ) {
28
+ * console.log( 'A cell at row', tableSlot.row, 'and column', tableSlot.column );
29
+ * }
30
+ * ```
31
+ *
32
+ * For instance the code above for the following table:
33
+ *
34
+ * +----+----+----+----+----+----+
35
+ * | 00 | 02 | 03 | 04 | 05 |
36
+ * | +----+----+----+----+
37
+ * | | 12 | 14 | 15 |
38
+ * | +----+----+----+ +
39
+ * | | 22 | |
40
+ * |----+----+----+----+----+ +
41
+ * | 30 | 31 | 32 | 33 | 34 | |
42
+ * +----+----+----+----+----+----+
43
+ *
44
+ * will log in the console:
45
+ *
46
+ * 'A cell at row 1 and column 2'
47
+ * 'A cell at row 1 and column 4'
48
+ * 'A cell at row 1 and column 5'
49
+ * 'A cell at row 2 and column 2'
50
+ *
51
+ * To also iterate over spanned cells:
52
+ *
53
+ * ```ts
54
+ * const tableWalker = new TableWalker( table, { row: 1, includeAllSlots: true } );
55
+ *
56
+ * for ( const tableSlot of tableWalker ) {
57
+ * console.log( 'Slot at', tableSlot.row, 'x', tableSlot.column, ':', tableSlot.isAnchor ? 'is anchored' : 'is spanned' );
58
+ * }
59
+ * ```
60
+ *
61
+ * will log in the console for the table from the previous example:
62
+ *
63
+ * 'Cell at 1 x 0 : is spanned'
64
+ * 'Cell at 1 x 1 : is spanned'
65
+ * 'Cell at 1 x 2 : is anchored'
66
+ * 'Cell at 1 x 3 : is spanned'
67
+ * 'Cell at 1 x 4 : is anchored'
68
+ * 'Cell at 1 x 5 : is anchored'
69
+ *
70
+ * **Note**: Option `row` is a shortcut that sets both `startRow` and `endRow` to the same row.
71
+ * (Use either `row` or `startRow` and `endRow` but never together). Similarly the `column` option sets both `startColumn`
72
+ * and `endColumn` to the same column (Use either `column` or `startColumn` and `endColumn` but never together).
73
+ *
74
+ * @param table A table over which the walker iterates.
75
+ * @param options An object with configuration.
76
+ * @param options.row A row index for which this iterator will output cells. Can't be used together with `startRow` and `endRow`.
77
+ * @param options.startRow A row index from which this iterator should start. Can't be used together with `row`. Default value is 0.
78
+ * @param options.endRow A row index at which this iterator should end. Can't be used together with `row`.
79
+ * @param options.column A column index for which this iterator will output cells.
80
+ * Can't be used together with `startColumn` and `endColumn`.
81
+ * @param options.startColumn A column index from which this iterator should start.
82
+ * Can't be used together with `column`. Default value is 0.
83
+ * @param options.endColumn A column index at which this iterator should end. Can't be used together with `column`.
84
+ * @param options.includeAllSlots Also return values for spanned cells. Default value is "false".
85
+ */
86
+ constructor(table, options = {}) {
87
+ this._table = table;
88
+ this._startRow = options.row !== undefined ? options.row : options.startRow || 0;
89
+ this._endRow = options.row !== undefined ? options.row : options.endRow;
90
+ this._startColumn = options.column !== undefined ? options.column : options.startColumn || 0;
91
+ this._endColumn = options.column !== undefined ? options.column : options.endColumn;
92
+ this._includeAllSlots = !!options.includeAllSlots;
93
+ this._skipRows = new Set();
94
+ this._row = 0;
95
+ this._rowIndex = 0;
96
+ this._column = 0;
97
+ this._cellIndex = 0;
98
+ this._spannedCells = new Map();
99
+ this._nextCellAtColumn = -1;
100
+ }
101
+ /**
102
+ * Iterable interface.
103
+ */
104
+ [Symbol.iterator]() {
105
+ return this;
106
+ }
107
+ /**
108
+ * Gets the next table walker's value.
109
+ *
110
+ * @returns The next table walker's value.
111
+ */
112
+ next() {
113
+ const row = this._table.getChild(this._rowIndex);
114
+ // Iterator is done when there's no row (table ended) or the row is after `endRow` limit.
115
+ if (!row || this._isOverEndRow()) {
116
+ return { done: true, value: undefined };
117
+ }
118
+ // We step over current element when it is not a tableRow instance.
119
+ if (!row.is('element', 'tableRow')) {
120
+ this._rowIndex++;
121
+ return this.next();
122
+ }
123
+ if (this._isOverEndColumn()) {
124
+ return this._advanceToNextRow();
125
+ }
126
+ let outValue = null;
127
+ const spanData = this._getSpanned();
128
+ if (spanData) {
129
+ if (this._includeAllSlots && !this._shouldSkipSlot()) {
130
+ outValue = this._formatOutValue(spanData.cell, spanData.row, spanData.column);
131
+ }
132
+ }
133
+ else {
134
+ const cell = row.getChild(this._cellIndex);
135
+ if (!cell) {
136
+ // If there are no more cells left in row advance to the next row.
137
+ return this._advanceToNextRow();
138
+ }
139
+ const colspan = parseInt(cell.getAttribute('colspan') || '1');
140
+ const rowspan = parseInt(cell.getAttribute('rowspan') || '1');
141
+ // Record this cell spans if it's not 1x1 cell.
142
+ if (colspan > 1 || rowspan > 1) {
143
+ this._recordSpans(cell, rowspan, colspan);
144
+ }
145
+ if (!this._shouldSkipSlot()) {
146
+ outValue = this._formatOutValue(cell);
147
+ }
148
+ this._nextCellAtColumn = this._column + colspan;
149
+ }
150
+ // Advance to the next column before returning value.
151
+ this._column++;
152
+ if (this._column == this._nextCellAtColumn) {
153
+ this._cellIndex++;
154
+ }
155
+ // The current value will be returned only if current row and column are not skipped.
156
+ return outValue || this.next();
157
+ }
158
+ /**
159
+ * Marks a row to skip in the next iteration. It will also skip cells from the current row if there are any cells from the current row
160
+ * to output.
161
+ *
162
+ * @param row The row index to skip.
163
+ */
164
+ skipRow(row) {
165
+ this._skipRows.add(row);
166
+ }
167
+ /**
168
+ * Advances internal cursor to the next row.
169
+ */
170
+ _advanceToNextRow() {
171
+ this._row++;
172
+ this._rowIndex++;
173
+ this._column = 0;
174
+ this._cellIndex = 0;
175
+ this._nextCellAtColumn = -1;
176
+ return this.next();
177
+ }
178
+ /**
179
+ * Checks if the current row is over {@link #_endRow}.
180
+ */
181
+ _isOverEndRow() {
182
+ // If #_endRow is defined skip all rows after it.
183
+ return this._endRow !== undefined && this._row > this._endRow;
184
+ }
185
+ /**
186
+ * Checks if the current cell is over {@link #_endColumn}
187
+ */
188
+ _isOverEndColumn() {
189
+ // If #_endColumn is defined skip all cells after it.
190
+ return this._endColumn !== undefined && this._column > this._endColumn;
191
+ }
192
+ /**
193
+ * A common method for formatting the iterator's output value.
194
+ *
195
+ * @param cell The table cell to output.
196
+ * @param anchorRow The row index of a cell anchor slot.
197
+ * @param anchorColumn The column index of a cell anchor slot.
198
+ */
199
+ _formatOutValue(cell, anchorRow = this._row, anchorColumn = this._column) {
200
+ return {
201
+ done: false,
202
+ value: new TableSlot(this, cell, anchorRow, anchorColumn)
203
+ };
204
+ }
205
+ /**
206
+ * Checks if the current slot should be skipped.
207
+ */
208
+ _shouldSkipSlot() {
209
+ const rowIsMarkedAsSkipped = this._skipRows.has(this._row);
210
+ const rowIsBeforeStartRow = this._row < this._startRow;
211
+ const columnIsBeforeStartColumn = this._column < this._startColumn;
212
+ const columnIsAfterEndColumn = this._endColumn !== undefined && this._column > this._endColumn;
213
+ return rowIsMarkedAsSkipped || rowIsBeforeStartRow || columnIsBeforeStartColumn || columnIsAfterEndColumn;
214
+ }
215
+ /**
216
+ * Returns the cell element that is spanned over the current cell location.
217
+ */
218
+ _getSpanned() {
219
+ const rowMap = this._spannedCells.get(this._row);
220
+ // No spans for given row.
221
+ if (!rowMap) {
222
+ return null;
223
+ }
224
+ // If spans for given rows has entry for column it means that this location if spanned by other cell.
225
+ return rowMap.get(this._column) || null;
226
+ }
227
+ /**
228
+ * Updates spanned cells map relative to the current cell location and its span dimensions.
229
+ *
230
+ * @param cell A cell that is spanned.
231
+ * @param rowspan Cell height.
232
+ * @param colspan Cell width.
233
+ */
234
+ _recordSpans(cell, rowspan, colspan) {
235
+ const data = {
236
+ cell,
237
+ row: this._row,
238
+ column: this._column
239
+ };
240
+ for (let rowToUpdate = this._row; rowToUpdate < this._row + rowspan; rowToUpdate++) {
241
+ for (let columnToUpdate = this._column; columnToUpdate < this._column + colspan; columnToUpdate++) {
242
+ if (rowToUpdate != this._row || columnToUpdate != this._column) {
243
+ this._markSpannedCell(rowToUpdate, columnToUpdate, data);
244
+ }
245
+ }
246
+ }
247
+ }
248
+ /**
249
+ * Marks the cell location as spanned by another cell.
250
+ *
251
+ * @param row The row index of the cell location.
252
+ * @param column The column index of the cell location.
253
+ * @param data A spanned cell details (cell element, anchor row and column).
254
+ */
255
+ _markSpannedCell(row, column, data) {
256
+ if (!this._spannedCells.has(row)) {
257
+ this._spannedCells.set(row, new Map());
258
+ }
259
+ const rowSpans = this._spannedCells.get(row);
260
+ rowSpans.set(column, data);
261
+ }
419
262
  }
420
-
421
263
  /**
422
264
  * An object returned by {@link module:table/tablewalker~TableWalker} when traversing table cells.
423
265
  */
424
266
  class TableSlot {
425
- /**
426
- * Creates an instance of the table walker value.
427
- *
428
- * @protected
429
- * @param {module:table/tablewalker~TableWalker} tableWalker The table walker instance.
430
- * @param {module:engine/model/element~Element} cell The current table cell.
431
- * @param {Number} anchorRow The row index of a cell anchor slot.
432
- * @param {Number} anchorColumn The column index of a cell anchor slot.
433
- */
434
- constructor( tableWalker, cell, anchorRow, anchorColumn ) {
435
- /**
436
- * The current table cell.
437
- *
438
- * @readonly
439
- * @member {module:engine/model/element~Element}
440
- */
441
- this.cell = cell;
442
-
443
- /**
444
- * The row index of a table slot.
445
- *
446
- * @readonly
447
- * @member {Number}
448
- */
449
- this.row = tableWalker._row;
450
-
451
- /**
452
- * The column index of a table slot.
453
- *
454
- * @readonly
455
- * @member {Number}
456
- */
457
- this.column = tableWalker._column;
458
-
459
- /**
460
- * The row index of a cell anchor slot.
461
- *
462
- * @readonly
463
- * @member {Number}
464
- */
465
- this.cellAnchorRow = anchorRow;
466
-
467
- /**
468
- * The column index of a cell anchor slot.
469
- *
470
- * @readonly
471
- * @member {Number}
472
- */
473
- this.cellAnchorColumn = anchorColumn;
474
-
475
- /**
476
- * The index of the current cell in the parent row.
477
- *
478
- * @readonly
479
- * @member {Number}
480
- * @private
481
- */
482
- this._cellIndex = tableWalker._cellIndex;
483
-
484
- /**
485
- * The index of the current row element in the table.
486
- *
487
- * @readonly
488
- * @member {Number}
489
- * @private
490
- */
491
- this._rowIndex = tableWalker._rowIndex;
492
-
493
- /**
494
- * The table element.
495
- *
496
- * @readonly
497
- * @member {module:engine/model/element~Element}
498
- * @private
499
- */
500
- this._table = tableWalker._table;
501
- }
502
-
503
- /**
504
- * Whether the cell is anchored in the current slot.
505
- *
506
- * @readonly
507
- * @returns {Boolean}
508
- */
509
- get isAnchor() {
510
- return this.row === this.cellAnchorRow && this.column === this.cellAnchorColumn;
511
- }
512
-
513
- /**
514
- * The width of a cell defined by a `colspan` attribute. If the model attribute is not present, it is set to `1`.
515
- *
516
- * @readonly
517
- * @returns {Number}
518
- */
519
- get cellWidth() {
520
- return parseInt( this.cell.getAttribute( 'colspan' ) || 1 );
521
- }
522
-
523
- /**
524
- * The height of a cell defined by a `rowspan` attribute. If the model attribute is not present, it is set to `1`.
525
- *
526
- * @readonly
527
- * @returns {Number}
528
- */
529
- get cellHeight() {
530
- return parseInt( this.cell.getAttribute( 'rowspan' ) || 1 );
531
- }
532
-
533
- /**
534
- * The index of the current row element in the table.
535
- *
536
- * @readonly
537
- * @returns {Number}
538
- */
539
- get rowIndex() {
540
- return this._rowIndex;
541
- }
542
-
543
- /**
544
- * Returns the {@link module:engine/model/position~Position} before the table slot.
545
- *
546
- * @returns {module:engine/model/position~Position}
547
- */
548
- getPositionBefore() {
549
- const model = this._table.root.document.model;
550
-
551
- return model.createPositionAt( this._table.getChild( this.row ), this._cellIndex );
552
- }
553
-
554
- // @if CK_DEBUG // get isSpanned() { throwMissingGetterError( 'isSpanned' ); }
555
- // @if CK_DEBUG // get colspan() { throwMissingGetterError( 'colspan' ); }
556
- // @if CK_DEBUG // get rowspan() { throwMissingGetterError( 'rowspan' ); }
557
- // @if CK_DEBUG // get cellIndex() { throwMissingGetterError( 'cellIndex' ); }
267
+ /**
268
+ * Creates an instance of the table walker value.
269
+ *
270
+ * @param tableWalker The table walker instance.
271
+ * @param cell The current table cell.
272
+ * @param anchorRow The row index of a cell anchor slot.
273
+ * @param anchorColumn The column index of a cell anchor slot.
274
+ */
275
+ constructor(tableWalker, cell, anchorRow, anchorColumn) {
276
+ this.cell = cell;
277
+ this.row = tableWalker._row;
278
+ this.column = tableWalker._column;
279
+ this.cellAnchorRow = anchorRow;
280
+ this.cellAnchorColumn = anchorColumn;
281
+ this._cellIndex = tableWalker._cellIndex;
282
+ this._rowIndex = tableWalker._rowIndex;
283
+ this._table = tableWalker._table;
284
+ }
285
+ // @if CK_DEBUG // public get isSpanned(): unknown { return throwMissingGetterError( 'isSpanned' ); }
286
+ // @if CK_DEBUG // public get colspan(): unknown { return throwMissingGetterError( 'colspan' ); }
287
+ // @if CK_DEBUG // public get rowspan(): unknown { return throwMissingGetterError( 'rowspan' ); }
288
+ // @if CK_DEBUG // public get cellIndex(): unknown { return throwMissingGetterError( 'cellIndex' ); }
289
+ /**
290
+ * Whether the cell is anchored in the current slot.
291
+ */
292
+ get isAnchor() {
293
+ return this.row === this.cellAnchorRow && this.column === this.cellAnchorColumn;
294
+ }
295
+ /**
296
+ * The width of a cell defined by a `colspan` attribute. If the model attribute is not present, it is set to `1`.
297
+ */
298
+ get cellWidth() {
299
+ return parseInt(this.cell.getAttribute('colspan') || '1');
300
+ }
301
+ /**
302
+ * The height of a cell defined by a `rowspan` attribute. If the model attribute is not present, it is set to `1`.
303
+ */
304
+ get cellHeight() {
305
+ return parseInt(this.cell.getAttribute('rowspan') || '1');
306
+ }
307
+ /**
308
+ * The index of the current row element in the table.
309
+ */
310
+ get rowIndex() {
311
+ return this._rowIndex;
312
+ }
313
+ /**
314
+ * Returns the {@link module:engine/model/position~Position} before the table slot.
315
+ */
316
+ getPositionBefore() {
317
+ const model = this._table.root.document.model;
318
+ return model.createPositionAt(this._table.getChild(this.row), this._cellIndex);
319
+ }
558
320
  }
559
-
560
321
  /**
561
322
  * This `TableSlot`'s getter (property) was removed in CKEditor 5 v20.0.0.
562
323
  *
563
324
  * Check out the new `TableWalker`'s API in the documentation.
564
325
  *
565
326
  * @error tableslot-getter-removed
566
- * @param {String} getterName
327
+ * @param getterName
567
328
  */
568
-
569
- // @if CK_DEBUG // function throwMissingGetterError( getterName ) {
570
- // @if CK_DEBUG // throw new CKEditorError( 'tableslot-getter-removed', this, {
329
+ // @if CK_DEBUG // function throwMissingGetterError( getterName: string ): void {
330
+ // @if CK_DEBUG // throw new CKEditorError( 'tableslot-getter-removed', null, {
571
331
  // @if CK_DEBUG // getterName
572
332
  // @if CK_DEBUG // } );
573
333
  // @if CK_DEBUG // }