All files / roosterjs-content-model-api/lib/publicApi/utils formatParagraphWithContentModel.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 2/2
100% Lines 9/9

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 321x 1x                 1x         22x   22x   22x   22x 22x   22x              
import { getSelectedParagraphs } from 'roosterjs-content-model-dom';
import { splitSelectedParagraphByBr } from '../../modelApi/block/splitSelectedParagraphByBr';
import type { IEditor, ShallowMutableContentModelParagraph } from 'roosterjs-content-model-types';
 
/**
 * Invoke a callback to format the selected paragraph using Content Model
 * @param editor The editor object
 * @param apiName Name of API this calling this function. This is mostly for logging.
 * @param setStyleCallback The callback to format the paragraph. It will be called with current selected table. If no table is selected, it will not be called.
 */
export function formatParagraphWithContentModel(
    editor: IEditor,
    apiName: string,
    setStyleCallback: (paragraph: ShallowMutableContentModelParagraph) => void
) {
    editor.formatContentModel(
        (model, context) => {
            splitSelectedParagraphByBr(model);
 
            const paragraphs = getSelectedParagraphs(model, true /*mutate*/);
 
            paragraphs.forEach(setStyleCallback);
            context.newPendingFormat = 'preserve';
 
            return paragraphs.length > 0;
        },
        {
            apiName,
        }
    );
}