@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
75 lines • 3.79 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 var DeleteRowItem = function DeleteRowItem(props) {
var _tableMenuContext$sel, _tooltip;
var api = props.api;
var tableMenuContext = useTableMenuContext();
var _ref = tableMenuContext !== null && tableMenuContext !== void 0 ? tableMenuContext : {},
editorView = _ref.editorView;
var _useSharedPluginState = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], function (states) {
var _states$tableState;
return {
isHeaderRowRequired: (_states$tableState = states.tableState) === null || _states$tableState === void 0 || (_states$tableState = _states$tableState.pluginConfig) === null || _states$tableState === void 0 ? void 0 : _states$tableState.isHeaderRowRequired
};
}),
isHeaderRowRequired = _useSharedPluginState.isHeaderRowRequired;
var selectedRowCount = (_tableMenuContext$sel = tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.selectedRowCount) !== null && _tableMenuContext$sel !== void 0 ? _tableMenuContext$sel : 1;
var _useIntl = useIntl(),
formatMessage = _useIntl.formatMessage;
var handleMouseEnter = function handleMouseEnter() {
if (!editorView) {
return;
}
var selectionRect = getSelectionRect(editorView.state.selection);
if (!selectionRect) {
return;
}
hoverRows(getSelectedRowIndexes(selectionRect), true)(editorView.state, editorView.dispatch);
};
var handleMouseLeave = function handleMouseLeave() {
if (!editorView) {
return;
}
clearHoverSelection()(editorView.state, editorView.dispatch);
};
var handleClick = function handleClick() {
var _api$analytics;
if (!editorView) {
return;
}
var selectionRect = getSelectionRect(editorView.state.selection);
if (!selectionRect) {
return;
}
deleteRowsWithAnalytics(api === null || api === 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 || api.core.actions.execute(closeActiveTableMenu());
api === null || api === 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
}));
};