All files / roosterjs-content-model-plugins/lib/paste/pasteSourceValidations getDocumentSource.ts

100% Statements 16/16
100% Branches 8/8
100% Functions 2/2
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 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 981x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                     1x                                     1x 25x   25x 225x 17x       25x    
import { documentContainWacElements } from './documentContainWacElements';
import { isExcelDesktopDocument } from './isExcelDesktopDocument';
import { isExcelNotNativeEvent } from './isExcelNonNativeEvent';
import { isExcelOnlineDocument } from './isExcelOnlineDocument';
import { isGoogleSheetDocument } from './isGoogleSheetDocument';
import { isOneNoteDesktopDocument } from './isOneNoteDocument';
import { isPowerPointDesktopDocument } from './isPowerPointDesktopDocument';
import { isWordDesktopDocument } from './isWordDesktopDocument';
import { shouldConvertToSingleImage } from './shouldConvertToSingleImage';
import type { EditorEnvironment } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * The input parameters for getDocumentSource function
 */
export type GetSourceInputParams = {
    /**
     * HTML attributes from the content that is being checked
     */
    htmlAttributes: Record<string, string>;
    /**
     * Document fragment of the checked content
     */
    fragment: DocumentFragment | Document;
    /**
     * Whether convert single image is enabled
     */
    shouldConvertSingleImage?: boolean;
    /**
     * Array of tag names of the first level child nodes
     */
    htmlFirstLevelChildTags?: string[];
    /**
     * The clipboard item types
     */
    clipboardItemTypes?: string[];
    /**
     * The editor environment
     */
    environment: Omit<EditorEnvironment, 'domToModelSettings' | 'modelToDomSettings'>;
    /**
     * The raw HTML string from clipboard
     */
    rawHtml?: string | null;
};
 
/**
 * @internal
 * Represent the types of sources to handle in the Paste Plugin
 */
export type KnownPasteSourceType =
    | 'wordDesktop'
    | 'excelDesktop'
    | 'excelOnline'
    | 'powerPointDesktop'
    | 'googleSheets'
    | 'wacComponents'
    | 'default'
    | 'singleImage'
    | 'excelNonNativeEvent'
    | 'oneNoteDesktop';
 
/**
 * @internal
 */
export type GetSourceFunction = (props: GetSourceInputParams) => boolean;
 
const getSourceFunctions = new Map<KnownPasteSourceType, GetSourceFunction>([
    ['wordDesktop', isWordDesktopDocument],
    ['excelDesktop', isExcelDesktopDocument],
    ['excelOnline', isExcelOnlineDocument],
    ['powerPointDesktop', isPowerPointDesktopDocument],
    ['wacComponents', documentContainWacElements],
    ['googleSheets', isGoogleSheetDocument],
    ['singleImage', shouldConvertToSingleImage],
    ['excelNonNativeEvent', isExcelNotNativeEvent],
    ['oneNoteDesktop', isOneNoteDesktopDocument],
]);
 
/**
 * @internal
 * This function tries to get the source of the Pasted content
 * @param event the before paste event
 * @param shouldConvertSingleImage Whether convert single image is enabled.
 * @returns The Type of pasted content, if no type found will return {KnownSourceType.Default}
 */
export function getDocumentSource(param: GetSourceInputParams): KnownPasteSourceType {
    let result: KnownPasteSourceType | null = null;
 
    getSourceFunctions.forEach((func, key) => {
        if (!result && func(param)) {
            result = key;
        }
    });
 
    return result ?? 'default';
}