@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 6.26 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/block-keyboard-shortcuts/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect } from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport {\n\tuseShortcut,\n\tstore as keyboardShortcutsStore,\n} from '@wordpress/keyboard-shortcuts';\nimport { __ } from '@wordpress/i18n';\nimport { createBlock } from '@wordpress/blocks';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\nfunction BlockKeyboardShortcuts() {\n\tconst { registerShortcut } = useDispatch( keyboardShortcutsStore );\n\tconst { replaceBlocks } = useDispatch( blockEditorStore );\n\tconst { getBlockName, getSelectedBlockClientId, getBlockAttributes } =\n\t\tuseSelect( blockEditorStore );\n\n\tconst handleTransformHeadingAndParagraph = ( event, level ) => {\n\t\tevent.preventDefault();\n\n\t\tconst currentClientId = getSelectedBlockClientId();\n\t\tif ( currentClientId === null ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst blockName = getBlockName( currentClientId );\n\t\tconst isParagraph = blockName === 'core/paragraph';\n\t\tconst isHeading = blockName === 'core/heading';\n\n\t\tif ( ! isParagraph && ! isHeading ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst destinationBlockName =\n\t\t\tlevel === 0 ? 'core/paragraph' : 'core/heading';\n\n\t\tconst attributes = getBlockAttributes( currentClientId );\n\n\t\t// Avoid unnecessary block transform when attempting to transform to\n\t\t// the same block type and/or same level.\n\t\tif (\n\t\t\t( isParagraph && level === 0 ) ||\n\t\t\t( isHeading && attributes.level === level )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tconst newAttributes = {\n\t\t\tcontent: attributes.content,\n\t\t};\n\n\t\t// Read textAlign from source block (could be in old or new format)\n\t\tconst sourceTextAlign =\n\t\t\tattributes.textAlign || attributes.style?.typography?.textAlign;\n\n\t\t// When destination is heading, set the level\n\t\tif ( destinationBlockName === 'core/heading' ) {\n\t\t\tnewAttributes.level = level;\n\t\t}\n\n\t\tif ( sourceTextAlign ) {\n\t\t\tnewAttributes.style = {\n\t\t\t\ttypography: {\n\t\t\t\t\ttextAlign: sourceTextAlign,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\treplaceBlocks(\n\t\t\tcurrentClientId,\n\t\t\tcreateBlock( destinationBlockName, newAttributes )\n\t\t);\n\t};\n\n\tuseEffect( () => {\n\t\tregisterShortcut( {\n\t\t\tname: 'core/block-editor/transform-heading-to-paragraph',\n\t\t\tcategory: 'block-library',\n\t\t\tdescription: __( 'Transform heading to paragraph.' ),\n\t\t\tkeyCombination: {\n\t\t\t\tmodifier: 'access',\n\t\t\t\tcharacter: '0',\n\t\t\t},\n\t\t\taliases: [\n\t\t\t\t{\n\t\t\t\t\tmodifier: 'access',\n\t\t\t\t\tcharacter: '7',\n\t\t\t\t},\n\t\t\t],\n\t\t} );\n\n\t\t[ 1, 2, 3, 4, 5, 6 ].forEach( ( level ) => {\n\t\t\tregisterShortcut( {\n\t\t\t\tname: `core/block-editor/transform-paragraph-to-heading-${ level }`,\n\t\t\t\tcategory: 'block-library',\n\t\t\t\tdescription: __( 'Transform paragraph to heading.' ),\n\t\t\t\tkeyCombination: {\n\t\t\t\t\tmodifier: 'access',\n\t\t\t\t\tcharacter: `${ level }`,\n\t\t\t\t},\n\t\t\t} );\n\t\t} );\n\t}, [ registerShortcut ] );\n\n\tuseShortcut(\n\t\t'core/block-editor/transform-heading-to-paragraph',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 0 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-1',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 1 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-2',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 2 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-3',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 3 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-4',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 4 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-5',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 5 )\n\t);\n\tuseShortcut(\n\t\t'core/block-editor/transform-paragraph-to-heading-6',\n\t\t( event ) => handleTransformHeadingAndParagraph( event, 6 )\n\t);\n\n\treturn null;\n}\n\nexport default BlockKeyboardShortcuts;\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA0B;AAC1B,kBAAuC;AACvC,gCAGO;AACP,kBAAmB;AACnB,oBAA4B;AAC5B,0BAA0C;AAE1C,SAAS,yBAAyB;AACjC,QAAM,EAAE,iBAAiB,QAAI,yBAAa,0BAAAA,KAAuB;AACjE,QAAM,EAAE,cAAc,QAAI,yBAAa,oBAAAC,KAAiB;AACxD,QAAM,EAAE,cAAc,0BAA0B,mBAAmB,QAClE,uBAAW,oBAAAA,KAAiB;AAE7B,QAAM,qCAAqC,CAAE,OAAO,UAAW;AAC9D,UAAM,eAAe;AAErB,UAAM,kBAAkB,yBAAyB;AACjD,QAAK,oBAAoB,MAAO;AAC/B;AAAA,IACD;AAEA,UAAM,YAAY,aAAc,eAAgB;AAChD,UAAM,cAAc,cAAc;AAClC,UAAM,YAAY,cAAc;AAEhC,QAAK,CAAE,eAAe,CAAE,WAAY;AACnC;AAAA,IACD;AAEA,UAAM,uBACL,UAAU,IAAI,mBAAmB;AAElC,UAAM,aAAa,mBAAoB,eAAgB;AAIvD,QACG,eAAe,UAAU,KACzB,aAAa,WAAW,UAAU,OACnC;AACD;AAAA,IACD;AACA,UAAM,gBAAgB;AAAA,MACrB,SAAS,WAAW;AAAA,IACrB;AAGA,UAAM,kBACL,WAAW,aAAa,WAAW,OAAO,YAAY;AAGv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