@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
70 lines (68 loc) • 3.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.applyMeasuredWidthToAllTables = void 0;
var _analytics = require("@atlaskit/editor-common/analytics");
var _styles = require("@atlaskit/editor-common/styles");
var _table = require("@atlaskit/editor-common/table");
var _contentMode = require("../../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.
*/
var applyMeasuredWidthToAllTables = exports.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 || (0, _table.hasTableBeenResized)(node) && node.attrs.layout !== 'align-start') {
return;
}
var domNode = view.domAtPos(offset + 1).node;
var tableWrapper = domNode instanceof HTMLElement ? domNode.closest(".".concat(_styles.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: (0, _contentMode.getTableMeasurement)(tableRef)
});
});
measuredTables.forEach(function (_ref) {
var node = _ref.node,
offset = _ref.offset,
measurement = _ref.measurement;
tr = (0, _contentMode.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: _analytics.TABLE_ACTION.FIT_TO_CONTENT_AUTO_CONVERTED,
actionSubject: _analytics.ACTION_SUBJECT.TABLE,
actionSubjectId: null,
eventType: _analytics.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));
}
};