@ckeditor/ckeditor5-table 40.0.0 → 40.2.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.
- package/CHANGELOG.md +15 -15
- package/LICENSE.md +3 -3
- package/build/table.js +1 -1
- package/build/translations/fi.js +1 -1
- package/lang/translations/fi.po +3 -3
- package/package.json +2 -2
- package/src/augmentation.d.ts +76 -76
- package/src/augmentation.js +5 -5
- package/src/commands/insertcolumncommand.d.ts +55 -55
- package/src/commands/insertcolumncommand.js +67 -67
- package/src/commands/insertrowcommand.d.ts +54 -54
- package/src/commands/insertrowcommand.js +66 -66
- package/src/commands/inserttablecommand.d.ts +44 -44
- package/src/commands/inserttablecommand.js +69 -69
- package/src/commands/mergecellcommand.d.ts +68 -68
- package/src/commands/mergecellcommand.js +198 -198
- package/src/commands/mergecellscommand.d.ts +28 -28
- package/src/commands/mergecellscommand.js +94 -94
- package/src/commands/removecolumncommand.d.ts +29 -29
- package/src/commands/removecolumncommand.js +109 -109
- package/src/commands/removerowcommand.d.ts +29 -29
- package/src/commands/removerowcommand.js +82 -82
- package/src/commands/selectcolumncommand.d.ts +33 -33
- package/src/commands/selectcolumncommand.js +60 -60
- package/src/commands/selectrowcommand.d.ts +33 -33
- package/src/commands/selectrowcommand.js +56 -56
- package/src/commands/setheadercolumncommand.d.ts +50 -50
- package/src/commands/setheadercolumncommand.js +71 -71
- package/src/commands/setheaderrowcommand.d.ts +53 -53
- package/src/commands/setheaderrowcommand.js +79 -79
- package/src/commands/splitcellcommand.d.ts +43 -43
- package/src/commands/splitcellcommand.js +54 -54
- package/src/converters/downcast.d.ts +63 -63
- package/src/converters/downcast.js +146 -146
- package/src/converters/table-caption-post-fixer.d.ts +20 -20
- package/src/converters/table-caption-post-fixer.js +53 -53
- package/src/converters/table-cell-paragraph-post-fixer.d.ts +32 -32
- package/src/converters/table-cell-paragraph-post-fixer.js +107 -107
- package/src/converters/table-cell-refresh-handler.d.ts +18 -18
- package/src/converters/table-cell-refresh-handler.js +45 -45
- package/src/converters/table-headings-refresh-handler.d.ts +17 -17
- package/src/converters/table-headings-refresh-handler.js +49 -49
- package/src/converters/table-layout-post-fixer.d.ts +226 -226
- package/src/converters/table-layout-post-fixer.js +367 -367
- package/src/converters/tableproperties.d.ts +54 -54
- package/src/converters/tableproperties.js +159 -159
- package/src/converters/upcasttable.d.ts +49 -49
- package/src/converters/upcasttable.js +243 -243
- package/src/index.d.ts +60 -60
- package/src/index.js +30 -30
- package/src/plaintableoutput.d.ts +26 -26
- package/src/plaintableoutput.js +123 -123
- package/src/table.d.ts +40 -40
- package/src/table.js +44 -44
- package/src/tablecaption/tablecaptionediting.d.ts +63 -63
- package/src/tablecaption/tablecaptionediting.js +122 -122
- package/src/tablecaption/tablecaptionui.d.ts +21 -21
- package/src/tablecaption/tablecaptionui.js +57 -57
- package/src/tablecaption/toggletablecaptioncommand.d.ts +68 -68
- package/src/tablecaption/toggletablecaptioncommand.js +105 -104
- package/src/tablecaption/utils.d.ts +38 -42
- package/src/tablecaption/utils.js +57 -67
- package/src/tablecaption.d.ts +24 -24
- package/src/tablecaption.js +28 -28
- package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.d.ts +32 -32
- package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.js +30 -30
- package/src/tablecellproperties/commands/tablecellbordercolorcommand.d.ts +37 -37
- package/src/tablecellproperties/commands/tablecellbordercolorcommand.js +44 -44
- package/src/tablecellproperties/commands/tablecellborderstylecommand.d.ts +37 -37
- package/src/tablecellproperties/commands/tablecellborderstylecommand.js +44 -44
- package/src/tablecellproperties/commands/tablecellborderwidthcommand.d.ts +51 -51
- package/src/tablecellproperties/commands/tablecellborderwidthcommand.js +64 -64
- package/src/tablecellproperties/commands/tablecellheightcommand.d.ts +46 -46
- package/src/tablecellproperties/commands/tablecellheightcommand.js +51 -51
- package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.d.ts +32 -32
- package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.js +30 -30
- package/src/tablecellproperties/commands/tablecellpaddingcommand.d.ts +51 -51
- package/src/tablecellproperties/commands/tablecellpaddingcommand.js +64 -64
- package/src/tablecellproperties/commands/tablecellpropertycommand.d.ts +62 -62
- package/src/tablecellproperties/commands/tablecellpropertycommand.js +92 -92
- package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.d.ts +40 -40
- package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.js +38 -38
- package/src/tablecellproperties/tablecellpropertiesediting.d.ts +43 -43
- package/src/tablecellproperties/tablecellpropertiesediting.js +241 -241
- package/src/tablecellproperties/tablecellpropertiesui.d.ts +112 -112
- package/src/tablecellproperties/tablecellpropertiesui.js +330 -330
- package/src/tablecellproperties/ui/tablecellpropertiesview.d.ts +228 -228
- package/src/tablecellproperties/ui/tablecellpropertiesview.js +548 -548
- package/src/tablecellproperties.d.ts +30 -30
- package/src/tablecellproperties.js +34 -34
- package/src/tablecellwidth/commands/tablecellwidthcommand.d.ts +46 -46
- package/src/tablecellwidth/commands/tablecellwidthcommand.js +51 -51
- package/src/tablecellwidth/tablecellwidthediting.d.ts +29 -29
- package/src/tablecellwidth/tablecellwidthediting.js +45 -45
- package/src/tableclipboard.d.ts +65 -65
- package/src/tableclipboard.js +450 -450
- package/src/tablecolumnresize/constants.d.ts +20 -20
- package/src/tablecolumnresize/constants.js +20 -20
- package/src/tablecolumnresize/converters.d.ts +18 -18
- package/src/tablecolumnresize/converters.js +46 -46
- package/src/tablecolumnresize/tablecolumnresizeediting.d.ts +139 -139
- package/src/tablecolumnresize/tablecolumnresizeediting.js +583 -583
- package/src/tablecolumnresize/tablewidthscommand.d.ts +38 -38
- package/src/tablecolumnresize/tablewidthscommand.js +61 -61
- package/src/tablecolumnresize/utils.d.ts +148 -148
- package/src/tablecolumnresize/utils.js +358 -358
- package/src/tablecolumnresize.d.ts +26 -26
- package/src/tablecolumnresize.js +30 -30
- package/src/tableconfig.d.ts +343 -343
- package/src/tableconfig.js +5 -5
- package/src/tableediting.d.ts +98 -98
- package/src/tableediting.js +191 -191
- package/src/tablekeyboard.d.ts +68 -68
- package/src/tablekeyboard.js +279 -279
- package/src/tablemouse/mouseeventsobserver.d.ts +62 -62
- package/src/tablemouse/mouseeventsobserver.js +35 -35
- package/src/tablemouse.d.ts +48 -48
- package/src/tablemouse.js +172 -172
- package/src/tableproperties/commands/tablealignmentcommand.d.ts +32 -32
- package/src/tableproperties/commands/tablealignmentcommand.js +30 -30
- package/src/tableproperties/commands/tablebackgroundcolorcommand.d.ts +32 -32
- package/src/tableproperties/commands/tablebackgroundcolorcommand.js +30 -30
- package/src/tableproperties/commands/tablebordercolorcommand.d.ts +37 -37
- package/src/tableproperties/commands/tablebordercolorcommand.js +44 -44
- package/src/tableproperties/commands/tableborderstylecommand.d.ts +37 -37
- package/src/tableproperties/commands/tableborderstylecommand.js +44 -44
- package/src/tableproperties/commands/tableborderwidthcommand.d.ts +51 -51
- package/src/tableproperties/commands/tableborderwidthcommand.js +64 -64
- package/src/tableproperties/commands/tableheightcommand.d.ts +46 -46
- package/src/tableproperties/commands/tableheightcommand.js +54 -54
- package/src/tableproperties/commands/tablepropertycommand.d.ts +61 -61
- package/src/tableproperties/commands/tablepropertycommand.js +81 -80
- package/src/tableproperties/commands/tablewidthcommand.d.ts +46 -46
- package/src/tableproperties/commands/tablewidthcommand.js +54 -54
- package/src/tableproperties/tablepropertiesediting.d.ts +40 -40
- package/src/tableproperties/tablepropertiesediting.js +206 -206
- package/src/tableproperties/tablepropertiesui.d.ts +114 -114
- package/src/tableproperties/tablepropertiesui.js +321 -321
- package/src/tableproperties/ui/tablepropertiesview.d.ts +207 -207
- package/src/tableproperties/ui/tablepropertiesview.js +466 -466
- package/src/tableproperties.d.ts +30 -30
- package/src/tableproperties.js +34 -34
- package/src/tableselection.d.ts +107 -107
- package/src/tableselection.js +297 -297
- package/src/tabletoolbar.d.ts +32 -32
- package/src/tabletoolbar.js +57 -57
- package/src/tableui.d.ts +53 -53
- package/src/tableui.js +309 -309
- package/src/tableutils.d.ts +448 -448
- package/src/tableutils.js +1055 -1055
- package/src/tablewalker.d.ts +362 -362
- package/src/tablewalker.js +393 -393
- package/src/ui/colorinputview.d.ts +140 -140
- package/src/ui/colorinputview.js +271 -271
- package/src/ui/formrowview.d.ts +61 -61
- package/src/ui/formrowview.js +57 -57
- package/src/ui/inserttableview.d.ts +77 -77
- package/src/ui/inserttableview.js +169 -169
- package/src/utils/common.d.ts +46 -42
- package/src/utils/common.js +68 -57
- package/src/utils/structure.d.ts +245 -245
- package/src/utils/structure.js +426 -426
- package/src/utils/table-properties.d.ts +67 -67
- package/src/utils/table-properties.js +86 -86
- package/src/utils/ui/contextualballoon.d.ts +34 -34
- package/src/utils/ui/contextualballoon.js +110 -106
- package/src/utils/ui/table-properties.d.ts +195 -195
- package/src/utils/ui/table-properties.js +362 -362
- package/src/utils/ui/widget.d.ts +20 -16
- package/src/utils/ui/widget.js +48 -38
- package/build/table.js.map +0 -1
package/src/tablewalker.js
CHANGED
|
@@ -1,393 +1,393 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* The table iterator class. It allows to iterate over table cells. For each cell the iterator yields
|
|
7
|
-
* {@link module:table/tablewalker~TableSlot} with proper table cell attributes.
|
|
8
|
-
*/
|
|
9
|
-
export default class TableWalker {
|
|
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
|
-
/**
|
|
88
|
-
* Indicates whether the iterator jumped to (or close to) the start row, ignoring rows that don't need to be traversed.
|
|
89
|
-
*/
|
|
90
|
-
this._jumpedToStartRow = false;
|
|
91
|
-
this._table = table;
|
|
92
|
-
this._startRow = options.row !== undefined ? options.row : options.startRow || 0;
|
|
93
|
-
this._endRow = options.row !== undefined ? options.row : options.endRow;
|
|
94
|
-
this._startColumn = options.column !== undefined ? options.column : options.startColumn || 0;
|
|
95
|
-
this._endColumn = options.column !== undefined ? options.column : options.endColumn;
|
|
96
|
-
this._includeAllSlots = !!options.includeAllSlots;
|
|
97
|
-
this._skipRows = new Set();
|
|
98
|
-
this._row = 0;
|
|
99
|
-
this._rowIndex = 0;
|
|
100
|
-
this._column = 0;
|
|
101
|
-
this._cellIndex = 0;
|
|
102
|
-
this._spannedCells = new Map();
|
|
103
|
-
this._nextCellAtColumn = -1;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Iterable interface.
|
|
107
|
-
*/
|
|
108
|
-
[Symbol.iterator]() {
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Gets the next table walker's value.
|
|
113
|
-
*
|
|
114
|
-
* @returns The next table walker's value.
|
|
115
|
-
*/
|
|
116
|
-
next() {
|
|
117
|
-
if (this._canJumpToStartRow()) {
|
|
118
|
-
this._jumpToNonSpannedRowClosestToStartRow();
|
|
119
|
-
}
|
|
120
|
-
const row = this._table.getChild(this._rowIndex);
|
|
121
|
-
// Iterator is done when there's no row (table ended) or the row is after `endRow` limit.
|
|
122
|
-
if (!row || this._isOverEndRow()) {
|
|
123
|
-
return { done: true, value: undefined };
|
|
124
|
-
}
|
|
125
|
-
// We step over current element when it is not a tableRow instance.
|
|
126
|
-
if (!row.is('element', 'tableRow')) {
|
|
127
|
-
this._rowIndex++;
|
|
128
|
-
return this.next();
|
|
129
|
-
}
|
|
130
|
-
if (this._isOverEndColumn()) {
|
|
131
|
-
return this._advanceToNextRow();
|
|
132
|
-
}
|
|
133
|
-
let outValue = null;
|
|
134
|
-
const spanData = this._getSpanned();
|
|
135
|
-
if (spanData) {
|
|
136
|
-
if (this._includeAllSlots && !this._shouldSkipSlot()) {
|
|
137
|
-
outValue = this._formatOutValue(spanData.cell, spanData.row, spanData.column);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
const cell = row.getChild(this._cellIndex);
|
|
142
|
-
if (!cell) {
|
|
143
|
-
// If there are no more cells left in row advance to the next row.
|
|
144
|
-
return this._advanceToNextRow();
|
|
145
|
-
}
|
|
146
|
-
const colspan = parseInt(cell.getAttribute('colspan') || '1');
|
|
147
|
-
const rowspan = parseInt(cell.getAttribute('rowspan') || '1');
|
|
148
|
-
// Record this cell spans if it's not 1x1 cell.
|
|
149
|
-
if (colspan > 1 || rowspan > 1) {
|
|
150
|
-
this._recordSpans(cell, rowspan, colspan);
|
|
151
|
-
}
|
|
152
|
-
if (!this._shouldSkipSlot()) {
|
|
153
|
-
outValue = this._formatOutValue(cell);
|
|
154
|
-
}
|
|
155
|
-
this._nextCellAtColumn = this._column + colspan;
|
|
156
|
-
}
|
|
157
|
-
// Advance to the next column before returning value.
|
|
158
|
-
this._column++;
|
|
159
|
-
if (this._column == this._nextCellAtColumn) {
|
|
160
|
-
this._cellIndex++;
|
|
161
|
-
}
|
|
162
|
-
// The current value will be returned only if current row and column are not skipped.
|
|
163
|
-
return outValue || this.next();
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* 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
|
|
167
|
-
* to output.
|
|
168
|
-
*
|
|
169
|
-
* @param row The row index to skip.
|
|
170
|
-
*/
|
|
171
|
-
skipRow(row) {
|
|
172
|
-
this._skipRows.add(row);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Advances internal cursor to the next row.
|
|
176
|
-
*/
|
|
177
|
-
_advanceToNextRow() {
|
|
178
|
-
this._row++;
|
|
179
|
-
this._rowIndex++;
|
|
180
|
-
this._column = 0;
|
|
181
|
-
this._cellIndex = 0;
|
|
182
|
-
this._nextCellAtColumn = -1;
|
|
183
|
-
return this.next();
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Checks if the current row is over {@link #_endRow}.
|
|
187
|
-
*/
|
|
188
|
-
_isOverEndRow() {
|
|
189
|
-
// If #_endRow is defined skip all rows after it.
|
|
190
|
-
return this._endRow !== undefined && this._row > this._endRow;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Checks if the current cell is over {@link #_endColumn}
|
|
194
|
-
*/
|
|
195
|
-
_isOverEndColumn() {
|
|
196
|
-
// If #_endColumn is defined skip all cells after it.
|
|
197
|
-
return this._endColumn !== undefined && this._column > this._endColumn;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* A common method for formatting the iterator's output value.
|
|
201
|
-
*
|
|
202
|
-
* @param cell The table cell to output.
|
|
203
|
-
* @param anchorRow The row index of a cell anchor slot.
|
|
204
|
-
* @param anchorColumn The column index of a cell anchor slot.
|
|
205
|
-
*/
|
|
206
|
-
_formatOutValue(cell, anchorRow = this._row, anchorColumn = this._column) {
|
|
207
|
-
return {
|
|
208
|
-
done: false,
|
|
209
|
-
value: new TableSlot(this, cell, anchorRow, anchorColumn)
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Checks if the current slot should be skipped.
|
|
214
|
-
*/
|
|
215
|
-
_shouldSkipSlot() {
|
|
216
|
-
const rowIsMarkedAsSkipped = this._skipRows.has(this._row);
|
|
217
|
-
const rowIsBeforeStartRow = this._row < this._startRow;
|
|
218
|
-
const columnIsBeforeStartColumn = this._column < this._startColumn;
|
|
219
|
-
const columnIsAfterEndColumn = this._endColumn !== undefined && this._column > this._endColumn;
|
|
220
|
-
return rowIsMarkedAsSkipped || rowIsBeforeStartRow || columnIsBeforeStartColumn || columnIsAfterEndColumn;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Returns the cell element that is spanned over the current cell location.
|
|
224
|
-
*/
|
|
225
|
-
_getSpanned() {
|
|
226
|
-
const rowMap = this._spannedCells.get(this._row);
|
|
227
|
-
// No spans for given row.
|
|
228
|
-
if (!rowMap) {
|
|
229
|
-
return null;
|
|
230
|
-
}
|
|
231
|
-
// If spans for given rows has entry for column it means that this location if spanned by other cell.
|
|
232
|
-
return rowMap.get(this._column) || null;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Updates spanned cells map relative to the current cell location and its span dimensions.
|
|
236
|
-
*
|
|
237
|
-
* @param cell A cell that is spanned.
|
|
238
|
-
* @param rowspan Cell height.
|
|
239
|
-
* @param colspan Cell width.
|
|
240
|
-
*/
|
|
241
|
-
_recordSpans(cell, rowspan, colspan) {
|
|
242
|
-
const data = {
|
|
243
|
-
cell,
|
|
244
|
-
row: this._row,
|
|
245
|
-
column: this._column
|
|
246
|
-
};
|
|
247
|
-
for (let rowToUpdate = this._row; rowToUpdate < this._row + rowspan; rowToUpdate++) {
|
|
248
|
-
for (let columnToUpdate = this._column; columnToUpdate < this._column + colspan; columnToUpdate++) {
|
|
249
|
-
if (rowToUpdate != this._row || columnToUpdate != this._column) {
|
|
250
|
-
this._markSpannedCell(rowToUpdate, columnToUpdate, data);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Marks the cell location as spanned by another cell.
|
|
257
|
-
*
|
|
258
|
-
* @param row The row index of the cell location.
|
|
259
|
-
* @param column The column index of the cell location.
|
|
260
|
-
* @param data A spanned cell details (cell element, anchor row and column).
|
|
261
|
-
*/
|
|
262
|
-
_markSpannedCell(row, column, data) {
|
|
263
|
-
if (!this._spannedCells.has(row)) {
|
|
264
|
-
this._spannedCells.set(row, new Map());
|
|
265
|
-
}
|
|
266
|
-
const rowSpans = this._spannedCells.get(row);
|
|
267
|
-
rowSpans.set(column, data);
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Checks if part of the table can be skipped.
|
|
271
|
-
*/
|
|
272
|
-
_canJumpToStartRow() {
|
|
273
|
-
return !!this._startRow &&
|
|
274
|
-
this._startRow > 0 &&
|
|
275
|
-
!this._jumpedToStartRow;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Sets the current row to `this._startRow` or the first row before it that has the number of cells
|
|
279
|
-
* equal to the number of columns in the table.
|
|
280
|
-
*
|
|
281
|
-
* Example:
|
|
282
|
-
* +----+----+----+
|
|
283
|
-
* | 00 | 01 | 02 |
|
|
284
|
-
* |----+----+----+
|
|
285
|
-
* | 10 | 12 |
|
|
286
|
-
* | +----+
|
|
287
|
-
* | | 22 |
|
|
288
|
-
* | +----+
|
|
289
|
-
* | | 32 | <--- Start row
|
|
290
|
-
* +----+----+----+
|
|
291
|
-
* | 40 | 41 | 42 |
|
|
292
|
-
* +----+----+----+
|
|
293
|
-
*
|
|
294
|
-
* If the 4th row is a `this._startRow`, this method will:
|
|
295
|
-
* 1.) Count the number of columns this table has based on the first row (3 columns in this case).
|
|
296
|
-
* 2.) Check if the 4th row contains 3 cells. It doesn't, so go to the row before it.
|
|
297
|
-
* 3.) Check if the 3rd row contains 3 cells. It doesn't, so go to the row before it.
|
|
298
|
-
* 4.) Check if the 2nd row contains 3 cells. It does, so set the current row to that row.
|
|
299
|
-
*
|
|
300
|
-
* Setting the current row this way is necessary to let the `next()` method loop over the cells
|
|
301
|
-
* spanning multiple rows or columns and update the `this._spannedCells` property.
|
|
302
|
-
*/
|
|
303
|
-
_jumpToNonSpannedRowClosestToStartRow() {
|
|
304
|
-
const firstRowLength = this._getRowLength(0);
|
|
305
|
-
for (let i = this._startRow; !this._jumpedToStartRow; i--) {
|
|
306
|
-
if (firstRowLength === this._getRowLength(i)) {
|
|
307
|
-
this._row = i;
|
|
308
|
-
this._rowIndex = i;
|
|
309
|
-
this._jumpedToStartRow = true;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Returns a number of columns in a row taking `colspan` into consideration.
|
|
315
|
-
*/
|
|
316
|
-
_getRowLength(rowIndex) {
|
|
317
|
-
const row = this._table.getChild(rowIndex);
|
|
318
|
-
return [...row.getChildren()].reduce((cols, row) => {
|
|
319
|
-
return cols + parseInt(row.getAttribute('colspan') || '1');
|
|
320
|
-
}, 0);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* An object returned by {@link module:table/tablewalker~TableWalker} when traversing table cells.
|
|
325
|
-
*/
|
|
326
|
-
class TableSlot {
|
|
327
|
-
/**
|
|
328
|
-
* Creates an instance of the table walker value.
|
|
329
|
-
*
|
|
330
|
-
* @param tableWalker The table walker instance.
|
|
331
|
-
* @param cell The current table cell.
|
|
332
|
-
* @param anchorRow The row index of a cell anchor slot.
|
|
333
|
-
* @param anchorColumn The column index of a cell anchor slot.
|
|
334
|
-
*/
|
|
335
|
-
constructor(tableWalker, cell, anchorRow, anchorColumn) {
|
|
336
|
-
this.cell = cell;
|
|
337
|
-
this.row = tableWalker._row;
|
|
338
|
-
this.column = tableWalker._column;
|
|
339
|
-
this.cellAnchorRow = anchorRow;
|
|
340
|
-
this.cellAnchorColumn = anchorColumn;
|
|
341
|
-
this._cellIndex = tableWalker._cellIndex;
|
|
342
|
-
this._rowIndex = tableWalker._rowIndex;
|
|
343
|
-
this._table = tableWalker._table;
|
|
344
|
-
}
|
|
345
|
-
// @if CK_DEBUG // public get isSpanned(): unknown { return throwMissingGetterError( 'isSpanned' ); }
|
|
346
|
-
// @if CK_DEBUG // public get colspan(): unknown { return throwMissingGetterError( 'colspan' ); }
|
|
347
|
-
// @if CK_DEBUG // public get rowspan(): unknown { return throwMissingGetterError( 'rowspan' ); }
|
|
348
|
-
// @if CK_DEBUG // public get cellIndex(): unknown { return throwMissingGetterError( 'cellIndex' ); }
|
|
349
|
-
/**
|
|
350
|
-
* Whether the cell is anchored in the current slot.
|
|
351
|
-
*/
|
|
352
|
-
get isAnchor() {
|
|
353
|
-
return this.row === this.cellAnchorRow && this.column === this.cellAnchorColumn;
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* The width of a cell defined by a `colspan` attribute. If the model attribute is not present, it is set to `1`.
|
|
357
|
-
*/
|
|
358
|
-
get cellWidth() {
|
|
359
|
-
return parseInt(this.cell.getAttribute('colspan') || '1');
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* The height of a cell defined by a `rowspan` attribute. If the model attribute is not present, it is set to `1`.
|
|
363
|
-
*/
|
|
364
|
-
get cellHeight() {
|
|
365
|
-
return parseInt(this.cell.getAttribute('rowspan') || '1');
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* The index of the current row element in the table.
|
|
369
|
-
*/
|
|
370
|
-
get rowIndex() {
|
|
371
|
-
return this._rowIndex;
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Returns the {@link module:engine/model/position~Position} before the table slot.
|
|
375
|
-
*/
|
|
376
|
-
getPositionBefore() {
|
|
377
|
-
const model = this._table.root.document.model;
|
|
378
|
-
return model.createPositionAt(this._table.getChild(this.row), this._cellIndex);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* This `TableSlot`'s getter (property) was removed in CKEditor 5 v20.0.0.
|
|
383
|
-
*
|
|
384
|
-
* Check out the new `TableWalker`'s API in the documentation.
|
|
385
|
-
*
|
|
386
|
-
* @error tableslot-getter-removed
|
|
387
|
-
* @param getterName
|
|
388
|
-
*/
|
|
389
|
-
// @if CK_DEBUG // function throwMissingGetterError( getterName: string ): void {
|
|
390
|
-
// @if CK_DEBUG // throw new CKEditorError( 'tableslot-getter-removed', null, {
|
|
391
|
-
// @if CK_DEBUG // getterName
|
|
392
|
-
// @if CK_DEBUG // } );
|
|
393
|
-
// @if CK_DEBUG // }
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The table iterator class. It allows to iterate over table cells. For each cell the iterator yields
|
|
7
|
+
* {@link module:table/tablewalker~TableSlot} with proper table cell attributes.
|
|
8
|
+
*/
|
|
9
|
+
export default class TableWalker {
|
|
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
|
+
/**
|
|
88
|
+
* Indicates whether the iterator jumped to (or close to) the start row, ignoring rows that don't need to be traversed.
|
|
89
|
+
*/
|
|
90
|
+
this._jumpedToStartRow = false;
|
|
91
|
+
this._table = table;
|
|
92
|
+
this._startRow = options.row !== undefined ? options.row : options.startRow || 0;
|
|
93
|
+
this._endRow = options.row !== undefined ? options.row : options.endRow;
|
|
94
|
+
this._startColumn = options.column !== undefined ? options.column : options.startColumn || 0;
|
|
95
|
+
this._endColumn = options.column !== undefined ? options.column : options.endColumn;
|
|
96
|
+
this._includeAllSlots = !!options.includeAllSlots;
|
|
97
|
+
this._skipRows = new Set();
|
|
98
|
+
this._row = 0;
|
|
99
|
+
this._rowIndex = 0;
|
|
100
|
+
this._column = 0;
|
|
101
|
+
this._cellIndex = 0;
|
|
102
|
+
this._spannedCells = new Map();
|
|
103
|
+
this._nextCellAtColumn = -1;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Iterable interface.
|
|
107
|
+
*/
|
|
108
|
+
[Symbol.iterator]() {
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Gets the next table walker's value.
|
|
113
|
+
*
|
|
114
|
+
* @returns The next table walker's value.
|
|
115
|
+
*/
|
|
116
|
+
next() {
|
|
117
|
+
if (this._canJumpToStartRow()) {
|
|
118
|
+
this._jumpToNonSpannedRowClosestToStartRow();
|
|
119
|
+
}
|
|
120
|
+
const row = this._table.getChild(this._rowIndex);
|
|
121
|
+
// Iterator is done when there's no row (table ended) or the row is after `endRow` limit.
|
|
122
|
+
if (!row || this._isOverEndRow()) {
|
|
123
|
+
return { done: true, value: undefined };
|
|
124
|
+
}
|
|
125
|
+
// We step over current element when it is not a tableRow instance.
|
|
126
|
+
if (!row.is('element', 'tableRow')) {
|
|
127
|
+
this._rowIndex++;
|
|
128
|
+
return this.next();
|
|
129
|
+
}
|
|
130
|
+
if (this._isOverEndColumn()) {
|
|
131
|
+
return this._advanceToNextRow();
|
|
132
|
+
}
|
|
133
|
+
let outValue = null;
|
|
134
|
+
const spanData = this._getSpanned();
|
|
135
|
+
if (spanData) {
|
|
136
|
+
if (this._includeAllSlots && !this._shouldSkipSlot()) {
|
|
137
|
+
outValue = this._formatOutValue(spanData.cell, spanData.row, spanData.column);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const cell = row.getChild(this._cellIndex);
|
|
142
|
+
if (!cell) {
|
|
143
|
+
// If there are no more cells left in row advance to the next row.
|
|
144
|
+
return this._advanceToNextRow();
|
|
145
|
+
}
|
|
146
|
+
const colspan = parseInt(cell.getAttribute('colspan') || '1');
|
|
147
|
+
const rowspan = parseInt(cell.getAttribute('rowspan') || '1');
|
|
148
|
+
// Record this cell spans if it's not 1x1 cell.
|
|
149
|
+
if (colspan > 1 || rowspan > 1) {
|
|
150
|
+
this._recordSpans(cell, rowspan, colspan);
|
|
151
|
+
}
|
|
152
|
+
if (!this._shouldSkipSlot()) {
|
|
153
|
+
outValue = this._formatOutValue(cell);
|
|
154
|
+
}
|
|
155
|
+
this._nextCellAtColumn = this._column + colspan;
|
|
156
|
+
}
|
|
157
|
+
// Advance to the next column before returning value.
|
|
158
|
+
this._column++;
|
|
159
|
+
if (this._column == this._nextCellAtColumn) {
|
|
160
|
+
this._cellIndex++;
|
|
161
|
+
}
|
|
162
|
+
// The current value will be returned only if current row and column are not skipped.
|
|
163
|
+
return outValue || this.next();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 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
|
|
167
|
+
* to output.
|
|
168
|
+
*
|
|
169
|
+
* @param row The row index to skip.
|
|
170
|
+
*/
|
|
171
|
+
skipRow(row) {
|
|
172
|
+
this._skipRows.add(row);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Advances internal cursor to the next row.
|
|
176
|
+
*/
|
|
177
|
+
_advanceToNextRow() {
|
|
178
|
+
this._row++;
|
|
179
|
+
this._rowIndex++;
|
|
180
|
+
this._column = 0;
|
|
181
|
+
this._cellIndex = 0;
|
|
182
|
+
this._nextCellAtColumn = -1;
|
|
183
|
+
return this.next();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Checks if the current row is over {@link #_endRow}.
|
|
187
|
+
*/
|
|
188
|
+
_isOverEndRow() {
|
|
189
|
+
// If #_endRow is defined skip all rows after it.
|
|
190
|
+
return this._endRow !== undefined && this._row > this._endRow;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Checks if the current cell is over {@link #_endColumn}
|
|
194
|
+
*/
|
|
195
|
+
_isOverEndColumn() {
|
|
196
|
+
// If #_endColumn is defined skip all cells after it.
|
|
197
|
+
return this._endColumn !== undefined && this._column > this._endColumn;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* A common method for formatting the iterator's output value.
|
|
201
|
+
*
|
|
202
|
+
* @param cell The table cell to output.
|
|
203
|
+
* @param anchorRow The row index of a cell anchor slot.
|
|
204
|
+
* @param anchorColumn The column index of a cell anchor slot.
|
|
205
|
+
*/
|
|
206
|
+
_formatOutValue(cell, anchorRow = this._row, anchorColumn = this._column) {
|
|
207
|
+
return {
|
|
208
|
+
done: false,
|
|
209
|
+
value: new TableSlot(this, cell, anchorRow, anchorColumn)
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Checks if the current slot should be skipped.
|
|
214
|
+
*/
|
|
215
|
+
_shouldSkipSlot() {
|
|
216
|
+
const rowIsMarkedAsSkipped = this._skipRows.has(this._row);
|
|
217
|
+
const rowIsBeforeStartRow = this._row < this._startRow;
|
|
218
|
+
const columnIsBeforeStartColumn = this._column < this._startColumn;
|
|
219
|
+
const columnIsAfterEndColumn = this._endColumn !== undefined && this._column > this._endColumn;
|
|
220
|
+
return rowIsMarkedAsSkipped || rowIsBeforeStartRow || columnIsBeforeStartColumn || columnIsAfterEndColumn;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns the cell element that is spanned over the current cell location.
|
|
224
|
+
*/
|
|
225
|
+
_getSpanned() {
|
|
226
|
+
const rowMap = this._spannedCells.get(this._row);
|
|
227
|
+
// No spans for given row.
|
|
228
|
+
if (!rowMap) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
// If spans for given rows has entry for column it means that this location if spanned by other cell.
|
|
232
|
+
return rowMap.get(this._column) || null;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Updates spanned cells map relative to the current cell location and its span dimensions.
|
|
236
|
+
*
|
|
237
|
+
* @param cell A cell that is spanned.
|
|
238
|
+
* @param rowspan Cell height.
|
|
239
|
+
* @param colspan Cell width.
|
|
240
|
+
*/
|
|
241
|
+
_recordSpans(cell, rowspan, colspan) {
|
|
242
|
+
const data = {
|
|
243
|
+
cell,
|
|
244
|
+
row: this._row,
|
|
245
|
+
column: this._column
|
|
246
|
+
};
|
|
247
|
+
for (let rowToUpdate = this._row; rowToUpdate < this._row + rowspan; rowToUpdate++) {
|
|
248
|
+
for (let columnToUpdate = this._column; columnToUpdate < this._column + colspan; columnToUpdate++) {
|
|
249
|
+
if (rowToUpdate != this._row || columnToUpdate != this._column) {
|
|
250
|
+
this._markSpannedCell(rowToUpdate, columnToUpdate, data);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Marks the cell location as spanned by another cell.
|
|
257
|
+
*
|
|
258
|
+
* @param row The row index of the cell location.
|
|
259
|
+
* @param column The column index of the cell location.
|
|
260
|
+
* @param data A spanned cell details (cell element, anchor row and column).
|
|
261
|
+
*/
|
|
262
|
+
_markSpannedCell(row, column, data) {
|
|
263
|
+
if (!this._spannedCells.has(row)) {
|
|
264
|
+
this._spannedCells.set(row, new Map());
|
|
265
|
+
}
|
|
266
|
+
const rowSpans = this._spannedCells.get(row);
|
|
267
|
+
rowSpans.set(column, data);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Checks if part of the table can be skipped.
|
|
271
|
+
*/
|
|
272
|
+
_canJumpToStartRow() {
|
|
273
|
+
return !!this._startRow &&
|
|
274
|
+
this._startRow > 0 &&
|
|
275
|
+
!this._jumpedToStartRow;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Sets the current row to `this._startRow` or the first row before it that has the number of cells
|
|
279
|
+
* equal to the number of columns in the table.
|
|
280
|
+
*
|
|
281
|
+
* Example:
|
|
282
|
+
* +----+----+----+
|
|
283
|
+
* | 00 | 01 | 02 |
|
|
284
|
+
* |----+----+----+
|
|
285
|
+
* | 10 | 12 |
|
|
286
|
+
* | +----+
|
|
287
|
+
* | | 22 |
|
|
288
|
+
* | +----+
|
|
289
|
+
* | | 32 | <--- Start row
|
|
290
|
+
* +----+----+----+
|
|
291
|
+
* | 40 | 41 | 42 |
|
|
292
|
+
* +----+----+----+
|
|
293
|
+
*
|
|
294
|
+
* If the 4th row is a `this._startRow`, this method will:
|
|
295
|
+
* 1.) Count the number of columns this table has based on the first row (3 columns in this case).
|
|
296
|
+
* 2.) Check if the 4th row contains 3 cells. It doesn't, so go to the row before it.
|
|
297
|
+
* 3.) Check if the 3rd row contains 3 cells. It doesn't, so go to the row before it.
|
|
298
|
+
* 4.) Check if the 2nd row contains 3 cells. It does, so set the current row to that row.
|
|
299
|
+
*
|
|
300
|
+
* Setting the current row this way is necessary to let the `next()` method loop over the cells
|
|
301
|
+
* spanning multiple rows or columns and update the `this._spannedCells` property.
|
|
302
|
+
*/
|
|
303
|
+
_jumpToNonSpannedRowClosestToStartRow() {
|
|
304
|
+
const firstRowLength = this._getRowLength(0);
|
|
305
|
+
for (let i = this._startRow; !this._jumpedToStartRow; i--) {
|
|
306
|
+
if (firstRowLength === this._getRowLength(i)) {
|
|
307
|
+
this._row = i;
|
|
308
|
+
this._rowIndex = i;
|
|
309
|
+
this._jumpedToStartRow = true;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Returns a number of columns in a row taking `colspan` into consideration.
|
|
315
|
+
*/
|
|
316
|
+
_getRowLength(rowIndex) {
|
|
317
|
+
const row = this._table.getChild(rowIndex);
|
|
318
|
+
return [...row.getChildren()].reduce((cols, row) => {
|
|
319
|
+
return cols + parseInt(row.getAttribute('colspan') || '1');
|
|
320
|
+
}, 0);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* An object returned by {@link module:table/tablewalker~TableWalker} when traversing table cells.
|
|
325
|
+
*/
|
|
326
|
+
class TableSlot {
|
|
327
|
+
/**
|
|
328
|
+
* Creates an instance of the table walker value.
|
|
329
|
+
*
|
|
330
|
+
* @param tableWalker The table walker instance.
|
|
331
|
+
* @param cell The current table cell.
|
|
332
|
+
* @param anchorRow The row index of a cell anchor slot.
|
|
333
|
+
* @param anchorColumn The column index of a cell anchor slot.
|
|
334
|
+
*/
|
|
335
|
+
constructor(tableWalker, cell, anchorRow, anchorColumn) {
|
|
336
|
+
this.cell = cell;
|
|
337
|
+
this.row = tableWalker._row;
|
|
338
|
+
this.column = tableWalker._column;
|
|
339
|
+
this.cellAnchorRow = anchorRow;
|
|
340
|
+
this.cellAnchorColumn = anchorColumn;
|
|
341
|
+
this._cellIndex = tableWalker._cellIndex;
|
|
342
|
+
this._rowIndex = tableWalker._rowIndex;
|
|
343
|
+
this._table = tableWalker._table;
|
|
344
|
+
}
|
|
345
|
+
// @if CK_DEBUG // public get isSpanned(): unknown { return throwMissingGetterError( 'isSpanned' ); }
|
|
346
|
+
// @if CK_DEBUG // public get colspan(): unknown { return throwMissingGetterError( 'colspan' ); }
|
|
347
|
+
// @if CK_DEBUG // public get rowspan(): unknown { return throwMissingGetterError( 'rowspan' ); }
|
|
348
|
+
// @if CK_DEBUG // public get cellIndex(): unknown { return throwMissingGetterError( 'cellIndex' ); }
|
|
349
|
+
/**
|
|
350
|
+
* Whether the cell is anchored in the current slot.
|
|
351
|
+
*/
|
|
352
|
+
get isAnchor() {
|
|
353
|
+
return this.row === this.cellAnchorRow && this.column === this.cellAnchorColumn;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* The width of a cell defined by a `colspan` attribute. If the model attribute is not present, it is set to `1`.
|
|
357
|
+
*/
|
|
358
|
+
get cellWidth() {
|
|
359
|
+
return parseInt(this.cell.getAttribute('colspan') || '1');
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* The height of a cell defined by a `rowspan` attribute. If the model attribute is not present, it is set to `1`.
|
|
363
|
+
*/
|
|
364
|
+
get cellHeight() {
|
|
365
|
+
return parseInt(this.cell.getAttribute('rowspan') || '1');
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* The index of the current row element in the table.
|
|
369
|
+
*/
|
|
370
|
+
get rowIndex() {
|
|
371
|
+
return this._rowIndex;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Returns the {@link module:engine/model/position~Position} before the table slot.
|
|
375
|
+
*/
|
|
376
|
+
getPositionBefore() {
|
|
377
|
+
const model = this._table.root.document.model;
|
|
378
|
+
return model.createPositionAt(this._table.getChild(this.row), this._cellIndex);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* This `TableSlot`'s getter (property) was removed in CKEditor 5 v20.0.0.
|
|
383
|
+
*
|
|
384
|
+
* Check out the new `TableWalker`'s API in the documentation.
|
|
385
|
+
*
|
|
386
|
+
* @error tableslot-getter-removed
|
|
387
|
+
* @param getterName
|
|
388
|
+
*/
|
|
389
|
+
// @if CK_DEBUG // function throwMissingGetterError( getterName: string ): void {
|
|
390
|
+
// @if CK_DEBUG // throw new CKEditorError( 'tableslot-getter-removed', null, {
|
|
391
|
+
// @if CK_DEBUG // getterName
|
|
392
|
+
// @if CK_DEBUG // } );
|
|
393
|
+
// @if CK_DEBUG // }
|