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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 6x 6x 6x 6x 1x 1x 1x 2x 12x 4x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 9x 9x 9x 7x 9x 7x 7x 9x 9x 9x 9x 9x 8x 8x 8x 1x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 1x 6x 6x 6x 1x 6x 2x 2x 6x 1x 6x 2x 2x 6x 2x 2x 1x 6x 6x 6x 7x 7x 7x 7x 1x 7x 7x 7x 1x 1x 1x 1x 1x 7x 9x 1x 29x 1x | import { createCellResizer } from './features/CellResizer'; import { createTableInserter } from './features/TableInserter'; import { createTableMover } from './features/TableMover'; import { createTableResizer } from './features/TableResizer'; import { disposeTableEditFeature } from './features/TableEditFeature'; import { isNodeOfType, normalizeRect, parseTableCells } from 'roosterjs-content-model-dom'; import type { OnTableEditorCreatedCallback } from '../OnTableEditorCreatedCallback'; import type { TableEditFeature } from './features/TableEditFeature'; import type { IEditor, TableSelection } from 'roosterjs-content-model-types'; import type { TableEditFeatureName } from './features/TableEditFeatureName'; const INSERTER_HOVER_OFFSET = 6; const enum TOP_OR_SIDE { top = 0, side = 1, } /** * @internal * * A table has 6 hot areas to be resized/edited (take LTR example): * * [6] [ ] * +[ 1 ]+--------------------+ * |[ ]| | * [ ] [ ] | * [ ] [ ] | * [2] [3] | * [ ] [ ] | * [ ][ 4 ]| | * +------------------+--------------------+ * | | | * | | | * | | | * +------------------+--------------------+ * [5] * * 1 - Hover area to show insert column button * 2 - Hover area to show insert row button * 3 - Hover area to show vertical resizing bar * 4 - Hover area to show horizontal resizing bar * 5 - Hover area to show whole table resize handle * 6 - Hover area to show whole table mover handle * * When set a different current table or change current TD, we need to update these areas */ export class TableEditor { // 1, 2 - Insert a column or a row private horizontalInserter: TableEditFeature | null = null; private verticalInserter: TableEditFeature | null = null; // 3, 4 - Resize a column or a row from a cell private horizontalResizer: TableEditFeature | null = null; private verticalResizer: TableEditFeature | null = null; // 5 - Resize whole table private tableResizer: TableEditFeature | null = null; // 6 - Move as well as select whole table private tableMover: TableEditFeature | null = null; private isRTL: boolean; private range: Range | null = null; private isCurrentlyEditing: boolean; constructor( private editor: IEditor, public readonly table: HTMLTableElement, private onChanged: () => void, private anchorContainer?: HTMLElement, private contentDiv?: EventTarget | null, private onTableEditorCreated?: OnTableEditorCreatedCallback, private disableFeatures?: TableEditFeatureName[] ) { this.isRTL = editor.getDocument().defaultView?.getComputedStyle(table).direction == 'rtl'; this.setEditorFeatures(); this.isCurrentlyEditing = false; } dispose() { this.disposeTableResizer(); this.disposeCellResizers(); this.disposeTableInserter(); this.disposeTableMover(); } isEditing(): boolean { return this.isCurrentlyEditing; } isOwnedElement(node: Node) { return [ this.tableResizer, this.tableMover, this.horizontalInserter, this.verticalInserter, this.horizontalResizer, this.verticalResizer, ] .filter(feature => !!feature?.div) .some(feature => feature?.div == node); } /** * public only for testing purposes */ public onMouseMove(x: number, y: number) { // Get whole table rect const tableRect = normalizeRect(this.table.getBoundingClientRect()); Iif (!tableRect) { return; } // Determine if cursor is on top or side const topOrSide = y <= tableRect.top + INSERTER_HOVER_OFFSET ? TOP_OR_SIDE.top : this.isRTL ? x >= tableRect.right - INSERTER_HOVER_OFFSET ? TOP_OR_SIDE.side : undefined : x <= tableRect.left + INSERTER_HOVER_OFFSET ? TOP_OR_SIDE.side : undefined; const topOrSideBinary = topOrSide ? 1 : 0; // i is row index, j is column index for (let i = 0; i < this.table.rows.length; i++) { const tr = this.table.rows[i]; let j = 0; for (; j < tr.cells.length; j++) { const td = tr.cells[j]; const tdRect = normalizeRect(td.getBoundingClientRect()); Iif (!tdRect || !tableRect) { continue; } // Determine the cell the cursor is in range of // Offset is only used for first row and column const lessThanBottom = y <= tdRect.bottom; const lessThanRight = this.isRTL ? x <= tdRect.right + INSERTER_HOVER_OFFSET * topOrSideBinary : x <= tdRect.right; const moreThanLeft = this.isRTL ? x >= tdRect.left : x >= tdRect.left - INSERTER_HOVER_OFFSET * topOrSideBinary; Eif (lessThanBottom && lessThanRight && moreThanLeft) { Iif (i === 0 && topOrSide == TOP_OR_SIDE.top) { const center = (tdRect.left + tdRect.right) / 2; const isOnRightHalf = this.isRTL ? x < center : x > center; !this.isFeatureDisabled('VerticalTableInserter') && this.setInserterTd( isOnRightHalf ? td : tr.cells[j - 1], false /*isHorizontal*/ ); } else Eif (j === 0 && topOrSide == TOP_OR_SIDE.side) { const tdAbove = this.table.rows[i - 1]?.cells[0]; const tdAboveRect = tdAbove ? normalizeRect(tdAbove.getBoundingClientRect()) : null; const isTdNotAboveMerged = !tdAboveRect ? null : this.isRTL ? tdAboveRect.right === tdRect.right : tdAboveRect.left === tdRect.left; !this.isFeatureDisabled('HorizontalTableInserter') && this.setInserterTd( y < (tdRect.top + tdRect.bottom) / 2 && isTdNotAboveMerged ? tdAbove : td, true /*isHorizontal*/ ); } else { this.setInserterTd(null); } !this.isFeatureDisabled('CellResizer') && this.setResizingTd(td); //Cell found break; } } Eif (j < tr.cells.length) { break; } } // Create Mover and Resizer this.setEditorFeatures(); } private setEditorFeatures() { const disableSelector = this.isFeatureDisabled('TableSelector'); const disableMovement = this.isFeatureDisabled('TableMover'); if (!this.tableMover && !(disableSelector && disableMovement)) { this.tableMover = createTableMover( this.table, this.editor, this.isRTL, disableSelector ? () => {} : this.onSelect, this.onStartTableMove, this.onEndTableMove, this.contentDiv, this.anchorContainer, this.onEditorCreated, disableMovement ); } if (!this.tableResizer && !this.isFeatureDisabled('TableResizer')) { this.tableResizer = createTableResizer( this.table, this.editor, this.isRTL, this.onStartTableResize, this.onFinishEditing, this.contentDiv, this.anchorContainer, this.onTableEditorCreated ); } } private onEditorCreated = (featureType: TableEditFeatureName, element: HTMLElement) => { const disposer = this.onTableEditorCreated?.(featureType, element); const onMouseOut = element && this.getOnMouseOut(element); Eif (onMouseOut) { element.addEventListener('mouseout', onMouseOut); } return () => { disposer?.(); Eif (onMouseOut) { element.removeEventListener('mouseout', onMouseOut); } }; }; private setResizingTd(td: HTMLTableCellElement) { Iif (this.horizontalResizer && this.horizontalResizer.node != td) { this.disposeCellResizers(); } Eif (!this.horizontalResizer && td) { this.horizontalResizer = createCellResizer( this.editor, td, this.table, this.isRTL, true /*isHorizontal*/, this.onStartCellResize, this.onFinishEditing, this.anchorContainer ); this.verticalResizer = createCellResizer( this.editor, td, this.table, this.isRTL, false /*isHorizontal*/, this.onStartCellResize, this.onFinishEditing, this.anchorContainer ); } } /** * create or remove TableInserter * @param td td to attach to, set this to null to remove inserters (both horizontal and vertical) */ private setInserterTd(td: HTMLTableCellElement | null, isHorizontal?: boolean) { const inserter = isHorizontal ? this.horizontalInserter : this.verticalInserter; Iif (td === null || (inserter && inserter.node != td)) { this.disposeTableInserter(); } Eif (!this.horizontalInserter && !this.verticalInserter && td) { const newInserter = createTableInserter( this.editor, td, this.table, this.isRTL, !!isHorizontal, this.onInserted, this.anchorContainer, this.onEditorCreated ); Eif (isHorizontal) { this.horizontalInserter = newInserter; } else { this.verticalInserter = newInserter; } } } private disposeTableResizer() { Eif (this.tableResizer) { disposeTableEditFeature(this.tableResizer); this.tableResizer = null; } } private disposeTableInserter() { if (this.horizontalInserter) { disposeTableEditFeature(this.horizontalInserter); this.horizontalInserter = null; } Iif (this.verticalInserter) { disposeTableEditFeature(this.verticalInserter); this.verticalInserter = null; } } private disposeCellResizers() { if (this.horizontalResizer) { disposeTableEditFeature(this.horizontalResizer); this.horizontalResizer = null; } if (this.verticalResizer) { disposeTableEditFeature(this.verticalResizer); this.verticalResizer = null; } } private disposeTableMover() { Eif (this.tableMover) { disposeTableEditFeature(this.tableMover); this.tableMover = null; } } private onFinishEditing = (): false => { this.editor.focus(); if (this.range) { this.editor.setDOMSelection({ type: 'range', range: this.range, isReverted: false }); this.range = null; } this.editor.takeSnapshot(); // Pass in an empty callback to make sure ContentChangedEvent is triggered this.onChanged(); this.isCurrentlyEditing = false; return false; }; private onStartTableResize = () => { this.isCurrentlyEditing = true; this.onStartResize(); }; private onStartCellResize = () => { this.isCurrentlyEditing = true; this.disposeTableResizer(); this.onStartResize(); }; private onStartTableMove = () => { this.isCurrentlyEditing = true; this.disposeTableResizer(); this.disposeTableInserter(); this.disposeCellResizers(); }; private onStartResize() { this.isCurrentlyEditing = true; const range = this.editor.getDOMSelection(); if (range && range.type == 'range') { this.range = range.range; } this.editor.takeSnapshot(); } private onEndTableMove = (disposeHandler: boolean) => { if (disposeHandler) { this.disposeTableMover(); } return this.onFinishEditing(); }; private onInserted = () => { this.disposeTableResizer(); this.onFinishEditing(); }; /** * Public only for testing purposes * @param table the table to select */ public onSelect = (table: HTMLTableElement) => { this.editor.focus(); Eif (table) { const parsedTable = parseTableCells(table); const selection: TableSelection = { table: table, firstRow: 0, firstColumn: 0, lastRow: parsedTable.length - 1, lastColumn: (parsedTable[0]?.length ?? 0) - 1, type: 'table', }; this.editor.setDOMSelection(selection); } }; private getOnMouseOut = (feature: HTMLElement) => { return (ev: MouseEvent) => { if ( feature && ev.relatedTarget != feature && isNodeOfType(this.contentDiv as Node, 'ELEMENT_NODE') && isNodeOfType(ev.relatedTarget as Node, 'ELEMENT_NODE') && !(this.contentDiv == ev.relatedTarget) && !this.isEditing() ) { this.dispose(); } }; }; private isFeatureDisabled(feature: TableEditFeatureName) { return this.disableFeatures?.includes(feature); } } |