UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

52 lines • 2.46 kB
import React from 'react'; import { useIntl } from 'react-intl'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums'; import { addRowBefore, tooltip } from '@atlaskit/editor-common/keymaps'; import { messages } from '@atlaskit/editor-common/messages/table'; import { getSelectionRect } from '@atlaskit/editor-tables/utils/get-selection-rect'; import { TableRowAddAboveIcon } from '@atlaskit/editor-toolbar/row-add-above-icon'; import { ToolbarDropdownItem } from '@atlaskit/editor-toolbar/toolbar-dropdown-item'; import { ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar/toolbar-keyboard-shortcut-hint'; import { closeActiveTableMenu } from '../../../../pm-plugins/commands'; import { insertRowWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics'; import { useTableMenuContext } from '../../shared/TableMenuContext'; export const AddRowAboveItem = props => { var _useTableMenuContext, _tooltip; const { api } = props; const { editorView } = (_useTableMenuContext = useTableMenuContext()) !== null && _useTableMenuContext !== void 0 ? _useTableMenuContext : {}; const { formatMessage } = useIntl(); const handleClick = () => { var _api$analytics; if (!editorView) { return; } const selectionRect = getSelectionRect(editorView.state.selection); const index = selectionRect === null || selectionRect === void 0 ? void 0 : selectionRect.top; if (index === undefined) { return; } insertRowWithAnalytics(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, { index, moveCursorToInsertedRow: true })(editorView.state, editorView.dispatch); api === null || api === void 0 ? void 0 : api.core.actions.execute(closeActiveTableMenu(api)); api === null || api === void 0 ? void 0 : api.core.actions.focus(); }; return /*#__PURE__*/React.createElement(ToolbarDropdownItem, { onClick: handleClick, elemBefore: /*#__PURE__*/React.createElement(TableRowAddAboveIcon, { color: "currentColor", label: "", size: "small" }), elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, { shortcut: (_tooltip = tooltip(addRowBefore)) !== null && _tooltip !== void 0 ? _tooltip : '' }) }, formatMessage(messages.addRowAbove)); };