UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 7 kB
{ "version": 3, "sources": ["../../../src/components/post-schedule/index.js"], "sourcesContent": ["import { parseISO, endOfMonth, startOfMonth } from 'date-fns';\nimport { speak } from '@wordpress/a11y';\nimport { getSettings } from '@wordpress/date';\nimport { __, _x, sprintf } from '@wordpress/i18n';\nimport { Button } from '@wordpress/components';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\nimport { useState, useMemo } from '@wordpress/element';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as editorStore } from '../../store';\nimport { getFullPostScheduleLabel } from './label';\nimport { unlock } from '../../lock-unlock';\n\nconst { PrivatePublishDateTimePicker } = unlock( blockEditorPrivateApis );\n\n/**\n * Renders the PostSchedule component. It allows the user to schedule a post.\n *\n * @param {Object} props Props.\n * @param {Function} props.onClose Function to close the component.\n *\n * @return {React.ReactNode} The rendered component.\n */\nexport default function PostSchedule( props ) {\n\treturn (\n\t\t<PrivatePostSchedule\n\t\t\t{ ...props }\n\t\t\tshowPopoverHeaderActions\n\t\t\tisCompact={ false }\n\t\t/>\n\t);\n}\n\nexport function PrivatePostSchedule( {\n\tonClose,\n\tshowPopoverHeader = true,\n\tshowPopoverHeaderActions,\n\tisCompact,\n} ) {\n\tconst { postDate, postType, isDateFloating } = useSelect(\n\t\t( select ) => ( {\n\t\t\tpostDate: select( editorStore ).getEditedPostAttribute( 'date' ),\n\t\t\tpostType: select( editorStore ).getCurrentPostType(),\n\t\t\tisDateFloating: select( editorStore ).isEditedPostDateFloating(),\n\t\t} ),\n\t\t[]\n\t);\n\n\tconst { editPost } = useDispatch( editorStore );\n\tconst onUpdateDate = ( date ) => {\n\t\teditPost( { date } );\n\t\tspeak(\n\t\t\tdate\n\t\t\t\t? sprintf(\n\t\t\t\t\t\t// translators: %s: The new publish date and time, e.g. \"June 3, 2025 12:00 pm UTC+0\".\n\t\t\t\t\t\t__( 'Publish date set to %s.' ),\n\t\t\t\t\t\tgetFullPostScheduleLabel( date )\n\t\t\t\t )\n\t\t\t\t: __( 'Publish date set to now.' ),\n\t\t\t'assertive'\n\t\t);\n\t};\n\n\tconst [ previewedMonth, setPreviewedMonth ] = useState(\n\t\tstartOfMonth( new Date( postDate ) )\n\t);\n\n\t// Pick up published and scheduled site posts.\n\tconst eventsByPostType = useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).getEntityRecords( 'postType', postType, {\n\t\t\t\tstatus: 'publish,future',\n\t\t\t\tafter: startOfMonth( previewedMonth ).toISOString(),\n\t\t\t\tbefore: endOfMonth( previewedMonth ).toISOString(),\n\t\t\t\texclude: [ select( editorStore ).getCurrentPostId() ],\n\t\t\t\tper_page: 100,\n\t\t\t\t_fields: 'id,date',\n\t\t\t} ),\n\t\t[ previewedMonth, postType ]\n\t);\n\n\tconst events = useMemo(\n\t\t() =>\n\t\t\t( eventsByPostType || [] ).map( ( { date: eventDate } ) => ( {\n\t\t\t\tdate: new Date( eventDate ),\n\t\t\t} ) ),\n\t\t[ eventsByPostType ]\n\t);\n\n\tconst settings = getSettings();\n\n\t// To know if the current timezone is a 12 hour time with look for \"a\" in the time format\n\t// We also make sure this a is not escaped by a \"/\"\n\tconst is12HourTime = /a(?!\\\\)/i.test(\n\t\tsettings.formats.time\n\t\t\t.toLowerCase() // Test only the lower case a.\n\t\t\t.replace( /\\\\\\\\/g, '' ) // Replace \"//\" with empty strings.\n\t\t\t.split( '' )\n\t\t\t.reverse()\n\t\t\t.join( '' ) // Reverse the string and test for \"a\" not followed by a slash.\n\t);\n\n\tconst picker = (\n\t\t<PrivatePublishDateTimePicker\n\t\t\tcurrentDate={ postDate }\n\t\t\tonChange={ onUpdateDate }\n\t\t\tis12Hour={ is12HourTime }\n\t\t\tdateOrder={\n\t\t\t\t/* translators: Order of day, month, and year. Available formats are 'dmy', 'mdy', and 'ymd'. */\n\t\t\t\t_x( 'dmy', 'date order' )\n\t\t\t}\n\t\t\tevents={ events }\n\t\t\tonMonthPreviewed={ ( date ) =>\n\t\t\t\tsetPreviewedMonth( parseISO( date ) )\n\t\t\t}\n\t\t\tonClose={ onClose }\n\t\t\tisCompact={ isCompact }\n\t\t\tshowPopoverHeader={ showPopoverHeader }\n\t\t\tshowPopoverHeaderActions={ showPopoverHeaderActions }\n\t\t/>\n\t);\n\n\t// The popover header carries its own reset action. Rendered inline there is\n\t// no header, so the action follows the picker as a button. It stays in place\n\t// when there is no date to clear, rather than disappearing under the focus\n\t// that just activated it.\n\tif ( showPopoverHeader ) {\n\t\treturn picker;\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t{ picker }\n\t\t\t<Button\n\t\t\t\tclassName=\"editor-post-schedule__reset\"\n\t\t\t\tvariant=\"secondary\"\n\t\t\t\t__next40pxDefaultSize\n\t\t\t\tdisabled={ isDateFloating }\n\t\t\t\taccessibleWhenDisabled\n\t\t\t\tonClick={ () => onUpdateDate( null ) }\n\t\t\t>\n\t\t\t\t{ __( 'Reset' ) }\n\t\t\t</Button>\n\t\t</>\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAmD;AACnD,kBAAsB;AACtB,kBAA4B;AAC5B,kBAAgC;AAChC,wBAAuB;AACvB,kBAAuC;AACvC,0BAAsD;AACtD,qBAAkC;AAClC,uBAAmC;AACnC,mBAAqC;AACrC,mBAAyC;AACzC,yBAAuB;AAcrB;AAZF,IAAM,EAAE,6BAA6B,QAAI,2BAAQ,oBAAAA,WAAuB;AAUzD,SAAR,aAA+B,OAAQ;AAC7C,SACC;AAAA,IAAC;AAAA;AAAA,MACE,GAAG;AAAA,MACL,0BAAwB;AAAA,MACxB,WAAY;AAAA;AAAA,EACb;AAEF;AAEO,SAAS,oBAAqB;AAAA,EACpC;AAAA,EACA,oBAAoB;AAAA,EACpB;AAAA,EACA;AACD,GAAI;AACH,QAAM,EAAE,UAAU,UAAU,eAAe,QAAI;AAAA,IAC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