| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 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 |
|
| 48 |
|
| 49 |
OO.inheritClass( ve.dm.TableSelection, ve.dm.Selection ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ve.dm.TableSelection.static.name = 'table'; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 205 |
|
| 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 |
|
| 218 |
|
| 219 |
ve.dm.TableSelection.prototype.getCoveringRange = function () { |
| 220 |
|
| 221 |
|
| 222 |
return this.tableRange; |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ve.dm.TableSelection.prototype.getTableSliceRanges = function ( doc ) { |
| 235 |
const ranges = [], |
| 236 |
matrix = this.getTableNode( doc ).getMatrix(); |
| 237 |
|
| 238 |
|
| 239 |
|
| 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 |
|
| 247 |
|
| 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 |
|
| 264 |
.filter( ( r ) => r ) |
| 265 |
|
| 266 |
.concat( this.getOuterRanges( doc ) ) |
| 267 |
|
| 268 |
.sort( ( a, b ) => a.start - b.start ); |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 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 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
ve.dm.TableSelection.prototype.isEditable = function ( doc ) { |
| 313 |
return this.getMatrixCells( doc ).every( ( cell ) => cell.node.isCellEditable() ); |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
ve.dm.TableSelection.prototype.isCollapsed = function () { |
| 320 |
return false; |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
ve.dm.TableSelection.prototype.translateByTransaction = function ( tx ) { |
| 327 |
const newRange = tx.translateRange( |
| 328 |
this.tableRange, |
| 329 |
|
| 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 |
|
| 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 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
ve.dm.TableSelection.prototype.isSingleCell = function ( doc ) { |
| 358 |
|
| 359 |
return ( this.fromRow === this.toRow && this.fromCol === this.toCol ) || |
| 360 |
|
| 361 |
this.getMatrixCells( doc ).length === 1; |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
|
| 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 |
|
| 394 |
return false; |
| 395 |
} |
| 396 |
lastSectionNode = sectionNode; |
| 397 |
} |
| 398 |
return true; |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
ve.dm.TableSelection.prototype.getTableNode = function ( doc ) { |
| 408 |
return doc.getBranchNodeFromOffset( this.tableRange.start + 1 ); |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 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 |
|
| 446 |
if ( col >= matrix.getColCount( row ) ) { |
| 447 |
if ( wrap && row < matrix.getRowCount() - 1 ) { |
| 448 |
|
| 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 |
|
| 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 |
|
| 469 |
break; |
| 470 |
} |
| 471 |
} |
| 472 |
nextCell = matrix.getCell( row, col ); |
| 473 |
|
| 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 |
|
| 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 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 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 |
|
| 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 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
ve.dm.TableSelection.prototype.getRowCount = function () { |
| 550 |
return this.endRow - this.startRow + 1; |
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
ve.dm.TableSelection.prototype.getColCount = function () { |
| 559 |
return this.endCol - this.startCol + 1; |
| 560 |
}; |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 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 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 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 |
|
| 585 |
|
| 586 |
ve.dm.selectionFactory.register( ve.dm.TableSelection ); |