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

100% Statements 72/72
86.36% Branches 38/44
100% Functions 8/8
100% Lines 72/72

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 1x 1x                   1x                                 1x               27x 27x   27x 27x 10x   27x 27x 23x     27x 27x 1x     27x                     27x 27x 27x 27x 27x     1x           27x     27x           27x 4x 4x 4x 4x 4x     27x 27x     1x                 27x 27x 27x   27x       27x 27x       27x       27x 27x 27x 27x   27x 23x 184x     27x 10x 10x     27x 1x 5x       27x     1x 27x 27x 27x       27x     1x         27x 27x 27x 27x 27x 27x 27x 27x 27x   27x    
import { createImageCropper } from '../Cropper/createImageCropper';
import { createImageResizer } from '../Resizer/createImageResizer';
import { createImageRotator } from '../Rotator/createImageRotator';
import { wrap } from 'roosterjs-content-model-dom';
 
import type {
    IEditor,
    ImageEditOperation,
    ImageMetadataFormat,
} from 'roosterjs-content-model-types';
import type { ImageEditOptions } from '../types/ImageEditOptions';
import type { ImageHtmlOptions } from '../types/ImageHtmlOptions';
 
const IMAGE_EDIT_SHADOW_ROOT = 'ImageEditShadowRoot';
 
/**
 * @internal
 */
export interface WrapperElements {
    wrapper: HTMLSpanElement;
    shadowSpan: HTMLElement;
    imageClone: HTMLImageElement;
    resizers: HTMLDivElement[];
    rotators: HTMLDivElement[];
    croppers: HTMLDivElement[];
}
 
/**
 * @internal
 */
export function createImageWrapper(
    editor: IEditor,
    image: HTMLImageElement,
    options: ImageEditOptions,
    editInfo: ImageMetadataFormat,
    htmlOptions: ImageHtmlOptions,
    operation: ImageEditOperation[]
): WrapperElements {
    const imageClone = cloneImage(image, editInfo, options.resolveImageSource);
    const doc = editor.getDocument();
 
    let rotators: HTMLDivElement[] = [];
    if (!options.disableRotate && operation.indexOf('rotate') > -1) {
        rotators = createImageRotator(doc, htmlOptions);
    }
    let resizers: HTMLDivElement[] = [];
    if (operation.indexOf('resize') > -1) {
        resizers = createImageResizer(doc, !!options.disableSideResize);
    }
 
    let croppers: HTMLDivElement[] = [];
    if (operation.indexOf('crop') > -1) {
        croppers = createImageCropper(doc);
    }
 
    const wrapper = createWrapper(
        editor,
        imageClone,
        options,
        editInfo,
        resizers,
        rotators,
        croppers
    );
    // Capture the image footprint before attaching the shadow root, since the light-DOM image
    // stops being rendered once the shadow root takes over.
    const imageWidth = image.offsetWidth;
    const imageHeight = image.offsetHeight;
    const imageSpan = wrap(doc, image, 'span');
    const shadowSpan = createShadowSpan(wrapper, imageSpan, imageWidth, imageHeight);
    return { wrapper, shadowSpan, imageClone, resizers, rotators, croppers };
}
 
const createShadowSpan = (
    wrapper: HTMLElement,
    imageSpan: HTMLSpanElement,
    imageWidth: number,
    imageHeight: number
) => {
    const shadowRoot = imageSpan.attachShadow({
        mode: 'open',
    });
    imageSpan.id = IMAGE_EDIT_SHADOW_ROOT;
 
    // Pin the shadow host to the original image's box so that wrapping the image does not grow the
    // surrounding line box. Without this, the taller edit wrapper enlarges the line and the browser
    // scrolls the selection back into view, making the editor jump. The wrapper and handles still
    // overflow the host visually because overflow is left visible.
    if (imageWidth > 0 && imageHeight > 0) {
        imageSpan.style.display = 'inline-block';
        imageSpan.style.width = `${imageWidth}px`;
        imageSpan.style.height = `${imageHeight}px`;
        imageSpan.style.verticalAlign = 'bottom';
        imageSpan.style.overflow = 'visible';
    }
 
    shadowRoot.appendChild(wrapper);
    return imageSpan;
};
 
const createWrapper = (
    editor: IEditor,
    image: HTMLImageElement,
    options: ImageEditOptions,
    editInfo: ImageMetadataFormat,
    resizers?: HTMLDivElement[],
    rotators?: HTMLDivElement[],
    cropper?: HTMLDivElement[]
) => {
    const doc = editor.getDocument();
    const wrapper = doc.createElement('span');
    const imageBox = doc.createElement('div');
 
    imageBox.setAttribute(
        `style`,
        `position:relative;width:100%;height:100%;overflow:hidden;transform:scale(1);`
    );
    imageBox.appendChild(image);
    wrapper.setAttribute(
        'style',
        `font-size: 24px; margin: 0px; transform: rotate(${editInfo.angleRad ?? 0}rad);`
    );
    wrapper.style.display = editor.getEnvironment().isSafari
        ? '-webkit-inline-flex'
        : 'inline-flex';
 
    const border = createBorder(editor, options.borderColor);
    wrapper.appendChild(imageBox);
    wrapper.appendChild(border);
    wrapper.style.userSelect = 'none';
 
    if (resizers && resizers?.length > 0) {
        resizers.forEach(resizer => {
            wrapper.appendChild(resizer);
        });
    }
    if (rotators && rotators.length > 0) {
        rotators.forEach(r => {
            wrapper.appendChild(r);
        });
    }
    if (cropper && cropper.length > 0) {
        cropper.forEach(c => {
            wrapper.appendChild(c);
        });
    }
 
    return wrapper;
};
 
const createBorder = (editor: IEditor, borderColor?: string) => {
    const doc = editor.getDocument();
    const resizeBorder = doc.createElement('div');
    resizeBorder.setAttribute(
        `style`,
        `position:absolute;left:0;right:0;top:0;bottom:0;border:solid 2px ${borderColor};pointer-events:none;`
    );
    return resizeBorder;
};
 
const cloneImage = (
    image: HTMLImageElement,
    editInfo: ImageMetadataFormat,
    resolveImageSource?: (src: string) => string | undefined
) => {
    const imageClone = image.cloneNode(true) as HTMLImageElement;
    imageClone.style.removeProperty('transform');
    Eif (editInfo.src) {
        imageClone.src = resolveImageSource?.(editInfo.src) ?? editInfo.src;
        imageClone.removeAttribute('id');
        imageClone.style.removeProperty('max-width');
        imageClone.style.removeProperty('max-height');
        imageClone.style.width = editInfo.widthPx + 'px';
        imageClone.style.height = editInfo.heightPx + 'px';
    }
    return imageClone;
};