Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/selections/ve.dm.TableSelection.js

raw symbols SCIP
1 /*!
2 * VisualEditor Table Selection class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 * @extends ve.dm.Selection
10 * @constructor
11 * @param {ve.Range} tableRange Table range
12 * @param {number} fromCol Starting column
13 * @param {number} fromRow Starting row
14 * @param {number} [toCol] End column
15 * @param {number} [toRow] End row
16 */
17 ve.dm.TableSelection = function VeDmTableSelection( tableRange, fromCol, fromRow, toCol, toRow ) {
18 if ( ve.dm.Document && arguments[ 0 ] instanceof ve.dm.Document ) {
19 throw new Error( 'Got obsolete ve.dm.Document argument' );
20 }
21 if ( arguments.length > 5 ) {
22 throw new Error( 'Got obsolete argument (probably `expand`)' );
23 }
24
25 // Parent constructor
26 ve.dm.TableSelection.super.call( this );
27
28 this.tableRange = tableRange;
29
30 toCol = toCol === undefined ? fromCol : toCol;
31 toRow = toRow === undefined ? fromRow : toRow;
32
33 this.fromCol = fromCol;
34 this.fromRow = fromRow;
35 this.toCol = toCol;
36 this.toRow = toRow;
37 this.startCol = fromCol < toCol ? fromCol : toCol;
38 this.startRow = fromRow < toRow ? fromRow : toRow;
39 this.endCol = fromCol < toCol ? toCol : fromCol;
40 this.endRow = fromRow < toRow ? toRow : fromRow;
41 this.intendedFromCol = this.fromCol;
42 this.intendedFromRow = this.fromRow;
43 this.intendedToCol = this.toCol;
44 this.intendedToRow = this.toRow;
45 };
46
47 /* Inheritance */
48
49 OO.inheritClass( ve.dm.TableSelection, ve.dm.Selection );
50
51 /* Static Properties */
52
53 ve.dm.TableSelection.static.name = 'table';
54
55 /* Static Methods */
56
57 /**
58 * @inheritdoc
59 */
60 ve.dm.TableSelection.static.newFromHash = function ( hash ) {
61 return new ve.dm.TableSelection(
62 ve.Range.static.newFromHash( hash.tableRange ),
63 hash.fromCol,
64 hash.fromRow,
65 hash.toCol,
66 hash.toRow
67 );
68 };
69
70 /**
71 * Retrieves all cells within a given selection.
72 *
73 * @static
74 * @param {ve.dm.TableMatrix} matrix The table matrix
75 * @param {Object} selectionOffsets Selection col/row offsets (startRow/endRow/startCol/endCol)
76 * @param {boolean} [includePlaceholders] Include placeholders in result
77 * @return {ve.dm.TableMatrixCell[]} List of table cells
78 */
79 ve.dm.TableSelection.static.getTableMatrixCells = function ( matrix, selectionOffsets, includePlaceholders ) {
80 const cells = [],
81 visited = {};
82
83 for ( let row = selectionOffsets.startRow; row <= selectionOffsets.endRow; row++ ) {
84 for ( let col = selectionOffsets.startCol; col <= selectionOffsets.endCol; col++ ) {
85 let cell = matrix.getCell( row, col );
86 if ( !cell ) {
87 continue;
88 }
89 if ( !includePlaceholders && cell.isPlaceholder() ) {
90 cell = cell.owner;
91 }
92 if ( !visited[ cell.key ] ) {
93 cells.push( cell );
94 visited[ cell.key ] = true;
95 }
96 }
97 }
98 return cells;
99 };
100
101 /* Methods */
102
103 /**
104 * Expand the selection to cover all merged cells
105 *
106 * @private
107 * @param {ve.dm.Document} doc The document to which this selection applies
108 * @return {ve.dm.TableSelection} Expanded table selection
109 */
110 ve.dm.TableSelection.prototype.expand = function ( doc ) {
111 const matrix = this.getTableNode( doc ).getMatrix(),
112 colBackwards = this.fromCol > this.toCol,
113 rowBackwards = this.fromRow > this.toRow;
114 let lastCellCount = 0,
115 startCol = Infinity,
116 startRow = Infinity,
117 endCol = -Infinity,
118 endRow = -Infinity,
119 cells = this.getMatrixCells( doc );
120
121 while ( cells.length > lastCellCount ) {
122 for ( let i = 0; i < cells.length; i++ ) {
123 const cell = cells[ i ];
124 startCol = Math.min( startCol, cell.col );
125 startRow = Math.min( startRow, cell.row );
126 endCol = Math.max( endCol, cell.col + cell.node.getColspan() - 1 );
127 endRow = Math.max( endRow, cell.row + cell.node.getRowspan() - 1 );
128 }
129
130 lastCellCount = cells.length;
131 cells = this.constructor.static.getTableMatrixCells( matrix, {
132 startCol: startCol,
133 startRow: startRow,
134 endCol: endCol,
135 endRow: endRow
136 } );
137 }
138 return new this.constructor(
139 this.tableRange,
140 colBackwards ? endCol : startCol,
141 rowBackwards ? endRow : startRow,
142 colBackwards ? startCol : endCol,
143 rowBackwards ? startRow : endRow
144 );
145 };
146
147 /**
148 * @inheritdoc
149 */
150 ve.dm.TableSelection.prototype.toJSON = function () {
151 return {
152 type: this.constructor.static.name,
153 tableRange: this.tableRange,
154 fromCol: this.fromCol,
155 fromRow: this.fromRow,
156 toCol: this.toCol,
157 toRow: this.toRow
158 };
159 };
160
161 /**
162 * @inheritdoc
163 */
164 ve.dm.TableSelection.prototype.getDescription = function () {
165 return (
166 'Table: ' +
167 this.tableRange.from + ' - ' + this.tableRange.to +
168 ', ' +
169 'c' + this.fromCol + ' r' + this.fromRow +
170 ' - ' +
171 'c' + this.toCol + ' r' + this.toRow
172 );
173 };
174
175 /**
176 * @inheritdoc
177 */
178 ve.dm.TableSelection.prototype.collapseToStart = function () {
179 return new this.constructor( this.tableRange, this.startCol, this.startRow, this.startCol, this.startRow );
180 };
181
182 /**
183 * @inheritdoc
184 */
185 ve.dm.TableSelection.prototype.collapseToEnd = function () {
186 return new this.constructor( this.tableRange, this.endCol, this.endRow, this.endCol, this.endRow );
187 };
188
189 /**
190 * @inheritdoc
191 */
192 ve.dm.TableSelection.prototype.collapseToFrom = function () {
193 return new this.constructor( this.tableRange, this.fromCol, this.fromRow, this.fromCol, this.fromRow );
194 };
195
196 /**
197 * @inheritdoc
198 */
199 ve.dm.TableSelection.prototype.collapseToTo = function () {
200 return new this.constructor( this.tableRange, this.toCol, this.toRow, this.toCol, this.toRow );
201 };
202
203 /**
204 * @inheritdoc
205 * @param {ve.dm.Document} doc The document to which this selection applies
206 */
207 ve.dm.TableSelection.prototype.getRanges = function ( doc ) {
208 const ranges = [],
209 cells = this.getMatrixCells( doc );
210 for ( let i = 0, l = cells.length; i < l; i++ ) {
211 ranges.push( cells[ i ].node.getRange() );
212 }
213 return ranges;
214 };
215
216 /**
217 * @inheritdoc
218 */
219 ve.dm.TableSelection.prototype.getCoveringRange = function () {
220 // Note that this returns the table range, and not the minimal range covering
221 // all cells, as that would be far more expensive to compute.
222 return this.tableRange;
223 };
224
225 /**
226 * Get all the ranges required to build a table slice from the selection
227 *
228 * In addition to the outer ranges of the cells, this also includes the start and
229 * end tags of table rows, sections and the table itself.
230 *
231 * @param {ve.dm.Document} doc The document to which this selection applies
232 * @return {ve.Range[]} Ranges
233 */
234 ve.dm.TableSelection.prototype.getTableSliceRanges = function ( doc ) {
235 const ranges = [],
236 matrix = this.getTableNode( doc ).getMatrix();
237
238 // Arrays are non-overlapping so avoid duplication
239 // by indexing by range.start
240 function pushNode( n ) {
241 const range = n.getOuterRange();
242 ranges[ range.start ] = new ve.Range( range.start, range.start + 1 );
243 ranges[ range.end - 1 ] = new ve.Range( range.end - 1, range.end );
244 }
245
246 // Get the start and end tags of every parent of the cell
247 // up to and including the TableNode
248 for ( let i = this.startRow; i <= this.endRow; i++ ) {
249 let node = matrix.getRowNode( i );
250 if ( !node ) {
251 continue;
252 }
253 pushNode( node );
254 while ( ( node = node.getParent() ) && node ) {
255 pushNode( node );
256 if ( node instanceof ve.dm.TableNode ) {
257 break;
258 }
259 }
260 }
261
262 return ranges
263 // Condense sparse array
264 .filter( ( r ) => r )
265 // Add cell ranges
266 .concat( this.getOuterRanges( doc ) )
267 // Sort
268 .sort( ( a, b ) => a.start - b.start );
269 };
270
271 /**
272 * Get outer ranges of the selected cells
273 *
274 * @param {ve.dm.Document} doc The document to which this selection applies
275 * @return {ve.Range[]} Outer ranges
276 */
277 ve.dm.TableSelection.prototype.getOuterRanges = function ( doc ) {
278 const ranges = [],
279 cells = this.getMatrixCells( doc );
280 for ( let i = 0, l = cells.length; i < l; i++ ) {
281 ranges.push( cells[ i ].node.getOuterRange() );
282 }
283 return ranges;
284 };
285
286 /**
287 * Retrieves all cells within a given selection.
288 *
289 * @param {ve.dm.Document} doc The document to which this selection applies
290 * @param {boolean} [includePlaceholders] Include placeholders in result
291 * @return {ve.dm.TableMatrixCell[]} List of table cells
292 */
293 ve.dm.TableSelection.prototype.getMatrixCells = function ( doc, includePlaceholders ) {
294 return this.constructor.static.getTableMatrixCells(
295 this.getTableNode( doc ).getMatrix(),
296 {
297 startCol: this.startCol,
298 startRow: this.startRow,
299 endCol: this.endCol,
300 endRow: this.endRow
301 },
302 includePlaceholders
303 );
304 };
305
306 /**
307 * Check the selected cells are all editable
308 *
309 * @param {ve.dm.Document} doc The document to which this selection applies
310 * @return {boolean} Cells are all editable
311 */
312 ve.dm.TableSelection.prototype.isEditable = function ( doc ) {
313 return this.getMatrixCells( doc ).every( ( cell ) => cell.node.isCellEditable() );
314 };
315
316 /**
317 * @inheritdoc
318 */
319 ve.dm.TableSelection.prototype.isCollapsed = function () {
320 return false;
321 };
322
323 /**
324 * @inheritdoc
325 */
326 ve.dm.TableSelection.prototype.translateByTransaction = function ( tx ) {
327 const newRange = tx.translateRange(
328 this.tableRange,
329 // Table selections should always exclude insertions
330 true
331 );
332
333 if ( newRange.isCollapsed() ) {
334 return new ve.dm.NullSelection();
335 }
336 return new this.constructor( newRange, this.fromCol, this.fromRow, this.toCol, this.toRow );
337 };
338
339 /**
340 * @inheritdoc
341 */
342 ve.dm.TableSelection.prototype.translateByTransactionWithAuthor = function ( tx, authorId ) {
343 const newRange = tx.translateRangeWithAuthor( this.tableRange, authorId );
344
345 if ( newRange.isCollapsed() ) {
346 return new ve.dm.NullSelection();
347 }
348 return new this.constructor( newRange, this.fromCol, this.fromRow, this.toCol, this.toRow );
349 };
350
351 /**
352 * Check if the selection spans a single cell
353 *
354 * @param {ve.dm.Document} doc The document to which this selection applies
355 * @return {boolean} The selection spans a single cell
356 */
357 ve.dm.TableSelection.prototype.isSingleCell = function ( doc ) {
358 // Quick check for single non-merged cell
359 return ( this.fromRow === this.toRow && this.fromCol === this.toCol ) ||
360 // Check for a merged single cell by ignoring placeholders
361 this.getMatrixCells( doc ).length === 1;
362 };
363
364 /**
365 * Check if the selection is mergeable or unmergeable
366 *
367 * The selection must span more than one matrix cell, but only
368 * one table section.
369 *
370 * @param {ve.dm.Document} doc The document to which this selection applies
371 * @return {boolean} The selection is mergeable or unmergeable
372 */
373 ve.dm.TableSelection.prototype.isMergeable = function ( doc ) {
374 if ( !this.isEditable( doc ) ) {
375 return false;
376 }
377
378 if ( this.getMatrixCells( doc, true ).length <= 1 ) {
379 return false;
380 }
381
382 const matrix = this.getTableNode( doc ).getMatrix();
383
384 let lastSectionNode;
385 // Check all sections are the same
386 for ( let r = this.endRow; r >= this.startRow; r-- ) {
387 const rowNode = matrix.getRowNode( r );
388 if ( !rowNode ) {
389 continue;
390 }
391 const sectionNode = rowNode.findParent( ve.dm.TableSectionNode );
392 if ( lastSectionNode && sectionNode !== lastSectionNode ) {
393 // Can't merge across sections
394 return false;
395 }
396 lastSectionNode = sectionNode;
397 }
398 return true;
399 };
400
401 /**
402 * Get the selection's table node
403 *
404 * @param {ve.dm.Document} doc The document to which this selection applies
405 * @return {ve.dm.TableNode} Table node
406 */
407 ve.dm.TableSelection.prototype.getTableNode = function ( doc ) {
408 return doc.getBranchNodeFromOffset( this.tableRange.start + 1 );
409 };
410
411 /**
412 * Get a new selection with adjusted row and column positions
413 *
414 * Placeholder cells are skipped over so this method can be used for cursoring.
415 *
416 * @param {ve.dm.Document} doc The document to which this selection applies
417 * @param {number} fromColOffset Starting column offset
418 * @param {number} fromRowOffset Starting row offset
419 * @param {number} [toColOffset] End column offset
420 * @param {number} [toRowOffset] End row offset
421 * @param {number} [wrap] Wrap to the next/previous row if column limits are exceeded
422 * @return {ve.dm.TableSelection} Adjusted selection
423 */
424 ve.dm.TableSelection.prototype.newFromAdjustment = function ( doc, fromColOffset, fromRowOffset, toColOffset, toRowOffset, wrap ) {
425 if ( toColOffset === undefined ) {
426 toColOffset = fromColOffset;
427 }
428
429 if ( toRowOffset === undefined ) {
430 toRowOffset = fromRowOffset;
431 }
432
433 const matrix = this.getTableNode( doc ).getMatrix();
434 let wrapDir;
435
436 function adjust( mode, cell, offset ) {
437 const dir = offset > 0 ? 1 : -1;
438 let nextCell,
439 col = cell.col,
440 row = cell.row;
441
442 while ( offset !== 0 ) {
443 if ( mode === 'col' ) {
444 col += dir;
445 // Out of bounds
446 if ( col >= matrix.getColCount( row ) ) {
447 if ( wrap && row < matrix.getRowCount() - 1 ) {
448 // Subtract columns in current row
449 col -= matrix.getColCount( row );
450 row++;
451 wrapDir = 1;
452 } else {
453 break;
454 }
455 } else if ( col < 0 ) {
456 if ( wrap && row > 0 ) {
457 row--;
458 // Add columns in previous row
459 col += matrix.getColCount( row );
460 wrapDir = -1;
461 } else {
462 break;
463 }
464 }
465 } else {
466 row += dir;
467 if ( row >= matrix.getRowCount() || row < 0 ) {
468 // Out of bounds
469 break;
470 }
471 }
472 nextCell = matrix.getCell( row, col );
473 // Skip if same as current cell (i.e. merged cells), or null
474 if ( !nextCell || nextCell.equals( cell ) ) {
475 continue;
476 }
477 offset -= dir;
478 cell = nextCell;
479 }
480 return cell;
481 }
482
483 let fromCell = matrix.getCell( this.intendedFromRow, this.intendedFromCol );
484 if ( fromColOffset ) {
485 fromCell = adjust( 'col', fromCell, fromColOffset );
486 }
487 if ( fromRowOffset ) {
488 fromCell = adjust( 'row', fromCell, fromRowOffset );
489 }
490
491 let toCell = matrix.getCell( this.intendedToRow, this.intendedToCol );
492 if ( toColOffset ) {
493 toCell = adjust( 'col', toCell, toColOffset );
494 }
495 if ( toRowOffset ) {
496 toCell = adjust( 'row', toCell, toRowOffset );
497 }
498
499 // Collapse to end/start if wrapping forwards/backwards
500 if ( wrapDir > 0 ) {
501 fromCell = toCell;
502 } else if ( wrapDir < 0 ) {
503 toCell = fromCell;
504 }
505
506 let selection = new this.constructor(
507 this.tableRange,
508 fromCell.col,
509 fromCell.row,
510 toCell.col,
511 toCell.row
512 );
513 selection = selection.expand( doc );
514 return selection;
515 };
516
517 /**
518 * Check if a given cell is within this selection
519 *
520 * @param {ve.dm.TableMatrixCell} cell Table matrix cell
521 * @return {boolean} Cell is within this selection
522 */
523 ve.dm.TableSelection.prototype.containsCell = function ( cell ) {
524 return cell.node.findParent( ve.dm.TableNode ).getOuterRange().equals( this.tableRange ) &&
525 cell.col >= this.startCol && cell.col <= this.endCol &&
526 cell.row >= this.startRow && cell.row <= this.endRow;
527 };
528
529 /**
530 * @inheritdoc
531 */
532 ve.dm.TableSelection.prototype.equals = function ( other ) {
533 return this === other || (
534 !!other &&
535 other.constructor === this.constructor &&
536 this.tableRange.equals( other.tableRange ) &&
537 this.fromCol === other.fromCol &&
538 this.fromRow === other.fromRow &&
539 this.toCol === other.toCol &&
540 this.toRow === other.toRow
541 );
542 };
543
544 /**
545 * Get the number of rows covered by the selection
546 *
547 * @return {number} Number of rows covered
548 */
549 ve.dm.TableSelection.prototype.getRowCount = function () {
550 return this.endRow - this.startRow + 1;
551 };
552
553 /**
554 * Get the number of columns covered by the selection
555 *
556 * @return {number} Number of columns covered
557 */
558 ve.dm.TableSelection.prototype.getColCount = function () {
559 return this.endCol - this.startCol + 1;
560 };
561
562 /**
563 * Check if the table selection covers one or more full rows
564 *
565 * @param {ve.dm.Document} doc The document to which this selection applies
566 * @return {boolean} The table selection covers one or more full rows
567 */
568 ve.dm.TableSelection.prototype.isFullRow = function ( doc ) {
569 const matrix = this.getTableNode( doc ).getMatrix();
570 return this.getColCount() === matrix.getMaxColCount();
571 };
572
573 /**
574 * Check if the table selection covers one or more full columns
575 *
576 * @param {ve.dm.Document} doc The document to which this selection applies
577 * @return {boolean} The table selection covers one or more full columns
578 */
579 ve.dm.TableSelection.prototype.isFullCol = function ( doc ) {
580 const matrix = this.getTableNode( doc ).getMatrix();
581 return this.getRowCount() === matrix.getRowCount();
582 };
583
584 /* Registration */
585
586 ve.dm.selectionFactory.register( ve.dm.TableSelection );