-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Experimental: Add revisions panel
#76735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3f8b4f
7fac5de
49a248e
5415315
36511d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| PanelBody, | ||
| Button, | ||
| __experimentalHStack as HStack, | ||
| __experimentalVStack as VStack, | ||
| privateApis as componentsPrivateApis, | ||
| } from '@wordpress/components'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { DataViews } from '@wordpress/dataviews'; | ||
| import { dateI18n, getDate, humanTimeDiff, getSettings } from '@wordpress/date'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { authorField } from '@wordpress/fields'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import PostLastRevisionCheck from '../post-last-revision/check'; | ||
| import { store as editorStore } from '../../store'; | ||
| import { unlock } from '../../lock-unlock'; | ||
|
|
||
| const { Badge } = unlock( componentsPrivateApis ); | ||
| const DAY_IN_MILLISECONDS = 86400000; | ||
| const EMPTY_ARRAY = []; | ||
|
|
||
| const REVISIONS_QUERY = { | ||
| per_page: 3, | ||
| orderby: 'date', | ||
| order: 'desc', | ||
| context: 'embed', | ||
| _fields: 'id,date,author', | ||
| }; | ||
| const defaultLayouts = { activity: {} }; | ||
| const view = { | ||
| type: 'activity', | ||
| titleField: 'date', | ||
| fields: [ 'author' ], | ||
| layout: { | ||
| density: 'compact', | ||
| }, | ||
| }; | ||
| const fields = [ | ||
| { | ||
| id: 'date', | ||
| label: __( 'Date' ), | ||
| render: ( { item } ) => { | ||
| const dateNowInMs = getDate( null ).getTime(); | ||
| const date = getDate( item.date ?? null ); | ||
| const displayDate = | ||
| dateNowInMs - date.getTime() > DAY_IN_MILLISECONDS | ||
| ? dateI18n( | ||
| getSettings().formats.datetimeAbbreviated, | ||
| date | ||
| ) | ||
| : humanTimeDiff( date ); | ||
| return ( | ||
| <time | ||
| className="editor-post-revisions-panel__revision-date" | ||
| dateTime={ item.date } | ||
| > | ||
| { displayDate } | ||
| </time> | ||
| ); | ||
| }, | ||
| enableSorting: false, | ||
| enableHiding: false, | ||
| }, | ||
| authorField, | ||
| ]; | ||
| const noop = () => {}; | ||
| const paginationInfo = {}; | ||
|
|
||
| function PostRevisionsPanelContent() { | ||
| const { setCurrentRevisionId } = unlock( useDispatch( editorStore ) ); | ||
| const { revisionsCount, revisions, isLoading, lastRevisionId } = useSelect( | ||
| ( select ) => { | ||
| const { getCurrentPostId, getCurrentPostType } = | ||
| select( editorStore ); | ||
| const { | ||
| getCurrentPostRevisionsCount, | ||
| getCurrentPostLastRevisionId, | ||
| } = select( editorStore ); | ||
| const { getRevisions, isResolving } = select( coreStore ); | ||
| const query = [ | ||
| 'postType', | ||
| getCurrentPostType(), | ||
| getCurrentPostId(), | ||
| REVISIONS_QUERY, | ||
| ]; | ||
| const _revisions = getRevisions( ...query ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This request is only necessary for the data view, but I suspect it will execute even when the panel is closed. Should we create a child component to handle this request to improve performance?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good thought! I'll update later, as there are ongoing design discussions right now. |
||
| return { | ||
| revisionsCount: getCurrentPostRevisionsCount(), | ||
| lastRevisionId: getCurrentPostLastRevisionId(), | ||
| revisions: _revisions, | ||
| isLoading: isResolving( 'getRevisions', query ), | ||
| }; | ||
| }, | ||
| [] | ||
| ); | ||
| return ( | ||
| <PanelBody | ||
| title={ | ||
| <HStack justify="space-between" align="center" as="span"> | ||
| <span>{ __( 'Revisions' ) }</span> | ||
| <Badge className="editor-post-revisions-panel__revisions-count"> | ||
| { revisionsCount } | ||
| </Badge> | ||
| </HStack> | ||
| } | ||
| initialOpen={ false } | ||
| > | ||
| <VStack className="editor-post-revisions-panel"> | ||
| <DataViews | ||
| view={ view } | ||
| onChangeView={ noop } | ||
| fields={ fields } | ||
| data={ revisions || EMPTY_ARRAY } | ||
| isLoading={ isLoading } | ||
| paginationInfo={ paginationInfo } | ||
| defaultLayouts={ defaultLayouts } | ||
| getItemId={ ( item ) => item.id } | ||
| isItemClickable={ () => true } | ||
| onClickItem={ ( item ) => { | ||
| setCurrentRevisionId( item.id ); | ||
| } } | ||
| > | ||
| <DataViews.Layout /> | ||
| </DataViews> | ||
| <Button | ||
| className="editor-post-revisions-panel__view-all" | ||
| __next40pxDefaultSize | ||
| variant="secondary" | ||
| onClick={ () => setCurrentRevisionId( lastRevisionId ) } | ||
| > | ||
| { __( 'View all revisions' ) } | ||
| </Button> | ||
| </VStack> | ||
| </PanelBody> | ||
| ); | ||
| } | ||
|
|
||
| export default function PostRevisionsPanel() { | ||
| return ( | ||
| <PostLastRevisionCheck> | ||
| <PostRevisionsPanelContent /> | ||
| </PostLastRevisionCheck> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| .editor-post-revisions-panel { | ||
| .editor-post-revisions-panel__view-all { | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .editor-post-revisions-panel__revision-date { | ||
| text-transform: uppercase; | ||
| font-weight: 600; | ||
| font-size: 12px; | ||
| } | ||
|
Comment on lines
+6
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should be overriding Dataviews styles like this. |
||
| } | ||
|
|
||
| .editor-post-revisions-panel__revisions-count { | ||
| margin-top: -4.5px; | ||
| margin-bottom: -4.5px; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine rendering dates in "human time diff" is a common use case. Checked the datetime format but it doesn't offer mechanics for this.
I wonder if there're ways to incorporate this into the Field API.