UNPKG

@wordpress/block-editor

Version:
84 lines (72 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useBlockEditingMode = useBlockEditingMode; var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _store = require("../../store"); var _lockUnlock = require("../../lock-unlock"); var _blockListBlockContext = require("../block-list/block-list-block-context"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * @typedef {'disabled'|'contentOnly'|'default'} BlockEditingMode */ /** * Allows a block to restrict the user interface that is displayed for editing * that block and its inner blocks. * * @example * ```js * function MyBlock( { attributes, setAttributes } ) { * useBlockEditingMode( 'disabled' ); * return <div { ...useBlockProps() }></div>; * } * ``` * * `mode` can be one of three options: * * - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be * selected. * - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the * toolbar, the block movers, block settings. * - `'default'`: Allows editing the block as normal. * * The mode is inherited by all of the block's inner blocks, unless they have * their own mode. * * If called outside of a block context, the mode is applied to all blocks. * * @param {?BlockEditingMode} mode The editing mode to apply. If undefined, the * current editing mode is not changed. * * @return {BlockEditingMode} The current editing mode. */ function useBlockEditingMode(mode) { var _useContext; const { clientId = '' } = (_useContext = (0, _element.useContext)(_blockListBlockContext.BlockListBlockContext)) !== null && _useContext !== void 0 ? _useContext : {}; const blockEditingMode = (0, _data.useSelect)(select => (0, _lockUnlock.unlock)(select(_store.store)).getBlockEditingMode(clientId), [clientId]); const { setBlockEditingMode, unsetBlockEditingMode } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store)); (0, _element.useEffect)(() => { if (mode) { setBlockEditingMode(clientId, mode); } return () => { if (mode) { unsetBlockEditingMode(clientId); } }; }, [clientId, mode]); return blockEditingMode; } //# sourceMappingURL=index.js.map