@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
56 lines (53 loc) • 2.24 kB
JavaScript
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
/**
* Returns true if any cell in the first row of the table has a colwidth attribute set.
*
* Used by both the editor (as `hasTableColumnBeenResized`) and the renderer (as `hasColWidths`)
* to determine whether a table has had its columns manually resized.
*/
export const hasTableColumnBeenResized = tableNode => {
const firstRow = tableNode.content.firstChild;
if (!firstRow) {
return false;
}
for (let i = 0; i < firstRow.childCount; i++) {
if (firstRow.child(i).attrs.colwidth) {
return true;
}
}
return false;
};
/**
* Returns true if the table has been explicitly resized — either the table itself has a width
* attribute set, or any column has been individually resized (colwidth present on cells).
*/
export const hasTableBeenResized = tableNode => tableNode.attrs.width !== null || hasTableColumnBeenResized(tableNode);
/**
* Determines whether a table should render in content mode.
*
* Content mode tables have no fixed column widths — the browser sizes columns to fit their
* content (`table-layout: auto`). This is the shared core predicate used by both the editor
* and the renderer. Each consumer is responsible for computing `isSupported` from its own
* feature flags / props before calling this function.
*
* A table is in content mode when ALL of the following are true:
* 1. The `platform_editor_table_fit_to_content_auto_convert` experiment is enabled
* 2. `isSupported` is true (caller has verified resizing is allowed and appearance is full-page)
* 3. The table is not nested inside another table or block node
* 4. The table node exists
* 5. The table has not been explicitly resized (`width === null` and no `colwidth` on cells)
* 6. The table's layout is `'align-start'`
*/
export const isTableInContentMode = ({
tableNode,
isSupported,
isTableNested
}) => {
if (!expValEqualsNoExposure('platform_editor_table_fit_to_content_auto_convert', 'isEnabled', true)) {
return false;
}
if (!tableNode || isTableNested) {
return false;
}
return isSupported && !hasTableBeenResized(tableNode) && tableNode.attrs.layout === 'align-start';
};