UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

56 lines 3.47 kB
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 { addColumnBefore, tooltip } from '@atlaskit/editor-common/keymaps'; import { tableMessages as messages } from '@atlaskit/editor-common/messages'; import { getSelectionRect } from '@atlaskit/editor-tables/utils'; import { TableColumnAddLeftIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar'; import { closeActiveTableMenu } from '../../../../pm-plugins/commands'; import { insertColumnWithAnalytics } from '../../../../pm-plugins/commands/commands-with-analytics'; import { useTableMenuContext } from '../../shared/TableMenuContext'; export var AddColumnLeftItem = function AddColumnLeftItem(_ref) { var _useTableMenuContext, _tooltip; var api = _ref.api; var _ref2 = (_useTableMenuContext = useTableMenuContext()) !== null && _useTableMenuContext !== void 0 ? _useTableMenuContext : {}, editorView = _ref2.editorView; var _useIntl = useIntl(), formatMessage = _useIntl.formatMessage; var _useSharedPluginState = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table'], function (states) { var tableState = states.tableState; return { isCommentEditor: tableState === null || tableState === void 0 ? void 0 : tableState.isCommentEditor, isTableFixedColumnWidthsOptionEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isTableFixedColumnWidthsOptionEnabled, isTableScalingEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isTableScalingEnabled }; }), isCommentEditor = _useSharedPluginState.isCommentEditor, isTableFixedColumnWidthsOptionEnabled = _useSharedPluginState.isTableFixedColumnWidthsOptionEnabled, isTableScalingEnabled = _useSharedPluginState.isTableScalingEnabled; var handleClick = function handleClick() { var _api$analytics; if (!editorView) { return; } var selectionRect = getSelectionRect(editorView.state.selection); var index = selectionRect === null || selectionRect === void 0 ? void 0 : selectionRect.left; if (index === undefined) { return; } var shouldUseIncreasedScalingPercent = isTableScalingEnabled && (isTableFixedColumnWidthsOptionEnabled || isCommentEditor); insertColumnWithAnalytics(api, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent, isCommentEditor)(INPUT_METHOD.TABLE_CONTEXT_MENU, index)(editorView.state, editorView.dispatch, editorView); 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, elemBefore: /*#__PURE__*/React.createElement(TableColumnAddLeftIcon, { color: "currentColor", label: "", size: "small" }), elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, { shortcut: (_tooltip = tooltip(addColumnBefore)) !== null && _tooltip !== void 0 ? _tooltip : '' }) }, formatMessage(messages.addColumnLeft)); };