UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

65 lines 3.93 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 { tableMessages as messages } from '@atlaskit/editor-common/messages'; import { getSelectionRect } from '@atlaskit/editor-tables/utils'; import { TableColumnsDistributeIcon, ToolbarDropdownItem } from '@atlaskit/editor-toolbar'; import { closeActiveTableMenu } from '../../../../pm-plugins/commands'; import { distributeColumnsWidthsWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics'; import { getNewResizeStateFromSelectedColumns } from '../../../../pm-plugins/table-resizing/utils/resize-state'; import { useTableMenuContext } from '../../shared/TableMenuContext'; /** Distribute columns is only visible when more than one column is selected. */ var shouldShowDistributeColumns = function shouldShowDistributeColumns(selectedColumnCount) { return (selectedColumnCount !== null && selectedColumnCount !== void 0 ? selectedColumnCount : 0) > 1; }; export var DistributeColumnsItem = function DistributeColumnsItem(_ref) { var api = _ref.api; var tableMenuContext = useTableMenuContext(); var _ref2 = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {}, editorView = _ref2.editorView; 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 handleClick = function handleClick() { var _api$width, _api$analytics; if (!editorView) { return; } var selectionRect = getSelectionRect(editorView.state.selection); var editorContainerWidth = api === null || api === void 0 || (_api$width = api.width) === null || _api$width === void 0 ? void 0 : _api$width.sharedState.currentState(); if (!selectionRect || !editorContainerWidth) { return; } var newResizeState = getNewResizeStateFromSelectedColumns(selectionRect, editorView.state, editorView.domAtPos.bind(editorView), function () { return editorContainerWidth; }, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, isCommentEditor); if (!newResizeState) { return; } distributeColumnsWidthsWithAnalytics(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, api)(INPUT_METHOD.TABLE_CONTEXT_MENU, newResizeState)(editorView.state, editorView.dispatch); api === null || api === void 0 || api.core.actions.execute(closeActiveTableMenu()); api === null || api === void 0 || api.core.actions.focus(); }; if (!shouldShowDistributeColumns(tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedColumnCount)) { return null; } return /*#__PURE__*/React.createElement(ToolbarDropdownItem, { onClick: handleClick, elemBefore: /*#__PURE__*/React.createElement(TableColumnsDistributeIcon, { color: "currentColor", label: "", size: "small" }) }, formatMessage(messages.distributeColumns)); };