UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

188 lines 10 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React, { Component } from 'react'; import classnames from 'classnames'; import { isSSR } from '@atlaskit/editor-common/core-utils'; import { Selection } from '@atlaskit/editor-prosemirror/state'; import { isRowSelected } from '@atlaskit/editor-tables/utils'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { clearHoverSelection } from '../../../pm-plugins/commands'; import { getRowHeights } from '../../../pm-plugins/utils/row-controls'; import { TableCssClassName as ClassName } from '../../../types'; import { tableBorderColor } from '../../consts'; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components, @typescript-eslint/no-explicit-any var NumberColumn = /*#__PURE__*/function (_Component) { function NumberColumn() { var _this; _classCallCheck(this, NumberColumn); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, NumberColumn, [].concat(args)); _defineProperty(_this, "hoverRows", function (index) { return _this.props.tableActive ? _this.props.hoverRows([index]) : null; }); _defineProperty(_this, "selectRow", function (index, event) { var _this$props = _this.props, tableActive = _this$props.tableActive, editorView = _this$props.editorView, selectRow = _this$props.selectRow; // If selection is outside the table then first reset the selection inside table if (!tableActive && event.target && event.target instanceof Node) { var _editorView$state = editorView.state, doc = _editorView$state.doc, selection = _editorView$state.selection, tr = _editorView$state.tr; var pos = editorView.posAtDOM(event.target, 1); var $pos = doc.resolve(pos); var newPos = selection.head > pos ? // Selection is after table // nodeSize - 3 will move the position inside last table cell Selection.near(doc.resolve(pos + ($pos.parent.nodeSize - 3)), -1) : // Selection is before table Selection.near($pos); editorView.dispatch(tr.setSelection(newPos)); } selectRow(index, event.shiftKey); }); _defineProperty(_this, "clearHoverSelection", function () { var _this$props2 = _this.props, tableActive = _this$props2.tableActive, editorView = _this$props2.editorView; if (tableActive) { var state = editorView.state, dispatch = editorView.dispatch; clearHoverSelection()(state, dispatch); } }); _defineProperty(_this, "getCellStyles", function (index, rowHeight) { var _this$props3 = _this.props, stickyTop = _this$props3.stickyTop, hasHeaderRow = _this$props3.hasHeaderRow; if (stickyTop && hasHeaderRow && index === 0) { var topOffset = 0; return { height: rowHeight, top: "".concat(topOffset, "px") }; } return { height: rowHeight }; }); _defineProperty(_this, "getClassNames", function (index) { var isButtonDisabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var _this$props4 = _this.props, hoveredRows = _this$props4.hoveredRows, editorView = _this$props4.editorView, isInDanger = _this$props4.isInDanger, isResizing = _this$props4.isResizing, tableActive = _this$props4.tableActive; var isActive = isRowSelected(index)(editorView.state.selection) || (hoveredRows || []).indexOf(index) !== -1 && !isResizing; return classnames(ClassName.NUMBERED_COLUMN_BUTTON, _defineProperty(_defineProperty(_defineProperty({}, ClassName.NUMBERED_COLUMN_BUTTON_DISABLED, isButtonDisabled), ClassName.HOVERED_CELL_IN_DANGER, tableActive && isActive && isInDanger), ClassName.HOVERED_CELL_ACTIVE, tableActive && isActive)); }); return _this; } _inherits(NumberColumn, _Component); return _createClass(NumberColumn, [{ key: "render", value: function render() { var _this2 = this; var _this$props5 = this.props, tableRef = _this$props5.tableRef, hasHeaderRow = _this$props5.hasHeaderRow, isDragAndDropEnabled = _this$props5.isDragAndDropEnabled, tableActive = _this$props5.tableActive, updateCellHoverLocation = _this$props5.updateCellHoverLocation; var rowHeights = getRowHeights(tableRef); if (isSSR()) { return /*#__PURE__*/React.createElement("div", { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 className: ClassName.NUMBERED_COLUMN, style: { // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage/preview marginTop: hasHeaderRow && this.props.stickyTop !== undefined ? rowHeights[0] : undefined, borderLeft: // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766 isDragAndDropEnabled && tableActive ? "1px solid ".concat(tableBorderColor) : undefined, // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop visibility: 'hidden' // Ensure the column is not visible during SSR }, contentEditable: false }); } return /*#__PURE__*/React.createElement("div", { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 className: ClassName.NUMBERED_COLUMN, style: { // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage/preview marginTop: hasHeaderRow && this.props.stickyTop !== undefined ? rowHeights[0] : undefined, borderLeft: // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766 isDragAndDropEnabled && tableActive ? "1px solid ".concat(tableBorderColor) : undefined, // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop visibility: 'visible' }, contentEditable: false }, rowHeights.map(function (rowHeight, index) { return isDragAndDropEnabled ? /*#__PURE__*/React.createElement("div", { // Ignored via go/ees005 // eslint-disable-next-line react/no-array-index-key key: "wrapper-".concat(index) // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 , className: _this2.getClassNames(index, true), "data-index": index // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766 , style: _this2.getCellStyles(index, rowHeight), onFocus: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? function () { return updateCellHoverLocation(index); } : undefined, onMouseOver: function onMouseOver() { return updateCellHoverLocation(index); } }, hasHeaderRow ? index > 0 ? index : null : index + 1) : /*#__PURE__*/React.createElement("div", { // Ignored via go/ees005 role: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? 'button' : undefined // eslint-disable-next-line react/no-array-index-key , key: "wrapper-".concat(index) // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 , className: _this2.getClassNames(index), "data-index": index // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766 , style: _this2.getCellStyles(index, rowHeight), onClick: function onClick(event) { return _this2.selectRow(index, event); }, onFocus: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? function () { return _this2.hoverRows(index); } : undefined, onMouseOver: function onMouseOver() { return _this2.hoverRows(index); }, tabIndex: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? 0 : undefined, onKeyDown: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? function (event) { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); _this2.selectRow(index, event); } } : undefined, onMouseOut: _this2.clearHoverSelection, onBlur: expValEquals('platform_editor_table_a11y_eslint_fix', 'isEnabled', true) ? _this2.clearHoverSelection : undefined }, hasHeaderRow ? index > 0 ? index : null : index + 1); })); } }]); }(Component); export { NumberColumn as default };