@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
63 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 { moveRowUp, tooltip } from '@atlaskit/editor-common/keymaps';
import { tableMessages as messages } from '@atlaskit/editor-common/messages';
import { getSelectionRect } from '@atlaskit/editor-tables/utils';
import { TableRowMoveUpIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar';
import { closeActiveTableMenu } from '../../../../pm-plugins/commands';
import { moveSourceWithAnalytics } from '../../../../pm-plugins/drag-and-drop/commands-with-analytics';
import { getPluginState } from '../../../../pm-plugins/plugin-factory';
import { getSelectedRowIndexes } from '../../../../pm-plugins/utils/selection';
import { TABLE_ROW } from '../../shared/consts';
import { useTableMenuContext } from '../../shared/TableMenuContext';
var shouldShowMoveRowUp = function shouldShowMoveRowUp(isFirstRow) {
return !isFirstRow;
};
export var MoveRowUpItem = function MoveRowUpItem(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 {
tableNode: (_states$tableState = states.tableState) === null || _states$tableState === void 0 ? void 0 : _states$tableState.tableNode
};
}),
tableNode = _useSharedPluginState.tableNode;
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 handleClick = function handleClick() {
var _api$analytics, _api$accessibilityUti;
if (!editorView) {
return;
}
var selectionRect = getSelectionRect(editorView.state.selection);
if (!selectionRect) {
return;
}
moveSourceWithAnalytics(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, api === null || api === void 0 || (_api$accessibilityUti = api.accessibilityUtils) === null || _api$accessibilityUti === void 0 ? void 0 : _api$accessibilityUti.actions.ariaNotify, getPluginState(editorView.state).getIntl)(INPUT_METHOD.TABLE_CONTEXT_MENU, TABLE_ROW, getSelectedRowIndexes(selectionRect), selectionRect.top - 1)(editorView.state, editorView.dispatch);
api === null || api === void 0 || api.core.actions.execute(closeActiveTableMenu());
api === null || api === void 0 || api.core.actions.focus();
};
if (!tableNode || !shouldShowMoveRowUp(tableMenuContext === null || tableMenuContext === void 0 ? void 0 : tableMenuContext.isFirstRow)) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
onClick: handleClick,
elemBefore: /*#__PURE__*/React.createElement(TableRowMoveUpIcon, {
color: "currentColor",
label: "",
size: "small"
}),
elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, {
shortcut: (_tooltip = tooltip(moveRowUp)) !== null && _tooltip !== void 0 ? _tooltip : ''
})
}, formatMessage(messages.moveRowUp, {
0: selectedRowCount
}));
};