UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 5.48 kB
{ "version": 3, "sources": ["../../src/calendar/edit.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport memoize from 'memize';\n\n/**\n * WordPress dependencies\n */\nimport { calendar as icon } from '@wordpress/icons';\nimport { Placeholder, Spinner } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { useServerSideRender } from '@wordpress/server-side-render';\nimport { useBlockProps } from '@wordpress/block-editor';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { useDisabled } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport HtmlRenderer from '../utils/html-renderer';\n\n/**\n * Returns the year and month of a specified date.\n *\n * @see `WP_REST_Posts_Controller::prepare_date_response()`.\n *\n * @param {string} date Date in `ISO8601/RFC3339` format.\n * @return {Object} Year and date of the specified date.\n */\nconst getYearMonth = memoize( ( date ) => {\n\tif ( ! date ) {\n\t\treturn {};\n\t}\n\tconst dateObj = new Date( date );\n\treturn {\n\t\tyear: dateObj.getFullYear(),\n\t\tmonth: dateObj.getMonth() + 1,\n\t};\n} );\n\nexport default function CalendarEdit( { attributes, name } ) {\n\tconst { date, hasPosts, hasPostsResolved } = useSelect( ( select ) => {\n\t\tconst { getEntityRecords, hasFinishedResolution } = select( coreStore );\n\n\t\tconst singlePublishedPostQuery = {\n\t\t\tstatus: 'publish',\n\t\t\tper_page: 1,\n\t\t};\n\t\tconst posts = getEntityRecords(\n\t\t\t'postType',\n\t\t\t'post',\n\t\t\tsinglePublishedPostQuery\n\t\t);\n\t\tconst postsResolved = hasFinishedResolution( 'getEntityRecords', [\n\t\t\t'postType',\n\t\t\t'post',\n\t\t\tsinglePublishedPostQuery,\n\t\t] );\n\n\t\tlet _date;\n\n\t\t// FIXME: @wordpress/block-library should not depend on @wordpress/editor.\n\t\t// Blocks can be loaded into a *non-post* block editor.\n\t\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\t\tconst editorSelectors = select( 'core/editor' );\n\t\tif ( editorSelectors ) {\n\t\t\tconst postType = editorSelectors.getEditedPostAttribute( 'type' );\n\t\t\t// Dates are used to overwrite year and month used on the calendar.\n\t\t\t// This overwrite should only happen for 'post' post types.\n\t\t\t// For other post types the calendar always displays the current month.\n\t\t\tif ( postType === 'post' ) {\n\t\t\t\t_date = editorSelectors.getEditedPostAttribute( 'date' );\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tdate: _date,\n\t\t\thasPostsResolved: postsResolved,\n\t\t\thasPosts: postsResolved && posts?.length === 1,\n\t\t};\n\t}, [] );\n\n\tconst { content, status, error } = useServerSideRender( {\n\t\tattributes: {\n\t\t\t...attributes,\n\t\t\t...getYearMonth( date ),\n\t\t},\n\t\tblock: name,\n\t} );\n\n\tconst disabledRef = useDisabled();\n\tconst blockProps = useBlockProps( { ref: disabledRef } );\n\n\tif ( ! hasPosts ) {\n\t\treturn (\n\t\t\t<div { ...blockProps }>\n\t\t\t\t<Placeholder icon={ icon } label={ __( 'Calendar' ) }>\n\t\t\t\t\t{ ! hasPostsResolved ? (\n\t\t\t\t\t\t<Spinner />\n\t\t\t\t\t) : (\n\t\t\t\t\t\t__( 'No published posts found.' )\n\t\t\t\t\t) }\n\t\t\t\t</Placeholder>\n\t\t\t</div>\n\t\t);\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t{ status === 'loading' && (\n\t\t\t\t<div { ...blockProps }>\n\t\t\t\t\t<Spinner />\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t{ status === 'error' && (\n\t\t\t\t<div { ...blockProps }>\n\t\t\t\t\t<p>\n\t\t\t\t\t\t{ sprintf(\n\t\t\t\t\t\t\t/* translators: %s: error message returned when rendering the block. */\n\t\t\t\t\t\t\t__( 'Error: %s' ),\n\t\t\t\t\t\t\terror\n\t\t\t\t\t\t) }\n\t\t\t\t\t</p>\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t{ status === 'success' && (\n\t\t\t\t<Htm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