All files / roosterjs-content-model-dom/lib/modelToDom/context createModelToDomContext.ts

100% Statements 33/33
100% Branches 40/40
100% Functions 16/16
100% Lines 33/33

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 1531x 1x 1x 1x                                             1x   1926x   686x               1x       852x                     852x                     852x                   852x                       1x     800x       774x     774x 774x       774x       774x                 1x 806x 806x   803x   803x   20075x     110011x           19350x       20075x   20075x             803x 774x 2x       803x    
import { defaultContentModelFormatMap } from '../../config/defaultContentModelFormatMap';
import { defaultContentModelHandlers } from './defaultContentModelHandlers';
import { getObjectKeys } from '../../domUtils/getObjectKeys';
import {
    defaultFormatAppliers,
    defaultFormatKeysPerCategory,
} from '../../formatHandlers/defaultFormatHandlers';
import type {
    RewriteFromModelContext,
    EditorContext,
    FormatApplier,
    FormatAppliers,
    FormatAppliersPerCategory,
    ModelToDomContext,
    ModelToDomFormatContext,
    ModelToDomOption,
    ModelToDomSelectionContext,
    ModelToDomSettings,
    TextFormatApplier,
} from 'roosterjs-content-model-types';
 
/**
 * Create context object fro Content Model to DOM conversion
 * @param editorContext Context of editor
 * @param options Option array to customize the Model to DOM conversion behavior
 */
export function createModelToDomContext(
    editorContext?: EditorContext,
    ...options: (ModelToDomOption | undefined)[]
): ModelToDomContext {
    return createModelToDomContextWithConfig(createModelToDomConfig(options), editorContext);
}
 
/**
 * Create context object for Content Model to DOM conversion with an existing configure
 * @param config A full config object to define how to convert Content Model to DOM tree
 * @param editorContext Context of editor
 */
export function createModelToDomContextWithConfig(
    config: ModelToDomSettings,
    editorContext?: EditorContext
): ModelToDomContext {
    return Object.assign(
        {},
        editorContext,
        createModelToDomSelectionContext(),
        createModelToDomFormatContext(),
        createRewriteFromModelContext(),
        config
    );
}
 
function createModelToDomSelectionContext(): ModelToDomSelectionContext {
    return {
        regularSelection: {
            current: {
                block: null,
                segment: null,
            },
        },
    };
}
 
function createModelToDomFormatContext(): ModelToDomFormatContext {
    return {
        listFormat: {
            threadItemCounts: [],
            nodeStack: [],
        },
        implicitFormat: {},
    };
}
 
function createRewriteFromModelContext(): RewriteFromModelContext {
    return {
        rewriteFromModel: {
            addedBlockElements: [],
            removedBlockElements: [],
        },
    };
}
 
/**
 * Create Content Model to DOM Config object
 * @param options All customizations of DOM creation
 */
export function createModelToDomConfig(
    options: (ModelToDomOption | undefined)[]
): ModelToDomSettings {
    return {
        modelHandlers: Object.assign(
            {},
            defaultContentModelHandlers,
            ...options.map(x => x?.modelHandlerOverride)
        ),
        formatAppliers: buildFormatAppliers(
            options.map(x => x?.formatApplierOverride),
            options.map(x => x?.additionalFormatAppliers)
        ),
        defaultModelHandlers: defaultContentModelHandlers,
        defaultFormatAppliers,
        metadataAppliers: Object.assign({}, ...options.map(x => x?.metadataAppliers)),
        defaultContentModelFormatMap: Object.assign(
            {},
            defaultContentModelFormatMap,
            ...options.map(x => x?.defaultContentModelFormatOverride)
        ),
    };
}
 
/**
 * @internal Export for test only
 * Build format appliers used by Content Model to DOM conversion
 */
export function buildFormatAppliers(
    overrides: (Partial<FormatAppliers> | undefined)[] = [],
    additionalAppliersArray: (Partial<FormatAppliersPerCategory> | undefined)[] = []
): FormatAppliersPerCategory {
    const combinedOverrides = Object.assign({}, ...overrides);
 
    const result = getObjectKeys(defaultFormatKeysPerCategory).reduce(
        (result, key) => {
            const value = defaultFormatKeysPerCategory[key]
                .map(
                    formatKey =>
                        (combinedOverrides[formatKey] === undefined
                            ? defaultFormatAppliers[formatKey]
                            : combinedOverrides[formatKey]) as FormatApplier<any>
                )
                .concat(
                    ...additionalAppliersArray.map(
                        appliers => (appliers?.[key] ?? []) as FormatApplier<any>[]
                    )
                );
 
            result[key] = value;
 
            return result;
        },
        {
            text: [] as TextFormatApplier[],
        } as FormatAppliersPerCategory
    );
 
    additionalAppliersArray.forEach(appliers => {
        if (appliers?.text) {
            result.text = result.text.concat(appliers.text);
        }
    });
 
    return result;
}