All files / roosterjs-content-model-plugins/lib/edit keyboardEnter.ts

95.83% Statements 23/24
86.96% Branches 20/23
80% Functions 4/5
95.83% Lines 23/24

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 731x 1x 1x 1x 1x                     1x         33x   33x     33x     33x     33x   33x       33x 32x     33x     33x   30x   30x   30x 30x   3x                           2x   2x      
import { deleteEmptyQuote } from './deleteSteps/deleteEmptyQuote';
import { handleAutoLink } from './inputSteps/handleAutoLink';
import { handleEnterOnList } from './inputSteps/handleEnterOnList';
import { handleEnterOnParagraph } from './inputSteps/handleEnterOnParagraph';
import {
    ChangeSource,
    deleteSelection,
    normalizeContentModel,
    runEditSteps,
} from 'roosterjs-content-model-dom';
import type { IEditor, ReadonlyContentModelParagraph } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function keyboardEnter(
    editor: IEditor,
    rawEvent: KeyboardEvent,
    handleNormalEnter: boolean
) {
    const selection = editor.getDOMSelection();
 
    editor.formatContentModel(
        (model, context) => {
            // 1. delete the expanded selection if any, then merge paragraph
            const result = deleteSelection(model, [], context);
 
            // 2. Add line break
            Eif (selection && selection.type != 'table') {
                // For ENTER key, although we may have deleted something, since we still need to split the line, we always treat it as not delete
                // so further delete steps can keep working
                result.deleteResult = 'notDeleted';
 
                const steps = rawEvent.shiftKey
                    ? []
                    : [handleAutoLink, handleEnterOnList, deleteEmptyQuote];
 
                if (handleNormalEnter || handleEnterForEntity(result.insertPoint?.paragraph)) {
                    steps.push(handleEnterOnParagraph);
                }
 
                runEditSteps(steps, result);
            }
 
            if (result.deleteResult == 'range') {
                // We have deleted something, next input should inherit the segment format from deleted content, so set pending format here
                context.newPendingFormat = result.insertPoint?.marker.format;
 
                normalizeContentModel(model);
 
                rawEvent.preventDefault();
                return true;
            } else {
                return false;
            }
        },
        {
            rawEvent,
            scrollCaretIntoView: true,
            changeSource: ChangeSource.Keyboard,
            getChangeData: () => rawEvent.which,
            apiName: 'handleEnterKey',
        }
    );
}
 
function handleEnterForEntity(paragraph: ReadonlyContentModelParagraph | undefined) {
    return (
        paragraph &&
        (paragraph.isImplicit || paragraph.segments.some(x => x.segmentType == 'Entity'))
    );
}