UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

65 lines (63 loc) 3.19 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 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 (_ref) { var node = _ref.node, offset = _ref.offset, measurement = _ref.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 (_ref2) { var measurement = _ref2.measurement; return { tableWidth: measurement.tableWidth, totalColumnCount: measurement.colWidths.length }; }) } })(tr); view.dispatch(tr.setMeta('addToHistory', false)); } };