@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
80 lines • 3.67 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 { deleteRow, tooltip } from '@atlaskit/editor-common/keymaps';
import { tableMessages as messages } from '@atlaskit/editor-common/messages';
import { getSelectionRect } from '@atlaskit/editor-tables/utils';
import { DeleteIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar';
import { clearHoverSelection, closeActiveTableMenu, hoverRows } from '../../../../pm-plugins/commands';
import { deleteRowsWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics';
import { getSelectedRowIndexes } from '../../../../pm-plugins/utils/selection';
import { useTableMenuContext } from '../../shared/TableMenuContext';
export const DeleteRowItem = props => {
var _tableMenuContext$sel, _tooltip;
const {
api
} = props;
const tableMenuContext = useTableMenuContext();
const {
editorView
} = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {};
const {
isHeaderRowRequired
} = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], states => {
var _states$tableState, _states$tableState$pl;
return {
isHeaderRowRequired: (_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.isHeaderRowRequired
};
});
const selectedRowCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedRowCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 1;
const {
formatMessage
} = useIntl();
const handleMouseEnter = () => {
if (!editorView) {
return;
}
const selectionRect = getSelectionRect(editorView.state.selection);
if (!selectionRect) {
return;
}
hoverRows(getSelectedRowIndexes(selectionRect), true)(editorView.state, editorView.dispatch);
};
const handleMouseLeave = () => {
if (!editorView) {
return;
}
clearHoverSelection()(editorView.state, editorView.dispatch);
};
const handleClick = () => {
var _api$analytics;
if (!editorView) {
return;
}
const selectionRect = getSelectionRect(editorView.state.selection);
if (!selectionRect) {
return;
}
deleteRowsWithAnalytics(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(INPUT_METHOD.TABLE_CONTEXT_MENU, selectionRect, !!isHeaderRowRequired)(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();
};
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
onFocus: handleMouseEnter,
onMouseEnter: handleMouseEnter,
onBlur: handleMouseLeave,
onMouseLeave: handleMouseLeave,
elemBefore: /*#__PURE__*/React.createElement(DeleteIcon, {
color: "currentColor",
label: "",
size: "small"
}),
elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, {
shortcut: (_tooltip = tooltip(deleteRow)) !== null && _tooltip !== void 0 ? _tooltip : ''
})
}, formatMessage(messages.removeRows, {
0: selectedRowCount
}));
};