UNPKG

@wordpress/widget-dashboard

Version:

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

1 lines • 2.19 kB
{"version":3,"file":"merge-widget-actions.mjs","names":[],"sources":["../../src/utils/merge-widget-actions.ts"],"sourcesContent":["import type {\n\tWidgetAction,\n\tWidgetRuntimeAction,\n} from '@wordpress/widget-primitives';\n\n/*\n * The runtime action keeps the declared `icon` and `relevance` it leaves out.\n */\nfunction upgradeDeclaredAction(\n\tdeclared: WidgetAction,\n\truntime: WidgetRuntimeAction\n): WidgetRuntimeAction {\n\tconst { icon, relevance } = declared;\n\n\treturn {\n\t\t...( icon !== undefined && { icon } ),\n\t\t...( relevance !== undefined && { relevance } ),\n\t\t...runtime,\n\t};\n}\n\n/**\n * Joins the type's declared actions with the instance's runtime ones. A\n * runtime action carrying a declared `id` takes that action's position;\n * the rest follow in their own order.\n *\n * @param {WidgetAction[]} declared The type's declared actions.\n * @param {WidgetRuntimeAction[]} runtime The instance's runtime actions.\n * @return {WidgetRuntimeAction[]} The actions the surfaces place.\n */\nexport function mergeWidgetActions(\n\tdeclared: WidgetAction[],\n\truntime: WidgetRuntimeAction[]\n): WidgetRuntimeAction[] {\n\tif ( runtime.length === 0 ) {\n\t\treturn declared;\n\t}\n\n\tconst byId = new Map< string, WidgetRuntimeAction >(\n\t\truntime.map( ( action ) => [ action.id, action ] as const )\n\t);\n\n\tconst merged: WidgetRuntimeAction[] = declared.map( ( action ) => {\n\t\tconst replacement = byId.get( action.id );\n\t\tif ( ! replacement ) {\n\t\t\treturn action;\n\t\t}\n\n\t\tbyId.delete( action.id );\n\t\treturn upgradeDeclaredAction( action, replacement );\n\t} );\n\n\treturn [ ...merged, ...byId.values() ];\n}\n"],"mappings":";AAQA,SAAS,sBACR,UACA,SACsB;CACtB,MAAM,EAAE,MAAM,cAAc;CAE5B,OAAO;EACN,GAAK,SAAS,KAAA,KAAa,EAAE,KAAK;EAClC,GAAK,cAAc,KAAA,KAAa,EAAE,UAAU;EAC5C,GAAG;CACJ;AACD;;;;;;;;;;AAWA,SAAgB,mBACf,UACA,SACwB;CACxB,IAAK,QAAQ,WAAW,GACvB,OAAO;CAGR,MAAM,OAAO,IAAI,IAChB,QAAQ,KAAO,WAAY,CAAE,OAAO,IAAI,MAAO,CAAW,CAC3D;CAYA,OAAO,CAAE,GAV6B,SAAS,KAAO,WAAY;EACjE,MAAM,cAAc,KAAK,IAAK,OAAO,EAAG;EACxC,IAAK,CAAE,aACN,OAAO;EAGR,KAAK,OAAQ,OAAO,EAAG;EACvB,OAAO,sBAAuB,QAAQ,WAAY;CACnD,CAEiB,GAAG,GAAG,KAAK,OAAO,CAAE;AACtC"}