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 | 1x 1x 1x 1x 1x 1x 1x 1x 62x 34x 34x 34x 34x 34x 34x 34x 34x 34x 36x 36x 30x 4x 26x 36x 28x 28x 37x 35x 35x 23x 14x 23x 35x 35x 37x 37x 37x 52x 37x 37x 4x 3x 3x 1x 1x 28x 36x 11x 12x 4x 3x 3x 7x 7x 34x 18x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 18x 35x 35x 35x 35x 28x 28x 28x 28x 28x 28x 28x 28x 36x 36x 36x 36x 1x 1x 1x 1x 36x 7x 7x 7x 16x 7x 1x 3x 3x 3x 3x 3x 3x 3x 3x 668x 1301x 668x 528x 253x 140x 5x 2x 2x 3x 2x 1x 3x 18x 18x 18x 15x 3x 35x | import { extractBorderValues } from '../../domUtils/style/borderValues'; import { getClosestAncestorBlockGroupIndex } from './getClosestAncestorBlockGroupIndex'; import { getImageMetadata } from '../metadata/updateImageMetadata'; import { getTableMetadata } from '../metadata/updateTableMetadata'; import { isBold } from '../../domUtils/style/isBold'; import { iterateSelections } from '../selection/iterateSelections'; import { parseValueWithUnit } from '../../formatHandlers/utils/parseValueWithUnit'; import type { ConflictFormatSolution, ContentModelFormatState, ContentModelSegmentFormat, ReadonlyContentModelBlockGroup, ReadonlyContentModelBlock, ReadonlyContentModelImage, ReadonlyTableSelectionContext, ReadonlyContentModelParagraph, ReadonlyContentModelFormatContainer, ReadonlyContentModelListItem, ReadonlyContentModelDocument, DOMHelper, DarkColorHandler, } from 'roosterjs-content-model-types'; /** * Retrieve format state from the given Content Model * @param model The Content Model to retrieve format state from * @param pendingFormat Existing pending format, if any * @param formatState Existing format state object, used for receiving the result * @param conflictSolution The strategy for handling format conflicts * @param domHelper Optional DOMHelper to retrieve container format * @param isInDarkMode Optional flag to indicate if the environment is in dark mode * @param colorHandler Optional DarkColorHandler to handle dark mode colors * */ export function retrieveModelFormatState( model: ReadonlyContentModelDocument, pendingFormat: ContentModelSegmentFormat | null, formatState: ContentModelFormatState, conflictSolution: ConflictFormatSolution = 'remove', domHelper?: DOMHelper, isInDarkMode?: boolean, colorHandler?: DarkColorHandler ) { let firstTableContext: ReadonlyTableSelectionContext | undefined; let firstBlock: ReadonlyContentModelBlock | undefined; let isFirst = true; let isFirstImage = true; let isFirstSegment = true; let containerFormat: ContentModelSegmentFormat | undefined = undefined; const modelFormat = { ...model.format }; delete modelFormat.italic; delete modelFormat.underline; delete modelFormat.fontWeight; iterateSelections( model, (path, tableContext, block, segments) => { // Structure formats retrieveStructureFormat(formatState, path, isFirst, conflictSolution); // Multiple line format if (block) { if (firstBlock) { formatState.isMultilineSelection = true; } else { firstBlock = block; } } if (block?.blockType == 'Paragraph') { // Paragraph formats retrieveParagraphFormat(formatState, block, isFirst, conflictSolution); // Segment formats segments?.forEach(segment => { if (isFirstSegment || segment.segmentType != 'SelectionMarker') { let currentFormat = Object.assign( {}, block.format, block.decorator?.format, segment.format, segment.code?.format, segment.link?.format, pendingFormat ); // Sometimes the content may not specify all required format but just leverage the container format to do so. // In this case, we need to merge the container format into the current format // to make sure the current format contains all required format. if (!hasAllRequiredFormat(currentFormat)) { if (!containerFormat) { containerFormat = domHelper?.getContainerFormat(isInDarkMode, colorHandler) ?? modelFormat; } currentFormat = Object.assign({}, containerFormat, currentFormat); } retrieveSegmentFormat( formatState, isFirst, currentFormat, conflictSolution ); mergeValue( formatState, 'isCodeInline', !!segment?.code, isFirst, conflictSolution ); } // We only care the format of selection marker when it is the first selected segment. This is because when selection marker // is after some other selected segments, it mostly like appears at the beginning of a seconde line when the whole first line // is selected (e.g. triple-click on a line) then the second selection marker doesn't contain a correct format, so we need to // ignore it isFirstSegment = false; formatState.canUnlink = formatState.canUnlink || !!segment.link; formatState.canAddImageAltText = formatState.canAddImageAltText || segments.some(segment => segment.segmentType == 'Image'); isFirst = false; if (segment.segmentType === 'Image') { if (isFirstImage) { retrieveImageFormat(segment, formatState); isFirstImage = false; } else { formatState.imageFormat = undefined; formatState.imageEditingMetadata = undefined; } } }); isFirst = false; } if (tableContext) { if (firstTableContext) { const { table, colIndex, rowIndex } = firstTableContext; // Merge table format if ( tableContext.table == table && (tableContext.colIndex != colIndex || tableContext.rowIndex != rowIndex) ) { formatState.canMergeTableCell = true; formatState.isMultilineSelection = true; } } else { // Table formats retrieveTableFormat(tableContext, formatState); firstTableContext = tableContext; } } }, { includeListFormatHolder: 'never', } ); if (formatState.fontSize) { formatState.fontSize = px2Pt(formatState.fontSize); } } function retrieveSegmentFormat( result: ContentModelFormatState, isFirst: boolean, mergedFormat: ContentModelSegmentFormat, IconflictSolution: ConflictFormatSolution = 'remove' ) { const superOrSubscript = mergedFormat.superOrSubScriptSequence?.split(' ')?.pop(); mergeValue(result, 'isBold', isBold(mergedFormat.fontWeight), isFirst, conflictSolution); mergeValue(result, 'isItalic', mergedFormat.italic, isFirst, conflictSolution); mergeValue(result, 'isUnderline', mergedFormat.underline, isFirst, conflictSolution); mergeValue(result, 'isStrikeThrough', mergedFormat.strikethrough, isFirst, conflictSolution); mergeValue(result, 'isSuperscript', superOrSubscript == 'super', isFirst, conflictSolution); mergeValue(result, 'isSubscript', superOrSubscript == 'sub', isFirst, conflictSolution); mergeValue(result, 'letterSpacing', mergedFormat.letterSpacing, isFirst, conflictSolution); mergeValue(result, 'fontName', mergedFormat.fontFamily, isFirst, conflictSolution); mergeValue( result, 'fontSize', mergedFormat.fontSize, isFirst, conflictSolution, val => parseValueWithUnit(val, undefined, 'pt') + 'pt' ); mergeValue(result, 'backgroundColor', mergedFormat.backgroundColor, isFirst, conflictSolution); mergeValue(result, 'textColor', mergedFormat.textColor, isFirst, conflictSolution); mergeValue(result, 'fontWeight', mergedFormat.fontWeight, isFirst, conflictSolution); mergeValue(result, 'lineHeight', mergedFormat.lineHeight, isFirst, conflictSolution); } function retrieveParagraphFormat( result: ContentModelFormatState, paragraph: ReadonlyContentModelParagraph, isFirst: boolean, IconflictSolution: ConflictFormatSolution = 'remove' ) { const headingLevel = parseInt((paragraph.decorator?.tagName || '').substring(1)); const validHeadingLevel = headingLevel >= 1 && headingLevel <= 6 ? headingLevel : undefined; mergeValue(result, 'marginBottom', paragraph.format.marginBottom, isFirst, conflictSolution); mergeValue(result, 'marginTop', paragraph.format.marginTop, isFirst, conflictSolution); mergeValue(result, 'headingLevel', validHeadingLevel, isFirst, conflictSolution); mergeValue(result, 'textAlign', paragraph.format.textAlign, isFirst, conflictSolution); mergeValue(result, 'direction', paragraph.format.direction, isFirst, conflictSolution); } function retrieveStructureFormat( result: ContentModelFormatState, path: ReadonlyContentModelBlockGroup[], isFirst: boolean, IconflictSolution: ConflictFormatSolution = 'remove' ) { const listItemIndex = getClosestAncestorBlockGroupIndex(path, ['ListItem'], []); const containerIndex = getClosestAncestorBlockGroupIndex(path, ['FormatContainer'], []); if (listItemIndex >= 0) { const listItem = path[listItemIndex] as ReadonlyContentModelListItem; const listType = listItem?.levels[listItem.levels.length - 1]?.listType; mergeValue(result, 'isBullet', listType == 'UL', isFirst, conflictSolution); mergeValue(result, 'isNumbering', listType == 'OL', isFirst, conflictSolution); } mergeValue( result, 'isBlockQuote', containerIndex >= 0 && (path[containerIndex] as ReadonlyContentModelFormatContainer)?.tagName == 'blockquote', isFirst, conflictSolution ); } function retrieveTableFormat( tableContext: ReadonlyTableSelectionContext, result: ContentModelFormatState ) { const tableFormat = getTableMetadata(tableContext.table); result.isInTable = true; result.tableHasHeader = tableContext.table.rows.some(row => row.cells.some(cell => cell.isHeader) ); if (tableFormat) { result.tableFormat = tableFormat; } } function retrieveImageFormat(image: ReadonlyContentModelImage, result: ContentModelFormatState) { const { format } = image; const borderKey = 'borderTop'; const extractedBorder = extractBorderValues(format[borderKey]); const borderColor = extractedBorder.color; const borderWidth = extractedBorder.width; const borderStyle = extractedBorder.style; result.imageFormat = { borderColor, borderWidth, borderStyle, boxShadow: format.boxShadow, borderRadius: format.borderRadius, }; result.imageEditingMetadata = getImageMetadata(image); } function mergeValue<K extends keyof ContentModelFormatState>( format: ContentModelFormatState, key: K, newValue: ContentModelFormatState[K] | undefined, isFirst: boolean, IconflictSolution: ConflictFormatSolution = 'remove', parseFn: (val: ContentModelFormatState[K]) => ContentModelFormatState[K] = val => val ) { if (isFirst) { if (newValue !== undefined) { format[key] = newValue; } } else if (parseFn(newValue) !== parseFn(format[key])) { switch (conflictSolution) { case 'remove': delete format[key]; break; case 'keepFirst': break; case 'returnMultiple': if (typeof format[key] === 'string') { (format[key] as string) = 'Multiple'; } else { delete format[key]; } break; } } } function px2Pt(px: string) { Eif (px) { const index = px.indexOf('px'); if (index !== -1 && index === px.length - 2) { // Edge may not handle the floating computing well which causes the calculated value to be a little less than the actual value // So add 0.05 to fix it return Math.round(parseFloat(px) * 75 + 0.05) / 100 + 'pt'; } } return px; } function hasAllRequiredFormat(format: ContentModelSegmentFormat) { return !!format.fontFamily && !!format.fontSize && !!format.textColor; } |