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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 11x 11x 1x 8x 8x 8x 1x 11x 11x 11x 11x 13x 13x 8x 8x 5x 10x 5x 5x 5x 5x 5x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 36x 9x 9x 9x 9x 2x 2x 2x 7x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { createElement } from '../../../pluginUtils/CreateElement/createElement'; import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper'; import { formatInsertPointWithContentModel } from 'roosterjs-content-model-api'; import { getCMTableFromTable } from '../utils/getTableFromContentModel'; import type { TableEditFeature } from './TableEditFeature'; import type { OnTableEditorCreatedCallback } from '../../OnTableEditorCreatedCallback'; import type { DragAndDropHandler } from '../../../pluginUtils/DragAndDrop/DragAndDropHandler'; import { cloneModel, createContentModelDocument, createSelectionMarker, getFirstSelectedTable, isNodeOfType, mergeModel, mutateBlock, normalizeRect, setParagraphNotImplicit, setSelection, } from 'roosterjs-content-model-dom'; import type { DOMInsertPoint, DOMSelection, IEditor, ReadonlyContentModelTable, Rect, ShallowMutableContentModelDocument, } from 'roosterjs-content-model-types'; const TABLE_MOVER_LENGTH = 12; /** * @internal */ export const TABLE_MOVER_ID = '_Table_Mover'; const TABLE_MOVER_STYLE_KEY = '_TableMoverCursorStyle'; /** * @internal * Allows user to move table to another position * Contains the function to select whole table */ export function createTableMover( table: HTMLTableElement, editor: IEditor, isRTL: boolean, onFinishDragging: (table: HTMLTableElement) => void, onStart: () => void, onEnd: (disposeHandler: boolean) => void, contentDiv?: EventTarget | null, anchorContainer?: HTMLElement, onTableEditorCreated?: OnTableEditorCreatedCallback, disableMovement?: boolean ): TableEditFeature | null { const rect = normalizeRect(table.getBoundingClientRect()); if (!isTableTopVisible(editor, rect, contentDiv as Node)) { return null; } const zoomScale = editor.getDOMHelper().calculateZoomScale(); const document = table.ownerDocument; const createElementData = { tag: 'div', style: 'position: fixed; cursor: move; user-select: none; border: 1px solid #808080', }; const div = createElement(createElementData, document) as HTMLDivElement; div.id = TABLE_MOVER_ID; div.style.width = `${TABLE_MOVER_LENGTH}px`; div.style.height = `${TABLE_MOVER_LENGTH}px`; (anchorContainer || document.body).appendChild(div); const context: TableMoverContext = { table, zoomScale, rect, isRTL, editor, div, onFinishDragging, onStart, onEnd, disableMovement, }; setDivPosition(context, div); const featureHandler = new TableMoverFeature( div, context, () => {}, disableMovement ? { onDragEnd } : { onDragStart, onDragging, onDragEnd, }, context.zoomScale, onTableEditorCreated, editor.getEnvironment().isMobileOrTablet ); return { node: table, div, featureHandler }; } /** * @internal * Exported for testing */ export interface TableMoverContext { table: HTMLTableElement; zoomScale: number; rect: Rect | null; isRTL: boolean; editor: IEditor; div: HTMLElement; onFinishDragging: (table: HTMLTableElement) => void; onStart: () => void; onEnd: (disposeHandler: boolean) => void; disableMovement?: boolean; } /** * @internal * Exported for testing */ export interface TableMoverInitValue { cmTable: ReadonlyContentModelTable | undefined; initialSelection: DOMSelection | null; tableRect: HTMLDivElement; } class TableMoverFeature extends DragAndDropHelper<TableMoverContext, TableMoverInitValue> { private disposer: undefined | (() => void); constructor( div: HTMLElement, context: TableMoverContext, onSubmit: ( context: TableMoverContext, trigger: HTMLElement, container?: HTMLElement ) => void, handler: DragAndDropHandler<TableMoverContext, TableMoverInitValue>, zoomScale: number, onTableEditorCreated?: OnTableEditorCreatedCallback, forceMobile?: boolean | undefined ) { super(div, context, onSubmit, handler, zoomScale, forceMobile); this.disposer = onTableEditorCreated?.('TableMover', div); } dispose(): void { this.disposer?.(); this.disposer = undefined; super.dispose(); } } function setDivPosition(context: TableMoverContext, trigger: HTMLElement) { const { rect } = context; Eif (rect) { trigger.style.top = `${rect.top - TABLE_MOVER_LENGTH}px`; trigger.style.left = `${rect.left - TABLE_MOVER_LENGTH - 2}px`; } } function isTableTopVisible(editor: IEditor, rect: Rect | null, contentDiv?: Node | null): boolean { const visibleViewport = editor.getVisibleViewport(); if (isNodeOfType(contentDiv, 'ELEMENT_NODE') && visibleViewport && rect) { const containerRect = normalizeRect(contentDiv.getBoundingClientRect()); return !!containerRect && containerRect.top <= rect.top && visibleViewport.top <= rect.top; } return true; } function setTableMoverCursor(editor: IEditor, state: boolean, type?: 'move' | 'copy') { editor?.setEditorStyle(TABLE_MOVER_STYLE_KEY, state ? 'cursor: ' + type ?? 'move' : null); } // Get insertion point from coordinate. function getNodePositionFromEvent(editor: IEditor, x: number, y: number): DOMInsertPoint | null { const doc = editor.getDocument(); const domHelper = editor.getDOMHelper(); Eif (doc.caretRangeFromPoint) { // Chrome, Edge, Safari, Opera const range = doc.caretRangeFromPoint(x, y); if (range && domHelper.isNodeInEditor(range.startContainer)) { return { node: range.startContainer, offset: range.startOffset }; } } Eif ('caretPositionFromPoint' in doc) { // Firefox const pos = (doc as any).caretPositionFromPoint(x, y); Iif (pos && domHelper.isNodeInEditor(pos.offsetNode)) { return { node: pos.offsetNode, offset: pos.offset }; } } Eif (doc.elementFromPoint) { // Fallback const element = doc.elementFromPoint(x, y); Iif (element && domHelper.isNodeInEditor(element)) { return { node: element, offset: 0 }; } } return null; } /** * @internal * Exported for testing */ export function onDragStart(context: TableMoverContext): TableMoverInitValue { context.onStart(); const { editor, table, div } = context; setTableMoverCursor(editor, true, 'move'); // Create table outline rectangle const trect = table.getBoundingClientRect(); const createElementData = { tag: 'div', style: 'position: fixed; user-select: none; border: 1px solid #808080', }; const tableRect = createElement(createElementData, document) as HTMLDivElement; tableRect.style.width = `${trect.width}px`; tableRect.style.height = `${trect.height}px`; tableRect.style.top = `${trect.top}px`; tableRect.style.left = `${trect.left}px`; div.parentNode?.appendChild(tableRect); // Get drag start selection const initialSelection = editor.getDOMSelection(); // Get Table block in content model const cmTable = getCMTableFromTable(editor, table); return { cmTable, initialSelection, tableRect, }; } /** * @internal * Exported for testing */ export function onDragging( context: TableMoverContext, event: MouseEvent, initValue: TableMoverInitValue ) { const { tableRect } = initValue; const { editor } = context; // Move table outline rectangle tableRect.style.top = `${event.clientY + TABLE_MOVER_LENGTH}px`; tableRect.style.left = `${event.clientX + TABLE_MOVER_LENGTH}px`; const pos = getNodePositionFromEvent(editor, event.clientX, event.clientY); if (pos) { const range = editor.getDocument().createRange(); range.setStart(pos.node, pos.offset); range.collapse(true); editor.setDOMSelection({ type: 'range', range, isReverted: false }); return true; } return false; } /** * @internal * Exported for testing */ export function onDragEnd( context: TableMoverContext, event: MouseEvent, initValue: TableMoverInitValue | undefined ) { const { editor, table, onFinishDragging: selectWholeTable, disableMovement } = context; const element = event.target; // Remove table outline rectangle initValue?.tableRect.remove(); // Reset cursor setTableMoverCursor(editor, false); if (element == context.div) { // Table mover was only clicked, select whole table and do not dismiss the handler element. selectWholeTable(table); context.onEnd(false /* disposeHandler */); return true; } else { // Check if table was dragged on itself, element is not in editor, or movement is disabled if ( table.contains(element as Node) || !editor.getDOMHelper().isNodeInEditor(element as Node) || disableMovement ) { editor.setDOMSelection(initValue?.initialSelection ?? null); context.onEnd(true /* disposeHandler */); return false; } let insertionSuccess: boolean = false; // Get position to insert table const insertPosition = getNodePositionFromEvent(editor, event.clientX, event.clientY); Eif (insertPosition) { // Move table to new position formatInsertPointWithContentModel( editor, insertPosition, (model, context, ip) => { // Remove old table const [oldTable, path] = getFirstSelectedTable(model); Eif (oldTable) { const index = path[0].blocks.indexOf(oldTable); mutateBlock(path[0]).blocks.splice(index, 1); } Eif (ip && initValue?.cmTable) { // Insert new table const doc: ShallowMutableContentModelDocument = createContentModelDocument(); doc.blocks.push(oldTable ?? mutateBlock(initValue.cmTable)); insertionSuccess = !!mergeModel(model, cloneModel(doc), context, { mergeFormat: 'none', insertPosition: ip, }); Eif (insertionSuccess) { // After mergeModel, the new table should be selected const finalTable = getFirstSelectedTable(model)[0] ?? initValue.cmTable; Eif (finalTable) { // Add selection marker to the first cell of the table const firstCell = finalTable.rows[0].cells[0]; const markerParagraph = firstCell?.blocks[0]; Eif (markerParagraph?.blockType == 'Paragraph') { const marker = createSelectionMarker(model.format); mutateBlock(markerParagraph).segments.unshift(marker); setParagraphNotImplicit(markerParagraph); setSelection(model, marker); } } } return insertionSuccess; } }, { // Select first cell of the old table selectionOverride: { type: 'table', firstColumn: 0, firstRow: 0, lastColumn: 0, lastRow: 0, table: table, }, apiName: 'TableMover', } ); } else { // No movement, restore initial selection editor.setDOMSelection(initValue?.initialSelection ?? null); } context.onEnd(true /* disposeHandler */); return insertionSuccess; } } |