Skip to content

Commit

Permalink
Revisions: Add support for revision fields (#93677)
Browse files Browse the repository at this point in the history
* Revisions: Add support for revision fields

* Remove the heading if there's just one element and remove lodash dependency.
  • Loading branch information
BogdanUngureanu committed Aug 23, 2024
1 parent ddd6506 commit 56a94af
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
30 changes: 23 additions & 7 deletions client/post-editor/editor-diff-viewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scrollTo from 'calypso/lib/scroll-to';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getPostRevision } from 'calypso/state/posts/selectors/get-post-revision';
import './style.scss';
import { getPostRevisionFields } from 'calypso/state/posts/selectors/get-post-revision-fields';

const getCenterOffset = ( node ) =>
get( node, 'offsetTop', 0 ) + get( node, 'offsetHeight', 0 ) / 2;
Expand All @@ -19,6 +20,7 @@ class EditorDiffViewer extends PureComponent {
static propTypes = {
postId: PropTypes.number.isRequired,
selectedRevisionId: PropTypes.number,
revisionFields: PropTypes.object,
siteId: PropTypes.number.isRequired,
diff: PropTypes.shape( {
post_content: PropTypes.array,
Expand Down Expand Up @@ -149,7 +151,7 @@ class EditorDiffViewer extends PureComponent {
};

render() {
const { diff, diffView } = this.props;
const { diff, diffView, revisionFields } = this.props;
const classes = clsx( 'editor-diff-viewer', {
'is-loading':
! diff.hasOwnProperty( 'post_content' ) && ! diff.hasOwnProperty( 'post_title' ),
Expand All @@ -170,25 +172,38 @@ class EditorDiffViewer extends PureComponent {
const countAbove = this.changesAboveViewport.length;
const countBelow = this.changesBelowViewport.length;

const fields = [];

const { post_title, totals, ...fieldsWithoutTitle } = diff;

for ( const field in fieldsWithoutTitle ) {
fields.push(
<div key={ field }>
{ Object.keys( fieldsWithoutTitle ).length > 1 && (
<h2 className="editor-diff-viewer__fieldname">{ revisionFields[ field ] }</h2>
) }
<pre className="editor-diff-viewer__content">
<TextDiff operations={ diff[ field ] } splitLines />
</pre>
</div>
);
}

return (
<div className={ classes }>
<div className="editor-diff-viewer__scrollable" ref={ this.handleScrollableRef }>
<div className="editor-diff-viewer__main-pane">
<h1 className="editor-diff-viewer__title">
<TextDiff operations={ diff.post_title } />
</h1>
<pre className="editor-diff-viewer__content">
<TextDiff operations={ diff.post_content } splitLines />
</pre>
{ fields }
</div>
{ diffView === 'split' && (
<div className="editor-diff-viewer__secondary-pane">
<h1 className="editor-diff-viewer__title">
<TextDiff operations={ diff.post_title } />
</h1>
<pre className="editor-diff-viewer__content">
<TextDiff operations={ diff.post_content } splitLines />
</pre>
{ fields }
</div>
) }
</div>
Expand Down Expand Up @@ -219,6 +234,7 @@ class EditorDiffViewer extends PureComponent {

export default connect(
( state, { siteId, postId, selectedRevisionId } ) => ( {
revisionFields: getPostRevisionFields( state, siteId, postId ),
revision: getPostRevision( state, siteId, postId, selectedRevisionId, 'display' ),
} ),
{ recordTracksEvent }
Expand Down
5 changes: 5 additions & 0 deletions client/post-editor/editor-diff-viewer/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
-webkit-overflow-scrolling: touch;
}

.editor-diff-viewer__fieldname {
font-size: $font-title-small;
font-weight: 600;
}

.editor-diff-viewer__title {
font-family: $serif;
font-size: $font-title-large;
Expand Down
3 changes: 2 additions & 1 deletion client/state/posts/revisions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export const requestPostRevisions = ( siteId, postId, postType = 'posts', compar
/**
* Action creator function: POST_REVISIONS_RECEIVE
*/
export const receivePostRevisions = ( { diffs, postId, revisions, siteId } ) => ( {
export const receivePostRevisions = ( { diffs, postId, revisions, revision_fields, siteId } ) => ( {
type: POST_REVISIONS_RECEIVE,
diffs,
postId,
revisions,
revision_fields,
siteId,
} );

Expand Down
6 changes: 5 additions & 1 deletion client/state/posts/revisions/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
import { combineReducers } from 'calypso/state/utils';
import authors from './authors/reducer';

export function diffs( state = {}, { diffs: diffsFromServer, postId, revisions, siteId, type } ) {
export function diffs(
state = {},
{ diffs: diffsFromServer, postId, revisions, revision_fields, siteId, type }
) {
if ( type !== POST_REVISIONS_RECEIVE ) {
return state;
}
Expand Down Expand Up @@ -62,6 +65,7 @@ export function diffs( state = {}, { diffs: diffsFromServer, postId, revisions,
...keyBy( filteredDiffs, ( d ) => `${ d.from }:${ d.to }` ),
},
revisions: mergedRevisions,
revisionFields: revision_fields,
},
},
};
Expand Down
8 changes: 8 additions & 0 deletions client/state/posts/selectors/get-post-revision-fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createSelector } from '@automattic/state-utils';

export const getPostRevisionFields = createSelector(
( state, siteId, postId ) => {
return state.posts.revisions.diffs?.[ siteId ]?.[ postId ]?.revisionFields;
},
( state ) => [ state.posts.revisions.diffs ]
);

0 comments on commit 56a94af

Please sign in to comment.