UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

70 lines (68 loc) 3.09 kB
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics'; import { TableSharedCssClassName } from '@atlaskit/editor-common/styles'; import { hasTableBeenResized } from '@atlaskit/editor-common/table'; import { applyTableMeasurement, getTableMeasurement } from '../../transforms/content-mode'; /** * 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 const applyMeasuredWidthToAllTables = (view, pluginInjectionApi) => { const { state: { doc, schema } } = view; let tr = view.state.tr; const { table } = schema.nodes; let modified = false; const measuredTables = []; // modify only top-level tables doc.forEach((node, offset) => { if (node.type !== table || hasTableBeenResized(node) && node.attrs.layout !== 'align-start') { return; } const domNode = view.domAtPos(offset + 1).node; const tableWrapper = domNode instanceof HTMLElement ? domNode.closest(`.${TableSharedCssClassName.TABLE_VIEW_CONTENT_WRAP}`) : null; const tableRef = tableWrapper === null || tableWrapper === void 0 ? void 0 : tableWrapper.querySelector('table'); if (!tableRef) { return; } measuredTables.push({ node, offset, measurement: getTableMeasurement(tableRef) }); }); measuredTables.forEach(({ node, offset, measurement }) => { tr = applyTableMeasurement(tr, node, measurement, offset); modified = true; }); if (modified) { var _pluginInjectionApi$a, _pluginInjectionApi$a2, _pluginInjectionApi$w, _pluginInjectionApi$w2, _pluginInjectionApi$w3; pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$a = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a === void 0 ? void 0 : (_pluginInjectionApi$a2 = _pluginInjectionApi$a.actions) === null || _pluginInjectionApi$a2 === void 0 ? void 0 : _pluginInjectionApi$a2.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 ? void 0 : (_pluginInjectionApi$w2 = pluginInjectionApi.width) === null || _pluginInjectionApi$w2 === void 0 ? void 0 : (_pluginInjectionApi$w3 = _pluginInjectionApi$w2.sharedState.currentState()) === null || _pluginInjectionApi$w3 === void 0 ? void 0 : _pluginInjectionApi$w3.width) !== null && _pluginInjectionApi$w !== void 0 ? _pluginInjectionApi$w : 0, totalTablesResized: measuredTables.length, measurements: measuredTables.map(({ measurement }) => ({ tableWidth: measurement.tableWidth, totalColumnCount: measurement.colWidths.length })) } })(tr); view.dispatch(tr.setMeta('addToHistory', false)); } };