@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 4.89 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/navigation/edit/unsaved-inner-blocks.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { 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';\n\n/**\n * Internal dependencies\n */\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": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,0BAAoC;AACpC,wBAAyB;AACzB,uBAAmC;AACnC,kBAA0B;AAC1B,qBA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