All files / roosterjs-content-model-core/lib/coreApi/setContentModel setContentModel.ts

100% Statements 19/19
100% Branches 22/22
100% Functions 1/1
100% Lines 18/18

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 801x 1x                                 1x             165x 165x                       165x   165x   165x             165x   164x   164x   164x 36x   128x       165x   102x     63x                   165x    
import { updateCache } from '../../corePlugin/cache/updateCache';
import {
    contentModelToDom,
    createModelToDomContext,
    createModelToDomContextWithConfig,
} from 'roosterjs-content-model-dom';
import type { SetContentModel } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Set content with content model
 * @param core The editor core object
 * @param model The content model to set
 * @param option Additional options to customize the behavior of Content Model to DOM conversion
 * @param onNodeCreated An optional callback that will be called when a DOM node is created
 * @param isInitializing True means editor is being initialized then it will save modification nodes onto
 * lifecycleState instead of triggering events, false means other cases
 */
export const setContentModel: SetContentModel = (
    core,
    model,
    option,
    onNodeCreated,
    isInitializing
) => {
    const editorContext = core.api.createEditorContext(core, true /*saveIndex*/);
    const modelToDomContext = option
        ? createModelToDomContext(
              editorContext,
              core.environment.modelToDomSettings.builtIn,
              core.environment.modelToDomSettings.customized,
              option
          )
        : createModelToDomContextWithConfig(
              core.environment.modelToDomSettings.calculated,
              editorContext
          );
 
    modelToDomContext.onNodeCreated = onNodeCreated;
 
    core.onFixUpModel?.(model);
 
    const selection = contentModelToDom(
        core.logicalRoot.ownerDocument,
        core.logicalRoot,
        model,
        modelToDomContext
    );
 
    if (!core.lifecycle.shadowEditFragment) {
        // Clear pending mutations since we will use our latest model object to replace existing cache
        core.cache.textMutationObserver?.flushMutations(true /*ignoreMutations*/);
 
        updateCache(core.cache, model, selection);
 
        if (!option?.ignoreSelection && selection) {
            core.api.setDOMSelection(core, selection);
        } else {
            core.selection.selection = selection;
        }
    }
 
    if (isInitializing) {
        // When initialize, we should not trigger event until all plugins are initialized, so put these node in lifecycle state temporarily
        core.lifecycle.rewriteFromModel = modelToDomContext.rewriteFromModel;
    } else {
        // Otherwise, trigger RewriteFromModel event immediately
        core.api.triggerEvent(
            core,
            {
                eventType: 'rewriteFromModel',
                ...modelToDomContext.rewriteFromModel,
            },
            true /*broadcast*/
        );
    }
 
    return selection;
};