All files / roosterjs-content-model-core/lib/command/exportContent exportContent.ts

100% Statements 18/18
88.89% Branches 8/9
100% Functions 2/2
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 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 1121x                                                                                                       1x   7x         7x   1x   2x   2x             2x   2x 1x               2x       2x   2x 2x   2x                   2x         4x   4x    
import {
    contentModelToDom,
    contentModelToText,
    createModelToDomContext,
    transformColor,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelDocument,
    ExportContentMode,
    IEditor,
    ModelToDomOption,
    ModelToTextCallbacks,
} from 'roosterjs-content-model-types';
 
/**
 * Export HTML content. If there are entities, this will cause EntityOperation event with option = 'replaceTemporaryContent' to get a dehydrated entity
 * @param editor The editor to get content from
 * @param mode Specify HTML to get HTML. This is the default option
 * @param options @optional Options for Model to DOM conversion
 */
export function exportContent(editor: IEditor, mode?: 'HTML', options?: ModelToDomOption): string;
 
/**
 * Export HTML content. If there are entities, this will cause EntityOperation event with option = 'replaceTemporaryContent' to get a dehydrated entity.
 * This is a fast version, it retrieve HTML content directly from editor without going through content model conversion.
 * @param editor The editor to get content from
 * @param mode Specify HTMLFast to get HTML result.
 */
export function exportContent(editor: IEditor, mode: 'HTMLFast'): string;
 
/**
 * Export plain text content
 * @param editor The editor to get content from
 * @param mode Specify PlainText to get plain text result
 * @param callbacks @optional Callbacks to customize conversion behavior
 */
export function exportContent(
    editor: IEditor,
    mode: 'PlainText',
    callbacks?: ModelToTextCallbacks
): string;
 
/**
 * Export plain text using editor's textContent property directly
 * @param editor The editor to get content from
 * @param mode Specify PlainTextFast to get plain text result using textContent property
 * @param options @optional Options for Model to DOM conversion
 */
export function exportContent(editor: IEditor, mode: 'PlainTextFast'): string;
 
// Here I didn't add 'HTMLFast' to ExportContentMode type because it will make this a breaking change and EditorAdapter will see build time error without bumping version
// Once we are confident that 'HTMLFast' is stable, we can fully switch 'HTML' to use the 'HTMLFast' approach
export function exportContent(
    editor: IEditor,
    Imode: ExportContentMode | 'HTMLFast' = 'HTML',
    optionsOrCallbacks?: ModelToDomOption | ModelToTextCallbacks
): string {
    let model: ContentModelDocument;
 
    switch (mode) {
        case 'PlainTextFast':
            return editor.getDOMHelper().getTextContent();
        case 'PlainText':
            model = editor.getContentModelCopy('clean');
 
            return contentModelToText(
                model,
                undefined /*separator*/,
                optionsOrCallbacks as ModelToTextCallbacks
            );
 
        case 'HTMLFast':
            const clonedRoot = editor.getDOMHelper().getClonedRoot();
 
            if (editor.isDarkMode()) {
                transformColor(
                    clonedRoot,
                    false /*includeSelf*/,
                    'darkToLight',
                    editor.getColorManager()
                );
            }
 
            return getHTMLFromDOM(editor, clonedRoot);
 
        case 'HTML':
        default:
            model = editor.getContentModelCopy('clean');
 
            const doc = editor.getDocument();
            const div = doc.createElement('div');
 
            contentModelToDom(
                doc,
                div,
                model,
                createModelToDomContext(
                    undefined /*editorContext*/,
                    optionsOrCallbacks as ModelToDomOption
                )
            );
 
            return getHTMLFromDOM(editor, div);
    }
}
 
function getHTMLFromDOM(editor: IEditor, root: HTMLElement): string {
    editor.triggerEvent('extractContentWithDom', { clonedRoot: root }, true /*broadcast*/);
 
    return root.innerHTML;
}