@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
61 lines • 2.86 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. */
const shouldShowHeaderRowToggle = ({
isFirstRow,
isHeaderRowAllowed,
selectedRowCount
}) => isHeaderRowAllowed === true && isFirstRow && selectedRowCount === 1;
export const HeaderRowToggleItem = props => {
var _tableMenuContext$sel;
const {
api
} = props;
const tableMenuContext = useTableMenuContext();
const {
editorView
} = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {};
const {
isHeaderRowAllowed,
isHeaderRowEnabled
} = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], states => {
var _states$tableState, _states$tableState$pl, _states$tableState2;
return {
isHeaderRowAllowed: (_states$tableState = states.tableState) === null || _states$tableState === void 0 ? void 0 : (_states$tableState$pl = _states$tableState.pluginConfig) === null || _states$tableState$pl === void 0 ? void 0 : _states$tableState$pl.allowHeaderRow,
isHeaderRowEnabled: (_states$tableState2 = states.tableState) === null || _states$tableState2 === void 0 ? void 0 : _states$tableState2.isHeaderRowEnabled
};
});
const selectedRowCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedRowCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 0;
const {
formatMessage
} = useIntl();
const label = formatMessage(messages.headerRow);
const handleClick = () => {
var _api$analytics;
if (!editorView) {
return;
}
toggleHeaderRowWithAnalytics(api === null || api === void 0 ? 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,
selectedRowCount
})) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
elemAfter: /*#__PURE__*/React.createElement(Toggle, {
label: label,
isChecked: !!isHeaderRowEnabled,
onChange: handleClick
})
}, label);
};