UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

84 lines 4.08 kB
import React from 'react'; import { useIntl } from 'react-intl'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { deleteColumn, tooltip } from '@atlaskit/editor-common/keymaps'; import { tableMessages as messages } from '@atlaskit/editor-common/messages'; import { getSelectionRect } from '@atlaskit/editor-tables/utils'; import { DeleteIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar'; import { clearHoverSelection, closeActiveTableMenu, hoverColumns } from '../../../../pm-plugins/commands'; import { deleteColumnsWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics'; import { getSelectedColumnIndexes } from '../../../../pm-plugins/utils/selection'; import { useTableMenuContext } from '../../shared/TableMenuContext'; export const DeleteColumnItem = ({ api }) => { var _tableMenuContext$sel, _tooltip; const tableMenuContext = useTableMenuContext(); const { editorView } = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {}; const selectedColumnCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedColumnCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 1; const { formatMessage } = useIntl(); const { isCommentEditor, isTableFixedColumnWidthsOptionEnabled, isTableScalingEnabled } = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], states => { const tableState = states.tableState; return { isCommentEditor: tableState === null || tableState === void 0 ? void 0 : tableState.isCommentEditor, isTableFixedColumnWidthsOptionEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isTableFixedColumnWidthsOptionEnabled, isTableScalingEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isTableScalingEnabled }; }); const handleMouseEnter = () => { if (!editorView) { return; } const selectionRect = getSelectionRect(editorView.state.selection); if (!selectionRect) { return; } hoverColumns(getSelectedColumnIndexes(selectionRect), true)(editorView.state, editorView.dispatch); }; const handleMouseLeave = () => { if (!editorView) { return; } clearHoverSelection()(editorView.state, editorView.dispatch); }; const handleClick = () => { var _api$analytics; if (!editorView) { return; } const selectionRect = getSelectionRect(editorView.state.selection); if (!selectionRect) { return; } const shouldUseIncreasedScalingPercent = isTableScalingEnabled && (isTableFixedColumnWidthsOptionEnabled || isCommentEditor); deleteColumnsWithAnalytics(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent, isCommentEditor)(INPUT_METHOD.TABLE_CONTEXT_MENU, selectionRect)(editorView.state, editorView.dispatch, editorView); api === null || api === void 0 ? void 0 : api.core.actions.execute(closeActiveTableMenu()); api === null || api === void 0 ? void 0 : api.core.actions.focus(); }; return /*#__PURE__*/React.createElement(ToolbarDropdownItem, { onClick: handleClick, onFocus: handleMouseEnter, onMouseEnter: handleMouseEnter, onBlur: handleMouseLeave, onMouseLeave: handleMouseLeave, elemBefore: /*#__PURE__*/React.createElement(DeleteIcon, { color: "currentColor", label: "", size: "small" }), elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, { shortcut: (_tooltip = tooltip(deleteColumn)) !== null && _tooltip !== void 0 ? _tooltip : '' }) }, formatMessage(messages.removeColumns, { 0: selectedColumnCount })); };