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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 102x 102x 102x 7x 7x 7x 102x 100x 37x 37x 100x 123x 123x 88x 88x 6x 6x 1x 1x 1x 18x 9x 9x 18x 10x 3x 3x 7x 7x 10x 102x 102x 176x 88x 88x 88x 17x 32x 17x 88x 88x 117x 117x 117x 100x 2x 100x 21x 88x 10x 10x 4x 4x 4x 13x 4x 88x 25x 63x 18x 9x 15x 5x 5x 9x 17x 17x 3x 10x 10x 17x 2x 2x 2x 7x 7x 17x 17x 17x 5x 5x 5x 5x 5x 5x 5x 5x 4x 7x 14x 7x 7x 7x 7x 7x 7x 7x 2x 2x 165x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 4x 4x 4x 4x 4x 4x 4x 4x 4x 55x 55x 23x 23x 23x 23x 23x 23x 47x 54x 54x 1x 1x 1x 1x 3x 3x 9x 3x 50x 50x 50x 65x 65x 65x 11x 50x 13x 50x 54x 1x 11x 11x 11x 11x 11x 7x 2x 2x 66x 32x 13x 21x 23x 69x 23x 5x 23x 5x 23x 9x 23x 23x 253x 23x 2x 2x 1x 2x | import { addBlock } from '../common/addBlock'; import { addSegment } from '../common/addSegment'; import { applyTableFormat } from './applyTableFormat'; import { createListItem } from '../creators/createListItem'; import { createParagraph } from '../creators/createParagraph'; import { createSelectionMarker } from '../creators/createSelectionMarker'; import { createTableCell } from '../creators/createTableCell'; import { deleteSelection } from './deleteSelection'; import { EmptySegmentFormat } from '../../constants/EmptySegmentFormat'; import { getClosestAncestorBlockGroupIndex } from './getClosestAncestorBlockGroupIndex'; import { getObjectKeys } from '../..//domUtils/getObjectKeys'; import { mutateBlock } from '../common/mutate'; import { normalizeContentModel } from '../common/normalizeContentModel'; import { normalizeTable } from './normalizeTable'; import type { ContentModelBlock, ContentModelBlockFormat, ContentModelDocument, ContentModelHyperLinkFormat, ContentModelListItem, ContentModelParagraph, ContentModelSegmentFormat, ContentModelTable, FormatContentModelContext, InsertPoint, MergeModelOption, ReadonlyContentModelBlock, ReadonlyContentModelBlockGroup, ReadonlyContentModelDocument, ShallowMutableContentModelParagraph, } from 'roosterjs-content-model-types'; const HeadingTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; const KeysOfSegmentFormat = getObjectKeys(EmptySegmentFormat); type MergeFormatTypes = 'mergeAll' | 'keepSourceEmphasisFormat' | 'preferSource' | 'preferTarget'; /** * Merge source model into target mode * @param target Target Content Model that will merge content into * @param source Source Content Model will be merged to target model * @param context Format context. When call this function inside formatContentModel, provide this context so that formatContentModel will do extra handling to the result * @param options More options, see MergeModelOption * @returns Insert point after merge, or null if there is no insert point */ export function mergeModel( target: ReadonlyContentModelDocument, source: ContentModelDocument, context?: FormatContentModelContext, options?: MergeModelOption ): InsertPoint | null { const insertPosition = options?.insertPosition ?? deleteSelection(target, [], context).insertPoint; const { addParagraphAfterMergedContent, mergeFormat, mergeTable } = options || {}; if (addParagraphAfterMergedContent && !mergeTable) { const { paragraph, marker } = insertPosition || {}; const newPara = createParagraph(false /* isImplicit */, paragraph?.format, marker?.format); addBlock(source, newPara); } if (insertPosition) { if (mergeFormat && mergeFormat != 'none') { const newFormat: ContentModelSegmentFormat = { ...(target.format || {}), ...insertPosition.marker.format, }; applyDefaultFormat(source, newFormat, mergeFormat); } for (let i = 0; i < source.blocks.length; i++) { const block = source.blocks[i]; switch (block.blockType) { case 'Paragraph': mergeParagraph(insertPosition, block, i == 0, context, options); break; case 'Divider': insertBlock(insertPosition, block); break; case 'Entity': insertBlock(insertPosition, block); context?.newEntities.push(block); break; case 'Table': if (source.blocks.length == 1 && mergeTable) { mergeTables(insertPosition, block, source); } else { insertBlock(insertPosition, block); } break; case 'BlockGroup': switch (block.blockGroupType) { case 'General': case 'FormatContainer': insertBlock(insertPosition, block); break; case 'ListItem': mergeList(insertPosition, block); break; } break; } } } normalizeContentModel(target); return insertPosition; } function mergeParagraph( markerPosition: InsertPoint, newPara: ContentModelParagraph, mergeToCurrentParagraph: boolean, context?: FormatContentModelContext, option?: MergeModelOption ) { const { paragraph, marker } = markerPosition; const newParagraph = mergeToCurrentParagraph ? paragraph : splitParagraph(markerPosition, newPara.format); const segmentIndex = newParagraph.segments.indexOf(marker); if (option?.mergeFormat == 'none' && mergeToCurrentParagraph) { newParagraph.segments.forEach(segment => { segment.format = { ...(newParagraph.segmentFormat || {}), ...segment.format }; }); delete newParagraph.segmentFormat; } Eif (segmentIndex >= 0) { for (let i = 0; i < newPara.segments.length; i++) { const segment = newPara.segments[i]; newParagraph.segments.splice(segmentIndex + i, 0, segment); if (context) { if (segment.segmentType == 'Entity') { context.newEntities.push(segment); } if (segment.segmentType == 'Image') { context.newImages.push(segment); } } } } if (newPara.decorator) { newParagraph.decorator = { ...newPara.decorator }; if (HeadingTags.indexOf(newParagraph.decorator.tagName) > -1) { const sourceKeys: (keyof ContentModelSegmentFormat)[] = getObjectKeys( newParagraph.decorator.format ); const segmentDecoratorKeys: (keyof ContentModelSegmentFormat)[] = getObjectKeys( newParagraph.segmentFormat || {} ); sourceKeys.forEach(key => { if (segmentDecoratorKeys.indexOf(key) > -1) { delete newParagraph.segmentFormat?.[key]; } }); } } if (!mergeToCurrentParagraph) { newParagraph.format = newPara.format; } else { newParagraph.format = { ...newParagraph.format, ...newPara.format, }; } } function mergeTables( markerPosition: InsertPoint, newTable: ContentModelTable, source: ContentModelDocument ) { const { tableContext, marker } = markerPosition; if (tableContext && source.blocks.length == 1 && source.blocks[0] == newTable) { const { table: readonlyTable, colIndex, rowIndex } = tableContext; const table = mutateBlock(readonlyTable); for (let i = 0; i < newTable.rows.length; i++) { for (let j = 0; j < newTable.rows[i].cells.length; j++) { const newCell = newTable.rows[i].cells[j]; if (i == 0 && colIndex + j >= table.rows[0].cells.length) { for (let k = 0; k < table.rows.length; k++) { const leftCell = table.rows[k]?.cells[colIndex + j - 1]; table.rows[k].cells[colIndex + j] = createTableCell( false /*spanLeft*/, false /*spanAbove*/, leftCell?.isHeader, leftCell?.format ); } } if (j == 0 && rowIndex + i >= table.rows.length) { Eif (!table.rows[rowIndex + i]) { table.rows[rowIndex + i] = { cells: [], format: {}, height: 0, }; } for (let k = 0; k < table.rows[rowIndex].cells.length; k++) { const aboveCell = table.rows[rowIndex + i - 1]?.cells[k]; table.rows[rowIndex + i].cells[k] = createTableCell( false /*spanLeft*/, false /*spanAbove*/, false /*isHeader*/, aboveCell?.format ); } } const oldCell = table.rows[rowIndex + i].cells[colIndex + j]; table.rows[rowIndex + i].cells[colIndex + j] = newCell; if (i == 0 && j == 0) { const newMarker = createSelectionMarker(marker.format); const newPara = addSegment(newCell, newMarker); Eif (markerPosition.path[0] == oldCell) { // Update insert point to match the change result markerPosition.path[0] = newCell; markerPosition.marker = newMarker; markerPosition.paragraph = newPara; } } } } normalizeTable(table, markerPosition.marker.format); applyTableFormat(table, undefined /*newFormat*/, true /*keepCellShade*/); } else { insertBlock(markerPosition, newTable); } } function mergeList(markerPosition: InsertPoint, newList: ContentModelListItem) { splitParagraph(markerPosition, newList.format); const { path, paragraph } = markerPosition; const listItemIndex = getClosestAncestorBlockGroupIndex(path, ['ListItem']); const listItem = path[listItemIndex] as ContentModelListItem; const listParent = path[listItemIndex + 1]; // It is ok here when index is -1, that means there is no list and we just insert a new paragraph and use path[0] as its parent const blockIndex = listParent.blocks.indexOf(listItem || paragraph); Eif (blockIndex >= 0) { mutateBlock(listParent).blocks.splice(blockIndex, 0, newList); } if (listItem) { listItem?.levels.forEach((level, i) => { newList.levels[i] = { ...level }; }); } } function splitParagraph(markerPosition: InsertPoint, newParaFormat: ContentModelBlockFormat) { const { paragraph, marker, path } = markerPosition; const segmentIndex = paragraph.segments.indexOf(marker); const paraIndex = path[0].blocks.indexOf(paragraph); const newParagraph: ShallowMutableContentModelParagraph = createParagraph( false /*isImplicit*/, { ...paragraph.format, ...newParaFormat }, paragraph.segmentFormat ); Eif (segmentIndex >= 0) { newParagraph.segments = paragraph.segments.splice(segmentIndex); } Eif (paraIndex >= 0) { mutateBlock(path[0]).blocks.splice(paraIndex + 1, 0, newParagraph); } const listItemIndex = getClosestAncestorBlockGroupIndex( path, ['ListItem'], ['FormatContainer', 'TableCell'] ); const listItem = path[listItemIndex] as ContentModelListItem; if (listItem) { const listParent = listItemIndex >= 0 ? path[listItemIndex + 1] : null; const blockIndex = listParent ? listParent.blocks.indexOf(listItem) : -1; Eif (blockIndex >= 0 && listParent) { const newListItem = createListItem(listItem.levels, listItem.formatHolder.format); Eif (paraIndex >= 0) { newListItem.blocks = listItem.blocks.splice(paraIndex + 1); } Eif (blockIndex >= 0) { mutateBlock(listParent).blocks.splice(blockIndex + 1, 0, newListItem); } path[listItemIndex] = newListItem; } } markerPosition.paragraph = newParagraph; return newParagraph; } function insertBlock(markerPosition: InsertPoint, block: ContentModelBlock) { const { path } = markerPosition; const newParaFormat = block.blockType !== 'Paragraph' ? {} : block.format; const newPara = splitParagraph(markerPosition, newParaFormat); const blockIndex = path[0].blocks.indexOf(newPara); Eif (blockIndex >= 0) { mutateBlock(path[0]).blocks.splice(blockIndex, 0, block); } } function applyDefaultFormat( group: ReadonlyContentModelBlockGroup, format: ContentModelSegmentFormat, applyDefaultFormatOption: MergeFormatTypes ) { group.blocks.forEach(block => { mergeBlockFormat(applyDefaultFormatOption, block); switch (block.blockType) { case 'BlockGroup': Eif (block.blockGroupType == 'ListItem') { mutateBlock(block).formatHolder.format = mergeSegmentFormat( applyDefaultFormatOption, format, block.formatHolder.format ); } applyDefaultFormat(block, format, applyDefaultFormatOption); break; case 'Table': block.rows.forEach(row => row.cells.forEach(cell => { applyDefaultFormat(cell, format, applyDefaultFormatOption); }) ); break; case 'Paragraph': const paragraphFormat = block.decorator?.format || {}; const paragraph = mutateBlock(block); paragraph.segments.forEach(segment => { Iif (segment.segmentType == 'General') { applyDefaultFormat(segment, format, applyDefaultFormatOption); } segment.format = mergeSegmentFormat(applyDefaultFormatOption, format, { ...paragraphFormat, ...segment.format, }); if (segment.link) { segment.link.format = mergeLinkFormat( applyDefaultFormatOption, format, segment.link.format ); } }); if (applyDefaultFormatOption === 'keepSourceEmphasisFormat') { delete paragraph.decorator; } break; } }); } function mergeBlockFormat(applyDefaultFormatOption: string, block: ReadonlyContentModelBlock) { if (applyDefaultFormatOption == 'keepSourceEmphasisFormat' && block.format.backgroundColor) { delete mutateBlock(block).format.backgroundColor; } } /** * Hyperlink format type definition only contains backgroundColor and underline. * So create a minimum object with the styles supported in Hyperlink to be used in merge. */ function getSegmentFormatInLinkFormat( targetFormat: ContentModelSegmentFormat ): ContentModelSegmentFormat { const result: ContentModelHyperLinkFormat = {}; Iif (targetFormat.backgroundColor) { result.backgroundColor = targetFormat.backgroundColor; } Iif (targetFormat.underline) { result.underline = targetFormat.underline; } return result; } function mergeLinkFormat( applyDefaultFormatOption: MergeFormatTypes, targetFormat: ContentModelSegmentFormat, sourceFormat: ContentModelHyperLinkFormat ) { switch (applyDefaultFormatOption) { case 'mergeAll': case 'preferSource': return { ...getSegmentFormatInLinkFormat(targetFormat), ...sourceFormat }; case 'keepSourceEmphasisFormat': return { // Hyperlink segment format contains other attributes such as LinkFormat // so we have to retain them ...getFormatWithoutSegmentFormat(sourceFormat), // Link format only have Text color, background color, Underline, but only // text color + background color should be merged from the target ...getSegmentFormatInLinkFormat(targetFormat), // Get the semantic format of the source ...getSemanticFormat(sourceFormat), // The text color of the hyperlink should not be merged and // we should always retain the source text color ...getHyperlinkTextColor(sourceFormat), }; case 'preferTarget': return { ...sourceFormat, ...getSegmentFormatInLinkFormat(targetFormat) }; } } function mergeSegmentFormat( applyDefaultFormatOption: MergeFormatTypes, targetFormat: ContentModelSegmentFormat, sourceFormat: ContentModelSegmentFormat ): ContentModelSegmentFormat { switch (applyDefaultFormatOption) { case 'mergeAll': case 'preferSource': return { ...targetFormat, ...sourceFormat }; case 'preferTarget': return { ...sourceFormat, ...targetFormat }; case 'keepSourceEmphasisFormat': return { ...getFormatWithoutSegmentFormat(sourceFormat), ...targetFormat, ...getSemanticFormat(sourceFormat), }; } } function getSemanticFormat(segmentFormat: ContentModelSegmentFormat): ContentModelSegmentFormat { const result: ContentModelSegmentFormat = {}; const { fontWeight, italic, underline } = segmentFormat; if (fontWeight && fontWeight != 'normal') { result.fontWeight = fontWeight; } if (italic) { result.italic = italic; } if (underline) { result.underline = underline; } return result; } /** * Segment format can also contain other type of metadata, for example in Images/Hyperlink, * we want to preserve these properties when merging format */ function getFormatWithoutSegmentFormat( sourceFormat: ContentModelSegmentFormat ): ContentModelSegmentFormat { const resultFormat = { ...sourceFormat, }; KeysOfSegmentFormat.forEach(key => delete resultFormat[key]); return resultFormat; } function getHyperlinkTextColor(sourceFormat: ContentModelHyperLinkFormat) { const result: ContentModelHyperLinkFormat = {}; if (sourceFormat.textColor) { result.textColor = sourceFormat.textColor; } return result; } |