{"version":3,"file":"enforce-layout-policy.mjs","names":[],"sources":["../../src/utils/enforce-layout-policy.ts"],"sourcesContent":["import fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport type { WidgetType } from '@wordpress/widget-primitives';\nimport { canonicalizeLayout } from './canonicalize-layout';\nimport type {\n\tCanPerformDashboardOperation,\n\tDashboardInstanceOperation,\n\tDashboardTilePlacement,\n\tDashboardWidget,\n} from '../types';\n\n/*\n * Placement fields by facet. `resize` owns the spans; `move` owns the\n * masonry lane pin. Explicit `order` never reaches the diff: both\n * layouts are canonicalized first, so position IS the array position.\n */\nconst SIZE_FIELDS = [ 'width', 'height' ] as const;\nconst POSITION_FIELDS = [ 'lane' ] as const;\n\ntype PlacementFields = readonly string[];\n\nfunction placementOf( widget: DashboardWidget ): Record< string, unknown > {\n\treturn ( widget.placement ?? {} ) as Record< string, unknown >;\n}\n\nfunction placementFieldsEqual(\n\ta: DashboardWidget,\n\tb: DashboardWidget,\n\tfields: PlacementFields\n): boolean {\n\tconst pa = placementOf( a );\n\tconst pb = placementOf( b );\n\treturn fields.every( ( field ) => pa[ field ] === pb[ field ] );\n}\n\nfunction restorePlacementFields(\n\twidget: DashboardWidget,\n\tprior: DashboardWidget,\n\tfields: PlacementFields\n): DashboardWidget {\n\tconst restored = { ...placementOf( widget ) };\n\tconst priorPlacement = placementOf( prior );\n\tfor ( const field of fields ) {\n\t\tif ( field in priorPlacement ) {\n\t\t\trestored[ field ] = priorPlacement[ field ];\n\t\t} else {\n\t\t\tdelete restored[ field ];\n\t\t}\n\t}\n\tif ( ! Object.keys( restored ).length && ! prior.placement ) {\n\t\tconst { placement: _dropped, ...rest } = widget;\n\t\treturn rest;\n\t}\n\treturn { ...widget, placement: restored as DashboardTilePlacement };\n}\n\ninterface EnforceLayoutPolicyArgs {\n\t/**\n\t * The staging layout the incoming one replaces.\n\t */\n\tprevious: DashboardWidget[];\n\n\t/**\n\t * The incoming layout.\n\t */\n\tnext: DashboardWidget[];\n\n\t/**\n\t * The resolved policy.\n\t */\n\tcanPerform: CanPerformDashboardOperation;\n\n\t/**\n\t * Registered types, to resolve each instance's `WidgetType` for the\n\t * policy request.\n\t */\n\twidgetTypes: WidgetType[];\n}\n\n/**\n * Re-asserts every change the policy denies before a layout enters\n * staging, so no composed trigger can slip a denied operation past the\n * interface: what the UI hides, the staging layer rejects.\n *\n * Per instance: a new instance whose type the policy rejects for\n * `insert` is dropped. An unregistered type has no type to ask about,\n * so a new instance of one passes, and instance operations on a\n * surviving tile still ask with `widgetType` absent: a lock keyed on\n * the type does not fire. Denied `edit` keeps the staged `attributes`;\n * denied `resize` keeps the placement spans; denied `move` keeps the\n * masonry `lane` and holds the instance's canonical position, adjusted\n * for allowed membership changes: the same hold `remove` uses, and the\n * position the grid's pinned items keep.\n *\n * Both layouts are canonicalized before the diff (sorted by\n * `order ?? index` with `order` stripped, as the renderer and the\n * commit read them), so a move is a move whichever encoding expressed\n * it. Returns `next` itself when it was already canonical and nothing\n * was re-asserted.\n *\n * @param {EnforceLayoutPolicyArgs} args The layouts to diff and the policy.\n * @return {DashboardWidget[]} The incoming layout with denied changes re-asserted.\n */\nexport function enforceLayoutPolicy( {\n\tprevious: rawPrevious,\n\tnext: rawNext,\n\tcanPerform,\n\twidgetTypes,\n}: EnforceLayoutPolicyArgs ): DashboardWidget[] {\n\tconst previous = canonicalizeLayout( rawPrevious );\n\tconst next = canonicalizeLayout( rawNext );\n\n\tconst typeOf = ( widget: DashboardWidget ) =>\n\t\twidgetTypes.find( ( type ) => type.name === widget.type );\n\tconst priorByUuid = new Map(\n\t\tprevious.map( ( widget ) => [ widget.uuid, widget ] )\n\t);\n\n\t// Instances free to take the position `next` gives them, in `next`\n\t// order; move-denied survivors are staged aside to be held instead.\n\tconst free: DashboardWidget[] = [];\n\tconst heldByUuid = new Map< string, DashboardWidget >();\n\n\tfor ( const incoming of next ) {\n\t\tconst prior = priorByUuid.get( incoming.uuid );\n\n\t\tif ( ! prior ) {\n\t\t\tconst widgetType = typeOf( incoming );\n\t\t\tif (\n\t\t\t\twidgetType &&\n\t\t\t\t! canPerform( { operation: 'insert', widgetType } )\n\t\t\t) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfree.push( incoming );\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst widgetType = typeOf( prior );\n\t\tconst allows = ( operation: DashboardInstanceOperation ) =>\n\t\t\tcanPerform( { operation, widget: prior, widgetType } );\n\n\t\tlet staged = incoming;\n\t\tif (\n\t\t\t! fastDeepEqual( prior.attributes, incoming.attributes ) &&\n\t\t\t! allows( 'edit' )\n\t\t) {\n\t\t\tstaged = { ...staged };\n\t\t\tif ( 'attributes' in prior ) {\n\t\t\t\tstaged.attributes = prior.attributes;\n\t\t\t} else {\n\t\t\t\tdelete staged.attributes;\n\t\t\t}\n\t\t}\n\t\tif (\n\t\t\t! placementFieldsEqual( prior, staged, SIZE_FIELDS ) &&\n\t\t\t! allows( 'resize' )\n\t\t) {\n\t\t\tstaged = restorePlacementFields( staged, prior, SIZE_FIELDS );\n\t\t}\n\n\t\tif ( allows( 'move' ) ) {\n\t\t\tfree.push( staged );\n\t\t\tcontinue;\n\t\t}\n\t\tif ( ! placementFieldsEqual( prior, staged, POSITION_FIELDS ) ) {\n\t\t\tstaged = restorePlacementFields( staged, prior, POSITION_FIELDS );\n\t\t}\n\t\theldByUuid.set( staged.uuid, staged );\n\t}\n\n\t// Walk `previous` to place what holds a position: move-denied\n\t// survivors and denied removals land at their previous index,\n\t// discounting the instances that legitimately left.\n\tconst nextUuids = new Set( next.map( ( widget ) => widget.uuid ) );\n\tconst result = [ ...free ];\n\tlet absent = 0;\n\tconst place = ( widget: DashboardWidget, index: number ) =>\n\t\tresult.splice( Math.min( index - absent, result.length ), 0, widget );\n\n\tprevious.forEach( ( widget, index ) => {\n\t\tif ( ! nextUuids.has( widget.uuid ) ) {\n\t\t\tconst removable = canPerform( {\n\t\t\t\toperation: 'remove',\n\t\t\t\twidget,\n\t\t\t\twidgetType: typeOf( widget ),\n\t\t\t} );\n\t\t\tif ( removable ) {\n\t\t\t\tabsent += 1;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tplace( widget, index );\n\t\t\treturn;\n\t\t}\n\n\t\tconst held = heldByUuid.get( widget.uuid );\n\t\tif ( held ) {\n\t\t\tplace( held, index );\n\t\t}\n\t} );\n\n\tconst untouched =\n\t\tresult.length === next.length &&\n\t\tresult.every( ( widget, index ) => widget === next[ index ] );\n\treturn untouched ? next : result;\n}\n"],"mappings":";;;AAeA,MAAM,cAAc,CAAE,SAAS,QAAS;AACxC,MAAM,kBAAkB,CAAE,MAAO;AAIjC,SAAS,YAAa,QAAqD;CAC1E,OAAS,OAAO,aAAa,CAAC;AAC/B;AAEA,SAAS,qBACR,GACA,GACA,QACU;CACV,MAAM,KAAK,YAAa,CAAE;CAC1B,MAAM,KAAK,YAAa,CAAE;CAC1B,OAAO,OAAO,OAAS,UAAW,GAAI,WAAY,GAAI,MAAQ;AAC/D;AAEA,SAAS,uBACR,QACA,OACA,QACkB;CAClB,MAAM,WAAW,EAAE,GAAG,YAAa,MAAO,EAAE;CAC5C,MAAM,iBAAiB,YAAa,KAAM;CAC1C,KAAM,MAAM,SAAS,QACpB,IAAK,SAAS,gBACb,SAAU,SAAU,eAAgB;MAEpC,OAAO,SAAU;CAGnB,IAAK,CAAE,OAAO,KAAM,QAAS,CAAC,CAAC,UAAU,CAAE,MAAM,WAAY;EAC5D,MAAM,EAAE,WAAW,UAAU,GAAG,SAAS;EACzC,OAAO;CACR;CACA,OAAO;EAAE,GAAG;EAAQ,WAAW;CAAmC;AACnE;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,oBAAqB,EACpC,UAAU,aACV,MAAM,SACN,YACA,eAC+C;CAC/C,MAAM,WAAW,mBAAoB,WAAY;CACjD,MAAM,OAAO,mBAAoB,OAAQ;CAEzC,MAAM,UAAW,WAChB,YAAY,MAAQ,SAAU,KAAK,SAAS,OAAO,IAAK;CACzD,MAAM,cAAc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