From c4d953bc4e3d1e1dde8fc7424e796881f3012498 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 16 Nov 2023 10:58:17 +0100 Subject: [PATCH 1/3] Unify the PostSchedule component between site and post editors --- .../specs/editor/various/datepicker.test.js | 16 ++-- .../specs/editor/various/scheduling.test.js | 2 +- .../components/sidebar/post-schedule/index.js | 67 ------------- .../sidebar/post-schedule/style.scss | 36 ------- .../components/sidebar/post-status/index.js | 4 +- .../page-panels/page-summary.js | 10 +- .../page-panels/publish-date.js | 94 ------------------- packages/editor/src/components/index.js | 1 + .../src/components/post-schedule/panel.js | 79 ++++++++++++++++ .../src/components/post-schedule/style.scss | 21 +++++ packages/editor/src/style.scss | 1 + 11 files changed, 115 insertions(+), 216 deletions(-) delete mode 100644 packages/edit-post/src/components/sidebar/post-schedule/index.js delete mode 100644 packages/edit-post/src/components/sidebar/post-schedule/style.scss delete mode 100644 packages/edit-site/src/components/sidebar-edit-mode/page-panels/publish-date.js create mode 100644 packages/editor/src/components/post-schedule/panel.js create mode 100644 packages/editor/src/components/post-schedule/style.scss diff --git a/packages/e2e-tests/specs/editor/various/datepicker.test.js b/packages/e2e-tests/specs/editor/various/datepicker.test.js index b0fc3bf898d4a4..6838fd56a2ba9a 100644 --- a/packages/e2e-tests/specs/editor/various/datepicker.test.js +++ b/packages/e2e-tests/specs/editor/various/datepicker.test.js @@ -56,7 +56,7 @@ function formatDatePickerValues( async function getPublishingDate() { return page.$eval( - '.edit-post-post-schedule__toggle', + '.editor-post-schedule__dialog-toggle', ( dateLabel ) => dateLabel.textContent ); } @@ -83,7 +83,7 @@ describe.each( [ [ 'UTC-10' ], [ 'UTC' ], [ 'UTC+10' ] ] )( it( 'should show the publishing date if the date is in the past', async () => { // Open the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); // Change the publishing date to a year in the past. await page.click( '.components-datetime__time-field-year' ); @@ -91,7 +91,7 @@ describe.each( [ [ 'UTC-10' ], [ 'UTC' ], [ 'UTC+10' ] ] )( const datePickerValues = await getDatePickerValues(); // Close the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); const publishingDate = await getPublishingDate(); @@ -102,7 +102,7 @@ describe.each( [ [ 'UTC-10' ], [ 'UTC' ], [ 'UTC+10' ] ] )( it( 'should show the publishing date if the date is in the future', async () => { // Open the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); // Change the publishing date to a year in the future. await page.click( '.components-datetime__time-field-year' ); @@ -110,7 +110,7 @@ describe.each( [ [ 'UTC-10' ], [ 'UTC' ], [ 'UTC+10' ] ] )( const datePickerValues = await getDatePickerValues(); // Close the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); const publishingDate = await getPublishingDate(); @@ -123,17 +123,17 @@ describe.each( [ [ 'UTC-10' ], [ 'UTC' ], [ 'UTC+10' ] ] )( it( `should show the publishing date as "Immediately" if the date is cleared`, async () => { // Open the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); // Change the publishing date to a year in the future. await page.click( '.components-datetime__time-field-year' ); await page.keyboard.press( 'ArrowUp' ); // Close the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); // Open the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); // Clear the date. await page.click( diff --git a/packages/e2e-tests/specs/editor/various/scheduling.test.js b/packages/e2e-tests/specs/editor/various/scheduling.test.js index 61c3541c1f47e5..df75dcb92f2820 100644 --- a/packages/e2e-tests/specs/editor/various/scheduling.test.js +++ b/packages/e2e-tests/specs/editor/various/scheduling.test.js @@ -46,7 +46,7 @@ describe( 'Scheduling', () => { await page.keyboard.press( 'ArrowUp' ); // Close the datepicker. - await page.click( '.edit-post-post-schedule__toggle' ); + await page.click( '.editor-post-schedule__dialog-toggle' ); expect( await getPublishButtonText() ).toBe( 'Schedule…' ); } ); diff --git a/packages/edit-post/src/components/sidebar/post-schedule/index.js b/packages/edit-post/src/components/sidebar/post-schedule/index.js deleted file mode 100644 index 3b20880d736471..00000000000000 --- a/packages/edit-post/src/components/sidebar/post-schedule/index.js +++ /dev/null @@ -1,67 +0,0 @@ -/** - * WordPress dependencies - */ -import { __, sprintf } from '@wordpress/i18n'; -import { PanelRow, Dropdown, Button } from '@wordpress/components'; -import { useState, useMemo } from '@wordpress/element'; -import { - PostSchedule as PostScheduleForm, - PostScheduleCheck, - usePostScheduleLabel, -} from '@wordpress/editor'; - -export default function PostSchedule() { - // Use internal state instead of a ref to make sure that the component - // re-renders when the popover's anchor updates. - const [ popoverAnchor, setPopoverAnchor ] = useState( null ); - // Memoize popoverProps to avoid returning a new object every time. - const popoverProps = useMemo( - () => ( { anchor: popoverAnchor, placement: 'bottom-end' } ), - [ popoverAnchor ] - ); - - return ( - - - { __( 'Publish' ) } - ( - - ) } - renderContent={ ( { onClose } ) => ( - - ) } - /> - - - ); -} - -function PostScheduleToggle( { isOpen, onClick } ) { - const label = usePostScheduleLabel(); - const fullLabel = usePostScheduleLabel( { full: true } ); - return ( - - ); -} diff --git a/packages/edit-post/src/components/sidebar/post-schedule/style.scss b/packages/edit-post/src/components/sidebar/post-schedule/style.scss deleted file mode 100644 index 46fcd819640045..00000000000000 --- a/packages/edit-post/src/components/sidebar/post-schedule/style.scss +++ /dev/null @@ -1,36 +0,0 @@ -.edit-post-post-schedule { - width: 100%; - position: relative; - justify-content: flex-start; - align-items: flex-start; - - span { - display: block; - width: 30%; - margin-right: 8px; - } -} - -.edit-post-post-schedule__dropdown { - width: 70%; -} - -.components-button.edit-post-post-schedule__toggle { - text-align: left; - white-space: normal; - height: auto; - - // This span is added by the Popover in Tooltip when no anchor is - // provided. We set its width to 0 so that it does not cause the button text - // to wrap to a new line when displaying the tooltip. A better fix would be - // to pass anchorRef and avoid the need for a span alltogether, which is - // what this PR allows us to do: - // https://github.com/WordPress/gutenberg/pull/41268. - span { - width: 0; - } -} - -.edit-post-post-schedule__dialog .block-editor-publish-date-time-picker { - margin: $grid-unit-10; -} diff --git a/packages/edit-post/src/components/sidebar/post-status/index.js b/packages/edit-post/src/components/sidebar/post-status/index.js index d903cbb2fa12bc..c8c5a20adcfab1 100644 --- a/packages/edit-post/src/components/sidebar/post-status/index.js +++ b/packages/edit-post/src/components/sidebar/post-status/index.js @@ -8,6 +8,7 @@ import { } from '@wordpress/components'; import { useDispatch, useSelect } from '@wordpress/data'; import { + PostSchedulePanel, PostSwitchToDraftButton, PostSyncStatus, PostURLPanel, @@ -18,7 +19,6 @@ import { */ import PostVisibility from '../post-visibility'; import PostTrash from '../post-trash'; -import PostSchedule from '../post-schedule'; import PostSticky from '../post-sticky'; import PostAuthor from '../post-author'; import PostSlug from '../post-slug'; @@ -61,7 +61,7 @@ export default function PostStatus() { { ( fills ) => ( <> - + diff --git a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js index 26fa86c933f115..e847ec11953b12 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js @@ -2,13 +2,12 @@ * WordPress dependencies */ import { __experimentalVStack as VStack } from '@wordpress/components'; -import { PostURLPanel } from '@wordpress/editor'; +import { PostURLPanel, PostSchedulePanel } from '@wordpress/editor'; /** * Internal dependencies */ import PageStatus from './page-status'; -import PublishDate from './publish-date'; import EditTemplate from './edit-template'; export default function PageSummary( { @@ -27,12 +26,7 @@ export default function PageSummary( { postId={ postId } postType={ postType } /> - + diff --git a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/publish-date.js b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/publish-date.js deleted file mode 100644 index d000394f6816ba..00000000000000 --- a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/publish-date.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * WordPress dependencies - */ -import { - Button, - Dropdown, - __experimentalText as Text, - __experimentalHStack as HStack, -} from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; -import { useDispatch } from '@wordpress/data'; -import { useState, useMemo } from '@wordpress/element'; -import { store as coreStore } from '@wordpress/core-data'; -import { store as noticesStore } from '@wordpress/notices'; -import { __experimentalPublishDateTimePicker as PublishDateTimePicker } from '@wordpress/block-editor'; -import { humanTimeDiff } from '@wordpress/date'; - -export default function ChangeStatus( { postType, postId, status, date } ) { - const { editEntityRecord } = useDispatch( coreStore ); - const { createErrorNotice } = useDispatch( noticesStore ); - - const [ popoverAnchor, setPopoverAnchor ] = useState( null ); - // Memoize popoverProps to avoid returning a new object every time. - const popoverProps = useMemo( - () => ( { - // Anchor the popover to the middle of the entire row so that it doesn't - // move around when the label changes. - anchor: popoverAnchor, - 'aria-label': __( 'Change publish date' ), - placement: 'bottom-end', - } ), - [ popoverAnchor ] - ); - - const saveDate = async ( newDate ) => { - try { - let newStatus = status; - if ( status === 'future' && new Date( newDate ) < new Date() ) { - newStatus = 'publish'; - } else if ( - status === 'publish' && - new Date( newDate ) > new Date() - ) { - newStatus = 'future'; - } - await editEntityRecord( 'postType', postType, postId, { - status: newStatus, - date: newDate, - } ); - } catch ( error ) { - const errorMessage = - error.message && error.code !== 'unknown_error' - ? error.message - : __( 'An error occurred while updating the status' ); - - createErrorNotice( errorMessage, { - type: 'snackbar', - } ); - } - }; - - const relateToNow = date ? humanTimeDiff( date ) : __( 'Immediately' ); - - return ( - - - { __( 'Publish' ) } - - ( - - ) } - renderContent={ ( { onClose } ) => ( - - ) } - /> - - ); -} diff --git a/packages/editor/src/components/index.js b/packages/editor/src/components/index.js index bcfccc026ff727..a5a4fc7fcac365 100644 --- a/packages/editor/src/components/index.js +++ b/packages/editor/src/components/index.js @@ -50,6 +50,7 @@ export { default as PostScheduleLabel, usePostScheduleLabel, } from './post-schedule/label'; +export { default as PostSchedulePanel } from './post-schedule/panel'; export { default as PostSlug } from './post-slug'; export { default as PostSlugCheck } from './post-slug/check'; export { default as PostSticky } from './post-sticky'; diff --git a/packages/editor/src/components/post-schedule/panel.js b/packages/editor/src/components/post-schedule/panel.js new file mode 100644 index 00000000000000..c16eb7d985caea --- /dev/null +++ b/packages/editor/src/components/post-schedule/panel.js @@ -0,0 +1,79 @@ +/** + * WordPress dependencies + */ +import { + Button, + Dropdown, + __experimentalHStack as HStack, +} from '@wordpress/components'; +import { __, sprintf } from '@wordpress/i18n'; +import { useState, useMemo } from '@wordpress/element'; +import { humanTimeDiff } from '@wordpress/date'; +import { useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import PostScheduleCheck from './check'; +import PostScheduleForm from './index'; +import { store as editorStore } from '../../store'; +import { usePostScheduleLabel } from './label'; + +export default function PostSchedulePanel() { + const { date } = useSelect( + ( select ) => ( { + date: select( editorStore ).getEditedPostAttribute( 'date' ), + } ), + [] + ); + + const [ popoverAnchor, setPopoverAnchor ] = useState( null ); + // Memoize popoverProps to avoid returning a new object every time. + const popoverProps = useMemo( + () => ( { + // Anchor the popover to the middle of the entire row so that it doesn't + // move around when the label changes. + anchor: popoverAnchor, + 'aria-label': __( 'Change publish date' ), + placement: 'bottom-end', + } ), + [ popoverAnchor ] + ); + + const relateToNow = date ? humanTimeDiff( date ) : __( 'Immediately' ); + const label = usePostScheduleLabel(); + + return ( + + + { __( 'Publish' ) } + ( + + ) } + renderContent={ ( { onClose } ) => ( + + ) } + /> + + + ); +} diff --git a/packages/editor/src/components/post-schedule/style.scss b/packages/editor/src/components/post-schedule/style.scss new file mode 100644 index 00000000000000..3635042c7ce52b --- /dev/null +++ b/packages/editor/src/components/post-schedule/style.scss @@ -0,0 +1,21 @@ +.editor-post-schedule__panel { + width: 100%; + justify-content: flex-start; + align-items: flex-start; + + span { + display: block; + width: 30%; + } +} + +.editor-post-schedule__panel-dropdown { + width: 70%; +} + +.editor-post-schedule__dialog { + .components-popover__content { + min-width: 320px; + padding: $grid-unit-20; + } +} diff --git a/packages/editor/src/style.scss b/packages/editor/src/style.scss index dbffbbef4d5212..5428bf5cb1f151 100644 --- a/packages/editor/src/style.scss +++ b/packages/editor/src/style.scss @@ -11,6 +11,7 @@ @import "./components/post-publish-button/style.scss"; @import "./components/post-publish-panel/style.scss"; @import "./components/post-saved-state/style.scss"; +@import "./components/post-schedule/style.scss"; @import "./components/post-sync-status/style.scss"; @import "./components/post-taxonomies/style.scss"; @import "./components/post-text-editor/style.scss"; From 019cdc20ecac72e3cf04dec1026da302f418fd9f Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 16 Nov 2023 15:34:27 +0100 Subject: [PATCH 2/3] Fix build --- packages/edit-post/src/style.scss | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/edit-post/src/style.scss b/packages/edit-post/src/style.scss index 7a71d20ba0f1f4..89a5c4483b97ee 100644 --- a/packages/edit-post/src/style.scss +++ b/packages/edit-post/src/style.scss @@ -12,7 +12,6 @@ @import "./components/sidebar/last-revision/style.scss"; @import "./components/sidebar/post-author/style.scss"; @import "./components/sidebar/post-format/style.scss"; -@import "./components/sidebar/post-schedule/style.scss"; @import "./components/sidebar/post-slug/style.scss"; @import "./components/sidebar/post-template/style.scss"; @import "./components/sidebar/post-visibility/style.scss"; From df2fe7bf7298b27cbf359b4343987aafb35d6ae0 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 17 Nov 2023 08:58:22 +0100 Subject: [PATCH 3/3] Use the post editor date format --- .../src/components/post-schedule/panel.js | 19 ++++++------------- .../src/components/post-schedule/style.scss | 9 +++++++++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/editor/src/components/post-schedule/panel.js b/packages/editor/src/components/post-schedule/panel.js index c16eb7d985caea..834e7237e0e946 100644 --- a/packages/editor/src/components/post-schedule/panel.js +++ b/packages/editor/src/components/post-schedule/panel.js @@ -8,25 +8,15 @@ import { } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; import { useState, useMemo } from '@wordpress/element'; -import { humanTimeDiff } from '@wordpress/date'; -import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ import PostScheduleCheck from './check'; import PostScheduleForm from './index'; -import { store as editorStore } from '../../store'; import { usePostScheduleLabel } from './label'; export default function PostSchedulePanel() { - const { date } = useSelect( - ( select ) => ( { - date: select( editorStore ).getEditedPostAttribute( 'date' ), - } ), - [] - ); - const [ popoverAnchor, setPopoverAnchor ] = useState( null ); // Memoize popoverProps to avoid returning a new object every time. const popoverProps = useMemo( @@ -40,8 +30,8 @@ export default function PostSchedulePanel() { [ popoverAnchor ] ); - const relateToNow = date ? humanTimeDiff( date ) : __( 'Immediately' ); const label = usePostScheduleLabel(); + const fullLabel = usePostScheduleLabel( { full: true } ); return ( @@ -55,7 +45,7 @@ export default function PostSchedulePanel() { focusOnMount className="editor-post-schedule__panel-dropdown" contentClassName="editor-post-schedule__dialog" - renderToggle={ ( { onToggle } ) => ( + renderToggle={ ( { onToggle, isOpen } ) => ( ) } renderContent={ ( { onClose } ) => ( diff --git a/packages/editor/src/components/post-schedule/style.scss b/packages/editor/src/components/post-schedule/style.scss index 3635042c7ce52b..d70a9e9576fbbe 100644 --- a/packages/editor/src/components/post-schedule/style.scss +++ b/packages/editor/src/components/post-schedule/style.scss @@ -19,3 +19,12 @@ padding: $grid-unit-20; } } + +.editor-post-schedule__dialog-toggle { + display: block; + max-width: 100%; + overflow: hidden; + text-align: left; + text-overflow: ellipsis; + white-space: nowrap; +}