UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 3.53 kB
{ "version": 3, "sources": ["../../../src/components/visual-editor/use-padding-appender.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRegistry } from '@wordpress/data';\nimport { useRefEffect } from '@wordpress/compose';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { isUnmodifiedDefaultBlock } from '@wordpress/blocks';\n\n// Ruleset to add space for the typewriter effect. When typing in the last\n// block, there needs to be room to scroll up.\nconst CSS =\n\t':root :where(.editor-styles-wrapper)::after {content: \"\"; display: block; height: 40vh;}';\n\nexport function usePaddingAppender( enabled ) {\n\tconst registry = useRegistry();\n\tconst effect = useRefEffect(\n\t\t( node ) => {\n\t\t\tfunction onMouseDown( event ) {\n\t\t\t\tif (\n\t\t\t\t\tevent.target !== node &&\n\t\t\t\t\t// Tests for the parent element because in the iframed editor if the click is\n\t\t\t\t\t// below the padding the target will be the parent element (html) and should\n\t\t\t\t\t// still be treated as intent to append.\n\t\t\t\t\tevent.target !== node.parentElement\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Only handle clicks under the last child.\n\t\t\t\tconst lastChild = node.lastElementChild;\n\t\t\t\tif ( ! lastChild ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst lastChildRect = lastChild.getBoundingClientRect();\n\t\t\t\tif ( event.clientY < lastChildRect.bottom ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\tconst blockOrder = registry\n\t\t\t\t\t.select( blockEditorStore )\n\t\t\t\t\t.getBlockOrder( '' );\n\t\t\t\tconst lastBlockClientId = blockOrder[ blockOrder.length - 1 ];\n\n\t\t\t\tconst lastBlock = registry\n\t\t\t\t\t.select( blockEditorStore )\n\t\t\t\t\t.getBlock( lastBlockClientId );\n\t\t\t\tconst { selectBlock, insertDefaultBlock } =\n\t\t\t\t\tregistry.dispatch( blockEditorStore );\n\n\t\t\t\tif ( lastBlock && isUnmodifiedDefaultBlock( lastBlock ) ) {\n\t\t\t\t\tselectBlock( lastBlockClientId );\n\t\t\t\t} else {\n\t\t\t\t\tinsertDefaultBlock();\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst { ownerDocument } = node;\n\t\t\t// Adds the listener on the document so that in the iframed editor clicks below the\n\t\t\t// padding can be handled as they too should be treated as intent to append.\n\t\t\townerDocument.addEventListener( 'pointerdown', onMouseDown );\n\t\t\treturn () => {\n\t\t\t\townerDocument.removeEventListener( 'pointerdown', onMouseDown );\n\t\t\t};\n\t\t},\n\t\t[ registry ]\n\t);\n\treturn enabled ? [ effect, CSS ] : [];\n}\n"], "mappings": ";AAGA,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAC7B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,gCAAgC;AAIzC,IAAM,MACL;AAEM,SAAS,mBAAoB,SAAU;AAC7C,QAAM,WAAW,YAAY;AAC7B,QAAM,SAAS;AAAA,IACd,CAAE,SAAU;AACX,eAAS,YAAa,OAAQ;AAC7B,YACC,MAAM,WAAW;AAAA;AAAA;AAAA,QAIjB,MAAM,WAAW,KAAK,eACrB;AACD;AAAA,QACD;AAGA,cAAM,YAAY,KAAK;AACvB,YAAK,CAAE,WAAY;AAClB;AAAA,QACD;AAEA,cAAM,gBAAgB,UAAU,sBAAsB;AACtD,YAAK,MAAM,UAAU,cAAc,QAAS;AAC3C;AAAA,QACD;AAEA,cAAM,eAAe;AAErB,cAAM,aAAa,SACjB,OAAQ,gBAAiB,EACzB,cAAe,EAAG;AACpB,cAAM,oBAAoB,WAAY,WAAW,SAAS,CAAE;AAE5D,cAAM,YAAY,SAChB,OAAQ,gBAAiB,EACzB,SAAU,iBAAkB;AAC9B,cAAM,EAAE,aAAa,mBAAmB,IACvC,SAAS,SAAU,gBAAiB;AAErC,YAAK,aAAa,yBAA0B,SAAU,GAAI;AACzD,sBAAa,iBAAkB;AAAA,QAChC,OAAO;AACN,6BAAmB;AAAA,QACpB;AAAA,MACD;AACA,YAAM,EAAE,cAAc,IAAI;AAG1B,oBAAc,iBAAkB,eAAe,WAAY;AAC3D,aAAO,MAAM;AACZ,sBAAc,oBAAqB,eAAe,WAAY;AAAA,MAC/D;AAAA,IACD;AAAA,IACA,CAAE,QAAS;AAAA,EACZ;AACA,SAAO,UAAU,CAAE,QAAQ,GAAI,IAAI,CAAC;AACrC;", "names": [] }