UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

66 lines (63 loc) 2.51 kB
// @ts-ignore -- ReadonlyTransaction is a local declaration and will cause a TS2305 error in CCFE typecheck import { CellSelection } from '@atlaskit/editor-tables/cell-selection'; import { findTable } from '@atlaskit/editor-tables/utils'; import { TableDecorations } from '../../../types'; import { createColumnControlsDecoration, createColumnSelectedDecoration, findColumnControlSelectedDecoration, findControlsHoverDecoration, updateDecorations } from '../../utils/decoration'; import { composeDecorations } from './compose-decorations'; const isColumnSelected = tr => tr.selection instanceof CellSelection && tr.selection.isColSelection(); // @see: https://product-fabric.atlassian.net/browse/ED-3796 const removeControlsHoverDecoration = ({ decorationSet }) => decorationSet.remove(findControlsHoverDecoration(decorationSet)); const maybeUpdateColumnSelectedDecoration = ({ decorationSet, tr }) => { if (!isColumnSelected(tr)) { return decorationSet; } return updateDecorations(tr.doc, decorationSet, createColumnSelectedDecoration(tr), TableDecorations.COLUMN_SELECTED); }; const maybeUpdateColumnControlsDecoration = ({ decorationSet, tr }) => { const table = findTable(tr.selection); if (!table) { return decorationSet; } return updateDecorations(tr.doc, decorationSet, createColumnControlsDecoration(tr.selection), TableDecorations.COLUMN_CONTROLS_DECORATIONS); }; // @see: https://product-fabric.atlassian.net/browse/ED-7304 const removeColumnControlsSelectedDecoration = ({ decorationSet }) => decorationSet.remove(findColumnControlSelectedDecoration(decorationSet)); const hasColumnSelectedDecorations = decorationSet => !!findColumnControlSelectedDecoration(decorationSet).length; export const maybeUpdateColumnControlsSelectedDecoration = ({ decorationSet, tr }) => { if (!hasColumnSelectedDecorations(decorationSet)) { return decorationSet; } return removeColumnControlsSelectedDecoration({ decorationSet, tr }); }; export const buildColumnControlsDecorations = ({ decorationSet, tr, options }) => { if (options.isDragAndDropEnabled) { return composeDecorations([removeColumnControlsSelectedDecoration, removeControlsHoverDecoration, maybeUpdateColumnSelectedDecoration])({ decorationSet, tr }); } return composeDecorations([removeColumnControlsSelectedDecoration, removeControlsHoverDecoration, maybeUpdateColumnSelectedDecoration, maybeUpdateColumnControlsDecoration])({ decorationSet, tr }); };