@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
65 lines • 3.61 kB
JavaScript
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. */
const shouldShowDistributeColumns = selectedColumnCount => (selectedColumnCount !== null && selectedColumnCount !== void 0 ? selectedColumnCount : 0) > 1;
export const DistributeColumnsItem = ({
api
}) => {
const tableMenuContext = useTableMenuContext();
const {
editorView
} = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {};
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 handleClick = () => {
var _api$width, _api$analytics;
if (!editorView) {
return;
}
const selectionRect = getSelectionRect(editorView.state.selection);
const editorContainerWidth = api === null || api === void 0 ? void 0 : (_api$width = api.width) === null || _api$width === void 0 ? void 0 : _api$width.sharedState.currentState();
if (!selectionRect || !editorContainerWidth) {
return;
}
const newResizeState = getNewResizeStateFromSelectedColumns(selectionRect, editorView.state, editorView.domAtPos.bind(editorView), () => editorContainerWidth, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, isCommentEditor);
if (!newResizeState) {
return;
}
distributeColumnsWidthsWithAnalytics(api === null || api === void 0 ? 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 ? void 0 : api.core.actions.execute(closeActiveTableMenu());
api === null || api === void 0 ? 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));
};