All files / roosterjs-content-model-dom/lib/modelApi/common unwrapBlock.ts

100% Statements 13/13
88.89% Branches 16/18
100% Functions 2/2
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 461x 1x 1x                       1x         25x   25x 23x   23x 23x       24x   24x 16x             24x            
import { copyFormat } from '../../modelApi/block/copyFormat';
import { mutateBlock } from './mutate';
import { setParagraphNotImplicit } from '../block/setParagraphNotImplicit';
import type {
    ContentModelBlockFormat,
    ReadonlyContentModelBlock,
    ReadonlyContentModelBlockGroup,
} from 'roosterjs-content-model-types';
 
/**
 * Unwrap a given block group, move its child blocks to be under its parent group
 * @param parent Parent block group of the unwrapping group
 * @param groupToUnwrap  The block group to unwrap
 */
export function unwrapBlock(
    parent: ReadonlyContentModelBlockGroup | null,
    groupToUnwrap: ReadonlyContentModelBlockGroup & ReadonlyContentModelBlock,
    formatsToKeep?: (keyof ContentModelBlockFormat)[]
) {
    const index = parent?.blocks.indexOf(groupToUnwrap) ?? -1;
 
    if (index >= 0) {
        groupToUnwrap.blocks.forEach(setParagraphNotImplicit);
 
        Eif (parent) {
            mutateBlock(parent)?.blocks.splice(
                index,
                1,
                ...groupToUnwrap.blocks.map(x => {
                    const mutableBlock = mutateBlock(x);
 
                    if (formatsToKeep) {
                        copyFormat<ContentModelBlockFormat>(
                            mutableBlock.format,
                            groupToUnwrap.format,
                            formatsToKeep
                        );
                    }
 
                    return mutableBlock;
                })
            );
        }
    }
}