@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
8 lines (7 loc) • 2 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/canonicalize-layout.ts"],
"sourcesContent": ["import type { DashboardWidget } from '../types';\n\n/**\n * Canonical form of a layout: widgets sorted by `placement.order` (falling\n * back to array index), then `order` stripped since position now implies\n * it. The single positional encoding shared by the renderer, the commit\n * payload, and the staging enforcement.\n *\n * Returns `layout` itself when it is already canonical, and keeps the\n * reference of every widget without an `order` to strip, so untouched\n * layouts stay identity-comparable.\n *\n * @param {DashboardWidget[]} layout Layout to canonicalize.\n * @return {DashboardWidget[]} The canonical layout.\n */\nexport function canonicalizeLayout(\n\tlayout: DashboardWidget[]\n): DashboardWidget[] {\n\tconst indexed = layout.map( ( widget, index ) => ( {\n\t\twidget,\n\t\torder: widget.placement?.order ?? index,\n\t} ) );\n\n\tindexed.sort( ( a, b ) => a.order - b.order );\n\n\tlet changed = false;\n\tconst canonical = indexed.map( ( { widget }, index ) => {\n\t\tif ( widget !== layout[ index ] ) {\n\t\t\tchanged = true;\n\t\t}\n\t\tif ( ! widget.placement || ! ( 'order' in widget.placement ) ) {\n\t\t\treturn widget;\n\t\t}\n\t\tchanged = true;\n\t\tconst { order: _stripped, ...placement } = widget.placement;\n\t\treturn { ...widget, placement };\n\t} );\n\n\treturn changed ? canonical : layout;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAeO,SAAS,mBACf,QACoB;AACpB,QAAM,UAAU,OAAO,IAAK,CAAE,QAAQ,WAAa;AAAA,IAClD;AAAA,IACA,OAAO,OAAO,WAAW,SAAS;AAAA,EACnC,EAAI;AAEJ,UAAQ,KAAM,CAAE,GAAG,MAAO,EAAE,QAAQ,EAAE,KAAM;AAE5C,MAAI,UAAU;AACd,QAAM,YAAY,QAAQ,IAAK,CAAE,EAAE,OAAO,GAAG,UAAW;AACvD,QAAK,WAAW,OAAQ,KAAM,GAAI;AACjC,gBAAU;AAAA,IACX;AACA,QAAK,CAAE,OAAO,aAAa,EAAI,WAAW,OAAO,YAAc;AAC9D,aAAO;AAAA,IACR;AACA,cAAU;AACV,UAAM,EAAE,OAAO,WAAW,GAAG,UAAU,IAAI,OAAO;AAClD,WAAO,EAAE,GAAG,QAAQ,UAAU;AAAA,EAC/B,CAAE;AAEF,SAAO,UAAU,YAAY;AAC9B;",
"names": []
}