@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
166 lines (163 loc) • 6.15 kB
JavaScript
/**
* @jsxRuntime classic
* @jsx jsx
*/
import { Fragment, useCallback } from 'react';
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { css, jsx } from '@emotion/react';
import { getBrowserInfo } from '@atlaskit/editor-common/browser';
import { findTable } from '@atlaskit/editor-tables/utils';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { hoverCell, hoverRows, selectRow, selectRows } from '../../pm-plugins/commands';
import { isTableNested } from '../../pm-plugins/utils/nodes';
import { TableCssClassName as ClassName } from '../../types';
import { DragCornerControlsWithSelection } from './CornerControls/DragCornerControls';
import { FloatingControlsWithSelection } from './FloatingControlsWithSelection';
import NumberColumn from './NumberColumn';
import { DragControlsWithSelection } from './RowControls/DragControls';
const styles = css({
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors
'&:has(~ .pm-table-wrapper-no-overflow)': {
marginTop: 0
}
});
// Row controls
export const TableFloatingControls = ({
editorView,
tableRef,
tableNode,
isInDanger,
isResizing,
isNumberColumnEnabled,
isHeaderRowEnabled,
isHeaderColumnEnabled,
tableActive,
hasHeaderRow,
hoveredRows,
stickyHeader,
isDragAndDropEnabled,
hoveredCell,
isTableHovered,
tableWrapperWidth,
api,
isChromelessEditor
}) => {
var _findTable;
const _selectRow = useCallback((row, expand) => {
const {
state,
dispatch
} = editorView;
const browser = getBrowserInfo();
if (browser.ie_version === 11) {
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
editorView.dom.blur();
}
selectRow(row, expand)(state, dispatch);
}, [editorView]);
const _selectRows = useCallback(rowIndexes => {
const {
state,
dispatch
} = editorView;
const browser = getBrowserInfo();
if (browser.ie_version === 11) {
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
editorView.dom.blur();
}
selectRows(rowIndexes)(state, dispatch);
}, [editorView]);
const _hoverRows = useCallback((rows, danger) => {
const {
state,
dispatch
} = editorView;
hoverRows(rows, danger)(state, dispatch);
}, [editorView]);
// re-use across numbered columns and row controls
const updateCellHoverLocation = useCallback(rowIndex => {
const {
state,
dispatch
} = editorView;
if (tableActive) {
// For context: Whenever we mouse over a column or row drag handle, we will ALWAYS be hovering over the 0 index
// of the opposite dimension. For example; here when we mouse over the row drag handle then we're technically
// also hovering over column 0 index. And vice-versa with columns. This means we don't need to worry about knowing the
// current column index. We can just force it to 0.
hoverCell(rowIndex, 0)(state, dispatch);
}
}, [editorView, tableActive]);
if (!tableRef) {
return null;
}
const stickyTop = stickyHeader && stickyHeader.sticky && hasHeaderRow ? stickyHeader.top : undefined;
const wrapperClassName = isDragAndDropEnabled ? isChromelessEditor ? ClassName.DRAG_ROW_CONTROLS_WRAPPER + ' ' + ClassName.TABLE_CHROMELESS : ClassName.DRAG_ROW_CONTROLS_WRAPPER : ClassName.ROW_CONTROLS_WRAPPER;
const tablePos = (_findTable = findTable(editorView.state.selection)) === null || _findTable === void 0 ? void 0 : _findTable.pos;
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const isNested = tablePos !== undefined && isTableNested(editorView.state, tablePos);
const shouldShowCornerControls = isNested && !fg('platform_editor_nested_dnd_styles_changes');
return jsx("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: wrapperClassName,
css: [expValEquals('platform_editor_table_sticky_header_improvements', 'cohort', 'test_with_overflow') && !fg('platform_editor_table_sticky_header_patch_7') && styles]
}, jsx("div", {
role: "none",
onMouseDown: e => !isDragAndDropEnabled && e.preventDefault()
}, isNumberColumnEnabled ? jsx(NumberColumn, {
editorView: editorView,
hoverRows: _hoverRows,
tableRef: tableRef,
tableActive: tableActive,
hoveredRows: hoveredRows,
hasHeaderRow: hasHeaderRow,
isInDanger: isInDanger,
isResizing: isResizing,
selectRow: _selectRow,
updateCellHoverLocation: updateCellHoverLocation,
stickyTop: stickyTop,
isDragAndDropEnabled: isDragAndDropEnabled
}) : null, tableActive && jsx(Fragment, null, isDragAndDropEnabled ? jsx(Fragment, null, shouldShowCornerControls && jsx(DragCornerControlsWithSelection, {
editorView: editorView,
tableRef: tableRef,
isInDanger: isInDanger,
isResizing: isResizing,
api: api
}), jsx(DragControlsWithSelection, {
tableRef: tableRef,
tableNode: tableNode,
hoveredCell: hoveredCell,
isTableHovered: isTableHovered,
editorView: editorView,
tableActive: tableActive,
isInDanger: isInDanger,
isResizing: isResizing
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
,
tableWidth: tableWrapperWidth,
hoverRows: _hoverRows,
selectRow: _selectRow,
selectRows: _selectRows,
updateCellHoverLocation: updateCellHoverLocation,
api: api
})) : jsx(FloatingControlsWithSelection, {
editorView: editorView,
tableRef: tableRef,
isInDanger: isInDanger,
isResizing: isResizing,
isHeaderRowEnabled: isHeaderRowEnabled,
isHeaderColumnEnabled: isHeaderColumnEnabled,
hoveredRows: hoveredRows,
stickyTop: tableActive ? stickyTop : undefined,
tableActive: tableActive,
hoverRows: _hoverRows,
selectRow: _selectRow,
api: api
}))));
};
export default TableFloatingControls;