@wordpress/block-editor
Version:
8 lines (7 loc) • 10 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/hooks/block-fields/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tprivateApis as blocksPrivateApis,\n\tgetBlockType,\n} from '@wordpress/blocks';\nimport {\n\t__experimentalHStack as HStack,\n\t__experimentalTruncate as Truncate,\n} from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { DataForm } from '@wordpress/dataviews';\nimport { useContext, useState, useMemo } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport { unlock } from '../../lock-unlock';\nimport BlockIcon from '../../components/block-icon';\nimport useBlockDisplayTitle from '../../components/block-title/use-block-display-title';\nimport useBlockDisplayInformation from '../../components/use-block-display-information';\nconst { fieldsKey, formKey } = unlock( blocksPrivateApis );\nimport FieldsDropdownMenu from './fields-dropdown-menu';\nimport { PrivateBlockContext } from '../../components/block-list/private-block-context';\nimport InspectorControls from '../../components/inspector-controls/fill';\n\n// controls\nimport RichText from './rich-text';\nimport Media from './media';\nimport Link from './link';\n\n/**\n * Creates a configured control component that wraps a custom control\n * and passes configuration as props.\n *\n * @param {Component} ControlComponent The React component for the control.\n * @param {Object} config The control configuration passed as a prop.\n *\n * @return {Function} A wrapped control component\n */\nfunction createConfiguredControl( ControlComponent, config = {} ) {\n\treturn function ConfiguredControl( props ) {\n\t\treturn <ControlComponent { ...props } config={ config } />;\n\t};\n}\n\n/**\n * Component that renders a DataForm for a single block's attributes\n * @param {Object} props\n * @param {string} props.clientId The clientId of the block.\n * @param {Object} props.blockType The blockType definition.\n * @param {Function} props.setAttributes Action to set the block's attributes.\n * @param {boolean} props.isCollapsed Whether the DataForm is rendered as 'collapsed' with only the first field\n * displayed by default. When collapsed a dropdown is displayed to allow\n * displaying additional fields. The block's title is displayed as the title.\n * The collapsed mode is often used when multiple BlockForms are shown together.\n */\nfunction BlockFields( {\n\tclientId,\n\tblockType,\n\tsetAttributes,\n\tisCollapsed = false,\n} ) {\n\tconst blockTitle = useBlockDisplayTitle( {\n\t\tclientId,\n\t\tcontext: 'list-view',\n\t} );\n\tconst blockInformation = useBlockDisplayInformation( clientId );\n\n\tconst blockTypeFields = blockType?.[ fieldsKey ];\n\n\tconst attributes = useSelect(\n\t\t( select ) => select( blockEditorStore ).getBlockAttributes( clientId ),\n\t\t[ clientId ]\n\t);\n\n\tconst computedForm = useMemo( () => {\n\t\tif ( ! isCollapsed ) {\n\t\t\treturn blockType?.[ formKey ];\n\t\t}\n\n\t\t// For a collapsed form only show the first field by default.\n\t\treturn {\n\t\t\t...blockType?.[ formKey ],\n\t\t\tfields: [ blockType?.[ formKey ]?.fields?.[ 0 ] ],\n\t\t};\n\t}, [ blockType, isCollapsed ] );\n\n\tconst [ form, setForm ] = useState( computedForm );\n\n\t// Build DataForm fields with proper structure\n\tconst dataFormFields = useMemo( () => {\n\t\tif ( ! blockTypeFields?.length ) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn blockTypeFields.map( ( fieldDef ) => {\n\t\t\tconst field = {\n\t\t\t\t...fieldDef,\n\t\t\t};\n\n\t\t\t// These should be custom Edit components, not replaced here.\n\t\t\t//\n\t\t\t// - rich-text control: it needs clientId\n\t\t\t// - link control: does not need anything extra\n\t\t\t// - media control: needs the Edit config\n\t\t\tif (\n\t\t\t\t'string' === typeof fieldDef.Edit &&\n\t\t\t\tfieldDef.Edit === 'rich-text'\n\t\t\t) {\n\t\t\t\tfield.Edit = createConfiguredControl( RichText, {\n\t\t\t\t\tclientId,\n\t\t\t\t} );\n\t\t\t} else if (\n\t\t\t\t'string' === typeof fieldDef.Edit &&\n\t\t\t\tfieldDef.Edit === 'link'\n\t\t\t) {\n\t\t\t\tfield.Edit = createConfiguredControl( Link );\n\t\t\t} else if (\n\t\t\t\t'object' === typeof fieldDef.Edit &&\n\t\t\t\tfieldDef.Edit.control === 'media'\n\t\t\t) {\n\t\t\t\tfield.Edit = createConfiguredControl( Media, {\n\t\t\t\t\t...fieldDef.Edit,\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\treturn field;\n\t\t} );\n\t}, [ blockTypeFields, clientId ] );\n\n\tif ( ! blockTypeFields?.length ) {\n\t\t// TODO - we might still want to show a placeholder for blocks with no fields.\n\t\t// for example, a way to select the block.\n\t\treturn null;\n\t}\n\n\tconst handleToggleField = ( fieldId ) => {\n\t\tsetForm( ( prev ) => {\n\t\t\tif ( prev.fields?.includes( fieldId ) ) {\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tfields: prev.fields.filter( ( id ) => id !== fieldId ),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...prev,\n\t\t\t\tfields: [ ...( prev.fields || [] ), fieldId ],\n\t\t\t};\n\t\t} );\n\t};\n\n\treturn (\n\t\t<div className=\"block-editor-block-fields__container\">\n\t\t\t<div className=\"block-editor-block-fields__header\">\n\t\t\t\t<HStack spacing={ 1 }>\n\t\t\t\t\t{ isCollapsed && (\n\t\t\t\t\t\t<>\n\t\t\t\t\t\t\t<BlockIcon\n\t\t\t\t\t\t\t\tclassName=\"block-editor-block-fields__header-icon\"\n\t\t\t\t\t\t\t\ticon={ blockInformation?.icon }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t<h2 className=\"block-editor-block-fields__header-title\">\n\t\t\t\t\t\t\t\t<Truncate numberOfLines={ 1 }>\n\t\t\t\t\t\t\t\t\t{ blockTitle }\n\t\t\t\t\t\t\t\t</Truncate>\n\t\t\t\t\t\t\t</h2>\n\t\t\t\t\t\t\t<FieldsDropdownMenu\n\t\t\t\t\t\t\t\tfields={ dataFormFields }\n\t\t\t\t\t\t\t\tvisibleFields={ form.fields }\n\t\t\t\t\t\t\t\tonToggleField={ handleToggleField }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t</>\n\t\t\t\t\t) }\n\t\t\t\t\t{ ! isCollapsed && (\n\t\t\t\t\t\t<h2 className=\"block-editor-block-fields__header-title\">\n\t\t\t\t\t\t\t{ __( 'Content' ) }\n\t\t\t\t\t\t</h2>\n\t\t\t\t\t) }\n\t\t\t\t</HStack>\n\t\t\t</div>\n\t\t\t<DataForm\n\t\t\t\tdata={ attributes }\n\t\t\t\tfields={ dataFormFields }\n\t\t\t\tform={ form }\n\t\t\t\tonChange={ setAttributes }\n\t\t\t/>\n\t\t</div>\n\t);\n}\n\nfunction hasBlockFieldsSupport( blockName ) {\n\treturn !! (\n\t\twindow?.__experimentalContentOnlyInspectorFields &&\n\t\tgetBlockType( blockName )?.[ fieldsKey ]\n\t);\n}\n\nexport function BlockFieldsPanel( props ) {\n\tconst { blockType, isSelectionWithinCurrentSection } =\n\t\tuseContext( PrivateBlockContext );\n\n\treturn (\n\t\t<InspectorControls group=\"content\">\n\t\t\t<BlockFields\n\t\t\t\t{ ...props }\n\t\t\t\tblockType={ blockType }\n\t\t\t\tisCollapsed={ isSelectionWithinCurrentSection }\n\t\t\t/>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Export block support definition.\n */\nexport default {\n\tedit: BlockFieldsPanel,\n\thasSupport: hasBlockFieldsSupport,\n\tattributeKeys: [],\n\tsupportsPatternEditing: true,\n};\n"],
"mappings": ";AAGA;AAAA,EACC,eAAe;AAAA,EACf;AAAA,OACM;AACP;AAAA,EACC,wBAAwB;AAAA,EACxB,0BAA0B;AAAA,OACpB;AACP,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AACzB,SAAS,YAAY,UAAU,eAAe;AAC9C,SAAS,UAAU;AAKnB,SAAS,SAAS,wBAAwB;AAC1C,SAAS,cAAc;AACvB,OAAO,eAAe;AACtB,OAAO,0BAA0B;AACjC,OAAO,gCAAgC;AAEvC,OAAO,wBAAwB;AAC/B,SAAS,2BAA2B;AACpC,OAAO,uBAAuB;AAG9B,OAAO,cAAc;AACrB,OAAO,WAAW;AAClB,OAAO,UAAU;AAaR,SAoHH,UApHG,KAoH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