UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

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