All files / roosterjs-editor-adapter/lib/editor/utils selectionConverter.ts

92.86% Statements 13/14
95% Branches 19/20
100% Functions 2/2
92.86% Lines 13/14

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 881x 1x                   1x     6x   1x           2x                 2x                       1x             1x 3x                   1x             1x                       1x              
import { createRange } from 'roosterjs-editor-dom';
import { createTableRanges } from './createTableRanges';
import { SelectionRangeTypes } from 'roosterjs-editor-types';
import type { DOMSelection } from 'roosterjs-content-model-types';
import type { SelectionRangeEx } from 'roosterjs-editor-types';
 
// In theory, all functions below are not necessary. We keep these functions here only for compatibility with old IEditor interface
 
/**
 * @internal
 */
export function convertRangeExToDomSelection(
    rangeEx: SelectionRangeEx | null
): DOMSelection | null {
    switch (rangeEx?.type) {
        case SelectionRangeTypes.ImageSelection:
            return {
                type: 'image',
                image: rangeEx.image,
            };
 
        case SelectionRangeTypes.Normal:
            return rangeEx.ranges.length > 0
                ? {
                      type: 'range',
                      range: rangeEx.ranges[0],
                      isReverted: false,
                  }
                : null;
 
        case SelectionRangeTypes.TableSelection:
            return rangeEx.coordinates
                ? {
                      type: 'table',
                      table: rangeEx.table,
                      firstColumn: rangeEx.coordinates.firstCell.x,
                      firstRow: rangeEx.coordinates.firstCell.y,
                      lastColumn: rangeEx.coordinates.lastCell.x,
                      lastRow: rangeEx.coordinates.lastCell.y,
                  }
                : null;
 
        default:
            return null;
    }
}
 
/**
 * @internal
 */
export function convertDomSelectionToRangeEx(selection: DOMSelection | null): SelectionRangeEx {
    switch (selection?.type) {
        case 'image':
            return {
                type: SelectionRangeTypes.ImageSelection,
                image: selection.image,
                areAllCollapsed: false,
                ranges: [createRange(selection.image)],
            };
 
        case 'range':
            return {
                type: SelectionRangeTypes.Normal,
                ranges: [selection.range],
                areAllCollapsed: selection.range.collapsed,
            };
 
        case 'table':
            return {
                type: SelectionRangeTypes.TableSelection,
                ranges: createTableRanges(selection),
                areAllCollapsed: false,
                table: selection.table,
                coordinates: {
                    firstCell: { x: selection.firstColumn, y: selection.firstRow },
                    lastCell: { x: selection.lastColumn, y: selection.lastRow },
                },
            };
 
        default:
            return {
                type: SelectionRangeTypes.Normal,
                ranges: [],
                areAllCollapsed: true,
            };
    }
}