All files / roosterjs-content-model-plugins/lib/tableEdit/editors/features TableInserter.ts

96.88% Statements 62/64
80% Branches 40/50
100% Functions 7/7
96.83% Lines 61/63

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 1891x 1x 1x     1x                 1x 1x 1x 1x       1x       1x         1x                     4x 4x 4x     4x 4x 4x             4x   4x   2x 2x         2x 2x   2x 2x   2x     2x     4x   4x                     4x                   1x   6x 6x 6x 6x 6x 6x 6x 6x     6x 6x           1x 7x 7x 7x     6x   2x   2x     2x   2x       2x     2x       2x                           2x   1x               4x 4x 4x 4x 4x                 4x            
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { getIntersectedRect } from '../../../pluginUtils/Rect/getIntersectedRect';
import { isElementOfType, normalizeRect } from 'roosterjs-content-model-dom';
import type { TableEditFeature } from './TableEditFeature';
import type { OnTableEditorCreatedCallback } from '../../OnTableEditorCreatedCallback';
import {
    formatTableWithContentModel,
    insertTableColumn,
    insertTableRow,
} from 'roosterjs-content-model-api';
import type { CreateElementData } from '../../../pluginUtils/CreateElement/CreateElementData';
import type { Disposable } from '../../../pluginUtils/Disposable';
import type { IEditor } from 'roosterjs-content-model-types';
 
const INSERTER_COLOR = '#4A4A4A';
const INSERTER_COLOR_DARK_MODE = 'white';
const INSERTER_SIDE_LENGTH = 12;
const INSERTER_BORDER_SIZE = 1;
/**
 * @internal
 */
export const HORIZONTAL_INSERTER_ID = 'horizontalInserter';
/**
 * @internal
 */
export const VERTICAL_INSERTER_ID = 'verticalInserter';
 
/**
 * @internal
 */
export function createTableInserter(
    editor: IEditor,
    td: HTMLTableCellElement,
    table: HTMLTableElement,
    isRTL: boolean,
    isHorizontal: boolean,
    onBeforeInsert: () => void,
    onAfterInserted: () => void,
    anchorContainer?: HTMLElement,
    onTableEditorCreated?: OnTableEditorCreatedCallback
): TableEditFeature | null {
    const tdRect = normalizeRect(td.getBoundingClientRect());
    const viewPort = editor.getVisibleViewport();
    const tableRect = table && viewPort ? getIntersectedRect([table], [viewPort]) : null;
 
    // set inserter position
    Eif (tdRect && tableRect) {
        const document = td.ownerDocument;
        const createElementData = getInsertElementData(
            isHorizontal,
            editor.isDarkMode(),
            isRTL,
            editor.getDOMHelper().getDomStyle('backgroundColor') || 'white'
        );
 
        const div = createElement(createElementData, document) as HTMLDivElement;
 
        if (isHorizontal) {
            // tableRect.left/right is used because the Inserter is always intended to be on the side
            div.id = HORIZONTAL_INSERTER_ID;
            div.style.left = `${
                isRTL
                    ? tableRect.right
                    : tableRect.left - (INSERTER_SIDE_LENGTH - 1 + 2 * INSERTER_BORDER_SIZE)
            }px`;
            div.style.top = `${tdRect.bottom - 8}px`;
            (div.firstChild as HTMLElement).style.width = `${tableRect.right - tableRect.left}px`;
        } else {
            div.id = VERTICAL_INSERTER_ID;
            div.style.left = `${isRTL ? tdRect.left - 8 : tdRect.right - 8}px`;
            // tableRect.top is used because the Inserter is always intended to be on top
            div.style.top = `${
                tableRect.top - (INSERTER_SIDE_LENGTH - 1 + 2 * INSERTER_BORDER_SIZE)
            }px`;
            (div.firstChild as HTMLElement).style.height = `${tableRect.bottom - tableRect.top}px`;
        }
 
        (anchorContainer || document.body).appendChild(div);
 
        const handler = new TableInsertHandler(
            div,
            td,
            table,
            isHorizontal,
            editor,
            onBeforeInsert,
            onAfterInserted,
            onTableEditorCreated
        );
 
        return { div, featureHandler: handler, node: td };
    }
 
    return null;
}
 
/**
 * @internal
 * Exported for test only
 */
export class TableInsertHandler implements Disposable {
    private disposer: undefined | (() => void);
    constructor(
        private div: HTMLDivElement,
        private td: HTMLTableCellElement,
        private table: HTMLTableElement,
        private isHorizontal: boolean,
        private editor: IEditor,
        private onBeforeInsert: () => void,
        private onAfterInsert: () => void,
        onTableEditorCreated?: OnTableEditorCreatedCallback
    ) {
        this.div.addEventListener('click', this.insertTd);
        this.disposer = onTableEditorCreated?.(
            isHorizontal ? 'HorizontalTableInserter' : 'VerticalTableInserter',
            div
        );
    }
 
    dispose() {
        this.div.removeEventListener('click', this.insertTd);
        this.disposer?.();
        this.disposer = undefined;
    }
 
    private insertTd = () => {
        // Get cell coordinates
        const columnIndex = this.td.cellIndex;
        const row =
            this.td.parentElement && isElementOfType(this.td.parentElement, 'tr')
                ? this.td.parentElement
                : undefined;
        const rowIndex = row && row.rowIndex;
 
        Iif (row?.cells == undefined || rowIndex == undefined) {
            return;
        }
 
        this.onBeforeInsert();
 
        // Insert row or column
        formatTableWithContentModel(
            this.editor,
            'editTablePlugin',
            tableModel => {
                this.isHorizontal
                    ? insertTableRow(tableModel, 'insertBelow')
                    : insertTableColumn(tableModel, 'insertRight');
            }, // Select cell to make insertion
            {
                type: 'table',
                firstColumn: columnIndex,
                firstRow: rowIndex,
                lastColumn: columnIndex,
                lastRow: rowIndex,
                table: this.table,
            }
        );
 
        this.onAfterInsert();
    };
}
 
function getInsertElementData(
    isHorizontal: boolean,
    isDark: boolean,
    isRTL: boolean,
    backgroundColor: string
): CreateElementData {
    const inserterColor = isDark ? INSERTER_COLOR_DARK_MODE : INSERTER_COLOR;
    const outerDivStyle = `position: fixed; width: ${INSERTER_SIDE_LENGTH}px; height: ${INSERTER_SIDE_LENGTH}px; font-size: 16px; color: black; line-height: 8px; vertical-align: middle; text-align: center; cursor: pointer; border: solid ${INSERTER_BORDER_SIZE}px ${inserterColor}; border-radius: 50%; background-color: ${backgroundColor}`;
    const leftOrRight = isRTL ? 'right' : 'left';
    const childBaseStyles = `position: absolute; box-sizing: border-box; background-color: ${backgroundColor};`;
    const childInfo: CreateElementData = {
        tag: 'div',
        style:
            childBaseStyles +
            (isHorizontal
                ? `${leftOrRight}: 12px; top: 5px; height: 3px; border-top: 1px solid ${inserterColor}; border-bottom: 1px solid ${inserterColor}; border-right: 1px solid ${inserterColor}; border-left: 0px;`
                : `left: 5px; top: 12px; width: 3px; border-left: 1px solid ${inserterColor}; border-right: 1px solid ${inserterColor}; border-bottom: 1px solid ${inserterColor}; border-top: 0px;`),
    };
 
    return {
        tag: 'div',
        style: outerDivStyle,
        children: [childInfo, '+'],
    };
}