UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

80 lines 4.37 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 var DeleteColumnItem = function DeleteColumnItem(_ref) { var _tableMenuContext$sel, _tooltip; var api = _ref.api; var tableMenuContext = useTableMenuContext(); var _ref2 = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {}, editorView = _ref2.editorView; var selectedColumnCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedColumnCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 1; var _useIntl = useIntl(), formatMessage = _useIntl.formatMessage; var _useSharedPluginState = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], function (states) { var 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 }; }), isCommentEditor = _useSharedPluginState.isCommentEditor, isTableFixedColumnWidthsOptionEnabled = _useSharedPluginState.isTableFixedColumnWidthsOptionEnabled, isTableScalingEnabled = _useSharedPluginState.isTableScalingEnabled; var handleMouseEnter = function handleMouseEnter() { if (!editorView) { return; } var selectionRect = getSelectionRect(editorView.state.selection); if (!selectionRect) { return; } hoverColumns(getSelectedColumnIndexes(selectionRect), true)(editorView.state, editorView.dispatch); }; var handleMouseLeave = function handleMouseLeave() { if (!editorView) { return; } clearHoverSelection()(editorView.state, editorView.dispatch); }; var handleClick = function handleClick() { var _api$analytics; if (!editorView) { return; } var selectionRect = getSelectionRect(editorView.state.selection); if (!selectionRect) { return; } var shouldUseIncreasedScalingPercent = isTableScalingEnabled && (isTableFixedColumnWidthsOptionEnabled || isCommentEditor); deleteColumnsWithAnalytics(api === null || api === 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 || api.core.actions.execute(closeActiveTableMenu()); api === null || api === 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 })); };