@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
130 lines (127 loc) • 7.19 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 kebabCase from 'lodash/kebabCase';
import { table, tableWithNestedTable } from '@atlaskit/adf-schema';
import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { generateColgroupFromNode, getResizerMinWidth } from '../pm-plugins/table-resizing/utils/colgroup';
import { TABLE_MAX_WIDTH } from '../pm-plugins/table-resizing/utils/consts';
import { getTableResizerContainerMaxWidthInCSS, getTableResizerContainerForFullPageWidthInCSS, getTableResizerItemWidthInCSS } from '../pm-plugins/table-resizing/utils/misc';
import { getAlignmentStyle } from './table-container-styles';
export var tableNodeSpecWithFixedToDOM = function tableNodeSpecWithFixedToDOM(config) {
var tableNode = config.isNestingSupported ? tableWithNestedTable : table;
return _objectSpread(_objectSpread({}, tableNode), {}, {
toDOM: function toDOM(node) {
var alignmentStyle = Object.entries(getAlignmentStyle(node.attrs.layout)).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
k = _ref2[0],
v = _ref2[1];
return "".concat(kebabCase(k), ": ").concat(kebabCase(v));
}).join(';');
var tableMinWidth = getResizerMinWidth(node);
var isFullPageEditor = !config.isChromelessEditor && !config.isCommentEditor;
var attrs = {
'data-number-column': node.attrs.isNumberColumnEnabled,
'data-layout': node.attrs.layout,
'data-autosize': node.attrs.__autoSize,
'data-table-local-id': node.attrs.localId,
'data-table-width': node.attrs.width,
'data-ssr-placeholder': "table-".concat(node.attrs.localId),
'data-ssr-placeholder-replace': "table-".concat(node.attrs.localId)
};
// This would be used for table scaling in colgroup CSS
// cqw, or px is well supported
var resizableTableWidth = isFullPageEditor ? getTableResizerContainerForFullPageWidthInCSS(node, config.isTableScalingEnabled) : "calc(100cqw - calc(var(--ak-editor--large-gutter-padding) * 2))";
var colgroup = '';
if (config.allowColumnResizing) {
colgroup = ['colgroup', {}].concat(_toConsumableArray(generateColgroupFromNode(node, config.isCommentEditor, config.isChromelessEditor, config.isNested, config.isTableScalingEnabled, config.shouldUseIncreasedScalingPercent)));
}
// For Chromeless editor, and nested tables in full page editor
var tableContainerDivLegacy = ['div', {
class: 'pm-table-container',
'data-number-column': node.attrs.isNumberColumnEnabled,
'data-layout': node.attrs.layout,
'data-testid': 'table-container'
}, ['div', {
class: 'pm-table-sticky-sentinel-top',
'data-testid': 'sticky-sentinel-top'
}], ['div', {
class: 'pm-table-row-controls-wrapper'
}, ['div']], ['div', {
class: 'pm-table-with-left-shadow',
style: 'visibility: hidden'
}], ['div', {
class: 'pm-table-wrapper'
}, ['table', attrs, ['span', {
class: 'pm-table-shadow-sentinel-right'
}], ['span', {
class: 'pm-table-shadow-sentinel-left'
}], colgroup, ['tbody', 0]]], ['div', {
class: 'pm-table-with-right-shadow',
style: 'visibility: hidden'
}], ['div', {
class: 'pm-table-sticky-sentinel-bottom',
'data-testid': 'sticky-sentinel-bottom'
}]];
// removed the left/right shadow divs
var tableContainerDivNext = ['div', {
class: 'pm-table-container',
'data-number-column': node.attrs.isNumberColumnEnabled,
'data-layout': node.attrs.layout,
'data-testid': 'table-container'
}, ['div', {
class: 'pm-table-sticky-sentinel-top',
'data-testid': 'sticky-sentinel-top'
}], ['div', {
class: 'pm-table-row-controls-wrapper'
}, ['div']], ['div', {
class: 'pm-table-wrapper'
}, ['table', attrs, colgroup, ['tbody', 0]]], ['div', {
class: 'pm-table-sticky-sentinel-bottom',
'data-testid': 'sticky-sentinel-bottom'
}]];
var tableContainerDiv = expValEquals('platform_editor_disable_table_overflow_shadows', 'cohort', 'variant1') || expValEquals('platform_editor_disable_table_overflow_shadows', 'cohort', 'variant2') || expValEquals('platform_editor_disable_table_overflow_shadows', 'cohort', 'variant3') ? tableContainerDivNext : tableContainerDivLegacy;
if (!config.tableResizingEnabled || config.isNested) {
return ['div', {
class: 'tableView-content-wrap',
'data-prosemirror-initial-toDOM-render': 'true',
style: convertToInlineCss({
'--ak-editor-table-width': resizableTableWidth
})
}, tableContainerDiv];
}
var tableResizingDiv = ['div', {
'data-testid': 'table-alignment-container',
style: alignmentStyle
}, ['div', {
class: 'pm-table-resizer-container',
style: convertToInlineCss({
'--ak-editor-table-gutter-padding': config.isTableScalingEnabled ? 'calc(var(--ak-editor--large-gutter-padding) * 2)' : 'calc(var(--ak-editor--large-gutter-padding) * 2 - var(--ak-editor-resizer-handle-spacing))',
'--ak-editor-table-width': resizableTableWidth,
width: "var(--ak-editor-table-width)"
})
}, ['div', {
class: 'resizer-item display-handle',
style: convertToInlineCss({
position: 'relative',
userSelect: 'auto',
boxSizing: 'border-box',
'--ak-editor-table-max-width': "".concat(TABLE_MAX_WIDTH, "px"),
'--ak-editor-table-min-width': "".concat(tableMinWidth, "px"),
minWidth: 'var(--ak-editor-table-min-width)',
maxWidth: getTableResizerContainerMaxWidthInCSS(config.isCommentEditor, config.isChromelessEditor, config.isTableScalingEnabled),
width: getTableResizerItemWidthInCSS(node, config.isCommentEditor, config.isChromelessEditor)
})
}, ['span', {
class: 'resizer-hover-zone'
}, tableContainerDiv]]]];
return ['div', {
class: 'tableView-content-wrap',
'data-prosemirror-initial-toDOM-render': 'true'
}, tableResizingDiv];
}
});
};