All files / roosterjs-content-model-api/lib/publicApi/segment setFontSize.ts

100% Statements 13/13
91.67% Branches 11/12
100% Functions 4/4
100% Lines 13/13

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 491x                       1x 8x   8x     8x                   1x         30x     30x 2x   2x 3x 1x       2x      
import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';
import type {
    ContentModelSegmentFormat,
    IEditor,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
/**
 * Set font size
 * @param editor The editor to operate on
 * @param fontSize The font size to set
 */
export function setFontSize(editor: IEditor, fontSize: string) {
    editor.focus();
 
    formatSegmentWithContentModel(
        editor,
        'setFontSize',
        (format, _, __, paragraph) => setFontSizeInternal(fontSize, format, paragraph),
        undefined /* segmentHasStyleCallback*/,
        true /*includingFormatHandler*/
    );
}
 
/**
 * @internal
 * Internal set font function shared by setFontSize and changeFontSize
 */
export function setFontSizeInternal(
    fontSize: string,
    format: ContentModelSegmentFormat,
    paragraph: ShallowMutableContentModelParagraph | null
) {
    format.fontSize = fontSize;
 
    // Since we have set font size to segment, it can be smaller than the one in paragraph format, so delete font size from paragraph
    if (paragraph?.segmentFormat?.fontSize) {
        const size = paragraph.segmentFormat.fontSize;
 
        paragraph.segments.forEach(segment => {
            if (!segment.format.fontSize) {
                segment.format.fontSize = size;
            }
        });
 
        delete paragraph.segmentFormat.fontSize;
    }
}