{"version":3,"file":"use-tab-actions.mjs","names":["blockEditorStore"],"sources":["../../src/tabs/use-tab-actions.js"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch, useRegistry } from '@wordpress/data';\n\n/**\n * Provides callbacks to insert, remove and move tabs for a tabs block.\n *\n * The hook intentionally avoids subscribing to the store: all data is derived\n * lazily inside the callbacks via `registry.select`, so consumers don't\n * re-render when the tab structure changes.\n *\n * @param {string|null} tabsClientId The client ID of the parent tabs block.\n * @return {{ insertTab: Function, removeTab: Function, moveTab: Function }} Tab action callbacks.\n */\nexport default function useTabActions( tabsClientId ) {\n\tconst registry = useRegistry();\n\tconst {\n\t\tinsertBlock,\n\t\tremoveBlock,\n\t\tmoveBlocksToPosition,\n\t\tupdateBlockAttributes,\n\t} = useDispatch( blockEditorStore );\n\n\t// Derive the current tabs state at call time without subscribing.\n\tconst getTabsState = () => {\n\t\tconst { getBlocks, getBlockAttributes } =\n\t\t\tregistry.select( blockEditorStore );\n\n\t\tconst tabsAttributes = tabsClientId\n\t\t\t? getBlockAttributes( tabsClientId )\n\t\t\t: undefined;\n\t\tconst tabPanels = tabsClientId\n\t\t\t? getBlocks( tabsClientId ).find(\n\t\t\t\t\t( block ) => block.name === 'core/tab-panels'\n\t\t\t\t)\n\t\t\t: undefined;\n\n\t\treturn {\n\t\t\ttabPanelsClientId: tabPanels?.clientId ?? null,\n\t\t\ttabPanelBlocks: tabPanels?.innerBlocks ?? [],\n\t\t\tactiveIndex:\n\t\t\t\ttabsAttributes?.editorActiveTabIndex ??\n\t\t\t\ttabsAttributes?.activeTabIndex ??\n\t\t\t\t0,\n\t\t\tdefaultIndex: tabsAttributes?.activeTabIndex ?? 0,\n\t\t};\n\t};\n\n\t// Insert a new tab and make it active. Defaults to appending at the end.\n\tconst insertTab = ( atIndex ) => {\n\t\tconst { tabPanelsClientId, tabPanelBlocks, defaultIndex } =\n\t\t\tgetTabsState();\n\t\tif ( ! tabPanelsClientId ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Leave the active and default tabs alone when a tab panel can't be\n\t\t// inserted, as `insertBlock()` would then do nothing.\n\t\tconst { canInsertBlockType } = registry.select( blockEditorStore );\n\t\tif ( ! canInsertBlockType( 'core/tab-panel', tabPanelsClientId ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newIndex = atIndex ?? tabPanelBlocks.length;\n\n\t\t// Keep the default tab on the same tab panel when the new tab is\n\t\t// inserted before it. Fall back to the first tab when the default tab\n\t\t// no longer exists, for example after every tab panel was deleted.\n\t\tlet newDefaultIndex = 0;\n\t\tif ( tabPanelBlocks[ defaultIndex ] ) {\n\t\t\tnewDefaultIndex =\n\t\t\t\tnewIndex <= defaultIndex ? defaultIndex + 1 : defaultIndex;\n\t\t}\n\n\t\t// Batch the changes into a single undo step, so undo restores the\n\t\t// default tab along with removing the new tab.\n\t\tregistry.batch( () => {\n\t\t\t// Switch editor to the new tab and keep the default tab.\n\t\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\t\teditorActiveTabIndex: newIndex,\n\t\t\t\tactiveTabIndex: newDefaultIndex,\n\t\t\t} );\n\t\t\tinsertBlock(\n\t\t\t\tcreateBlock( 'core/tab-panel' ),\n\t\t\t\tnewIndex,\n\t\t\t\ttabPanelsClientId,\n\t\t\t\tfalse\n\t\t\t);\n\t\t} );\n\t};\n\n\t// Remove a tab and move the active tab to an adjacent one. Defaults to the\n\t// currently active tab.\n\tconst removeTab = ( atIndex ) => {\n\t\tconst { tabPanelBlocks, activeIndex, defaultIndex } = getTabsState();\n\t\tconst tabCount = tabPanelBlocks.length;\n\t\tif ( tabCount <= 1 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst removeIndex = atIndex ?? activeIndex;\n\t\tconst target = tabPanelBlocks[ removeIndex ];\n\t\tif ( ! target ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Calculate new active index after removal.\n\t\tconst newActiveIndex =\n\t\t\tremoveIndex >= tabCount - 1 ? tabCount - 2 : removeIndex;\n\n\t\t// Keep the default tab on the same tab panel, falling back to the\n\t\t// first tab when the default tab itself is removed.\n\t\tlet newDefaultIndex = defaultIndex;\n\t\tif ( removeIndex === defaultIndex ) {\n\t\t\tnewDefaultIndex = 0;\n\t\t} else if ( removeIndex < defaultIndex ) {\n\t\t\tnewDefaultIndex = defaultIndex - 1;\n\t\t}\n\n\t\t// Batch the changes into a single undo step, so undo restores the\n\t\t// default tab along with the removed tab.\n\t\tregistry.batch( () => {\n\t\t\tremoveBlock( target.clientId, false );\n\n\t\t\t// The tab panel stays when it's locked, or while the editor asks\n\t\t\t// to confirm removing a block inside it, such as a Query Loop in\n\t\t\t// a template. Only update the tabs once the tab panel is gone.\n\t\t\tconst { getBlock } = registry.select( blockEditorStore );\n\t\t\tif ( getBlock( target.clientId ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Switch editor to the adjacent tab and keep the default tab.\n\t\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\t\teditorActiveTabIndex: newActiveIndex,\n\t\t\t\tactiveTabIndex: newDefaultIndex,\n\t\t\t} );\n\t\t} );\n\t};\n\n\t// Move the active tab one position left or right by reordering the\n\t// underlying tab-panel. The tab-list labels follow their panel\n\t// automatically via useTabListItemsSync.\n\tconst moveTab = ( direction ) => {\n\t\tconst { tabPanelsClientId, tabPanelBlocks, activeIndex, defaultIndex } =\n\t\t\tgetTabsState();\n\t\tif ( ! tabPanelsClientId ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst toIndex = activeIndex + direction;\n\t\tconst target = tabPanelBlocks[ activeIndex ];\n\t\tif ( ! target || toIndex < 0 || toIndex >= tabPanelBlocks.length ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Leave the active and default tabs alone when the tab panel can't be\n\t\t// moved, for example when it's locked, as `moveBlocksToPosition()`\n\t\t// would then do nothing.\n\t\tconst { canMoveBlock } = registry.select( blockEditorStore );\n\t\tif ( ! canMoveBlock( target.clientId ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Keep the default tab on the same tab panel, whether it is the moved\n\t\t// tab or the neighboring tab that swaps places with it.\n\t\tlet newDefaultIndex = defaultIndex;\n\t\tif ( defaultIndex === activeIndex ) {\n\t\t\tnewDefaultIndex = toIndex;\n\t\t} else if ( defaultIndex === toIndex ) {\n\t\t\tnewDefaultIndex = activeIndex;\n\t\t}\n\n\t\t// Batch the changes into a single undo step, so undo restores the\n\t\t// default tab along with the tab order.\n\t\tregistry.batch( () => {\n\t\t\t// Switch editor to the moved tab and keep the default tab.\n\t\t\tupdateBlockAttributes( tabsClientId, {\n\t\t\t\teditorActiveTabIndex: toIndex,\n\t\t\t\tactiveTabIndex: newDefaultIndex,\n\t\t\t} );\n\t\t\tmoveBlocksToPosition(\n\t\t\t\t[ target.clientId ],\n\t\t\t\ttabPanelsClientId,\n\t\t\t\ttabPanelsClientId,\n\t\t\t\ttoIndex\n\t\t\t);\n\t\t} );\n\t};\n\n\treturn { insertTab, removeTab, moveTab };\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAwB,cAAe,cAAe;CACrD,MAAM,WAAW,YAAY;CAC7B,MAAM,EACL,aACA,aACA,sBACA,0BACG,YAAaA,KAAiB;CAGlC,MAAM,qBAAqB;EAC1B,MAAM,EAAE,WAAW,uBAClB,SAAS,OAAQA,KAAiB;EAEnC,MAAM,iBAAiB,eACpB,mBAAoB,YAAa,IACjC,KAAA;EACH,MAAM,YAAY,eACf,UAAW,YAAa,CAAC,CAAC,MACxB,UAAW,MAAM,SAAS,iBAC7B,IACC,KAAA;EAEH,OAAO;GACN,mBAAmB,WAAW,YAAY;GAC1C,gBAAgB,WAAW,eAAe,CAAC;GAC3C,aACC,gBAAgB,wBAChB,gBAAgB,kBAChB;GACD,cAAc,gBAAgB,kBAAkB;EACjD;CACD;CAGA,MAAM,aAAc,YAAa;EAChC,MAAM,EAAE,mBAAmB,gBAAgB,iBAC1C,aAAa;EACd,IAAK,CAAE,m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