UNPKG

@wordpress/block-library

Version:
98 lines (97 loc) • 4.23 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_data = require("@wordpress/data"); // packages/block-library/src/tabs/use-tab-actions.js /** * Provides callbacks to insert, remove and move tabs for a tabs block. * * The hook intentionally avoids subscribing to the store: all data is derived * lazily inside the callbacks via `registry.select`, so consumers don't * re-render when the tab structure changes. * * @param {string|null} tabsClientId The client ID of the parent tabs block. * @return {{ insertTab: Function, removeTab: Function, moveTab: Function }} Tab action callbacks. */ function useTabActions(tabsClientId) { const registry = (0, _wordpress_data.useRegistry)(); const { insertBlock, removeBlock, moveBlocksToPosition, updateBlockAttributes } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const getTabsState = () => { const { getBlocks, getBlockAttributes } = registry.select(_wordpress_block_editor.store); const tabsAttributes = tabsClientId ? getBlockAttributes(tabsClientId) : void 0; const tabPanels = tabsClientId ? getBlocks(tabsClientId).find((block) => block.name === "core/tab-panels") : void 0; return { tabPanelsClientId: tabPanels?.clientId ?? null, tabPanelBlocks: tabPanels?.innerBlocks ?? [], activeIndex: tabsAttributes?.editorActiveTabIndex ?? tabsAttributes?.activeTabIndex ?? 0, defaultIndex: tabsAttributes?.activeTabIndex ?? 0 }; }; const insertTab = (atIndex) => { const { tabPanelsClientId, tabPanelBlocks, defaultIndex } = getTabsState(); if (!tabPanelsClientId) return; const { canInsertBlockType } = registry.select(_wordpress_block_editor.store); if (!canInsertBlockType("core/tab-panel", tabPanelsClientId)) return; const newIndex = atIndex ?? tabPanelBlocks.length; let newDefaultIndex = 0; if (tabPanelBlocks[defaultIndex]) newDefaultIndex = newIndex <= defaultIndex ? defaultIndex + 1 : defaultIndex; registry.batch(() => { updateBlockAttributes(tabsClientId, { editorActiveTabIndex: newIndex, activeTabIndex: newDefaultIndex }); insertBlock((0, _wordpress_blocks.createBlock)("core/tab-panel"), newIndex, tabPanelsClientId, false); }); }; const removeTab = (atIndex) => { const { tabPanelBlocks, activeIndex, defaultIndex } = getTabsState(); const tabCount = tabPanelBlocks.length; if (tabCount <= 1) return; const removeIndex = atIndex ?? activeIndex; const target = tabPanelBlocks[removeIndex]; if (!target) return; const newActiveIndex = removeIndex >= tabCount - 1 ? tabCount - 2 : removeIndex; let newDefaultIndex = defaultIndex; if (removeIndex === defaultIndex) newDefaultIndex = 0; else if (removeIndex < defaultIndex) newDefaultIndex = defaultIndex - 1; registry.batch(() => { removeBlock(target.clientId, false); const { getBlock } = registry.select(_wordpress_block_editor.store); if (getBlock(target.clientId)) return; updateBlockAttributes(tabsClientId, { editorActiveTabIndex: newActiveIndex, activeTabIndex: newDefaultIndex }); }); }; const moveTab = (direction) => { const { tabPanelsClientId, tabPanelBlocks, activeIndex, defaultIndex } = getTabsState(); if (!tabPanelsClientId) return; const toIndex = activeIndex + direction; const target = tabPanelBlocks[activeIndex]; if (!target || toIndex < 0 || toIndex >= tabPanelBlocks.length) return; const { canMoveBlock } = registry.select(_wordpress_block_editor.store); if (!canMoveBlock(target.clientId)) return; let newDefaultIndex = defaultIndex; if (defaultIndex === activeIndex) newDefaultIndex = toIndex; else if (defaultIndex === toIndex) newDefaultIndex = activeIndex; registry.batch(() => { updateBlockAttributes(tabsClientId, { editorActiveTabIndex: toIndex, activeTabIndex: newDefaultIndex }); moveBlocksToPosition([target.clientId], tabPanelsClientId, tabPanelsClientId, toIndex); }); }; return { insertTab, removeTab, moveTab }; } exports.default = useTabActions; //# sourceMappingURL=use-tab-actions.cjs.map