UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 2.98 kB
{ "version": 3, "sources": ["../../../src/components/block-editing-mode/index.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useEffect } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport {\n\tuseBlockEditContext,\n\tblockEditingModeKey,\n} from '../block-edit/context';\n\n/**\n * @typedef {'disabled'|'contentOnly'|'default'} BlockEditingMode\n */\n\n/**\n * Allows a block to restrict the user interface that is displayed for editing\n * that block and its inner blocks.\n *\n * @example\n * ```js\n * function MyBlock( { attributes, setAttributes } ) {\n * useBlockEditingMode( 'disabled' );\n * return <div { ...useBlockProps() }></div>;\n * }\n * ```\n *\n * `mode` can be one of three options:\n *\n * - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be\n * selected.\n * - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the\n * toolbar, the block movers, block settings.\n * - `'default'`: Allows editing the block as normal.\n *\n * The mode is inherited by all of the block's inner blocks, unless they have\n * their own mode.\n *\n * If called outside of a block context, the mode is applied to all blocks.\n *\n * @param {?BlockEditingMode} mode The editing mode to apply. If undefined, the\n * current editing mode is not changed.\n *\n * @return {BlockEditingMode} The current editing mode.\n */\nexport function useBlockEditingMode( mode ) {\n\tconst context = useBlockEditContext();\n\tconst { clientId = '' } = context;\n\tconst { setBlockEditingMode, unsetBlockEditingMode } =\n\t\tuseDispatch( blockEditorStore );\n\tconst globalBlockEditingMode = useSelect(\n\t\t( select ) =>\n\t\t\t// Avoid adding the subscription if not needed!\n\t\t\tclientId ? null : select( blockEditorStore ).getBlockEditingMode(),\n\t\t[ clientId ]\n\t);\n\tuseEffect( () => {\n\t\tif ( mode ) {\n\t\t\tsetBlockEditingMode( clientId, mode );\n\t\t}\n\t\treturn () => {\n\t\t\tif ( mode ) {\n\t\t\t\tunsetBlockEditingMode( clientId );\n\t\t\t}\n\t\t};\n\t}, [ clientId, mode, setBlockEditingMode, unsetBlockEditingMode ] );\n\treturn clientId ? context[ blockEditingModeKey ] : globalBlockEditingMode;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAuC;AACvC,qBAA0B;AAK1B,mBAA0C;AAC1C,qBAGO;AAoCA,SAAS,oBAAqB,MAAO;AAC3C,QAAM,cAAU,oCAAoB;AACpC,QAAM,EAAE,WAAW,GAAG,IAAI;AAC1B,QAAM,EAAE,qBAAqB,sBAAsB,QAClD,yBAAa,aAAAA,KAAiB;AAC/B,QAAM,6BAAyB;AAAA,IAC9B,CAAE;AAAA;AAAA,MAED,WAAW,OAAO,OAAQ,aAAAA,KAAiB,EAAE,oBAAoB;AAAA;AAAA,IAClE,CAAE,QAAS;AAAA,EACZ;AACA,gCAAW,MAAM;AAChB,QAAK,MAAO;AACX,0BAAqB,UAAU,IAAK;AAAA,IACrC;AACA,WAAO,MAAM;AACZ,UAAK,MAAO;AACX,8BAAuB,QAAS;AAAA,MACjC;AAAA,IACD;AAAA,EACD,GAAG,CAAE,UAAU,MAAM,qBAAqB,qBAAsB,CAAE;AAClE,SAAO,WAAW,QAAS,kCAAoB,IAAI;AACpD;", "names": ["blockEditorStore"] }