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 | 1x 1x 1x 1x 1x 1x 1x 1x 327x 327x 24x 327x 327x 69x 258x 258x 258x 258x 40x 258x 146x 73x 73x 73x 73x 73x 32x 39x 41x 41x 73x 73x 73x 73x 71x 73x 185x 73x 73x 73x 73x 54x 54x 73x 73x 73x 73x 73x 54x 73x 73x 73x 73x 19x 19x 4x 4x 5x 5x 10x 10x 19x 19x 54x 7x 7x 10x 10x 2x 2x 35x 35x 73x 73x 27x 73x 54x 54x 54x 54x 54x 54x 54x 54x 43x 43x 43x 24x 54x 77x 77x 47x 7x | import { removeNegativeTextIndentParser } from './removeNegativeTextIndentParser'; import { addBlock, createListItem, createListLevel, getListStyleTypeFromString, isEmpty, parseFormat, updateListMetadata, } from 'roosterjs-content-model-dom'; import type { WordMetadata } from './WordMetadata'; import type { ContentModelBlockGroup, ContentModelListItem, ContentModelListItemFormat, ContentModelListItemLevelFormat, ContentModelListLevel, DomToModelContext, DomToModelListFormat, } from 'roosterjs-content-model-types'; /** Word list metadata style name */ const MSO_LIST = 'mso-list'; const MSO_LIST_IGNORE = 'ignore'; const WORD_FIRST_LIST = 'l0'; const TEMPLATE_VALUE_REGEX = /%[0-9a-zA-Z]+/g; interface WordDesktopListFormat extends DomToModelListFormat { wordLevel?: number | ''; wordList?: string; wordKnownLevels?: Map<string, ContentModelListLevel[]>; } interface WordListFormat extends ContentModelListItemFormat { wordList?: string; } const BULLET_METADATA = 'bullet'; /** * @internal * @param styles * @param group * @param element * @param context * @returns */ export function processWordList( styles: Record<string, string>, group: ContentModelBlockGroup, element: HTMLElement, context: DomToModelContext, metadata: Map<string, WordMetadata> ) { const listFormat = context.listFormat as WordDesktopListFormat; if (!listFormat.wordKnownLevels) { listFormat.wordKnownLevels = new Map<string, ContentModelListLevel[]>(); } const wordListStyle = styles[MSO_LIST] || ''; // If the element contains Ignore style, do not process it, // Usually this element contains the fake bullet used in Word Desktop. if (wordListStyle.toLowerCase() === MSO_LIST_IGNORE) { return true; } const [lNumber, level] = wordListStyle.split(' '); // Try get the list metadata from word, which follows this format: l1 level1 lfo2 // If we are able to get the level property means we can process this element to be a list listFormat.wordLevel = level && parseInt(level.substr('level'.length)); listFormat.wordList = lNumber || WORD_FIRST_LIST; if (listFormat.levels.length == 0) { listFormat.levels = (listFormat.wordList && listFormat.wordKnownLevels.get(listFormat.wordList)) || []; } if (wordListStyle && group && typeof listFormat.wordLevel === 'number') { const { wordLevel, wordList } = listFormat; // Retrieve the Fake bullet on the element and also the list type const listMetadata = metadata.get(`${lNumber}:${level}`); const listType = listMetadata?.['mso-level-number-format']?.toLowerCase() != BULLET_METADATA ? 'OL' : 'UL'; // Create the new level of the list item and parse the format const newLevel: ContentModelListLevel = createListLevel(listType); parseFormat( element, [...context.formatParsers.listLevel, wordListPaddingParser], newLevel.format, context ); // If the list format is in a different level, update the array so we get the new item // To be in the same level as the provided level metadata. if (wordLevel > listFormat.levels.length) { while (wordLevel != listFormat.levels.length) { listFormat.levels.push(newLevel); } } else { listFormat.levels.splice(wordLevel, listFormat.levels.length - 1); listFormat.levels[wordLevel - 1] = newLevel; } (listFormat.levels[listFormat.levels.length - 1] .format as WordListFormat).wordList = wordList; listFormat.listParent = group; processAsListItem(listFormat, context, element, group, listMetadata); if ( listFormat.levels.length > 0 && listFormat.wordKnownLevels.get(wordList) != listFormat.levels ) { listFormat.wordKnownLevels.set(wordList, [...listFormat.levels]); } return true; } return false; } function processAsListItem( listFormat: WordDesktopListFormat, context: DomToModelContext, element: HTMLElement, group: ContentModelBlockGroup, listMetadata: WordMetadata | undefined ) { const listLevel = listFormat.levels[listFormat.levels.length - 1]; const { listType } = listLevel; const bullet = getBulletFromMetadata(listMetadata, listType); if (bullet) { updateListMetadata(listFormat.levels[listFormat.levels.length - 1], metadata => Object.assign({}, metadata, { unorderedStyleType: listType == 'UL' ? bullet : undefined, orderedStyleType: listType == 'OL' ? bullet : undefined, }) ); } const listItem = createListItem(listFormat.levels, context.segmentFormat); parseFormat(element, context.formatParsers.segmentOnBlock, context.segmentFormat, context); parseFormat(element, context.formatParsers.listItemElement, listItem.format, context); parseFormat(element, [removeNegativeTextIndentParser], listItem.format, context); if (listType == 'OL') { setStartNumber(listItem, context, listMetadata); } context.elementProcessors.child(listItem, element, context); addBlock(group, listItem); } function getBulletFromMetadata(listMetadata: WordMetadata | undefined, listType: 'OL' | 'UL') { const templateType = listMetadata?.['mso-level-number-format'] || 'decimal'; let templateFinal: string; if (listMetadata?.['mso-level-text']) { let templateValue: string = ''; switch (templateType) { case 'alpha-upper': templateValue = 'UpperAlpha'; break; case 'alpha-lower': templateValue = 'LowerAlpha'; break; case 'roman-lower': templateValue = 'LowerRoman'; break; case 'roman-upper': templateValue = 'UpperRoman'; break; default: templateValue = 'Number'; break; } const template = (listMetadata['mso-level-text'] || '') .replace('\\', '') .replace('"', '') .replace(TEMPLATE_VALUE_REGEX, '${' + templateValue + '}'); templateFinal = '"' + template + ' "'; } else { switch (templateType) { case 'alpha-lower': templateFinal = 'lower-alpha'; break; case 'roman-lower': templateFinal = 'lower-roman'; break; case 'roman-upper': templateFinal = 'upper-roman'; break; default: templateFinal = 'decimal'; break; } } return getListStyleTypeFromString(listType, templateFinal); } function wordListPaddingParser( format: ContentModelListItemLevelFormat, element: HTMLElement ): void { if (element.style.marginLeft && element.style.marginLeft != '0in') { format.paddingLeft = '0px'; } Iif (element.style.marginRight && element.style.marginRight != '0in') { format.paddingRight = '0px'; } } function setStartNumber( listItem: ContentModelListItem, context: DomToModelContext, listMetadata: WordMetadata | undefined ) { const { listParent, wordList, wordKnownLevels, wordLevel, levels, } = context.listFormat as WordDesktopListFormat; const block = getLastNotEmptyBlock(listParent); if ( (block?.blockType != 'BlockGroup' || block.blockGroupType != 'ListItem' || (wordLevel && (block.levels[wordLevel]?.format as WordListFormat)?.wordList != wordList)) && wordList ) { const start = listMetadata?.['mso-level-start-at'] ? parseInt(listMetadata['mso-level-start-at']) : NaN; const knownLevel = wordKnownLevels?.get(wordList) || []; if (start != undefined && !isNaN(start) && knownLevel.length != levels.length) { listItem.levels[listItem.levels.length - 1].format.startNumberOverride = start; } } } function getLastNotEmptyBlock(listParent: ContentModelBlockGroup | undefined) { for (let index = (listParent?.blocks.length || 0) - 1; index > 0; index--) { const result = listParent?.blocks[index]; if (result && !isEmpty(result)) { return result; } } return undefined; } |