UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

36 lines (35 loc) • 1.26 kB
// 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; } export { canonicalizeLayout }; //# sourceMappingURL=canonicalize-layout.mjs.map