@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 5.09 kB
Source Map (JSON)
{"version":3,"file":"edit.mjs","names":["coreStore","icon","WCSpinner"],"sources":["../../src/calendar/edit.jsx"],"sourcesContent":["import memoize from 'memize';\nimport { calendar as icon } from '@wordpress/icons';\nimport { Placeholder, Spinner as WCSpinner } 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';\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<WCSpinner />\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<WCSpinner />\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<HtmlRender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