|
9 | 9 |
|
10 | 10 | import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
|
11 | 11 | import { findAncestor } from './commands/utils';
|
| 12 | +import TableWalker from './tablewalker'; |
12 | 13 |
|
13 | 14 | /**
|
14 | 15 | * Converts a given {@link module:engine/view/element~Element} to a table widget:
|
@@ -146,16 +147,46 @@ export function getSelectionAffectedTableCells( selection ) {
|
146 | 147 | *
|
147 | 148 | * console.log( `Selected rows ${ first } to ${ last }` );
|
148 | 149 | *
|
149 |
| - * @package {Array.<module:engine/model/element~Element>} |
| 150 | + * @param {Array.<module:engine/model/element~Element>} |
150 | 151 | * @returns {Object} Returns an object with `first` and `last` table row indexes.
|
151 | 152 | */
|
152 | 153 | export function getRowIndexes( tableCells ) {
|
153 |
| - const allIndexesSorted = tableCells.map( cell => cell.parent.index ).sort(); |
| 154 | + const indexes = tableCells.map( cell => cell.parent.index ); |
154 | 155 |
|
155 |
| - return { |
156 |
| - first: allIndexesSorted[ 0 ], |
157 |
| - last: allIndexesSorted[ allIndexesSorted.length - 1 ] |
158 |
| - }; |
| 156 | + return getFirstLastIndexesObject( indexes ); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Returns a helper object with `first` and `last` column index contained in given `tableCells`. |
| 161 | + * |
| 162 | + * const selectedTableCells = getSelectedTableCells( editor.model.document.selection ); |
| 163 | + * |
| 164 | + * const { first, last } = getColumnIndexes( selectedTableCells ); |
| 165 | + * |
| 166 | + * console.log( `Selected columns ${ first } to ${ last }` ); |
| 167 | + * |
| 168 | + * @param {Array.<module:engine/model/element~Element>} |
| 169 | + * @returns {Object} Returns an object with `first` and `last` table column indexes. |
| 170 | + */ |
| 171 | +export function getColumnIndexes( selectedCells ) { |
| 172 | + const table = findAncestor( 'table', selectedCells[ 0 ] ); |
| 173 | + const tableMap = [ ...new TableWalker( table ) ]; |
| 174 | + |
| 175 | + const indexes = tableMap |
| 176 | + .filter( entry => selectedCells.includes( entry.cell ) ) |
| 177 | + .map( entry => entry.column ); |
| 178 | + |
| 179 | + return getFirstLastIndexesObject( indexes ); |
| 180 | +} |
| 181 | + |
| 182 | +// Helper method to get an object with `first` and `last` indexes from an unsorted array of indexes. |
| 183 | +function getFirstLastIndexesObject( indexes ) { |
| 184 | + const allIndexesSorted = indexes.sort( ( indexA, indexB ) => indexA - indexB ); |
| 185 | + |
| 186 | + const first = allIndexesSorted[ 0 ]; |
| 187 | + const last = allIndexesSorted[ allIndexesSorted.length - 1 ]; |
| 188 | + |
| 189 | + return { first, last }; |
159 | 190 | }
|
160 | 191 |
|
161 | 192 | function sortRanges( rangesIterator ) {
|
|
0 commit comments