All files / roosterjs-content-model-plugins/lib/imageEdit/utils findEditingImage.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 2/2
100% Lines 10/10

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 361x                   1x       16x 16x       24x 24x       11x 11x     12x         16x    
import { queryContentModelBlocks } from 'roosterjs-content-model-api';
import type {
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelParagraph,
} from 'roosterjs-content-model-types';
import type { ImageAndParagraph } from '../types/ImageAndParagraph';
 
/**
 * @internal
 */
export function findEditingImage(
    group: ReadonlyContentModelBlockGroup,
    imageId?: string
): ImageAndParagraph | null {
    let imageAndParagraph: ImageAndParagraph | null = null;
    queryContentModelBlocks<ReadonlyContentModelParagraph>(
        group,
        'Paragraph',
        (paragraph: ReadonlyContentModelParagraph): paragraph is ReadonlyContentModelParagraph => {
            for (const segment of paragraph.segments) {
                if (
                    segment.segmentType == 'Image' &&
                    ((imageId && segment.format.id == imageId) || segment.dataset.isEditing)
                ) {
                    imageAndParagraph = { image: segment, paragraph };
                    return true;
                }
            }
            return false;
        },
        true /*findFirstOnly*/
    );
 
    return imageAndParagraph;
}