@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
57 lines • 3.16 kB
JavaScript
import React from 'react';
import { useIntl } from 'react-intl';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { tableMessages as messages } from '@atlaskit/editor-common/messages';
import { ToolbarDropdownItem } from '@atlaskit/editor-toolbar';
import Toggle from '@atlaskit/toggle';
import { toggleHeaderRowWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics';
import { useTableMenuContext } from '../../shared/TableMenuContext';
/** Header row toggle is only visible when the first row is the entire selection. */
var shouldShowHeaderRowToggle = function shouldShowHeaderRowToggle(_ref) {
var isFirstRow = _ref.isFirstRow,
isHeaderRowAllowed = _ref.isHeaderRowAllowed,
selectedRowCount = _ref.selectedRowCount;
return isHeaderRowAllowed === true && isFirstRow && selectedRowCount === 1;
};
export var HeaderRowToggleItem = function HeaderRowToggleItem(props) {
var _tableMenuContext$sel;
var api = props.api;
var tableMenuContext = useTableMenuContext();
var _ref2 = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {},
editorView = _ref2.editorView;
var _useSharedPluginState = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], function (states) {
var _states$tableState, _states$tableState2;
return {
isHeaderRowAllowed: (_states$tableState = states.tableState) === null || _states$tableState === void 0 || (_states$tableState = _states$tableState.pluginConfig) === null || _states$tableState === void 0 ? void 0 : _states$tableState.allowHeaderRow,
isHeaderRowEnabled: (_states$tableState2 = states.tableState) === null || _states$tableState2 === void 0 ? void 0 : _states$tableState2.isHeaderRowEnabled
};
}),
isHeaderRowAllowed = _useSharedPluginState.isHeaderRowAllowed,
isHeaderRowEnabled = _useSharedPluginState.isHeaderRowEnabled;
var selectedRowCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedRowCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 0;
var _useIntl = useIntl(),
formatMessage = _useIntl.formatMessage;
var label = formatMessage(messages.headerRow);
var handleClick = function handleClick() {
var _api$analytics;
if (!editorView) {
return;
}
toggleHeaderRowWithAnalytics(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(editorView.state, editorView.dispatch);
};
if (!shouldShowHeaderRowToggle({
isFirstRow: (tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.isFirstRow) === true,
isHeaderRowAllowed: isHeaderRowAllowed,
selectedRowCount: selectedRowCount
})) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
elemAfter: /*#__PURE__*/React.createElement(Toggle, {
label: label,
isChecked: !!isHeaderRowEnabled,
onChange: handleClick
})
}, label);
};