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 | 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 18x 5x 13x 18x 1x 1x 2x 2x 2x 3x 17x 17x 19x 20x 20x 20x 16x 16x 16x 16x 16x 16x 16x 16x 16x 2x 2x 4x 4x 4x 4x 4x 4x 2x 72x 10x 62x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | import { findListItemsInSameThread } from '../list/findListItemsInSameThread'; import { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr'; import { applyTableFormat, getOperationalBlocks, isBlockGroupOfType, mutateBlock, updateTableCellMetadata, } from 'roosterjs-content-model-dom'; import type { BorderFormat, ContentModelListItem, MarginFormat, PaddingFormat, ReadonlyContentModelBlock, ReadonlyContentModelDocument, ReadonlyContentModelText, } from 'roosterjs-content-model-types'; // Regexes for character direction detection // Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range. const RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g; const URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g; const WHITESPACE_REGEX = /\s/g; /** * @internal */ export function setModelDirection( model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto' ) { splitSelectedParagraphByBr(model); const paragraphOrListItemOrTable = getOperationalBlocks<ContentModelListItem>( model, ['ListItem'], ['TableCell'] ); paragraphOrListItemOrTable.forEach(({ block }) => { let calcDirection: 'ltr' | 'rtl'; if (direction === 'auto') { calcDirection = determineTextDirection(block); } else { calcDirection = direction; } if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) { const items = findListItemsInSameThread(model, block); items.forEach(readonlyItem => { const item = mutateBlock(readonlyItem); item.levels.forEach(level => { level.format.direction = calcDirection; }); item.blocks.forEach(block => internalSetDirection(block, calcDirection)); }); } else Eif (block) { internalSetDirection(block, calcDirection); } }); return paragraphOrListItemOrTable.length > 0; } function internalSetDirection(block: ReadonlyContentModelBlock, direction: 'ltr' | 'rtl') { const wasRtl = block.format.direction == 'rtl'; const isRtl = direction == 'rtl'; if (wasRtl != isRtl) { const { format } = mutateBlock(block); format.direction = direction; // Adjust margin when change direction const marginLeft = format.marginLeft; const paddingLeft = format.paddingLeft; setProperty(format, 'marginLeft', format.marginRight); setProperty(format, 'marginRight', marginLeft); setProperty(format, 'paddingLeft', format.paddingRight); setProperty(format, 'paddingRight', paddingLeft); // If whole Table direction changed, flip cell side borders if (block && block.blockType == 'Table') { block.rows.forEach(row => { row.cells.forEach(cell => { // Optimise by skipping cells with unchanged borders updateTableCellMetadata(mutateBlock(cell), metadata => { Eif (metadata?.borderOverride) { const storeBorderLeft = cell.format.borderLeft; setProperty(cell.format, 'borderLeft', cell.format.borderRight); setProperty(cell.format, 'borderRight', storeBorderLeft); } return metadata; }); }); }); // Apply changed borders applyTableFormat(block, undefined /* newFormat */, true /* keepCellShade*/); } } } function setProperty( format: MarginFormat & PaddingFormat & BorderFormat, key: keyof (MarginFormat & PaddingFormat & BorderFormat), value: string | undefined ) { if (value) { format[key] = value; } else { delete format[key]; } } // Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character function determineTextDirection(block: ReadonlyContentModelBlock): 'ltr' | 'rtl' { Eif (block.blockType === 'Paragraph') { const findTextSegements: ReadonlyContentModelText[] = block.segments.filter( (seg): seg is ReadonlyContentModelText => seg.segmentType === 'Text' ); let innerText = findTextSegements.length > 0 ? findTextSegements.reduce((prev, seg) => prev + seg.text, '') : undefined; Eif (!!innerText) { // Remove links innerText = innerText.replace(URL_CHAR_REGEX, ''); // Remove whitespace innerText = innerText.replace(WHITESPACE_REGEX, ''); const rtlMatches = innerText.match(RTL_CHAR_REGEX); const rtlCount = rtlMatches ? rtlMatches.length : 0; const ltrCount = innerText.length - rtlCount; return rtlCount > ltrCount ? 'rtl' : 'ltr'; } else { return 'ltr'; // Default to LTR if no text is found } } else { return 'ltr'; } } |