@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 9.97 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/post-template/hooks.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\nimport { useEntityProp, store as coreStore } from '@wordpress/core-data';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { store as editorStore } from '../../store';\n\nexport function useEditedPostContext() {\n\treturn useSelect( ( select ) => {\n\t\tconst { getCurrentPostId, getCurrentPostType } = select( editorStore );\n\t\treturn {\n\t\t\tpostId: getCurrentPostId(),\n\t\t\tpostType: getCurrentPostType(),\n\t\t};\n\t}, [] );\n}\nexport function useAllowSwitchingTemplates() {\n\tconst { postType, postId } = useEditedPostContext();\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\tconst { canUser, getEntityRecord, getEntityRecords } =\n\t\t\t\tselect( coreStore );\n\t\t\tconst siteSettings = canUser( 'read', {\n\t\t\t\tkind: 'root',\n\t\t\t\tname: 'site',\n\t\t\t} )\n\t\t\t\t? getEntityRecord( 'root', 'site' )\n\t\t\t\t: undefined;\n\n\t\t\tconst isPostsPage = +postId === siteSettings?.page_for_posts;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && +postId === siteSettings?.page_on_front;\n\t\t\t// If current page is set front page or posts page, we also need\n\t\t\t// to check if the current theme has a template for it. If not\n\t\t\tconst templates = isFrontPage\n\t\t\t\t? getEntityRecords( 'postType', 'wp_template', {\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t } )\n\t\t\t\t: [];\n\t\t\tconst hasFrontPage =\n\t\t\t\tisFrontPage &&\n\t\t\t\t!! templates?.some( ( { slug } ) => slug === 'front-page' );\n\t\t\treturn ! isPostsPage && ! hasFrontPage;\n\t\t},\n\t\t[ postId, postType ]\n\t);\n}\n\nfunction useTemplates( postType ) {\n\treturn useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).getEntityRecords( 'postType', 'wp_template', {\n\t\t\t\tper_page: -1,\n\t\t\t\tpost_type: postType,\n\t\t\t\t// We look at the combined templates for now (old endpoint)\n\t\t\t\t// because posts only accept slugs for templates, not IDs.\n\t\t\t} ),\n\t\t[ postType ]\n\t);\n}\n\nexport function useAvailableTemplates() {\n\tconst { postType, postId } = useEditedPostContext();\n\tconst [ postSlug ] = useEntityProp( 'postType', postType, 'slug', postId );\n\tconst currentTemplateSlug = useCurrentTemplateSlug();\n\tconst allowSwitchingTemplate = useAllowSwitchingTemplates();\n\tconst templates = useTemplates( postType );\n\t// Add the default template to the available ones. We don't care about\n\t// possible assignment to postspage/homepage because it's guarded by\n\t// `allowSwitchingTemplate` above.\n\tconst defaultTemplate = useSelect(\n\t\t( select ) => {\n\t\t\t// Only append the default template if the experiment is enabled.\n\t\t\tif ( ! window?.__experimentalDataFormInspector ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\t// If the default template is already assigned, no need\n\t\t\t// to add it to the available templates.\n\t\t\tif ( ! currentTemplateSlug ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tconst { getDefaultTemplateId, getEntityRecord } =\n\t\t\t\tselect( coreStore );\n\t\t\tlet slug;\n\t\t\tif ( postSlug ) {\n\t\t\t\tslug =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ postSlug }`\n\t\t\t\t\t\t: `single-${ postType }-${ postSlug }`;\n\t\t\t} else {\n\t\t\t\tslug = postType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\t\t\tconst templateId = getDefaultTemplateId( { slug } );\n\t\t\tif ( ! templateId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn getEntityRecord( 'postType', 'wp_template', templateId );\n\t\t},\n\t\t[ currentTemplateSlug, postSlug, postType ]\n\t);\n\treturn useMemo(\n\t\t() =>\n\t\t\tallowSwitchingTemplate &&\n\t\t\t[\n\t\t\t\t...( templates || [] ).filter(\n\t\t\t\t\t( template ) =>\n\t\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\t\ttemplate.slug !== currentTemplateSlug &&\n\t\t\t\t\t\t!! template.content.raw // Skip empty templates.\n\t\t\t\t),\n\t\t\t\tdefaultTemplate && {\n\t\t\t\t\t...defaultTemplate,\n\t\t\t\t\ttitle: {\n\t\t\t\t\t\trendered: sprintf(\n\t\t\t\t\t\t\t// translators: %s: Template name\n\t\t\t\t\t\t\t__( '%s (default)' ),\n\t\t\t\t\t\t\tdefaultTemplate.title.rendered\n\t\t\t\t\t\t),\n\t\t\t\t\t},\n\t\t\t\t\t// That's extra custom prop in order to update to an empty template\n\t\t\t\t\t// when we select the default template.\n\t\t\t\t\tisDefault: true,\n\t\t\t\t},\n\t\t\t].filter( Boolean ),\n\t\t[\n\t\t\ttemplates,\n\t\t\tdefaultTemplate,\n\t\t\tcurrentTemplateSlug,\n\t\t\tallowSwitchingTemplate,\n\t\t]\n\t);\n}\n\nexport function usePostTemplatePanelMode() {\n\treturn useSelect( ( select ) => {\n\t\tconst { getEditorSettings, getCurrentTemplateId, getCurrentPostType } =\n\t\t\tselect( editorStore );\n\t\tconst { getPostType, canUser } = select( coreStore );\n\t\tconst postTypeSlug = getCurrentPostType();\n\t\tconst postType = getPostType( postTypeSlug );\n\t\tconst settings = getEditorSettings();\n\t\tconst isBlockTheme = settings.__unstableIsBlockBasedTheme;\n\t\tconst hasTemplates =\n\t\t\t!! settings.availableTemplates &&\n\t\t\tObject.keys( settings.availableTemplates ).length > 0;\n\t\tlet isVisible;\n\t\tif ( ! postType?.viewable ) {\n\t\t\tisVisible = false;\n\t\t} else if ( hasTemplates ) {\n\t\t\tisVisible = true;\n\t\t} else if ( ! settings.supportsTemplateMode ) {\n\t\t\tisVisible = false;\n\t\t} else {\n\t\t\tisVisible =\n\t\t\t\tcanUser( 'create', {\n\t\t\t\t\tkind: 'postType',\n\t\t\t\t\tname: 'wp_template',\n\t\t\t\t} ) ?? false;\n\t\t}\n\t\tconst canViewTemplates = isVisible\n\t\t\t? !! canUser( 'read', {\n\t\t\t\t\tkind: 'postType',\n\t\t\t\t\tname: 'wp_template',\n\t\t\t } )\n\t\t\t: false;\n\t\tif ( ( ! isBlockTheme || ! canViewTemplates ) && isVisible ) {\n\t\t\treturn 'classic';\n\t\t}\n\t\tif ( isBlockTheme && !! getCurrentTemplateId() ) {\n\t\t\treturn 'block-theme';\n\t\t}\n\t\treturn null;\n\t}, [] );\n}\n\nexport function useCurrentTemplateSlug() {\n\tconst { postType, postId } = useEditedPostContext();\n\tconst templates = useTemplates( postType );\n\tconst entityTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tconst post = select( coreStore ).getEditedEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\tpostType,\n\t\t\t\tpostId\n\t\t\t);\n\t\t\treturn post?.template;\n\t\t},\n\t\t[ postType, postId ]\n\t);\n\n\tif ( ! entityTemplate ) {\n\t\treturn;\n\t}\n\t// If a page has a `template` set and is not included in the list\n\t// of the theme's templates, do not return it, in order to resolve\n\t// to the current theme's default template.\n\treturn templates?.find( ( template ) => template.slug === entityTemplate )\n\t\t?.slug;\n}\n"],
"mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB,SAAS,eAAe,SAAS,iBAAiB;AAClD,SAAS,IAAI,eAAe;AAK5B,SAAS,SAAS,mBAAmB;AAE9B,SAAS,uBAAuB;AACtC,SAAO,UAAW,CAAE,WAAY;AAC/B,UAAM,EAAE,kBAAkB,mBAAmB,IAAI,OAAQ,WAAY;AACrE,WAAO;AAAA,MACN,QAAQ,iBAAiB;AAAA,MACzB,UAAU,mBAAmB;AAAA,IAC9B;AAAA,EACD,GAAG,CAAC,CAAE;AACP;AACO,SAAS,6BAA6B;AAC5C,QAAM,EAAE,UAAU,OAAO,IAAI,qBAAqB;AAClD,SAAO;AAAA,IACN,CAAE,WAAY;AACb,YAAM,EAAE,SAAS,iBAAiB,iBAAiB,IAClD,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