@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
42 lines • 2.01 kB
JavaScript
import React from 'react';
import { useIntl } from 'react-intl';
import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { tableMessages as messages } from '@atlaskit/editor-common/messages';
import { TableCellSplitIcon, ToolbarDropdownItem } from '@atlaskit/editor-toolbar';
import { closeActiveTableMenu } from '../../../../pm-plugins/commands';
import { splitCellWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics';
import { useTableMenuContext } from '../../shared/TableMenuContext';
/**
* Split cell is only visible when the active selection sits in a cell whose
* `rowspan` or `colspan` is greater than one.
*/
var shouldShowSplitCell = function shouldShowSplitCell(tableMenuContext) {
return (tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.canSplitCell) === true;
};
export var SplitCellItem = function SplitCellItem(_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 handleClick = function handleClick() {
var _api$analytics;
if (!editorView) {
return;
}
splitCellWithAnalytics(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(INPUT_METHOD.CONTEXT_MENU)(editorView.state, editorView.dispatch);
api === null || api === void 0 || api.core.actions.execute(closeActiveTableMenu());
api === null || api === void 0 || api.core.actions.focus();
};
if (!shouldShowSplitCell(tableMenuContext)) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
elemBefore: /*#__PURE__*/React.createElement(TableCellSplitIcon, {
label: "",
size: "small"
})
}, formatMessage(messages.splitCell));
};