All files / roosterjs-content-model-plugins/lib/watermark isModelEmptyFast.ts

100% Statements 17/17
100% Branches 21/21
100% Functions 3/3
100% Lines 16/16

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          1x 17x   17x 1x 16x 1x 15x 3x 12x 1x 11x     14x           3x 8x       2x   9x      
import type { ReadonlyContentModelBlockGroup } from 'roosterjs-content-model-types';
 
/**
 * A fast way to check if content model is empty
 */
export function isModelEmptyFast(model: ReadonlyContentModelBlockGroup): boolean {
    const firstBlock = model.blocks[0];
 
    if (model.blocks.length > 1) {
        return false; // Multiple blocks, treat as not empty
    } else if (!firstBlock) {
        return true; // No block, it is empty
    } else if (firstBlock.blockType != 'Paragraph') {
        return false; // First block is not paragraph, treat as not empty
    } else if (firstBlock.segments.length == 0) {
        return true; // No segment, it is empty
    } else if (
        firstBlock.segments.some(
            x =>
                x.segmentType == 'Entity' ||
                x.segmentType == 'Image' ||
                x.segmentType == 'General' ||
                (x.segmentType == 'Text' && x.text)
        )
    ) {
        return false; // Has meaningful segments, it is not empty
    } else if (
        (firstBlock.format.marginRight && parseFloat(firstBlock.format.marginRight) > 0) ||
        (firstBlock.format.marginLeft && parseFloat(firstBlock.format.marginLeft) > 0)
    ) {
        return false; // Has margin (indentation is changed), it is not empty
    } else {
        return firstBlock.segments.filter(x => x.segmentType == 'Br').length <= 1; // If there are more than one BR, it is not empty, otherwise it is empty
    }
}