@atlaskit/editor-plugin-metrics
Version:
Metrics plugin for @atlaskit/editor-core
54 lines (50 loc) • 1.79 kB
JavaScript
import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
import { AttrStep } from '@atlaskit/editor-prosemirror/transform';
import { ActionType } from '../types';
export var checkTableColumnResized = function checkTableColumnResized(tr) {
var COL_WIDTH_ATTR = 'colwidth';
var steps = tr.steps;
// Find the step that is changing the column width
var resizingColumnStep = steps.find(function (step) {
return step instanceof AttrStep || step instanceof BatchAttrsStep;
});
if (!resizingColumnStep) {
return undefined;
}
var pos;
// Find the position of the first column in the step that is being resized
if (resizingColumnStep instanceof BatchAttrsStep) {
if (resizingColumnStep.data.length === 0) {
return undefined;
}
var _resizingColumnStep$d = resizingColumnStep.data[0],
attrs = _resizingColumnStep$d.attrs,
nodeType = _resizingColumnStep$d.nodeType,
position = _resizingColumnStep$d.position;
if (['tableHeader', 'tableCell'].includes(nodeType) && attrs !== null && attrs !== void 0 && attrs[COL_WIDTH_ATTR]) {
pos = position;
}
} else if (resizingColumnStep instanceof AttrStep && resizingColumnStep.attr === COL_WIDTH_ATTR) {
pos = resizingColumnStep.pos;
}
if (!pos) {
return undefined;
}
// Find the range of the table that is being resized
var $pos = tr.doc.resolve(pos);
var blockRange = $pos.blockRange();
if (!blockRange) {
return undefined;
}
var from = blockRange.start;
var to = from + blockRange.parent.content.size;
// Use to and from position of table to check whether column width of same table is being resized
return {
type: ActionType.CHANGING_ATTRS,
extraData: {
attr: COL_WIDTH_ATTR,
from: from,
to: to
}
};
};