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

100% Statements 13/13
83.33% Branches 5/6
100% Functions 1/1
100% Lines 13/13

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 771x                                                                               1x   5x     5x 1x   4x   4x 2x           2x 2x   2x                   2x   2x        
import {
    contentModelToDom,
    contentModelToText,
    createModelToDomContext,
} from 'roosterjs-content-model-dom';
import type {
    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 plain text result. 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 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;
 
export function exportContent(
    editor: IEditor,
    Imode: ExportContentMode = 'HTML',
    optionsOrCallbacks?: ModelToDomOption | ModelToTextCallbacks
): string {
    if (mode == 'PlainTextFast') {
        return editor.getDOMHelper().getTextContent();
    } else {
        const model = editor.getContentModelCopy('clean');
 
        if (mode == 'PlainText') {
            return contentModelToText(
                model,
                undefined /*separator*/,
                optionsOrCallbacks as ModelToTextCallbacks
            );
        } else {
            const doc = editor.getDocument();
            const div = doc.createElement('div');
 
            contentModelToDom(
                doc,
                div,
                model,
                createModelToDomContext(
                    undefined /*editorContext*/,
                    optionsOrCallbacks as ModelToDomOption
                )
            );
 
            editor.triggerEvent('extractContentWithDom', { clonedRoot: div }, true /*broadcast*/);
 
            return div.innerHTML;
        }
    }
}