@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 10.2 kB
Source Map (JSON)
{"version":3,"file":"edit.mjs","names":["blockEditorPrivateApis","useBlockPreview","coreStore"],"sources":["../../src/post-content/edit.jsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport {\n\tInspectorControls,\n\tuseBlockProps,\n\tuseInnerBlocksProps,\n\tRecursionProvider,\n\tuseHasRecursion,\n\tWarning,\n\tprivateApis as blockEditorPrivateApis,\n\t__experimentalUseBlockPreview as useBlockPreview,\n} from '@wordpress/block-editor';\nimport { parse } from '@wordpress/blocks';\nimport {\n\tuseEntityProp,\n\tuseEntityBlockEditor,\n\tstore as coreStore,\n} from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\nimport { useCanEditEntity } from '../utils/hooks';\nimport { unlock } from '../lock-unlock';\n\nconst { HTMLElementControl } = unlock( blockEditorPrivateApis );\n\nfunction ReadOnlyContent( {\n\tparentLayout,\n\tlayoutClassNames,\n\tuserCanEdit,\n\tpostType,\n\tpostId,\n\ttagName: TagName = 'div',\n} ) {\n\tconst [ , , content ] = useEntityProp(\n\t\t'postType',\n\t\tpostType,\n\t\t'content',\n\t\tpostId\n\t);\n\tconst blockProps = useBlockProps( { className: layoutClassNames } );\n\tconst blocks = useMemo( () => {\n\t\treturn content?.raw ? parse( content.raw ) : [];\n\t}, [ content?.raw ] );\n\tconst blockPreviewProps = useBlockPreview( {\n\t\tblocks,\n\t\tprops: blockProps,\n\t\tlayout: parentLayout,\n\t} );\n\n\tif ( userCanEdit ) {\n\t\t/*\n\t\t * Rendering the block preview using the raw content blocks allows for\n\t\t * block support styles to be generated and applied by the editor.\n\t\t *\n\t\t * The preview using the raw blocks can only be presented to users with\n\t\t * edit permissions for the post to prevent potential exposure of private\n\t\t * block content.\n\t\t */\n\t\treturn <div { ...blockPreviewProps }></div>;\n\t}\n\n\treturn content?.protected ? (\n\t\t<TagName { ...blockProps }>\n\t\t\t<Warning>{ __( 'This content is password protected.' ) }</Warning>\n\t\t</TagName>\n\t) : (\n\t\t<TagName\n\t\t\t{ ...blockProps }\n\t\t\tdangerouslySetInnerHTML={ { __html: content?.rendered } }\n\t\t></TagName>\n\t);\n}\n\nfunction EditableContentLoaded( {\n\tblockProps,\n\tblocks,\n\tonInput,\n\tonChange,\n\ttagName: TagName = 'div',\n} ) {\n\tconst props = useInnerBlocksProps( blockProps, {\n\t\tvalue: blocks,\n\t\tonInput,\n\t\tonChange,\n\t} );\n\treturn <TagName { ...props } />;\n}\n\nfunction EditableContent( { context = {}, tagName: TagName = 'div' } ) {\n\tconst { postType, postId } = context;\n\n\tconst [ blocks, onInput, onChange ] = useEntityBlockEditor(\n\t\t'postType',\n\t\tpostType,\n\t\t{ id: postId }\n\t);\n\n\t// Wait for the entity record before mounting the inner blocks area, so\n\t// that an empty `blocks` value means an empty post rather than one that\n\t// has not loaded yet.\n\tconst hasLoadedRecord = useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).getEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\tpostType,\n\t\t\t\tpostId\n\t\t\t) !== undefined ||\n\t\t\tselect( coreStore ).hasFinishedResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\tpostType,\n\t\t\t\tpostId,\n\t\t\t] ),\n\t\t[ postType, postId ]\n\t);\n\n\tconst blockProps = useBlockProps( { className: 'entry-content' } );\n\n\tif ( ! hasLoadedRecord ) {\n\t\treturn <TagName { ...blockProps } />;\n\t}\n\n\treturn (\n\t\t<EditableContentLoaded\n\t\t\tblockProps={ blockProps }\n\t\t\tblocks={ blocks }\n\t\t\tonInput={ onInput }\n\t\t\tonChange={ onChange }\n\t\t\ttagName={ TagName }\n\t\t/>\n\t);\n}\n\nfunction Content( props ) {\n\tconst {\n\t\tcontext: { queryId, postType, postId } = {},\n\t\tlayoutClassNames,\n\t\ttagName,\n\t} = props;\n\tconst userCanEdit = useCanEditEntity( 'postType', postType, postId );\n\tif ( userCanEdit === undefined ) {\n\t\treturn null;\n\t}\n\n\tconst isDescendentOfQueryLoop = Number.isFinite( queryId );\n\tconst isEditable = userCanEdit && ! isDescendentOfQueryLoop;\n\n\treturn isEditable ? (\n\t\t<EditableContent { ...props } />\n\t) : (\n\t\t<ReadOnlyContent\n\t\t\tparentLayout={ props.parentLayout }\n\t\t\tlayoutClassNames={ layoutClassNames }\n\t\t\tuserCanEdit={ userCanEdit }\n\t\t\tpostType={ postType }\n\t\t\tpostId={ postId }\n\t\t\ttagName={ tagName }\n\t\t/>\n\t);\n}\n\nfunction Placeholder( { layoutClassNames } ) {\n\tconst blockProps = useBlockProps( { className: layoutClassNames } );\n\treturn (\n\t\t<div { ...blockProps }>\n\t\t\t<p>\n\t\t\t\t{ __(\n\t\t\t\t\t'This is the Content block, it will display all the blocks in any single post or page.'\n\t\t\t\t) }\n\t\t\t</p>\n\t\t\t<p>\n\t\t\t\t{ __(\n\t\t\t\t\t'That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.'\n\t\t\t\t) }\n\t\t\t</p>\n\t\t\t<p>\n\t\t\t\t{ __(\n\t\t\t\t\t'If there are any Custom Post Types registered at your site, the Content block can display the contents of those entries as well.'\n\t\t\t\t) }\n\t\t\t</p>\n\t\t</div>\n\t);\n}\n\nfunction RecursionError() {\n\tconst blockProps = useBlockProps();\n\treturn (\n\t\t<div { ...blockProps }>\n\t\t\t<Warning>\n\t\t\t\t{ __( 'Block cannot be rendered inside itself.' ) }\n\t\t\t</Warning>\n\t\t</div>\n\t);\n}\n\n/**\n * Render inspector controls for the PostContent block.\n *\n * @param {Object} props Component props.\n * @param {string} props.tagName The HTML tag name.\n * @param {Function} props.onSelectTagName onChange function for the SelectControl.\n * @param {string} props.clientId The client ID of the current block.\n *\n * @return {React.JSX.Element} The control group.\n */\nfunction PostContentEditControls( { tagName, onSelectTagName, clientId } ) {\n\treturn (\n\t\t<InspectorControls group=\"advanced\">\n\t\t\t<HTMLElementControl\n\t\t\t\ttagName={ tagName }\n\t\t\t\tonChange={ onSelectTagName }\n\t\t\t\tclientId={ clientId }\n\t\t\t\toptions={ [\n\t\t\t\t\t{ label: __( 'Default (<div>)' ), value: 'div' },\n\t\t\t\t\t{ label: '<main>', value: 'main' },\n\t\t\t\t\t{ label: '<section>', value: 'section' },\n\t\t\t\t\t{ label: '<article>', value: 'article' },\n\t\t\t\t] }\n\t\t\t/>\n\t\t</InspectorControls>\n\t);\n}\n\nexport default function PostContentEdit( {\n\tcontext,\n\tattributes: { tagName = 'div' },\n\tsetAttributes,\n\tclientId,\n\t__unstableLayoutClassNames: layoutClassNames,\n\t__unstableParentLayout: parentLayout,\n} ) {\n\tconst { postId: contextPostId, postType: contextPostType } = context;\n\tconst hasAlreadyRendered = useHasRecursion( contextPostId );\n\n\tif ( contextPostId && contextPostType && hasAlreadyRendered ) {\n\t\treturn <RecursionError />;\n\t}\n\n\tconst handleSelectTagName = ( value ) => {\n\t\tsetAttributes( { tagName: value } );\n\t};\n\n\treturn (\n\t\t<>\n\t\t\t<PostContentEditControls\n\t\t\t\ttagName={ tagName }\n\t\t\t\tonSelectTagName={ handleSelectTagName }\n\t\t\t\tclientId={ clientId }\n\t\t\t/>\n\t\t\t<RecursionProvider uniqueId={ contextPostId }>\n\t\t\t\t{ contextPostId && contextPostType ? (\n\t\t\t\t\t<Content\n\t\t\t\t\t\tcontext={ context }\n\t\t\t\t\t\tparentLayout={ parentLayout }\n\t\t\t\t\t\tlayoutClassNames={ layoutClassNames }\n\t\t\t\t\t/>\n\t\t\t\t) : (\n\t\t\t\t\t<Placeholder layoutClassNames={ layoutClassNames } />\n\t\t\t\t) }\n\t\t\t</RecursionProvider>\n\t\t</>\n\t);\n}\n"],"mappings":";;;;;;;;;;AAsBA,MAAM,EAAE,uBAAuB,OAAQA,WAAuB;AAE9D,SAAS,gBAAiB,EACzB,cACA,kBACA,aACA,UACA,QACA,SAAS,UAAU,SAChB;CACH,MAAM,KA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