@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
38 lines (37 loc) • 2.39 kB
JavaScript
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
import { findTable } from '@atlaskit/editor-tables/utils';
import { applyTableMeasurement } from '../../transforms/content-mode';
import { measureTableWithAutoLayout } from './measure-table-with-auto-layout';
/**
* Used to measure a selected table width with it's content being laid out natively by the browser
*/
export const applyMeasuredWidthToSelectedTable = (view, api) => {
var _api$width, _api$width$sharedStat, _api$analytics, _api$analytics$action, _api$width$sharedStat2, _api$width2, _api$width2$sharedSta;
const tableObject = findTable(view.state.selection);
if (!tableObject) {
return;
}
const {
node,
pos
} = tableObject;
const tableState = api === null || api === void 0 ? void 0 : api.table.sharedState.currentState();
if (!(tableState !== null && tableState !== void 0 && tableState.tableRef)) {
return;
}
const editorContainerWidth = api === null || api === void 0 ? void 0 : (_api$width = api.width) === null || _api$width === void 0 ? void 0 : (_api$width$sharedStat = _api$width.sharedState.currentState()) === null || _api$width$sharedStat === void 0 ? void 0 : _api$width$sharedStat.width;
const measurement = measureTableWithAutoLayout(tableState.tableRef, editorContainerWidth);
const tr = applyTableMeasurement(view.state.tr, node, measurement, pos);
api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : (_api$analytics$action = _api$analytics.actions) === null || _api$analytics$action === void 0 ? void 0 : _api$analytics$action.attachAnalyticsEvent({
action: TABLE_ACTION.FIT_TO_CONTENT_ON_DEMAND,
actionSubject: ACTION_SUBJECT.TABLE,
actionSubjectId: null,
eventType: EVENT_TYPE.TRACK,
attributes: {
editorContainerWidth: (_api$width$sharedStat2 = api === null || api === void 0 ? void 0 : (_api$width2 = api.width) === null || _api$width2 === void 0 ? void 0 : (_api$width2$sharedSta = _api$width2.sharedState.currentState()) === null || _api$width2$sharedSta === void 0 ? void 0 : _api$width2$sharedSta.width) !== null && _api$width$sharedStat2 !== void 0 ? _api$width$sharedStat2 : 0,
tableWidth: measurement.tableWidth,
totalColumnCount: measurement.colWidths.length
}
})(tr);
view.dispatch(tr);
};