@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 { TableCellMergeIcon, ToolbarDropdownItem } from '@atlaskit/editor-toolbar';
import { closeActiveTableMenu } from '../../../../pm-plugins/commands';
import { mergeCellsWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics';
import { useTableMenuContext } from '../../shared/TableMenuContext';
/**
* Merge cells is only visible when the active selection can actually be merged
* (multi-cell, non-overlapping).
*/
var shouldShowMergeCells = function shouldShowMergeCells(tableMenuContext) {
return (tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.canMergeCells) === true;
};
export var MergeCellsItem = function MergeCellsItem(_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;
}
mergeCellsWithAnalytics(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 (!shouldShowMergeCells(tableMenuContext)) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
elemBefore: /*#__PURE__*/React.createElement(TableCellMergeIcon, {
label: "",
size: "small"
})
}, formatMessage(messages.mergeCells));
};