{"version":3,"file":"unsaved-inner-blocks.mjs","names":["coreStore"],"sources":["../../../src/navigation/edit/unsaved-inner-blocks.jsx"],"sourcesContent":["import { useInnerBlocksProps } from '@wordpress/block-editor';\nimport { Disabled } from '@wordpress/components';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { useContext, useEffect, useRef } from '@wordpress/element';\nimport { areBlocksDirty } from './are-blocks-dirty';\nimport { DEFAULT_BLOCK, SELECT_NAVIGATION_MENUS_ARGS } from '../constants';\n\nconst EMPTY_OBJECT = {};\n\nexport default function UnsavedInnerBlocks( {\n\tblocks,\n\tcreateNavigationMenu,\n\thasSelection,\n} ) {\n\tconst originalBlocksRef = useRef();\n\n\tuseEffect( () => {\n\t\t// Initially store the uncontrolled inner blocks for\n\t\t// dirty state comparison.\n\t\tif ( ! originalBlocksRef?.current ) {\n\t\t\toriginalBlocksRef.current = blocks;\n\t\t}\n\t}, [ blocks ] );\n\n\t// If the current inner blocks are different from the original inner blocks\n\t// from the post content then the user has made changes to the inner blocks.\n\t// At this point the inner blocks can be considered \"dirty\".\n\t// Note: referential equality is not sufficient for comparison as the inner blocks\n\t// of the page list are controlled and may be updated async due to syncing with\n\t// entity records. As a result we need to perform a deep equality check skipping\n\t// the page list's inner blocks.\n\tconst innerBlocksAreDirty = areBlocksDirty(\n\t\toriginalBlocksRef?.current,\n\t\tblocks\n\t);\n\n\t// The block will be disabled in a block preview, use this as a way of\n\t// avoiding the side-effects of this component for block previews.\n\tconst isDisabled = useContext( Disabled.Context );\n\n\tconst innerBlocksProps = useInnerBlocksProps(\n\t\t{\n\t\t\tclassName: 'wp-block-navigation__container',\n\t\t},\n\t\t{\n\t\t\trenderAppender: hasSelection ? undefined : false,\n\t\t\tdefaultBlock: DEFAULT_BLOCK,\n\t\t\tdirectInsert: true,\n\t\t}\n\t);\n\n\tconst { isSaving, hasResolvedAllNavigationMenus } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn EMPTY_OBJECT;\n\t\t\t}\n\n\t\t\tconst { hasFinishedResolution, isSavingEntityRecord } =\n\t\t\t\tselect( coreStore );\n\n\t\t\treturn {\n\t\t\t\tisSaving: isSavingEntityRecord( 'postType', 'wp_navigation' ),\n\t\t\t\thasResolvedAllNavigationMenus: hasFinishedResolution(\n\t\t\t\t\t'getEntityRecords',\n\t\t\t\t\tSELECT_NAVIGATION_MENUS_ARGS\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ isDisabled ]\n\t);\n\n\t// Automatically save the uncontrolled blocks.\n\tuseEffect( () => {\n\t\t// The block will be disabled when used in a BlockPreview.\n\t\t// In this case avoid automatic creation of a wp_navigation post.\n\t\t// Otherwise the user will be spammed with lots of menus!\n\t\t//\n\t\t// Also ensure other navigation menus have loaded so an\n\t\t// accurate name can be created.\n\t\t//\n\t\t// Don't try saving when another save is already\n\t\t// in progress.\n\t\t//\n\t\t// And finally only create the menu when the block is selected,\n\t\t// which is an indication they want to start editing.\n\t\tif (\n\t\t\tisDisabled ||\n\t\t\tisSaving ||\n\t\t\t! hasResolvedAllNavigationMenus ||\n\t\t\t! hasSelection ||\n\t\t\t! innerBlocksAreDirty\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tcreateNavigationMenu( null, blocks );\n\t}, [\n\t\tblocks,\n\t\tcreateNavigationMenu,\n\t\tisDisabled,\n\t\tisSaving,\n\t\thasResolvedAllNavigationMenus,\n\t\tinnerBlocksAreDirty,\n\t\thasSelection,\n\t] );\n\n\tconst Wrapper = isSaving ? Disabled : 'div';\n\n\treturn <Wrapper { ...innerBlocksProps } />;\n}\n"],"mappings":";;;;;;;;;AAQA,MAAM,eAAe,CAAC;AAEtB,SAAwB,mBAAoB,EAC3C,QACA,sBACA,gBACG;CACH,MAAM,oBAAoB,OAAO;CAEjC,gBAAiB;EAGhB,IAAK,CAAE,mBAAmB,SACzB,kBAAkB,UAAU;CAE9B,GAAG,CAAE,MAAO,CAAE;CASd,MAAM,sBAAsB,eAC3B,mBAAmB,SACnB,MACD;CAIA,MAAM,aAAa,WAAY,SAAS,OAAQ;CAEhD,MAAM,mBAAmB,oBACxB,EACC,WAAW,iCACZ,GACA;EACC,gBAAgB,eAAe,KAAA,IAAY;EAC3C,cAAc;EACd,cAAc;CACf,CACD;CAEA,MAAM,EAAE,UAAU,kCAAkC,WACjD,WAAY;EACb,IAAK,YACJ,OAAO;EAGR,MAAM,EAAE,uBAAuB,yBAC9B,OAAQA,KAAU;EAEnB,OAAO;GACN,UAAU,qBAAsB,YAAY,eAAgB;GAC5D,+BAA+B,sBAC9B,oBACA,4BACD;EACD;CACD,GACA,CAAE,UAAW,CACd;CAGA,gBAAiB;EAahB,IACC,cACA,YACA,CAAE,iCACF,CAAE,gBACF,CAAE,qBAEF;EAGD,qBAAsB,MAAM,MAAO;CACpC,GAAG;EACF;EACA;EACA;EACA;EACA;EACA;EACA;CACD,CAAE;CAIF,OAAO,oBAFS,WAAW,WAAW,OAE/B,EAAS,GAAK,iBAAoB,CAAA;AAC1C"}