UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 2.56 kB
{ "version": 3, "sources": ["../../../src/components/provider/use-auto-switch-editor-sidebars.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useEffect } from '@wordpress/element';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { store as interfaceStore } from '@wordpress/interface';\n\n/**\n * This listener hook monitors for block selection and triggers the appropriate\n * sidebar state.\n */\nfunction useAutoSwitchEditorSidebars() {\n\tconst { hasBlockSelection } = useSelect( ( select ) => {\n\t\treturn {\n\t\t\thasBlockSelection:\n\t\t\t\t!! select( blockEditorStore ).getBlockSelectionStart(),\n\t\t};\n\t}, [] );\n\n\tconst { getActiveComplementaryArea } = useSelect( interfaceStore );\n\tconst { enableComplementaryArea } = useDispatch( interfaceStore );\n\tconst { get: getPreference } = useSelect( preferencesStore );\n\n\tuseEffect( () => {\n\t\tconst activeGeneralSidebar = getActiveComplementaryArea( 'core' );\n\t\tconst isEditorSidebarOpened = [\n\t\t\t'edit-post/document',\n\t\t\t'edit-post/block',\n\t\t].includes( activeGeneralSidebar );\n\t\tconst isDistractionFree = getPreference( 'core', 'distractionFree' );\n\t\tif ( ! isEditorSidebarOpened || isDistractionFree ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( hasBlockSelection ) {\n\t\t\tenableComplementaryArea( 'core', 'edit-post/block' );\n\t\t} else {\n\t\t\tenableComplementaryArea( 'core', 'edit-post/document' );\n\t\t}\n\t}, [\n\t\thasBlockSelection,\n\t\tgetActiveComplementaryArea,\n\t\tenableComplementaryArea,\n\t\tgetPreference,\n\t] );\n}\n\nexport default useAutoSwitchEditorSidebars;\n"], "mappings": ";AAGA,SAAS,WAAW,mBAAmB;AACvC,SAAS,iBAAiB;AAC1B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,SAAS,wBAAwB;AAC1C,SAAS,SAAS,sBAAsB;AAMxC,SAAS,8BAA8B;AACtC,QAAM,EAAE,kBAAkB,IAAI,UAAW,CAAE,WAAY;AACtD,WAAO;AAAA,MACN,mBACC,CAAC,CAAE,OAAQ,gBAAiB,EAAE,uBAAuB;AAAA,IACvD;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,EAAE,2BAA2B,IAAI,UAAW,cAAe;AACjE,QAAM,EAAE,wBAAwB,IAAI,YAAa,cAAe;AAChE,QAAM,EAAE,KAAK,cAAc,IAAI,UAAW,gBAAiB;AAE3D,YAAW,MAAM;AAChB,UAAM,uBAAuB,2BAA4B,MAAO;AAChE,UAAM,wBAAwB;AAAA,MAC7B;AAAA,MACA;AAAA,IACD,EAAE,SAAU,oBAAqB;AACjC,UAAM,oBAAoB,cAAe,QAAQ,iBAAkB;AACnE,QAAK,CAAE,yBAAyB,mBAAoB;AACnD;AAAA,IACD;AACA,QAAK,mBAAoB;AACxB,8BAAyB,QAAQ,iBAAkB;AAAA,IACpD,OAAO;AACN,8BAAyB,QAAQ,oBAAqB;AAAA,IACvD;AAAA,EACD,GAAG;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AACH;AAEA,IAAO,0CAAQ;", "names": [] }