@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
143 lines (140 loc) • 7.16 kB
JavaScript
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
import { TableSharedCssClassName } from '@atlaskit/editor-common/styles';
import { findTable } from '@atlaskit/editor-tables/utils';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { hasTableColumnBeenResized } from '../table-resizing/utils/colgroup';
import { applyTableMeasurement, getTableMeasurement } from '../transforms/content-mode';
import { ALIGN_START } from './alignment';
export var isTableInContentMode = function isTableInContentMode(_ref) {
var allowColumnResizing = _ref.allowColumnResizing,
allowTableResizing = _ref.allowTableResizing,
isFullPageEditor = _ref.isFullPageEditor,
isTableNested = _ref.isTableNested,
node = _ref.node;
if (!expValEqualsNoExposure('platform_editor_table_fit_to_content_auto_convert', 'isEnabled', true)) {
return false;
}
if (!node || isTableNested) {
return false;
}
return isContentModeSupported({
allowColumnResizing: allowColumnResizing,
allowTableResizing: allowTableResizing,
isFullPageEditor: isFullPageEditor
}) && !hasTableBeenResized(node) && node.attrs.layout === ALIGN_START;
};
export var isContentModeSupported = function isContentModeSupported(_ref2) {
var allowColumnResizing = _ref2.allowColumnResizing,
allowTableResizing = _ref2.allowTableResizing,
isFullPageEditor = _ref2.isFullPageEditor;
return allowColumnResizing && allowTableResizing && isFullPageEditor;
};
export var hasTableBeenResized = function hasTableBeenResized(node) {
return node.attrs.width !== null || hasTableColumnBeenResized(node);
};
/**
* Iterates all top-level tables in the document, and for those in content mode,
* measures rendered column widths and sets colwidth + table width attributes
* in a single batched transaction.
*/
export var applyMeasuredWidthToAllTables = function applyMeasuredWidthToAllTables(view, pluginInjectionApi) {
var _view$state = view.state,
doc = _view$state.doc,
schema = _view$state.schema;
var tr = view.state.tr;
var table = schema.nodes.table;
var modified = false;
var measuredTables = [];
// modify only top-level tables
doc.forEach(function (node, offset) {
if (node.type !== table || hasTableBeenResized(node) && node.attrs.layout !== ALIGN_START) {
return;
}
var domNode = view.domAtPos(offset + 1).node;
var tableWrapper = domNode instanceof HTMLElement ? domNode.closest(".".concat(TableSharedCssClassName.TABLE_VIEW_CONTENT_WRAP)) : null;
var tableRef = tableWrapper === null || tableWrapper === void 0 ? void 0 : tableWrapper.querySelector('table');
if (!tableRef) {
return;
}
measuredTables.push({
node: node,
offset: offset,
measurement: getTableMeasurement(tableRef)
});
});
measuredTables.forEach(function (_ref3) {
var node = _ref3.node,
offset = _ref3.offset,
measurement = _ref3.measurement;
tr = applyTableMeasurement(tr, node, measurement, offset);
modified = true;
});
if (modified) {
var _pluginInjectionApi$a, _pluginInjectionApi$w, _pluginInjectionApi$w2;
pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a === void 0 || (_pluginInjectionApi$a = _pluginInjectionApi$a.actions) === null || _pluginInjectionApi$a === void 0 || _pluginInjectionApi$a.attachAnalyticsEvent({
action: TABLE_ACTION.FIT_TO_CONTENT_AUTO_CONVERTED,
actionSubject: ACTION_SUBJECT.TABLE,
actionSubjectId: null,
eventType: EVENT_TYPE.TRACK,
attributes: {
editorContainerWidth: (_pluginInjectionApi$w = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$w2 = pluginInjectionApi.width) === null || _pluginInjectionApi$w2 === void 0 || (_pluginInjectionApi$w2 = _pluginInjectionApi$w2.sharedState.currentState()) === null || _pluginInjectionApi$w2 === void 0 ? void 0 : _pluginInjectionApi$w2.width) !== null && _pluginInjectionApi$w !== void 0 ? _pluginInjectionApi$w : 0,
totalTablesResized: measuredTables.length,
measurements: measuredTables.map(function (_ref4) {
var measurement = _ref4.measurement;
return {
tableWidth: measurement.tableWidth,
totalColumnCount: measurement.colWidths.length
};
})
}
})(tr);
view.dispatch(tr.setMeta('addToHistory', false));
}
};
export var applyMeasuredWidthToSelectedTable = function applyMeasuredWidthToSelectedTable(view, api) {
var _api$analytics, _api$width$sharedStat, _api$width;
var tableObject = findTable(view.state.selection);
if (!tableObject) {
return;
}
var node = tableObject.node,
pos = tableObject.pos;
var tableState = api === null || api === void 0 ? void 0 : api.table.sharedState.currentState();
if (!(tableState !== null && tableState !== void 0 && tableState.tableRef)) {
return;
}
var tableRef = tableState.tableRef;
// Instead of dispatching a transaction to "strip widths" and then waiting
// for a rAF to measure natural column widths, instea directly update the DOM elements and
// take a measurement.
var cols = Array.from(tableRef.querySelectorAll(':scope > colgroup > col'));
var contentWrap = tableRef.closest(".".concat(TableSharedCssClassName.TABLE_VIEW_CONTENT_WRAP));
var resizerContainer = contentWrap === null || contentWrap === void 0 ? void 0 : contentWrap.querySelector(".".concat(TableSharedCssClassName.TABLE_RESIZER_CONTAINER));
var resizerItem = resizerContainer === null || resizerContainer === void 0 ? void 0 : resizerContainer.querySelector('.resizer-item.display-handle');
tableRef.style.width = '';
tableRef.style.tableLayout = 'auto';
cols.forEach(function (col) {
return col.style.width = '';
});
if (resizerContainer) {
resizerContainer.style.width = 'max-content';
resizerContainer.style.setProperty('--ak-editor-table-width', 'max-content');
}
if (resizerItem) {
resizerItem.style.width = 'max-content';
}
var measurement = getTableMeasurement(tableRef);
var tr = applyTableMeasurement(view.state.tr, node, measurement, pos);
api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.actions) === null || _api$analytics === void 0 || _api$analytics.attachAnalyticsEvent({
action: TABLE_ACTION.FIT_TO_CONTENT_ON_DEMAND,
actionSubject: ACTION_SUBJECT.TABLE,
actionSubjectId: null,
eventType: EVENT_TYPE.TRACK,
attributes: {
editorContainerWidth: (_api$width$sharedStat = api === null || api === void 0 || (_api$width = api.width) === null || _api$width === void 0 || (_api$width = _api$width.sharedState.currentState()) === null || _api$width === void 0 ? void 0 : _api$width.width) !== null && _api$width$sharedStat !== void 0 ? _api$width$sharedStat : 0,
tableWidth: measurement.tableWidth,
totalColumnCount: measurement.colWidths.length
}
})(tr);
view.dispatch(tr);
};