@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 8.95 kB
Source Map (JSON)
{"version":3,"file":"edit.cjs","names":["useRegistry","useDispatch","blockEditorStore","migrateToListV2","useSelect","useCallback","createBlock","ToolbarButton","isRTL","formatOutdentRTL","formatOutdent","__","useBlockProps","useInnerBlocksProps","BlockControls","formatListBulletsRTL","formatListBullets","formatListNumberedRTL","formatListNumbered","TagName","OrderedListSettings"],"sources":["../../src/list/edit.jsx"],"sourcesContent":["import {\n\tBlockControls,\n\tuseBlockProps,\n\tuseInnerBlocksProps,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { ToolbarButton } from '@wordpress/components';\nimport { useDispatch, useSelect, useRegistry } from '@wordpress/data';\nimport { isRTL, __ } from '@wordpress/i18n';\nimport {\n\tformatListBullets,\n\tformatListBulletsRTL,\n\tformatListNumbered,\n\tformatListNumberedRTL,\n\tformatOutdent,\n\tformatOutdentRTL,\n} from '@wordpress/icons';\nimport { createBlock } from '@wordpress/blocks';\nimport { useCallback, useEffect } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\nimport OrderedListSettings from './ordered-list-settings';\nimport { migrateToListV2 } from './utils';\nimport TagName from './tag-name';\n\nconst DEFAULT_BLOCK = {\n\tname: 'core/list-item',\n};\n\n/**\n * At the moment, deprecations don't handle create blocks from attributes\n * (like when using CPT templates). For this reason, this hook is necessary\n * to avoid breaking templates using the old list block format.\n *\n * @param {Object} attributes Block attributes.\n * @param {string} clientId Block client ID.\n */\nfunction useMigrateOnLoad( attributes, clientId ) {\n\tconst registry = useRegistry();\n\tconst { updateBlockAttributes, replaceInnerBlocks, selectBlock } =\n\t\tuseDispatch( blockEditorStore );\n\n\tuseEffect( () => {\n\t\t// As soon as the block is loaded, migrate it to the new version.\n\n\t\tif ( ! attributes.values ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst [ newAttributes, newInnerBlocks ] = migrateToListV2( attributes );\n\n\t\tdeprecated( 'Value attribute on the list block', {\n\t\t\tsince: '6.0',\n\t\t\tversion: '6.5',\n\t\t\talternative: 'inner blocks',\n\t\t} );\n\n\t\t// The selection can be inside an inner block created from the block\n\t\t// type template at insertion, which the migration replaces; restore\n\t\t// the selection to the migrated block in that case.\n\t\tconst shouldReselectBlock = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.hasSelectedInnerBlock( clientId, true );\n\n\t\tregistry.batch( () => {\n\t\t\tupdateBlockAttributes( clientId, newAttributes );\n\t\t\treplaceInnerBlocks( clientId, newInnerBlocks );\n\t\t\tif ( shouldReselectBlock ) {\n\t\t\t\tselectBlock( clientId );\n\t\t\t}\n\t\t} );\n\t}, [ attributes.values ] );\n}\n\nfunction useOutdentList( clientId ) {\n\tconst { replaceBlocks, selectionChange } = useDispatch( blockEditorStore );\n\tconst { getBlockRootClientId, getBlockAttributes, getBlock } =\n\t\tuseSelect( blockEditorStore );\n\n\treturn useCallback( () => {\n\t\tconst parentBlockId = getBlockRootClientId( clientId );\n\t\tconst parentBlockAttributes = getBlockAttributes( parentBlockId );\n\t\t// Create a new parent block without the inner blocks.\n\t\tconst newParentBlock = createBlock(\n\t\t\t'core/list-item',\n\t\t\tparentBlockAttributes\n\t\t);\n\t\tconst { innerBlocks } = getBlock( clientId );\n\t\t// Replace the parent block with a new parent block without inner blocks,\n\t\t// and make the inner blocks siblings of the parent.\n\t\treplaceBlocks( [ parentBlockId ], [ newParentBlock, ...innerBlocks ] );\n\t\t// Select the last child of the list being outdent.\n\t\tselectionChange( innerBlocks[ innerBlocks.length - 1 ].clientId );\n\t}, [ clientId ] );\n}\n\nfunction IndentUI( { clientId } ) {\n\tconst outdentList = useOutdentList( clientId );\n\tconst canOutdent = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockRootClientId, getBlockName } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\treturn (\n\t\t\t\tgetBlockName( getBlockRootClientId( clientId ) ) ===\n\t\t\t\t'core/list-item'\n\t\t\t);\n\t\t},\n\t\t[ clientId ]\n\t);\n\treturn (\n\t\t<>\n\t\t\t<ToolbarButton\n\t\t\t\ticon={ isRTL() ? formatOutdentRTL : formatOutdent }\n\t\t\t\ttitle={ __( 'Outdent' ) }\n\t\t\t\tdescription={ __( 'Outdent list item' ) }\n\t\t\t\tdisabled={ ! canOutdent }\n\t\t\t\tonClick={ outdentList }\n\t\t\t/>\n\t\t</>\n\t);\n}\n\nexport default function Edit( { attributes, setAttributes, clientId } ) {\n\tconst { ordered, type, reversed, start } = attributes;\n\tconst blockProps = useBlockProps( {\n\t\tstyle: {\n\t\t\tlistStyleType: ordered && type !== 'decimal' ? type : undefined,\n\t\t},\n\t} );\n\n\tconst innerBlocksProps = useInnerBlocksProps( blockProps, {\n\t\tdefaultBlock: DEFAULT_BLOCK,\n\t\tdirectInsert: true,\n\t\ttemplateLock: false,\n\t\t__experimentalCaptureToolbars: true,\n\t} );\n\tuseMigrateOnLoad( attributes, clientId );\n\n\tconst controls = (\n\t\t<BlockControls group=\"block\">\n\t\t\t<ToolbarButton\n\t\t\t\ticon={ isRTL() ? formatListBulletsRTL : formatListBullets }\n\t\t\t\ttitle={ __( 'Unordered' ) }\n\t\t\t\tdescription={ __( 'Convert to unordered list' ) }\n\t\t\t\tisActive={ ordered === false }\n\t\t\t\tonClick={ () => {\n\t\t\t\t\tsetAttributes( { ordered: false } );\n\t\t\t\t} }\n\t\t\t/>\n\t\t\t<ToolbarButton\n\t\t\t\ticon={ isRTL() ? formatListNumberedRTL : formatListNumbered }\n\t\t\t\ttitle={ __( 'Ordered' ) }\n\t\t\t\tdescription={ __( 'Convert to ordered list' ) }\n\t\t\t\tisActive={ ordered === true }\n\t\t\t\tonClick={ () => {\n\t\t\t\t\tsetAttributes( { ordered: true } );\n\t\t\t\t} }\n\t\t\t/>\n\t\t\t<IndentUI clientId={ clientId } />\n\t\t</BlockControls>\n\t);\n\n\treturn (\n\t\t<>\n\t\t\t<TagName\n\t\t\t\tordered={ ordered }\n\t\t\t\treversed={ reversed }\n\t\t\t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