All files / roosterjs-content-model-api/lib/publicApi/image adjustImageSelection.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 5/5
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 351x 1x             1x 5x   5x   5x     5x 2x 2x   2x   3x     2x             5x    
import { adjustSegmentSelection } from '../../modelApi/selection/adjustSegmentSelection';
import { mutateSegment } from 'roosterjs-content-model-dom';
import type { ContentModelImage, IEditor } from 'roosterjs-content-model-types';
 
/**
 * Adjust selection to make sure select an image if any
 * @return Content Model Image object if an image is select, or null
 */
export function adjustImageSelection(editor: IEditor): ContentModelImage | null {
    let image: ContentModelImage | null = null;
 
    editor.formatContentModel(
        model =>
            adjustSegmentSelection(
                model,
                (target, paragraph) => {
                    if (target.isSelected && target.segmentType == 'Image') {
                        mutateSegment(paragraph, target, segment => {
                            image = segment;
                        });
                        return true;
                    } else {
                        return false;
                    }
                },
                (target, ref) => target == ref
            ),
        {
            apiName: 'adjustImageSelection',
        }
    );
 
    return image;
}