UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

38 lines 1.39 kB
import { Selection } from '@atlaskit/editor-prosemirror/state'; import { emptyCell, findCellClosestToPos, isSelectionType } from '@atlaskit/editor-tables/utils'; export const clearMultipleCells = targetCellPosition => (state, dispatch) => { let cursorPos; let { tr } = state; if (isSelectionType(tr.selection, 'cell')) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any const selection = tr.selection; selection.forEachCell((_node, pos) => { const $pos = tr.doc.resolve(tr.mapping.map(pos + 1)); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion tr = emptyCell(findCellClosestToPos($pos), state.schema)(tr); }); cursorPos = selection.$headCell.pos; } else if (targetCellPosition) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const cell = findCellClosestToPos(tr.doc.resolve(targetCellPosition + 1)); tr = emptyCell(cell, state.schema)(tr); cursorPos = cell.pos; } if (tr.docChanged && cursorPos) { const $pos = tr.doc.resolve(tr.mapping.map(cursorPos)); const textSelection = Selection.findFrom($pos, 1, true); if (textSelection) { tr.setSelection(textSelection); } if (dispatch) { dispatch(tr); } return true; } return false; };