@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
41 lines (40 loc) • 1.4 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// packages/widget-dashboard/src/utils/canonicalize-layout.ts
/**
* Canonical form of a layout: widgets sorted by `placement.order` (falling
* back to array index), then `order` stripped since position now implies
* it. The single positional encoding shared by the renderer, the commit
* payload, and the staging enforcement.
*
* Returns `layout` itself when it is already canonical, and keeps the
* reference of every widget without an `order` to strip, so untouched
* layouts stay identity-comparable.
*
* @param {DashboardWidget[]} layout Layout to canonicalize.
* @return {DashboardWidget[]} The canonical layout.
*/
function canonicalizeLayout(layout) {
const indexed = layout.map((widget, index) => ({
widget,
order: widget.placement?.order ?? index
}));
indexed.sort((a, b) => a.order - b.order);
let changed = false;
const canonical = indexed.map(({ widget }, index) => {
if (widget !== layout[index]) changed = true;
if (!widget.placement || !("order" in widget.placement)) return widget;
changed = true;
const { order: _stripped, ...placement } = widget.placement;
return {
...widget,
placement
};
});
return changed ? canonical : layout;
}
exports.canonicalizeLayout = canonicalizeLayout;
//# sourceMappingURL=canonicalize-layout.cjs.map