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 | 1x 1x 10x 10x 10x 10x 10x 10x 10x 30x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 16x 1x 1x 1x 3x 3x 3x 4x 4x 8x 8x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 3x 3x 3x 3x 3x 4x 4x 3x 3x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 56x 56x 84x 56x 56x 56x 56x 10x 5x 7x 7x 10x 5x 7x 7x 10x 6x 8x 8x 10x 6x 8x 8x | import { extractBorderValues, getFirstSelectedTable, getSelectedCells, mutateBlock, getTableMetadata, parseValueWithUnit, setFirstColumnFormatBorders, updateTableCellMetadata, } from 'roosterjs-content-model-dom'; import type { IEditor, Border, BorderOperations, TableSelectionCoordinates, ReadonlyContentModelTableCell, ReadonlyContentModelTable, } from 'roosterjs-content-model-types'; /** * @internal * Border positions */ type BorderPositions = 'borderTop' | 'borderBottom' | 'borderLeft' | 'borderRight'; /** * @internal * Perimeter of the table selection * Used to determine where to apply border to the cells adjacent to the selection. */ type Perimeter = { Top: boolean; Bottom: boolean; Left: boolean; Right: boolean; }; /** * Operations to apply border * @param editor The editor instance * @param border The border to apply * @param operation The operation to apply */ export function applyTableBorderFormat( editor: IEditor, border: Border, operation: BorderOperations ) { editor.formatContentModel( model => { const [tableModel] = getFirstSelectedTable(model); Eif (tableModel) { const sel = getSelectedCells(tableModel); const perimeter: Perimeter = { Top: false, Bottom: false, Left: false, Right: false, }; // Create border format with table format as backup let borderFormat = ''; const format = tableModel.format; const { width, style, color } = border; const extractedBorder = extractBorderValues(format.borderTop); const borderColor = extractedBorder.color; const borderWidth = extractedBorder.width; const borderStyle = extractedBorder.style; Eif (width) { borderFormat = parseValueWithUnit(width) + 'px'; } else if (borderWidth) { borderFormat = borderWidth; } else { borderFormat = '1px'; } Eif (style) { borderFormat = `${borderFormat} ${style}`; } else if (borderStyle) { borderFormat = `${borderFormat} ${borderStyle}`; } else { borderFormat = `${borderFormat} solid`; } Eif (color) { borderFormat = `${borderFormat} ${color}`; } else if (borderColor) { borderFormat = `${borderFormat} ${borderColor}`; } // undefined is treated as Left to Right const isRtl = tableModel.format.direction == 'rtl'; Eif (sel) { const operations: BorderOperations[] = [operation]; while (operations.length) { switch (operations.pop()) { case 'noBorders': // Do All borders but with empty border format borderFormat = ''; operations.push('allBorders'); break; case 'allBorders': const allBorders: BorderPositions[] = [ 'borderTop', 'borderBottom', 'borderLeft', 'borderRight', ]; for ( let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++ ) { for ( let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++ ) { const cell = tableModel.rows[rowIndex].cells[colIndex]; // Format cells - All borders applyBorderFormat(cell, borderFormat, allBorders); } } // Format perimeter perimeter.Top = true; perimeter.Bottom = true; perimeter.Left = true; perimeter.Right = true; break; case 'leftBorders': const leftBorder: BorderPositions[] = ['borderLeft']; for ( let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++ ) { const cell = tableModel.rows[rowIndex].cells[ isRtl ? sel.lastColumn : sel.firstColumn ]; // Format cells - Left border applyBorderFormat(cell, borderFormat, leftBorder); } // Format perimeter isRtl ? (perimeter.Right = true) : (perimeter.Left = true); break; case 'rightBorders': const rightBorder: BorderPositions[] = ['borderRight']; for ( let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++ ) { const cell = tableModel.rows[rowIndex].cells[ isRtl ? sel.firstColumn : sel.lastColumn ]; // Format cells - Right border applyBorderFormat(cell, borderFormat, rightBorder); } // Format perimeter isRtl ? (perimeter.Left = true) : (perimeter.Right = true); break; case 'topBorders': const topBorder: BorderPositions[] = ['borderTop']; for ( let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++ ) { const cell = tableModel.rows[sel.firstRow].cells[colIndex]; // Format cells - Top border applyBorderFormat(cell, borderFormat, topBorder); } // Format perimeter perimeter.Top = true; break; case 'bottomBorders': const bottomBorder: BorderPositions[] = ['borderBottom']; for ( let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++ ) { const cell = tableModel.rows[sel.lastRow].cells[colIndex]; // Format cells - Bottom border applyBorderFormat(cell, borderFormat, bottomBorder); } // Format perimeter perimeter.Bottom = true; break; case 'insideBorders': // Format cells - Inside borders const singleCol = sel.lastColumn == sel.firstColumn; const singleRow = sel.lastRow == sel.firstRow; // Single cell selection Iif (singleCol && singleRow) { break; } // Single column selection Iif (singleCol) { applyBorderFormat( tableModel.rows[sel.firstRow].cells[sel.firstColumn], borderFormat, ['borderBottom'] ); for ( let rowIndex = sel.firstRow + 1; rowIndex <= sel.lastRow - 1; rowIndex++ ) { const cell = tableModel.rows[rowIndex].cells[sel.firstColumn]; applyBorderFormat(cell, borderFormat, [ 'borderTop', 'borderBottom', ]); } applyBorderFormat( tableModel.rows[sel.lastRow].cells[sel.firstColumn], borderFormat, ['borderTop'] ); break; } // Single row selection Iif (singleRow) { applyBorderFormat( tableModel.rows[sel.firstRow].cells[ isRtl ? sel.lastColumn : sel.firstColumn ], borderFormat, ['borderRight'] ); for ( let colIndex = sel.firstColumn + 1; colIndex <= sel.lastColumn - 1; colIndex++ ) { const cell = tableModel.rows[sel.firstRow].cells[colIndex]; applyBorderFormat(cell, borderFormat, [ 'borderLeft', 'borderRight', ]); } applyBorderFormat( tableModel.rows[sel.firstRow].cells[ isRtl ? sel.firstColumn : sel.lastColumn ], borderFormat, ['borderLeft'] ); break; } // For multiple rows and columns selections // Top left cell applyBorderFormat( tableModel.rows[sel.firstRow].cells[ isRtl ? sel.lastColumn : sel.firstColumn ], borderFormat, ['borderBottom', 'borderRight'] ); // Top right cell applyBorderFormat( tableModel.rows[sel.firstRow].cells[ isRtl ? sel.firstColumn : sel.lastColumn ], borderFormat, ['borderBottom', 'borderLeft'] ); // Bottom left cell applyBorderFormat( tableModel.rows[sel.lastRow].cells[ isRtl ? sel.lastColumn : sel.firstColumn ], borderFormat, ['borderTop', 'borderRight'] ); // Bottom right cell applyBorderFormat( tableModel.rows[sel.lastRow].cells[ isRtl ? sel.firstColumn : sel.lastColumn ], borderFormat, ['borderTop', 'borderLeft'] ); // First row for ( let colIndex = sel.firstColumn + 1; colIndex < sel.lastColumn; colIndex++ ) { const cell = tableModel.rows[sel.firstRow].cells[colIndex]; applyBorderFormat(cell, borderFormat, [ 'borderBottom', 'borderLeft', 'borderRight', ]); } // Last row for ( let colIndex = sel.firstColumn + 1; colIndex < sel.lastColumn; colIndex++ ) { const cell = tableModel.rows[sel.lastRow].cells[colIndex]; applyBorderFormat(cell, borderFormat, [ 'borderTop', 'borderLeft', 'borderRight', ]); } // First column for ( let rowIndex = sel.firstRow + 1; rowIndex < sel.lastRow; rowIndex++ ) { const cell = tableModel.rows[rowIndex].cells[sel.firstColumn]; applyBorderFormat(cell, borderFormat, [ 'borderTop', 'borderBottom', isRtl ? 'borderLeft' : 'borderRight', ]); } // Last column for ( let rowIndex = sel.firstRow + 1; rowIndex < sel.lastRow; rowIndex++ ) { const cell = tableModel.rows[rowIndex].cells[sel.lastColumn]; applyBorderFormat(cell, borderFormat, [ 'borderTop', 'borderBottom', isRtl ? 'borderRight' : 'borderLeft', ]); } // Inner cells sel.firstColumn++; sel.firstRow++; sel.lastColumn--; sel.lastRow--; operations.push('allBorders'); break; case 'outsideBorders': // Format cells - Outside borders operations.push('topBorders'); operations.push('bottomBorders'); operations.push('leftBorders'); operations.push('rightBorders'); break; default: break; } } //Format perimeter if necessary or possible modifyPerimeter(tableModel, sel, borderFormat, perimeter, isRtl); } const tableMeta = getTableMetadata(tableModel); Iif (tableMeta) { // Enforce first column format if necessary setFirstColumnFormatBorders(mutateBlock(tableModel).rows, tableMeta); } return true; } else { return false; } }, { apiName: 'tableBorder', } ); } /** * @internal * Apply border format to a cell * @param cell The cell to apply border format * @param borderFormat The border format to apply * @param positions The positions to apply */ function applyBorderFormat( cell: ReadonlyContentModelTableCell, borderFormat: string, positions: BorderPositions[] ) { const mutableCell = mutateBlock(cell); positions.forEach(pos => { mutableCell.format[pos] = borderFormat; }); updateTableCellMetadata(mutableCell, metadata => { metadata = metadata || {}; metadata.borderOverride = true; return metadata; }); } /** * @internal * Modify the perimeter of the table selection * @param tableModel The table model * @param sel The table selection * @param borderFormat The border format to apply * If borderFormat is empty, the border will be removed * @param perimeter Where in the perimeter to apply */ function modifyPerimeter( tableModel: ReadonlyContentModelTable, sel: TableSelectionCoordinates, borderFormat: string, perimeter: Perimeter, isRtl: boolean ) { // Top of selection if (perimeter.Top && sel.firstRow - 1 >= 0) { for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) { const cell = tableModel.rows[sel.firstRow - 1].cells[colIndex]; applyBorderFormat(cell, borderFormat, ['borderBottom']); } } // Bottom of selection if (perimeter.Bottom && sel.lastRow + 1 < tableModel.rows.length) { for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) { const cell = tableModel.rows[sel.lastRow + 1].cells[colIndex]; applyBorderFormat(cell, borderFormat, ['borderTop']); } } // Left of selection if (perimeter.Left && sel.firstColumn - 1 >= 0) { for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) { const cell = tableModel.rows[rowIndex].cells[sel.firstColumn - 1]; applyBorderFormat(cell, borderFormat, [isRtl ? 'borderLeft' : 'borderRight']); } } // Right of selection if (perimeter.Right && sel.lastColumn + 1 < tableModel.rows[0].cells.length) { for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) { const cell = tableModel.rows[rowIndex].cells[sel.lastColumn + 1]; applyBorderFormat(cell, borderFormat, [isRtl ? 'borderRight' : 'borderLeft']); } } } |