UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 9.49 kB
{ "version": 3, "sources": ["../../../src/components/inner-blocks/use-nested-settings-update.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useLayoutEffect, useState } from '@wordpress/element';\nimport { useRegistry } from '@wordpress/data';\nimport deprecated from '@wordpress/deprecated';\nimport { isShallowEqual } from '@wordpress/is-shallow-equal';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport { getLayoutType } from '../../layouts';\n\n/** @typedef {import('../../selectors').WPDirectInsertBlock } WPDirectInsertBlock */\n\nconst pendingSettingsUpdates = new WeakMap();\n\n// Creates a memoizing caching function that remembers the last value and keeps returning it\n// as long as the new values are shallowly equal. Helps keep dependencies stable.\nfunction createShallowMemo() {\n\tlet value;\n\treturn ( newValue ) => {\n\t\tif ( value === undefined || ! isShallowEqual( value, newValue ) ) {\n\t\t\tvalue = newValue;\n\t\t}\n\t\treturn value;\n\t};\n}\n\nfunction useShallowMemo( value ) {\n\tconst [ memo ] = useState( createShallowMemo );\n\treturn memo( value );\n}\n\n/**\n * This hook is a side effect which updates the block-editor store when changes\n * happen to inner block settings. The given props are transformed into a\n * settings object, and if that is different from the current settings object in\n * the block-editor store, then the store is updated with the new settings which\n * came from props.\n *\n * @param {string} clientId The client ID of the block to update.\n * @param {string} parentLock\n * @param {string[]} allowedBlocks An array of block names which are permitted\n * in inner blocks.\n * @param {string[]} prioritizedInserterBlocks Block names and/or block variations to be prioritized in the inserter, in the format {blockName}/{variationName}.\n * @param {?WPDirectInsertBlock} defaultBlock The default block to insert: [ blockName, { blockAttributes } ].\n * @param {?boolean} directInsert If a default block should be inserted directly by the appender.\n *\n * @param {?WPDirectInsertBlock} __experimentalDefaultBlock A deprecated prop for the default block to insert: [ blockName, { blockAttributes } ]. Use `defaultBlock` instead.\n *\n * @param {?boolean} __experimentalDirectInsert A deprecated prop for whether a default block should be inserted directly by the appender. Use `directInsert` instead.\n *\n * @param {string} [templateLock] The template lock specified for the inner\n * blocks component. (e.g. \"all\")\n * @param {boolean} captureToolbars Whether or children toolbars should be shown\n * in the inner blocks component rather than on\n * the child block.\n * @param {string} orientation The direction in which the block\n * should face.\n * @param {Object} layout The layout object for the block container.\n */\nexport default function useNestedSettingsUpdate(\n\tclientId,\n\tparentLock,\n\tallowedBlocks,\n\tprioritizedInserterBlocks,\n\tdefaultBlock,\n\tdirectInsert,\n\t__experimentalDefaultBlock,\n\t__experimentalDirectInsert,\n\ttemplateLock,\n\tcaptureToolbars,\n\torientation,\n\tlayout\n) {\n\t// Instead of adding a useSelect mapping here, please add to the useSelect\n\t// mapping in InnerBlocks! Every subscription impacts performance.\n\n\tconst registry = useRegistry();\n\n\t// Implementors often pass a new array on every render,\n\t// and the contents of the arrays are just strings, so the entire array\n\t// can be passed as dependencies but We need to include the length of the array,\n\t// otherwise if the arrays change length but the first elements are equal the comparison,\n\t// does not works as expected.\n\tconst _allowedBlocks = useShallowMemo( allowedBlocks );\n\tconst _prioritizedInserterBlocks = useShallowMemo(\n\t\tprioritizedInserterBlocks\n\t);\n\n\tconst _templateLock =\n\t\ttemplateLock === undefined || parentLock === 'contentOnly'\n\t\t\t? parentLock\n\t\t\t: templateLock;\n\n\tuseLayoutEffect( () => {\n\t\tconst newSettings = {\n\t\t\tallowedBlocks: _allowedBlocks,\n\t\t\tprioritizedInserterBlocks: _prioritizedInserterBlocks,\n\t\t\ttemplateLock: _templateLock,\n\t\t};\n\n\t\t// These values are not defined for RN, so only include them if they\n\t\t// are defined.\n\t\tif ( captureToolbars !== undefined ) {\n\t\t\tnewSettings.__experimentalCaptureToolbars = captureToolbars;\n\t\t}\n\n\t\t// Orientation depends on layout,\n\t\t// ideally the separate orientation prop should be deprecated.\n\t\tif ( orientation !== undefined ) {\n\t\t\tnewSettings.orientation = orientation;\n\t\t} else {\n\t\t\tconst layoutType = getLayoutType( layout?.type );\n\t\t\tnewSettings.orientation = layoutType.getOrientation( layout );\n\t\t}\n\n\t\tif ( __experimentalDefaultBlock !== undefined ) {\n\t\t\tdeprecated( '__experimentalDefaultBlock', {\n\t\t\t\talternative: 'defaultBlock',\n\t\t\t\tsince: '6.3',\n\t\t\t\tversion: '6.4',\n\t\t\t} );\n\t\t\tnewSettings.defaultBlock = __experimentalDefaultBlock;\n\t\t}\n\n\t\tif ( defaultBlock !== undefined ) {\n\t\t\tnewSettings.defaultBlock = defaultBlock;\n\t\t}\n\n\t\tif ( __experimentalDirectInsert !== undefined ) {\n\t\t\tdeprecated( '__experimentalDirectInsert', {\n\t\t\t\talternative: 'directInsert',\n\t\t\t\tsince: '6.3',\n\t\t\t\tversion: '6.4',\n\t\t\t} );\n\t\t\tnewSettings.directInsert = __experimentalDirectInsert;\n\t\t}\n\n\t\tif ( directInsert !== undefined ) {\n\t\t\tnewSettings.directInsert = directInsert;\n\t\t}\n\n\t\tif (\n\t\t\tnewSettings.directInsert !== undefined &&\n\t\t\ttypeof newSettings.directInsert !== 'boolean'\n\t\t) {\n\t\t\tdeprecated( 'Using `Function` as a `directInsert` argument', {\n\t\t\t\talternative: '`boolean` values',\n\t\t\t\tsince: '6.5',\n\t\t\t} );\n\t\t}\n\n\t\t// Batch updates to block list settings to avoid triggering cascading renders\n\t\t// for each container block included in a tree and optimize initial render.\n\t\t// To avoid triggering updateBlockListSettings for each container block\n\t\t// causing X re-renderings for X container blocks,\n\t\t// we batch all the updatedBlockListSettings in a single \"data\" batch\n\t\t// which results in a single re-render.\n\t\tif ( ! pendingSettingsUpdates.get( registry ) ) {\n\t\t\tpendingSettingsUpdates.set( registry, {} );\n\t\t}\n\t\tpendingSettingsUpdates.get( registry )[ clientId ] = newSettings;\n\t\twindow.queueMicrotask( () => {\n\t\t\tconst settings = pendingSettingsUpdates.get( registry );\n\t\t\tif ( Object.keys( settings ).length ) {\n\t\t\t\tconst { updateBlockListSettings } =\n\t\t\t\t\tregistry.dispatch( blockEditorStore );\n\t\t\t\tupdateBlockListSettings( settings );\n\t\t\t\tpendingSettingsUpdates.set( registry, {} );\n\t\t\t}\n\t\t} );\n\t}, [\n\t\tclientId,\n\t\t_allowedBlocks,\n\t\t_prioritizedInserterBlocks,\n\t\t_templateLock,\n\t\tdefaultBlock,\n\t\tdirectInsert,\n\t\t__experimentalDefaultBlock,\n\t\t__experimentalDirectInsert,\n\t\tcaptureToolbars,\n\t\torientation,\n\t\tlayout,\n\t\tregistry,\n\t] );\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA0C;AAC1C,kBAA4B;AAC5B,wBAAuB;AACvB,8BAA+B;AAK/B,mBAA0C;AAC1C,qBAA8B;AAI9B,IAAM,yBAAyB,oBAAI,QAAQ;AAI3C,SAAS,oBAAoB;AAC5B,MAAI;AACJ,SAAO,CAAE,aAAc;AACtB,QAAK,UAAU,UAAa,KAAE,wCAAgB,OAAO,QAAS,GAAI;AACjE,cAAQ;AAAA,IACT;AACA,WAAO;AAAA,EACR;AACD;AAEA,SAAS,eAAgB,OAAQ;AAChC,QAAM,CAAE,IAAK,QAAI,yBAAU,iBAAkB;AAC7C,SAAO,KAAM,KAAM;AACpB;AA8Be,SAAR,wBACN,UACA,YACA,eACA,2BACA,cACA,cACA,4BACA,4BACA,cACA,iBACA,aACA,QACC;AAID,QAAM,eAAW,yBAAY;AAO7B,QAAM,iBAAiB,eAAgB,aAAc;AACrD,QAAM,6BAA6B;AAAA,IAClC;AAAA,EACD;AAEA,QAAM,gBACL,iBAAiB,UAAa,eAAe,gBAC1C,aACA;AAEJ,sCAAiB,MAAM;AACtB,UAAM,cAAc;AAAA,MACnB,eAAe;AAAA,MACf,2BAA2B;AAAA,MAC3B,cAAc;AAAA,IACf;AAIA,QAAK,oBAAoB,QAAY;AACpC,kBAAY,gCAAgC;AAAA,IAC7C;AAIA,QAAK,gBAAgB,QAAY;AAChC,kBAAY,cAAc;AAAA,IAC3B,OAAO;AACN,YAAM,iBAAa,8BAAe,QAAQ,IAAK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