@wordpress/block-editor
Version:
8 lines (7 loc) • 2.44 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/block-selection-clearer/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useRefEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\n\n/**\n * Pass the returned ref callback to an element that should clear block\n * selection. Selection will only be cleared if the element is clicked directly,\n * not if a child element is clicked.\n *\n * @return {import('react').RefCallback} Ref callback.\n */\nexport function useBlockSelectionClearer() {\n\tconst { getSettings, hasSelectedBlock, hasMultiSelection } =\n\t\tuseSelect( blockEditorStore );\n\tconst { clearSelectedBlock } = useDispatch( blockEditorStore );\n\tconst { clearBlockSelection: isEnabled } = getSettings();\n\n\treturn useRefEffect(\n\t\t( node ) => {\n\t\t\tif ( ! isEnabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tfunction onMouseDown( event ) {\n\t\t\t\tif ( ! hasSelectedBlock() && ! hasMultiSelection() ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Only handle clicks on the element, not the children.\n\t\t\t\tif ( event.target !== node ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tclearSelectedBlock();\n\t\t\t}\n\n\t\t\tnode.addEventListener( 'mousedown', onMouseDown );\n\n\t\t\treturn () => {\n\t\t\t\tnode.removeEventListener( 'mousedown', onMouseDown );\n\t\t\t};\n\t\t},\n\t\t[ hasSelectedBlock, hasMultiSelection, clearSelectedBlock, isEnabled ]\n\t);\n}\n\nexport default function BlockSelectionClearer( props ) {\n\treturn <div ref={ useBlockSelectionClearer() } { ...props } />;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAuC;AACvC,qBAA6B;AAK7B,mBAA0C;AA6ClC;AApCD,SAAS,2BAA2B;AAC1C,QAAM,EAAE,aAAa,kBAAkB,kBAAkB,QACxD,uBAAW,aAAAA,KAAiB;AAC7B,QAAM,EAAE,mBAAmB,QAAI,yBAAa,aAAAA,KAAiB;AAC7D,QAAM,EAAE,qBAAqB,UAAU,IAAI,YAAY;AAEvD,aAAO;AAAA,IACN,CAAE,SAAU;AACX,UAAK,CAAE,WAAY;AAClB;AAAA,MACD;AAEA,eAAS,YAAa,OAAQ;AAC7B,YAAK,CAAE,iBAAiB,KAAK,CAAE,kBAAkB,GAAI;AACpD;AAAA,QACD;AAGA,YAAK,MAAM,WAAW,MAAO;AAC5B;AAAA,QACD;AAEA,2BAAmB;AAAA,MACpB;AAEA,WAAK,iBAAkB,aAAa,WAAY;AAEhD,aAAO,MAAM;AACZ,aAAK,oBAAqB,aAAa,WAAY;AAAA,MACpD;AAAA,IACD;AAAA,IACA,CAAE,kBAAkB,mBAAmB,oBAAoB,SAAU;AAAA,EACtE;AACD;AAEe,SAAR,sBAAwC,OAAQ;AACtD,SAAO,4CAAC,SAAI,KAAM,yBAAyB,GAAM,GAAG,OAAQ;AAC7D;",
"names": ["blockEditorStore"]
}