<commit_before>import Observer from './Observer.js';

/**
 * Table Observer
 */
export default class TableObserver extends Observer {
    /**
     * @inheritDoc
     */
    observe(ev) {
        ev.forEach(item => item.addedNodes.forEach(node => {
            if (node instanceof HTMLTableElement) {
                this.initTable(node)
            } else if (node instanceof HTMLElement) {
                node.querySelectorAll('table').forEach(item => this.initTable(item));
            }
        }));
    }

    /**
     * Initializes table
     *
     * @private
     *
     * @param {HTMLTableElement} table
     */
    initTable(table) {
        table.addEventListener('keydown', ev => {
            const cell = ev.target;
            const row = cell.parentElement;

            if (cell instanceof HTMLTableCellElement
                && row instanceof HTMLTableRowElement
                && ev.ctrlKey
                && ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(ev.key)
            ) {
                let length = row.cells.length;
                let index;

                if (ev.key === 'ArrowLeft' || ev.key === 'ArrowRight') {
                    index = cell.cellIndex + (ev.key === 'ArrowLeft' ? 0 : 1);
                    Array.from(table.rows).forEach(item => item.insertCell(index));
                } else {
                    index = row.rowIndex + (ev.key === 'ArrowUp' ? 0 : 1);
                    const r = table.insertRow(index);

                    for (let i = 0; i < length; i++) {
                        r.insertCell();
                    }
                }

                ev.preventDefault();
                ev.cancelBubble = true;
            }
        });
    }
}<commit_msg>Adjust TableObserver keyboard support for adding rows and cells<commit_after>import Observer from './Observer.js';

/**
 * Table Observer
 */
export default class TableObserver extends Observer {
    /**
     * @inheritDoc
     */
    observe(ev) {
        ev.forEach(item => item.addedNodes.forEach(node => {
            if (node instanceof HTMLTableElement) {
                this.initTable(node)
            } else if (node instanceof HTMLElement) {
                node.querySelectorAll('table').forEach(item => this.initTable(item));
            }
        }));
    }

    /**
     * Initializes table
     *
     * @private
     *
     * @param {HTMLTableElement} table
     */
    initTable(table) {
        table.addEventListener('keydown', ev => {
            const cell = ev.target;
            const row = cell.parentElement;
            const base = row.parentElement;

            if (cell instanceof HTMLTableCellElement
                && row instanceof HTMLTableRowElement
                && (base instanceof HTMLTableElement || base instanceof HTMLTableSectionElement)
                && ev.ctrlKey
                && ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(ev.key)
            ) {
                let length = row.cells.length;
                let index;

                if (ev.key === 'ArrowLeft' || ev.key === 'ArrowRight') {
                    index = cell.cellIndex + (ev.key === 'ArrowLeft' ? 0 : 1);
                    Array.from(table.rows).forEach(item => {
                        if (!item.querySelector(':scope > td')) {
                            item.insertBefore(this.editor.document.createElement('th'), item.cells[index]);
                        } else {
                            item.insertCell(index);
                        }
                    });
                } else {
                    index = (base instanceof HTMLTableElement ? row.rowIndex : row.sectionRowIndex) + (ev.key === 'ArrowUp' ? 0 : 1);
                    const r = base.insertRow(index);

                    for (let i = 0; i < length; i++) {
                        r.insertCell();
                    }
                }

                ev.preventDefault();
                ev.cancelBubble = true;
            }
        });
    }
}
{'fraction_non_alphanumeric': 0.12204234122042341, 'fraction_numerical': 0.0024906600249066002, 'mean_word_length': 1.3819691577698696, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810480', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1007, 'n_words': 215}