@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
121 lines (118 loc) • 6.03 kB
JavaScript
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 const tableNodeSpecWithFixedToDOM = config => {
const tableNode = config.isNestingSupported ? tableWithNestedTable : table;
return {
...tableNode,
toDOM: node => {
const alignmentStyle = Object.entries(getAlignmentStyle(node.attrs.layout)).map(([k, v]) => `${kebabCase(k)}: ${kebabCase(v)}`).join(';');
const tableMinWidth = getResizerMinWidth(node);
const isFullPageEditor = !config.isChromelessEditor && !config.isCommentEditor;
const 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-${node.attrs.localId}`,
'data-ssr-placeholder-replace': `table-${node.attrs.localId}`
};
// This would be used for table scaling in colgroup CSS
// cqw, or px is well supported
const resizableTableWidth = isFullPageEditor ? getTableResizerContainerForFullPageWidthInCSS(node, config.isTableScalingEnabled) : `calc(100cqw - calc(var(--ak-editor--large-gutter-padding) * 2))`;
let colgroup = '';
if (config.allowColumnResizing) {
colgroup = ['colgroup', {}, ...generateColgroupFromNode(node, config.isCommentEditor, config.isChromelessEditor, config.isNested, config.isTableScalingEnabled, config.shouldUseIncreasedScalingPercent)];
}
// For Chromeless editor, and nested tables in full page editor
const 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
const 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'
}]];
const 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];
}
const 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': `${TABLE_MAX_WIDTH}px`,
'--ak-editor-table-min-width': `${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];
}
};
};