UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

66 lines 3.93 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { memo, useMemo } from 'react'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { TableMap } from '@atlaskit/editor-tables/table-map'; import { getSelectionRect } from '@atlaskit/editor-tables/utils'; import { ToolbarMenuContainer } from '@atlaskit/editor-toolbar/toolbar-menu-container'; import { SurfaceRenderer } from '@atlaskit/editor-ui-control-model'; import { canSplitCellSelection } from '../../../pm-plugins/commands/split-cell'; import { canMergeCellSelection } from '../../../pm-plugins/transforms/merge'; import { TableMenuProvider } from './TableMenuContext'; export var TableMenu = /*#__PURE__*/memo(function (_ref) { var api = _ref.api, editorView = _ref.editorView, surface = _ref.surface; var components = useMemo(function () { var _api$uiControlRegistr, _api$uiControlRegistr2; return (_api$uiControlRegistr = api === null || api === void 0 || (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(surface.key)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : []; }, [api, surface.key]); var _useSharedPluginState = useSharedPluginStateWithSelector(api !== null && api !== void 0 ? api : undefined, ['table', 'selection'], function (states) { var _states$tableState, _states$selectionStat; return { tableNode: (_states$tableState = states.tableState) === null || _states$tableState === void 0 ? void 0 : _states$tableState.tableNode, selection: (_states$selectionStat = states.selectionState) === null || _states$selectionStat === void 0 ? void 0 : _states$selectionStat.selection }; }), tableNode = _useSharedPluginState.tableNode, selection = _useSharedPluginState.selection; var tableMenuContext = useMemo(function () { if (!selection || !tableNode) { return { editorView: editorView }; } var tableMap = TableMap.get(tableNode); var selectionRect = getSelectionRect(selection); var cellOps = { editorView: editorView, canMergeCells: canMergeCellSelection(selection), canSplitCell: canSplitCellSelection(selection), hasMergedCellsInTable: tableMap.hasMergedCells() }; if (!selectionRect) { return cellOps; } return _objectSpread(_objectSpread({}, cellOps), {}, { isFirstRow: selectionRect.top === 0, isLastRow: selectionRect.bottom === tableMap.height, selectedRowCount: selectionRect.bottom - selectionRect.top, isFirstColumn: selectionRect.left === 0, isLastColumn: selectionRect.right === tableMap.width, selectedColumnCount: selectionRect.right - selectionRect.left }); }, [editorView, selection, tableNode]); if (components.length === 0) { return null; } return /*#__PURE__*/React.createElement(TableMenuProvider, { value: tableMenuContext }, /*#__PURE__*/React.createElement(ToolbarMenuContainer, null, /*#__PURE__*/React.createElement(SurfaceRenderer, { surface: surface, components: components }))); }); TableMenu.displayName = 'TableMenu';