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

100% Statements 53/53
88% Branches 44/50
100% Functions 6/6
100% Lines 48/48

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 153 154 155 156 157 158 1591x 1x 1x 1x                                               1x       10x 70x 16x     54x 54x 54x   54x   54x 54x       54x 15x 15x       15x   39x       2x 2x 37x 93x   31x 21x   21x   2x 2x     19x 9x   10x 2x     10x           10x     19x           19x       21x   10x                 10x             6x           2x 2x                 39x                             54x   54x 41x   20x 19x        
import { getLeafSiblingBlock } from '../utils/getLeafSiblingBlock';
import { preserveParagraphFormat } from '../utils/preserveParagraphFormat';
import { setModelIndentation } from 'roosterjs-content-model-api';
import {
    deleteBlock,
    deleteSegment,
    getClosestAncestorBlockGroupIndex,
    mutateBlock,
    setParagraphNotImplicit,
} from 'roosterjs-content-model-dom';
import type { ReadonlyBlockAndPath } from '../utils/getLeafSiblingBlock';
import type {
    DeleteSelectionStep,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelDocument,
    ReadonlyContentModelParagraph,
    ReadonlyContentModelSegment,
} from 'roosterjs-content-model-types';
import type { EditOptions } from '../EditOptions';
 
/**
 * @internal
 * Get a delete step for collapsed selection at specified direction
 * @param direction The direction to delete
 * @param options Options for the delete operation
 * @returns A delete step
 */
export function getDeleteCollapsedSelection(
    direction: 'forward' | 'backward',
    options: EditOptions
): DeleteSelectionStep {
    return context => {
        if (context.deleteResult != 'notDeleted') {
            return;
        }
 
        const isForward = direction == 'forward';
        const { paragraph, marker, path, tableContext } = context.insertPoint;
        const segments = paragraph.segments;
 
        fixupBr(paragraph);
 
        const index = segments.indexOf(marker) + (isForward ? 1 : -1);
        const segmentToDelete = segments[index];
        let blockToDelete: ReadonlyBlockAndPath | null;
        let root: ReadonlyContentModelDocument | null;
 
        if (segmentToDelete) {
            Eif (deleteSegment(paragraph, segmentToDelete, context.formatContext, direction)) {
                context.deleteResult = 'singleChar';
 
                // It is possible that we have deleted everything from this paragraph, so we need to mark it as not implicit
                // to avoid losing its format. See https://github.com/microsoft/roosterjs/issues/1953
                setParagraphNotImplicit(paragraph);
            }
        } else if (
            shouldOutdentParagraph(isForward, segments, paragraph, path) &&
            (root = getRoot(path))
        ) {
            setModelIndentation(root, 'outdent');
            context.deleteResult = 'range';
        } else if ((blockToDelete = getLeafSiblingBlock(path, paragraph, isForward))) {
            const { block: readonlyBlock, path, siblingSegment } = blockToDelete;
 
            if (readonlyBlock.blockType == 'Paragraph') {
                const block = mutateBlock(readonlyBlock);
 
                if (siblingSegment) {
                    // When selection is under general segment, need to check if it has a sibling sibling, and delete from it
                    Eif (deleteSegment(block, siblingSegment, context.formatContext, direction)) {
                        context.deleteResult = 'range';
                    }
                } else {
                    if (isForward) {
                        context.lastParagraph = block;
                    } else {
                        if (block.segments[block.segments.length - 1]?.segmentType == 'Br') {
                            mutateBlock(block).segments.pop();
                        }
 
                        context.insertPoint = {
                            marker,
                            paragraph: block,
                            path,
                            tableContext,
                        };
                        context.lastParagraph = paragraph;
                    }
 
                    preserveParagraphFormat(
                        options.formatsToPreserveOnMerge,
                        context.insertPoint.paragraph,
                        context.lastParagraph
                    );
 
                    context.deleteResult = 'range';
                }
 
                // When go across table, getLeafSiblingBlock will return null, when we are here, we must be in the same table context
                context.lastTableContext = tableContext;
            } else {
                Eif (
                    deleteBlock(
                        mutateBlock(path[0]).blocks,
                        readonlyBlock,
                        undefined /*replacement*/,
                        context.formatContext,
                        direction
                    )
                ) {
                    context.deleteResult = 'range';
                }
            }
        } else {
            // We have nothing to delete, in this case we don't want browser handle it as well.
            // Because when Backspace on an empty document, it will also delete the only DIV and SPAN element, causes
            // editor is really empty. We don't want that happen. So the handling should stop here.
            context.deleteResult = 'nothingToDelete';
        }
    };
}
 
function getRoot(path: ReadonlyContentModelBlockGroup[]): ReadonlyContentModelDocument | null {
    const lastInPath = path[path.length - 1];
    return lastInPath.blockGroupType == 'Document' ? lastInPath : null;
}
 
function shouldOutdentParagraph(
    isForward: boolean,
    segments: ReadonlyContentModelSegment[],
    paragraph: ReadonlyContentModelParagraph,
    path: ReadonlyContentModelBlockGroup[]
) {
    return (
        !isForward &&
        segments.length == 1 &&
        segments[0].segmentType == 'SelectionMarker' &&
        paragraph.format.marginLeft &&
        parseInt(paragraph.format.marginLeft) &&
        getClosestAncestorBlockGroupIndex(path, ['Document', 'TableCell'], ['ListItem']) > -1
    );
}
 
/**
 * If the last segment is BR, remove it for now. We may add it back later when normalize model.
 * So that if this is an empty paragraph, it will start to delete next block
 */
function fixupBr(paragraph: ReadonlyContentModelParagraph) {
    const { segments } = paragraph;
 
    if (segments[segments.length - 1]?.segmentType == 'Br') {
        const segmentsWithoutBr = segments.filter(x => x.segmentType != 'SelectionMarker');
 
        if (segmentsWithoutBr[segmentsWithoutBr.length - 2]?.segmentType != 'Br') {
            mutateBlock(paragraph).segments.pop();
        }
    }
}