UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 2.42 kB
{ "version": 3, "sources": ["../../../src/components/visual-editor/use-edit-content-only-section-exit.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useRefEffect } from '@wordpress/compose';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../../lock-unlock';\n\n/**\n * Allows content only section editing to be exited by clicking outside of the\n * edited blocks.\n */\nexport function useEditContentOnlySectionExit() {\n\tconst { getEditedContentOnlySection } = unlock(\n\t\tuseSelect( blockEditorStore )\n\t);\n\tconst { stopEditingContentOnlySection } = unlock(\n\t\tuseDispatch( blockEditorStore )\n\t);\n\n\treturn useRefEffect(\n\t\t( node ) => {\n\t\t\tfunction onClick( event ) {\n\t\t\t\tconst editedContentOnlySection = getEditedContentOnlySection();\n\t\t\t\tif ( ! editedContentOnlySection ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Check if the click is outside the edited block first.\n\t\t\t\tconst isClickOutside = ! event.target.closest(\n\t\t\t\t\t`[data-block=\"${ editedContentOnlySection }\"]`\n\t\t\t\t);\n\n\t\t\t\t// Only prevent default and stop editing if clicking outside.\n\t\t\t\t// This allows default behavior (e.g., file dialogs) to work when clicking inside.\n\t\t\t\tif ( isClickOutside && ! event.defaultPrevented ) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tstopEditingContentOnlySection();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tnode.addEventListener( 'click', onClick );\n\n\t\t\treturn () => {\n\t\t\t\tnode.removeEventListener( 'click', onClick );\n\t\t\t};\n\t\t},\n\t\t[ getEditedContentOnlySection, stopEditingContentOnlySection ]\n\t);\n}\n"], "mappings": ";AAGA,SAAS,WAAW,mBAAmB;AACvC,SAAS,oBAAoB;AAC7B,SAAS,SAAS,wBAAwB;AAK1C,SAAS,cAAc;AAMhB,SAAS,gCAAgC;AAC/C,QAAM,EAAE,4BAA4B,IAAI;AAAA,IACvC,UAAW,gBAAiB;AAAA,EAC7B;AACA,QAAM,EAAE,8BAA8B,IAAI;AAAA,IACzC,YAAa,gBAAiB;AAAA,EAC/B;AAEA,SAAO;AAAA,IACN,CAAE,SAAU;AACX,eAAS,QAAS,OAAQ;AACzB,cAAM,2BAA2B,4BAA4B;AAC7D,YAAK,CAAE,0BAA2B;AACjC;AAAA,QACD;AAGA,cAAM,iBAAiB,CAAE,MAAM,OAAO;AAAA,UACrC,gBAAiB,wBAAyB;AAAA,QAC3C;AAIA,YAAK,kBAAkB,CAAE,MAAM,kBAAmB;AACjD,gBAAM,eAAe;AACrB,wCAA8B;AAAA,QAC/B;AAAA,MACD;AAEA,WAAK,iBAAkB,SAAS,OAAQ;AAExC,aAAO,MAAM;AACZ,aAAK,oBAAqB,SAAS,OAAQ;AAAA,MAC5C;AAAA,IACD;AAAA,IACA,CAAE,6BAA6B,6BAA8B;AAAA,EAC9D;AACD;", "names": [] }