Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 | 1x 48x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 1x 3x 1x 3x 3x 3x 1x 3x 3x 1x 3x 4x 3x 3x 3x 3x 3x 3x 1x 11x 11x 11x 1x 10x 5x 5x 10x 10x 2x 1x 1x 2x 10x 10x 10x 1x 6x 6x 6x 1x 5x 5x 5x 3x 2x 1x 2x 1x 1x 5x 1x 6x 6x 6x 1x 5x 5x 5x 5x 3x 3x 1x 3x 2x 2x 1x 2x 5x 3x 2x 2x 5x 5x 5x 5x 5x 1x 5x 5x 2x 3x 3x 3x 1x 2x 1x 1x 1x 1x 1x 1x 2x 2x 3x 3x 1x 7x 7x 7x 7x 7x 7x 7x 2x 7x 3x 7x 9x 13x 13x 2x 2x 13x 7x 9x 13x 13x 13x 13x 13x 2x 11x 11x 11x 11x 11x 11x 22x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 3x 1x 1x 1x 8x 8x 8x 1x 7x 7x 7x 7x 2x 2x 2x 5x 5x 5x 5x 6x 6x 5x 4x 5x 5x 42x 5x 1x 5x 48x 5x 19x 19x 82x 12x 12x 19x 7x 5x 17x 17x 45x 13x 13x 17x 4x 5x 7x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 6x 12x 12x 10x 2x 12x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 10x 10x 10x 10x 20x 59x 59x 4x 1x 4x 55x 55x 8x 4x 4x 2x 2x 4x 4x 51x 51x 10x 20x 10x 7x 23x 3x 3x 7x 7x 3x 10x 2x 10x 10x 10x 10x 10x 23x 23x 23x 23x 21x 21x 17x 4x 21x 2x 2x 2x 21x 17x 4x 4x 4x 4x 21x 20x 33x 33x 33x 20x 10x 1x 2x 1x 1x 2x 2x 1x 15x 15x 9x 6x 15x 15x 1x 18x 18x 18x 18x 18x 18x 18x 18x 11x 11x 7x 7x 18x 85x 85x 5x 80x 64x 64x 15x 15x 64x 16x 16x 1x 1x 1x 1x 1x 1x 16x 6x 18x 18x 11x 1x 11x 11x 11x 2x 9x 9x 9x 9x 9x 9x 48x 10x 10x 1x 48x 7x 6x 6x 6x 7x 7x 33x 6x 6x 33x 18x 18x 31x 18x 1x 11x 11x 9x 9x 2x 2x 2x 2x 9x 9x 9x 1x 7x 7x 7x 7x 5x 2x 2x 2x 2x 1x 5x 5x 5x 1x 5x 5x 5x 3x 2x 2x 2x 2x 1x | /*! * VisualEditor ContentEditable TableNode class. * * @copyright See AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * Table action. * * @class * @extends ve.ui.Action * * @constructor * @param {ve.ui.Surface} surface Surface to act on * @param {string} [source] */ ve.ui.TableAction = function VeUiTableAction() { // Parent constructor ve.ui.TableAction.super.apply( this, arguments ); }; /* Inheritance */ OO.inheritClass( ve.ui.TableAction, ve.ui.Action ); /* Static Properties */ ve.ui.TableAction.static.name = 'table'; ve.ui.TableAction.static.methods = [ 'create', 'insert', 'moveRelative', 'move', 'delete', 'importTable', 'changeCellStyle', 'mergeCells', 'enterTableCell', 'exitTableCell' ]; /* Methods */ /** * Creates a new table. * * @param {Object} [options] Table creation options * @param {boolean} [options.caption] Include a caption * @param {boolean} [options.header] Include a header row * @param {number} [options.cols=4] Number of columns * @param {number} [options.rows=3] Number of rows (not including optional header row) * @param {Object} [options.type='table'] Table node type, must inherit from table * @param {Object} [options.attributes] Attributes to give the table * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.create = function ( options ) { options = options || {}; const type = options.type || 'table'; const tableElement = { type: type }; const surfaceModel = this.surface.getModel(); const fragment = surfaceModel.getFragment(); const numberOfCols = options.cols || 4; const numberOfRows = options.rows || 3; if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) { return false; } if ( options.attributes ) { tableElement.attributes = ve.copy( options.attributes ); } const data = []; data.push( tableElement ); if ( options.caption ) { data.push( { type: 'tableCaption' }, { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' }, { type: '/tableCaption' } ); } data.push( { type: 'tableSection', attributes: { style: 'body' } } ); if ( options.header ) { ve.batchPush( data, ve.dm.TableRowNode.static.createData( { style: 'header', cellCount: numberOfCols } ) ); } for ( let i = 0; i < numberOfRows; i++ ) { ve.batchPush( data, ve.dm.TableRowNode.static.createData( { style: 'data', cellCount: numberOfCols } ) ); } data.push( { type: '/tableSection' } ); data.push( { type: '/' + type } ); fragment.insertContent( data, false ); surfaceModel.setSelection( new ve.dm.TableSelection( fragment.getSelection().getRange(), 0, 0, 0, 0 ) ); ve.track( 'activity.table', { action: 'create' } ); return true; }; /** * Inserts a new row or column into the currently focused table. * * @param {string} mode Insertion mode; 'row' to insert a new row, 'col' for a new column * @param {string} position Insertion position; 'before' to insert before the current selection, * 'after' to insert after it * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.insert = function ( mode, position ) { const surfaceModel = this.surface.getModel(); let selection = surfaceModel.getSelection(); if ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } let index; if ( mode === 'col' ) { index = position === 'before' ? selection.startCol : selection.endCol; } else { index = position === 'before' ? selection.startRow : selection.endRow; } const documentModel = surfaceModel.getDocument(); if ( position === 'before' ) { if ( mode === 'col' ) { selection = selection.newFromAdjustment( documentModel, 1, 0 ); } else { selection = selection.newFromAdjustment( documentModel, 0, 1 ); } surfaceModel.setSelection( selection ); } this.insertRowOrCol( selection.getTableNode( documentModel ), mode, index, position, selection ); ve.track( 'activity.table', { action: 'insert-' + mode } ); return true; }; /** * Move a column or row relative to its current position * * @param {string} mode Move mode; 'col' or 'row' * @param {string} direction Direction; 'before' or 'after' * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.moveRelative = function ( mode, direction ) { const surfaceModel = this.surface.getModel(), selection = surfaceModel.getSelection(); if ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } const documentModel = surfaceModel.getDocument(); const matrix = selection.getTableNode( documentModel ).getMatrix(); let index; if ( mode === 'row' ) { if ( direction === 'before' ) { index = Math.max( 0, selection.startRow - 1 ); } else { index = Math.min( matrix.getRowCount(), selection.endRow + 2 ); } } else { if ( direction === 'before' ) { index = Math.max( 0, selection.startCol - 1 ); } else { index = Math.min( matrix.getMaxColCount(), selection.endCol + 2 ); } } return this.move( mode, index ); }; /** * Move a column or row. * * @param {string} mode Move mode; 'col' or 'row' * @param {number} index Row or column index to move to * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.move = function ( mode, index ) { const surfaceModel = this.surface.getModel(), selection = surfaceModel.getSelection(); if ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } const documentModel = surfaceModel.getDocument(); const tableNode = selection.getTableNode( documentModel ); const matrix = tableNode.getMatrix(); let removedMatrix, newOffsets; if ( mode === 'row' ) { removedMatrix = this.deleteRowsOrColumns( matrix, mode, selection.startRow, selection.endRow ); if ( index > selection.endRow ) { index = index - selection.getRowCount(); } newOffsets = [ selection.fromCol, index, selection.toCol, index + selection.getRowCount() - 1 ]; } else { removedMatrix = this.deleteRowsOrColumns( matrix, mode, selection.startCol, selection.endCol ); if ( index > selection.endCol ) { index = index - selection.getColCount(); } newOffsets = [ index, selection.fromRow, index + selection.getColCount() - 1, selection.toRow ]; } let position; if ( index === 0 ) { position = 'before'; } else { index--; position = 'after'; } for ( let i = removedMatrix.length - 1; i >= 0; i-- ) { this.insertRowOrCol( tableNode, mode, index, position, null, removedMatrix[ i ] ); } // Only set selection once for performance surfaceModel.setSelection( new ve.dm.TableSelection( // tableNode range was changed by deletion tableNode.getOuterRange(), newOffsets[ 0 ], newOffsets[ 1 ], newOffsets[ 2 ], newOffsets[ 3 ] ) ); ve.track( 'activity.table', { action: 'move-' + mode } ); return true; }; /** * Deletes selected rows, columns, or the whole table. * * @param {string} mode Deletion mode; 'row' to delete rows, 'col' for columns, 'table' to remove the whole table * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.delete = function ( mode ) { const selection = this.getTableSelectionFromSelection(); if ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } const documentModel = this.surface.getModel().getDocument(); const tableNode = selection.getTableNode( documentModel ); // Either delete the table or rows or columns if ( mode === 'table' ) { this.deleteTable( tableNode ); } else { let minIndex, maxIndex, isFull; if ( mode === 'col' ) { minIndex = selection.startCol; maxIndex = selection.endCol; isFull = selection.isFullRow( documentModel ); } else { minIndex = selection.startRow; maxIndex = selection.endRow; isFull = selection.isFullCol( documentModel ); } // Delete the whole table if all rows or cols get deleted Iif ( isFull ) { this.deleteTable( tableNode ); } else { this.deleteRowsOrColumns( tableNode.matrix, mode, minIndex, maxIndex ); } } ve.track( 'activity.table', { action: 'delete' + ( mode !== 'table' ? ( '-' + mode ) : '' ) } ); return true; }; /** * Import a table at the current selection, overwriting data cell by cell * * @param {ve.dm.TableNode} importedTableNode Table node to import * @param {boolean} importInternalList Import the table document's internalLiist * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.importTable = function ( importedTableNode, importInternalList ) { const importedMatrix = importedTableNode.getMatrix(), surfaceModel = this.surface.getModel(), documentModel = surfaceModel.getDocument(), selection = surfaceModel.getSelection(), tableNode = selection.getTableNode( documentModel ), matrix = tableNode.getMatrix(); let i, l; // Increase size of table to fit imported table for ( i = 0, l = selection.startRow + importedMatrix.getRowCount() - matrix.getRowCount(); i < l; i++ ) { this.insertRowOrCol( tableNode, 'row', matrix.getRowCount() - 1, 'after' ); } for ( i = 0, l = selection.startCol + importedMatrix.getMaxColCount() - matrix.getMaxColCount(); i < l; i++ ) { this.insertRowOrCol( tableNode, 'col', matrix.getMaxColCount() - 1, 'after' ); } let row, col, cell; // Unmerge all cells in the target area for ( row = importedMatrix.getRowCount() - 1; row >= 0; row-- ) { for ( col = importedMatrix.getColCount( row ) - 1; col >= 0; col-- ) { cell = matrix.getCell( selection.fromRow + row, selection.fromCol + col ); // Missing cell(s), add cell(s) onto the end of the row while ( !cell ) { surfaceModel.change( ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, matrix.getRowNode( selection.fromRow + row ).getRange().end, ve.dm.TableCellNode.static.createData() ) ); cell = matrix.getCell( selection.fromRow + row, selection.fromCol + col ); } Iif ( cell.isPlaceholder() || cell.node.getColspan() > 1 || cell.node.getRowspan() > 1 ) { this.unmergeCell( matrix, cell.owner ); } } } // Overwrite data for ( row = importedMatrix.getRowCount() - 1; row >= 0; row-- ) { for ( col = importedMatrix.getColCount( row ) - 1; col >= 0; col-- ) { cell = matrix.getCell( selection.fromRow + row, selection.fromCol + col ); const cellRange = cell.node.getRange(); const importedCell = importedMatrix.getCell( row, col ); Iif ( !importedCell ) { // Cell not found in source table. Likely some sort of invalid or sparse // table matrix (T262842). Just ignore the empty cell. continue; } if ( importedCell.node.type !== cell.node.type ) { // Since the imported cell isn't the same type as the // existing cell, we can't quite trust our assumptions about // how it's supposed to work. As such, it's safer to outright // replace the cell rather than trying to be clever and switch // out the attributes / data. We shouldn't have gotten to this // point without it being Cellable, so this should at least // work. surfaceModel.change( ve.dm.TransactionBuilder.static.newFromReplacement( documentModel, cell.node.getOuterRange(), importedTableNode.getDocument().getData( importedCell.node.getOuterRange() ) ) ); } else if ( !importedCell.isPlaceholder() ) { // Remove the existing cell contents surfaceModel.change( ve.dm.TransactionBuilder.static.newFromRemoval( documentModel, cellRange ) ); // Attribute changes are performed separately, and removing the whole // cell could change the dimensions of the table const txBuilders = [ ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, documentModel, cellRange.start - 1, ve.copy( importedCell.node.element.attributes ) ) ]; if ( importInternalList ) { txBuilders.push( ve.dm.TransactionBuilder.static.newFromDocumentInsertion.bind( null, documentModel, cellRange.start, importedTableNode.getDocument(), importedCell.node.getRange() ) ); } else E{ txBuilders.push( ve.dm.TransactionBuilder.static.newFromInsertion.bind( null, documentModel, cellRange.start, importedTableNode.getDocument().getData( importedCell.node.getRange() ) ) ); } // Perform the insertion as a separate change so the internalList offsets are correct txBuilders.forEach( ( txBuilder ) => { surfaceModel.change( txBuilder() ); } ); } else E{ // Remove the existing cell completely surfaceModel.change( ve.dm.TransactionBuilder.static.newFromRemoval( documentModel, cell.node.getOuterRange() ) ); } } } surfaceModel.setSelection( new ve.dm.TableSelection( tableNode.getOuterRange(), selection.startCol, selection.startRow, selection.startCol + importedMatrix.getMaxColCount() - 1, selection.startRow + importedMatrix.getRowCount() - 1 ) ); return true; }; /** * Change cell style * * @param {string} style Cell style; 'header' or 'data' * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.changeCellStyle = function ( style ) { const surfaceModel = this.surface.getModel(), selection = surfaceModel.getSelection(); Iif ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } const txBuilders = []; const documentModel = surfaceModel.getDocument(); const ranges = selection.getOuterRanges( documentModel ); for ( let i = ranges.length - 1; i >= 0; i-- ) { txBuilders.push( ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, documentModel, ranges[ i ].start, { style: style } ) ); } txBuilders.forEach( ( txBuilder ) => { surfaceModel.change( txBuilder() ); } ); ve.track( 'activity.table', { action: 'style-' + style } ); return true; }; /** * Merge multiple cells into one, or split a merged cell. * * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.mergeCells = function () { const surfaceModel = this.surface.getModel(), selection = surfaceModel.getSelection(); if ( !( selection instanceof ve.dm.TableSelection ) ) { return false; } const txBuilders = []; const documentModel = surfaceModel.getDocument(); const matrix = selection.getTableNode( documentModel ).getMatrix(); let cells; if ( selection.isSingleCell( documentModel ) ) { // Split cells = selection.getMatrixCells( documentModel ); this.unmergeCell( matrix, cells[ 0 ] ); ve.track( 'activity.table', { action: 'cell-split' } ); } else { // Merge Iif ( !selection.isMergeable( documentModel ) ) { return false; } cells = selection.getMatrixCells( documentModel ); txBuilders.push( ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, documentModel, cells[ 0 ].node.getOuterRange().start, { colspan: 1 + selection.endCol - selection.startCol, rowspan: 1 + selection.endRow - selection.startRow } ) ); let i, l; let contentData; // Find first cell with content for ( i = 0, l = cells.length; i < l; i++ ) { contentData = new ve.dm.ElementLinearData( documentModel.getStore(), documentModel.getData( cells[ i ].node.getRange() ) ); if ( contentData.hasContent() ) { // If the first cell contains content, we don't need to move any content if ( !i ) { contentData = null; } break; } } // Remove placeholders for ( i = cells.length - 1; i >= 1; i-- ) { txBuilders.push( ve.dm.TransactionBuilder.static.newFromRemoval.bind( null, documentModel, cells[ i ].node.getOuterRange() ) ); } // Move the first-found content to the merged cell if ( contentData ) { txBuilders.push( ve.dm.TransactionBuilder.static.newFromReplacement.bind( null, documentModel, cells[ 0 ].node.getRange(), contentData.data ) ); } txBuilders.forEach( ( txBuilder ) => { surfaceModel.change( txBuilder() ); } ); let hasNonPlaceholders, r, c, cell; // Check for rows filled with entirely placeholders. If such a row exists, delete it. for ( r = selection.endRow; r >= selection.startRow; r-- ) { hasNonPlaceholders = false; for ( c = 0; ( cell = matrix.getCell( r, c ) ) !== undefined; c++ ) { if ( cell && !cell.isPlaceholder() ) { hasNonPlaceholders = true; break; } } if ( !hasNonPlaceholders ) { this.deleteRowsOrColumns( matrix, 'row', r, r ); } } // Check for columns filled with entirely placeholders. If such a column exists, delete it. for ( c = selection.endCol; c >= selection.startCol; c-- ) { hasNonPlaceholders = false; for ( r = 0; ( cell = matrix.getCell( r, c ) ) !== undefined; r++ ) { if ( cell && !cell.isPlaceholder() ) { hasNonPlaceholders = true; break; } } if ( !hasNonPlaceholders ) { this.deleteRowsOrColumns( matrix, 'col', c, c ); } } ve.track( 'activity.table', { action: 'cell-merge' } ); } return true; }; /** * Enter a table cell for editing * * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.enterTableCell = function () { const tableNode = this.findClosestTableViewNode(); Iif ( !tableNode ) { return false; } tableNode.setEditing( true ); this.surface.getView().focus(); return true; }; /** * Exit a table cell for table-structure-editing * * @return {boolean} Action was executed */ ve.ui.TableAction.prototype.exitTableCell = function () { const tableNode = this.findClosestTableViewNode(); if ( !tableNode ) { return false; } tableNode.setEditing( false ); this.surface.getView().focus(); return true; }; /* Low-level API */ // TODO: This API does only depends on the model so it should possibly be moved /** * Deletes a whole table. * * @param {ve.dm.TableNode} tableNode Table node */ ve.ui.TableAction.prototype.deleteTable = function ( tableNode ) { this.surface.getModel().getLinearFragment( tableNode.getOuterRange() ).delete(); }; /** * Unmerge a cell * * @param {ve.dm.TableMatrix} matrix Table matrix the cell is in * @param {ve.dm.TableMatrixCell} ownerCell The cell to unmerge */ ve.ui.TableAction.prototype.unmergeCell = function ( matrix, ownerCell ) { const txBuilders = [], colspan = ownerCell.node.getColspan(), rowspan = ownerCell.node.getRowspan(), surfaceModel = this.surface.getModel(), documentModel = surfaceModel.getDocument(); txBuilders.push( ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, documentModel, ownerCell.node.getOuterRange().start, { colspan: 1, rowspan: 1 } ) ); for ( let row = ownerCell.row + rowspan - 1; row >= ownerCell.row; row-- ) { for ( let col = ownerCell.col + colspan - 1; col >= ownerCell.col; col-- ) { const cell = matrix.getCell( row, col ); if ( cell.isPlaceholder() ) { txBuilders.push( this.replacePlaceholder( matrix, cell, { style: ownerCell.node.getStyle() } ) ); } } } txBuilders.forEach( ( txBuilder ) => { surfaceModel.change( txBuilder() ); } ); }; /** * Inserts a new row or column. * * Example: a new row can be inserted after the 2nd row using * * insertRowOrCol( table, 'row', 1, 'after' ); * * @param {ve.dm.TableNode} tableNode Table node * @param {string} mode Insertion mode; 'row' or 'col' * @param {number} index Row or column index of the base row or column. * @param {string} position Insertion position; 'before' or 'after' * @param {ve.dm.TableSelection} [selection] Selection to move to after insertion * @param {Object} [dataMatrixLine] Data to insert * @param {Array} [dataMatrixLine.rowData] Row data if inserting a row * @param {ve.dm.TableMatrixCell[]} [dataMatrixLine.cells] Table cells to insert */ ve.ui.TableAction.prototype.insertRowOrCol = function ( tableNode, mode, index, position, selection, dataMatrixLine ) { const matrix = tableNode.matrix, insertCells = [], txBuilders = [], updated = {}, inserts = [], surfaceModel = this.surface.getModel(); let insertData = []; const before = position === 'before'; // Note: when we insert a new row (or column) we might need to increment a span property // instead of inserting a new cell. // To achieve this we look at the so called base row and a so called reference row. // The base row is the one after or before which the new row will be inserted. // The reference row is the one which is currently at the place of the new one. // E.g. consider inserting a new row after the second: the base row is the second, the // reference row is the third. // A span must be increased if the base cell and the reference cell have the same 'owner'. // E.g.: C* | P**; C | P* | P**, i.e., one of the two cells might be the owner of the other, // or vice versa, or both a placeholders of a common cell. // The index of the reference row or column const refIndex = index + ( before ? -1 : 1 ); // Cells of the selected row or column let cells, refCells; if ( mode === 'row' ) { cells = matrix.getRow( index ) || []; refCells = matrix.getRow( refIndex ) || []; } else { cells = matrix.getColumn( index ) || []; refCells = matrix.getColumn( refIndex ) || []; } for ( let i = 0, l = Math.max( cells.length, dataMatrixLine ? dataMatrixLine.cells.length : 0 ); i < l; i++ ) { let cell = cells[ i ]; if ( !cell ) { if ( dataMatrixLine && dataMatrixLine.cells[ i ] ) { // If we've been given data to fill the empty cells with, do so insertCells.push( dataMatrixLine.cells[ i ] ); } // Either way, continue on to the next cell continue; } const refCell = refCells[ i ]; // Detect if span update is necessary if ( refCell && ( cell.isPlaceholder() || refCell.isPlaceholder() ) ) { if ( cell.node === refCell.node ) { cell = cell.owner || cell; if ( !updated[ cell.key ] ) { // Note: we can safely record span modifications as they do not affect range offsets. txBuilders.push( this.incrementSpan( cell, mode ) ); updated[ cell.key ] = true; } // Resolve merged cell conflicts when moving Iif ( dataMatrixLine && dataMatrixLine.cells[ i ].owner.data && !dataMatrixLine.cells[ i ].owner.conflicted ) { if ( dataMatrixLine.cells[ i ].isPlaceholder() ) { // If placeholders conflict, collapse their owners dataMatrixLine.cells[ i ].owner.data[ 0 ].attributes.colspan = 1; dataMatrixLine.cells[ i ].owner.data[ 0 ].attributes.rowspan = 1; } // Mark owner (could be self) as conflicted so placeholders know it didn't get inserted dataMatrixLine.cells[ i ].owner.conflicted = true; } continue; } } // If it is not a span changer, we record the base cell as a reference for insertion inserts.push( cell ); if ( dataMatrixLine ) { insertCells.push( dataMatrixLine.cells[ i ] ); } } // Inserting a new row differs completely from inserting a new column: // For a new row, a new row node is created, and inserted relative to an existing row node. // For a new column, new cells are inserted into existing row nodes at appropriate positions, // i.e., relative to an existing cell node. if ( mode === 'row' ) { if ( !dataMatrixLine ) { insertData = ve.dm.TableRowNode.static.createData( { cellCount: inserts.length, style: cells.map( ( c ) => c.node.getStyle() ) } ); } else { insertData.push( dataMatrixLine.row[ 0 ] ); insertCells.forEach( ( c ) => { if ( c && c.data ) { ve.batchPush( insertData, c.data ); } else Eif ( !( c && c.isPlaceholder() && c.owner.data && !c.owner.conflicted ) ) { // If a placeholder, and the owner was not inserted, created a blank cell ve.batchPush( insertData, ve.dm.TableCellNode.static.createData() ); } } ); insertData.push( dataMatrixLine.row[ 1 ] ); } let rowNode; while ( ( rowNode = matrix.getRowNode( index ) ) === undefined ) { index--; } const range = rowNode.getOuterRange(); const offset = before ? range.start : range.end; txBuilders.push( ve.dm.TransactionBuilder.static.newFromInsertion.bind( null, surfaceModel.getDocument(), offset, insertData ) ); } else { // Make sure that the inserts are in descending offset order // so that the transactions do not affect subsequent range offsets. inserts.sort( ve.dm.TableMatrixCell.static.sortDescending ); // For inserting a new cell we need to find a reference cell node // which we can use to get a proper insertion offset. for ( let i = 0; i < inserts.length; i++ ) { let cell = inserts[ i ]; Iif ( !cell ) { continue; } // If the cell is a placeholder this will find a close cell node in the same row const refCell = matrix.findClosestCell( cell ); let style; let offset; if ( refCell ) { const range = refCell.node.getOuterRange(); // If the found cell is before the base cell the new cell must be placed after it, in any case, // Only if the base cell is not a placeholder we have to consider the insert mode. if ( refCell.col < cell.col || ( refCell.col === cell.col && !before ) ) { offset = range.end; } else { offset = range.start; } style = refCell.node.getStyle(); } else { // If there are only placeholders in the row, we use the row node's inner range // for the insertion offset const rowNode = matrix.getRowNode( cell.row ); Eif ( !rowNode ) { continue; } const range = rowNode.getRange(); offset = before ? range.start : range.end; style = cells[ 0 ].node.getStyle(); } let cellData; if ( !dataMatrixLine ) { cellData = ve.dm.TableCellNode.static.createData( { style: style } ); } else { cell = dataMatrixLine.cells[ cell.row ]; cellData = []; if ( cell && cell.data ) { cellData = cell.data; } else Eif ( !( cell && cell.isPlaceholder() && cell.owner.data && !cell.owner.conflicted ) ) { // If a placeholder, and the owner was not inserted, created a blank cell cellData = ve.dm.TableCellNode.static.createData(); } } txBuilders.push( ve.dm.TransactionBuilder.static.newFromInsertion.bind( null, surfaceModel.getDocument(), offset, cellData ) ); } } txBuilders.forEach( ( txBuilder ) => { const tx = txBuilder(); selection = selection && selection.translateByTransaction( tx ); surfaceModel.change( tx ); } ); if ( selection ) { surfaceModel.change( null, selection ); } }; /** * Increase the span of a cell by one. * * @param {ve.dm.TableMatrixCell} cell Table matrix cell * @param {string} mode Span to increment; 'row' or 'col' * @return {Function} Zero-argument function returning a ve.dm.Transaction */ ve.ui.TableAction.prototype.incrementSpan = function ( cell, mode ) { let data; if ( mode === 'row' ) { data = { rowspan: cell.node.getRowspan() + 1 }; } else { data = { colspan: cell.node.getColspan() + 1 }; } const surfaceModel = this.surface.getModel(); return ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, surfaceModel.getDocument(), cell.node.getOuterRange().start, data ); }; /** * Decreases the span of a cell so that the given interval is removed. * * @param {ve.dm.TableMatrixCell} cell Table matrix cell * @param {string} mode Span to decrement 'row' or 'col' * @param {number} minIndex Smallest row or column index (inclusive) * @param {number} maxIndex Largest row or column index (inclusive) * @return {Function} Zero-argument function returning a ve.dm.Transaction */ ve.ui.TableAction.prototype.decrementSpan = function ( cell, mode, minIndex, maxIndex ) { const span = ( minIndex - cell[ mode ] ) + Math.max( 0, cell[ mode ] + cell.node.getSpans()[ mode ] - 1 - maxIndex ); let data; if ( mode === 'row' ) { data = { rowspan: span }; } else { data = { colspan: span }; } const surfaceModel = this.surface.getModel(); return ve.dm.TransactionBuilder.static.newFromAttributeChanges.bind( null, surfaceModel.getDocument(), cell.node.getOuterRange().start, data ); }; /** * Deletes rows or columns within a given range. * * e.g. rows 2-4 can be deleted using * * ve.ui.TableAction.deleteRowsOrColumns( matrix, 'row', 1, 3 ); * * @param {ve.dm.TableMatrix} matrix Table matrix * @param {string} mode 'row' or 'col' * @param {number} minIndex Smallest row or column index to be deleted * @param {number} maxIndex Largest row or column index to be deleted (inclusive) * @return {Object[]} Plain sub-matrix of items removed. In column mode this matrix is transposed. */ ve.ui.TableAction.prototype.deleteRowsOrColumns = function ( matrix, mode, minIndex, maxIndex ) { const removedMatrix = [], txBuilders = [], adapted = {}, actions = [], cells = [], surfaceModel = this.surface.getModel(), documentModel = surfaceModel.getDocument(); // Deleting cells can have two additional consequences: // 1. The cell is a Placeholder. The owner's span must be decreased. // 2. The cell is owner of placeholders which get orphaned by the deletion. // The first of the placeholders now becomes the real cell, with the span adjusted. // It also inherits all of the properties and content of the removed cell. // Insertions and deletions of cells must be done in an appropriate order, so that the transactions // do not interfere with each other. To achieve that, we record insertions and deletions and // sort them by the position of the cell (row, column) in the table matrix. if ( mode === 'row' ) { for ( let row = minIndex; row <= maxIndex; row++ ) { ve.batchPush( cells, matrix.getRow( row ) ); } } else { for ( let col = minIndex; col <= maxIndex; col++ ) { ve.batchPush( cells, matrix.getColumn( col ) ); } } for ( let i = 0, l = cells.length; i < l; i++ ) { const cell = cells[ i ]; if ( !cell ) { continue; } if ( cell.isPlaceholder() ) { const key = cell.owner.key; if ( !adapted[ key ] ) { // Note: we can record this transaction immediately, as it does not have an effect on the // node range txBuilders.push( this.decrementSpan( cell.owner, mode, minIndex, maxIndex ) ); adapted[ key ] = true; } continue; } // Detect if the owner of a spanning cell gets deleted and // leaves orphaned placeholders const span = cell.node.getSpans()[ mode ]; if ( cell[ mode ] + span - 1 > maxIndex ) { let startRow, startCol; // add inserts for orphaned place holders Iif ( mode === 'col' ) { startRow = cell.row; startCol = maxIndex + 1; } else { startRow = maxIndex + 1; startCol = cell.col; } const endRow = cell.row + cell.node.getRowspan() - 1; const endCol = cell.col + cell.node.getColspan() - 1; // Record the insertion to apply it later actions.push( { action: 'insert', cell: matrix.getCell( startRow, startCol ), colspan: 1 + endCol - startCol, rowspan: 1 + endRow - startRow, style: cell.node.getStyle(), content: documentModel.getData( cell.node.getRange() ) } ); } // Cell nodes only get deleted when deleting columns (otherwise row nodes) if ( mode === 'col' ) { actions.push( { action: 'delete', cell: cell } ); } } // Make sure that the actions are in descending offset order // so that the transactions do not affect subsequent range offsets. // Sort recorded actions to make sure the transactions will not interfere with respect to offsets actions.sort( ( a, b ) => ve.dm.TableMatrixCell.static.sortDescending( a.cell, b.cell ) ); if ( mode === 'row' ) { // First replace orphaned placeholders which are below the last deleted row, // thus, this works with regard to transaction offsets for ( let i = 0; i < actions.length; i++ ) { txBuilders.push( this.replacePlaceholder( matrix, actions[ i ].cell, actions[ i ] ) ); } // Remove rows in reverse order to have valid transaction offsets for ( let row = maxIndex; row >= minIndex; row-- ) { const rowNode = matrix.getRowNode( row ); if ( !rowNode ) { continue; } txBuilders.push( ve.dm.TransactionBuilder.static.newFromRemoval.bind( null, documentModel, rowNode.getOuterRange() ) ); // Store removed data for moving const removedCells = matrix.getRow( row ); const rowRange = rowNode.getOuterRange(); const rowData = documentModel.getData( new ve.Range( rowRange.start, rowRange.start + 1 ), true ).concat( documentModel.getData( new ve.Range( rowRange.end - 1, rowRange.end ), true ) ); // Remove all but start and end tags rowData.splice( 1, rowData.length - 2 ); removedMatrix[ row - minIndex ] = { row: rowData, cells: removedCells.map( ( ce ) => { if ( ce && !ce.isPlaceholder() ) { ce.data = documentModel.getData( ce.node.getOuterRange(), true ); // When re-insterted the span can not exceed the size of the selection if ( ce.data[ 0 ].attributes.rowspan > 1 + maxIndex - minIndex ) { ce.data = null; } } return ce; } ) }; } } else { for ( let i = 0; i < actions.length; i++ ) { Iif ( actions[ i ].action === 'insert' ) { txBuilders.push( this.replacePlaceholder( matrix, actions[ i ].cell, actions[ i ] ) ); } else { txBuilders.push( ve.dm.TransactionBuilder.static.newFromRemoval.bind( null, documentModel, actions[ i ].cell.node.getOuterRange() ) ); actions[ i ].cell.data = documentModel.getData( actions[ i ].cell.node.getOuterRange(), true ); } } for ( let col = maxIndex; col >= minIndex; col-- ) { removedMatrix[ col - minIndex ] = { cells: matrix.getColumn( col ).map( ( c ) => { if ( c && !c.isPlaceholder() ) { c.data = documentModel.getData( c.node.getOuterRange(), true ); // When re-insterted the span can not exceed the size of the selection Iif ( c.data[ 0 ].attributes.colspan > 1 + maxIndex - minIndex ) { c.data = null; } } return c; } ) }; } } surfaceModel.change( null, new ve.dm.NullSelection() ); txBuilders.forEach( ( txBuilder ) => { surfaceModel.change( txBuilder() ); } ); return removedMatrix; }; /** * Inserts a new cell for an orphaned placeholder. * * @param {ve.dm.TableMatrix} matrix Table matrix * @param {ve.dm.TableMatrixCell} placeholder Placeholder cell to replace * @param {Object} [options] Options to pass to ve.dm.TableCellNode.static.createData * @return {Function} Zero-argument function returning a ve.dm.Transaction */ ve.ui.TableAction.prototype.replacePlaceholder = function ( matrix, placeholder, options ) { // For inserting the new cell a reference cell node // which is used to get an insertion offset. const refCell = matrix.findClosestCell( placeholder ); let range, offset; if ( refCell ) { range = refCell.node.getOuterRange(); offset = ( placeholder.col < refCell.col ) ? range.start : range.end; } else { const rowNode = matrix.getRowNode( placeholder.row ); Eif ( !rowNode ) { return function () { return null; }; } // if there are only placeholders in the row, the row node's inner range is used range = rowNode.getRange(); offset = range.start; } const data = ve.dm.TableCellNode.static.createData( options ); const surfaceModel = this.surface.getModel(); return ve.dm.TransactionBuilder.static.newFromInsertion.bind( null, surfaceModel.getDocument(), offset, data ); }; /** * Find the closest table node to the current selection * * This method is model-only. * * @return {ve.dm.TableNode|null} The closest table node, null if not found */ ve.ui.TableAction.prototype.findClosestTableNode = function () { const surfaceModel = this.surface.getModel(), documentModel = surfaceModel.getDocument(), selection = this.surface.getModel().getSelection(); if ( selection instanceof ve.dm.TableSelection ) { return selection.getTableNode( documentModel ); } else Eif ( selection instanceof ve.dm.LinearSelection ) { const node = documentModel.getBranchNodeFromOffset( selection.getRange().start ); Eif ( node ) { return node.findParent( ve.dm.TableNode ); } } return null; }; /** * Find the closest table view node to the current selection * * @return {ve.ce.TableNode|null} The closest table view, null if not found */ ve.ui.TableAction.prototype.findClosestTableViewNode = function () { const tableNode = this.findClosestTableNode(); Eif ( tableNode ) { return this.surface.getView().getDocument().getBranchNodeFromOffset( tableNode.getRange().start ); } return null; }; /** * Get a TableSelection that contains the current selection * * This method is model-only. * * @return {ve.dm.TableSelection|null} The closest TableSelection, null if not found */ ve.ui.TableAction.prototype.getTableSelectionFromSelection = function () { // If the current selection is contained within a table, we'd like the relevant TableSelection. const surfaceModel = this.surface.getModel(); let selection = surfaceModel.getSelection(); if ( selection instanceof ve.dm.TableSelection ) { return selection; } else Eif ( selection instanceof ve.dm.LinearSelection ) { const tableNode = this.findClosestTableNode(); Eif ( !tableNode ) { return null; } const documentModel = surfaceModel.getDocument(); const node = documentModel.getBranchNodeFromOffset( selection.getRange().start ); if ( !node ) { return null; } const cellNode = node.findParent( ve.dm.TableCellNode ); if ( !cellNode ) { return null; } const cell = tableNode.getMatrix().lookupCell( cellNode ); if ( !cell ) { return null; } selection = new ve.dm.TableSelection( tableNode.getOuterRange(), cell.col, cell.row ); return selection.expand( documentModel ); } return null; }; /* Registration */ ve.ui.actionFactory.register( ve.ui.TableAction ); |