@wordpress/block-editor
Version:
8 lines (7 loc) • 4.82 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/recursion-provider/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createContext, useContext, useMemo } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { useBlockEditContext } from '../block-edit/context';\n\nconst RenderedRefsContext = createContext( {} );\nRenderedRefsContext.displayName = 'RenderedRefsContext';\n\n/**\n * Immutably adds an unique identifier to a set scoped for a given block type.\n *\n * @param {Object} renderedBlocks Rendered blocks grouped by block name\n * @param {string} blockName Name of the block.\n * @param {*} uniqueId Any value that acts as a unique identifier for a block instance.\n *\n * @return {Object} The list of rendered blocks grouped by block name.\n */\nfunction addToBlockType( renderedBlocks, blockName, uniqueId ) {\n\tconst result = {\n\t\t...renderedBlocks,\n\t\t[ blockName ]: renderedBlocks[ blockName ]\n\t\t\t? new Set( renderedBlocks[ blockName ] )\n\t\t\t: new Set(),\n\t};\n\tresult[ blockName ].add( uniqueId );\n\n\treturn result;\n}\n\n/**\n * A React context provider for use with the `useHasRecursion` hook to prevent recursive\n * renders.\n *\n * Wrap block content with this provider and provide the same `uniqueId` prop as used\n * with `useHasRecursion`.\n *\n * @param {Object} props\n * @param {*} props.uniqueId Any value that acts as a unique identifier for a block instance.\n * @param {string} props.blockName Optional block name.\n * @param {JSX.Element} props.children React children.\n *\n * @return {JSX.Element} A React element.\n */\nexport function RecursionProvider( { children, uniqueId, blockName = '' } ) {\n\tconst previouslyRenderedBlocks = useContext( RenderedRefsContext );\n\tconst { name } = useBlockEditContext();\n\n\tblockName = blockName || name;\n\n\tconst newRenderedBlocks = useMemo(\n\t\t() => addToBlockType( previouslyRenderedBlocks, blockName, uniqueId ),\n\t\t[ previouslyRenderedBlocks, blockName, uniqueId ]\n\t);\n\n\treturn (\n\t\t<RenderedRefsContext.Provider value={ newRenderedBlocks }>\n\t\t\t{ children }\n\t\t</RenderedRefsContext.Provider>\n\t);\n}\n\n/**\n * A React hook for keeping track of blocks previously rendered up in the block\n * tree. Blocks susceptible to recursion can use this hook in their `Edit`\n * function to prevent said recursion.\n *\n * Use this with the `RecursionProvider` component, using the same `uniqueId` value\n * for both the hook and the provider.\n *\n * @param {*} uniqueId Any value that acts as a unique identifier for a block instance.\n * @param {string} blockName Optional block name.\n *\n * @return {boolean} A boolean describing whether the provided id has already been rendered.\n */\nexport function useHasRecursion( uniqueId, blockName = '' ) {\n\tconst previouslyRenderedBlocks = useContext( RenderedRefsContext );\n\tconst { name } = useBlockEditContext();\n\tblockName = blockName || name;\n\treturn Boolean( previouslyRenderedBlocks[ blockName ]?.has( uniqueId ) );\n}\n\nexport const DeprecatedExperimentalRecursionProvider = ( props ) => {\n\tdeprecated( 'wp.blockEditor.__experimentalRecursionProvider', {\n\t\tsince: '6.5',\n\t\talternative: 'wp.blockEditor.RecursionProvider',\n\t} );\n\treturn <RecursionProvider { ...props } />;\n};\n\nexport const DeprecatedExperimentalUseHasRecursion = ( ...args ) => {\n\tdeprecated( 'wp.blockEditor.__experimentalUseHasRecursion', {\n\t\tsince: '6.5',\n\t\talternative: 'wp.blockEditor.useHasRecursion',\n\t} );\n\treturn useHasRecursion( ...args );\n};\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAmD;AACnD,wBAAuB;AAKvB,qBAAoC;AAoDlC;AAlDF,IAAM,0BAAsB,8BAAe,CAAC,CAAE;AAC9C,oBAAoB,cAAc;AAWlC,SAAS,eAAgB,gBAAgB,WAAW,UAAW;AAC9D,QAAM,SAAS;AAAA,IACd,GAAG;AAAA,IACH,CAAE,SAAU,GAAG,eAAgB,SAAU,IACtC,IAAI,IAAK,eAAgB,SAAU,CAAE,IACrC,oBAAI,IAAI;AAAA,EACZ;AACA,SAAQ,SAAU,EAAE,IAAK,QAAS;AAElC,SAAO;AACR;AAgBO,S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