Skip to content
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

Site editor sidebar: extract get page details function. #51038

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* WordPress dependencies
*/
import { __, _x, sprintf } from '@wordpress/i18n';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { count as wordCount } from '@wordpress/wordcount';

/**
* Internal dependencies
*/
import StatusLabel from './status-label';

// Taken from packages/editor/src/components/time-to-read/index.js.
const AVERAGE_READING_RATE = 189;

export default function getPageDetails( page ) {
if ( ! page ) {
return [];
}

const details = [
{
label: __( 'Status' ),
value: (
<StatusLabel
status={ page?.password ? 'protected' : page.status }
date={ page?.date }
/>
),
},
{
label: __( 'Slug' ),
value: <Truncate numberOfLines={ 1 }>{ page.slug }</Truncate>,
},
];

if ( page?.templateTitle ) {
details.push( {
label: __( 'Template' ),
value: page.templateTitle,
} );
}

details.push( {
label: __( 'Parent' ),
value: page?.parentTitle,
} );

/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x( 'words', 'Word count type. Do not translate!' );
const wordsCounted = page?.content?.rendered
? wordCount( page.content.rendered, wordCountType )
: 0;
const readingTime = Math.round( wordsCounted / AVERAGE_READING_RATE );

if ( wordsCounted ) {
details.push(
{
label: __( 'Words' ),
value: wordsCounted.toLocaleString() || __( 'Unknown' ),
},
{
label: __( 'Time to read' ),
value:
readingTime > 1
? sprintf(
/* translators: %s: is the number of minutes. */
__( '%s mins' ),
readingTime.toLocaleString()
)
: __( '< 1 min' ),
}
);
}
return details;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, _x, sprintf } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import {
__experimentalUseNavigator as useNavigator,
Expand All @@ -22,7 +22,6 @@ import {
import { decodeEntities } from '@wordpress/html-entities';
import { pencil } from '@wordpress/icons';
import { humanTimeDiff } from '@wordpress/date';
import { count as wordCount } from '@wordpress/wordcount';
import { createInterpolateElement } from '@wordpress/element';
import { privateApis as privateEditorApis } from '@wordpress/editor';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
Expand All @@ -38,76 +37,7 @@ import SidebarButton from '../sidebar-button';
import SidebarNavigationSubtitle from '../sidebar-navigation-subtitle';
import SidebarDetails from '../sidebar-navigation-data-list';
import DataListItem from '../sidebar-navigation-data-list/data-list-item';
import StatusLabel from './status-label';

// Taken from packages/editor/src/components/time-to-read/index.js.
const AVERAGE_READING_RATE = 189;

function getPageDetails( page ) {
if ( ! page ) {
return [];
}

const details = [
{
label: __( 'Status' ),
value: (
<StatusLabel
status={ page?.password ? 'protected' : page.status }
date={ page?.date }
/>
),
},
{
label: __( 'Slug' ),
value: <Truncate numberOfLines={ 1 }>{ page.slug }</Truncate>,
},
];

if ( page?.templateTitle ) {
details.push( {
label: __( 'Template' ),
value: page.templateTitle,
} );
}

details.push( {
label: __( 'Parent' ),
value: page?.parentTitle,
} );

/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x( 'words', 'Word count type. Do not translate!' );
const wordsCounted = page?.content?.rendered
? wordCount( page.content.rendered, wordCountType )
: 0;
const readingTime = Math.round( wordsCounted / AVERAGE_READING_RATE );

if ( wordsCounted ) {
details.push(
{
label: __( 'Words' ),
value: wordsCounted.toLocaleString() || __( 'Unknown' ),
},
{
label: __( 'Time to read' ),
value:
readingTime > 1
? sprintf(
/* translators: %s: is the number of minutes. */
__( '%s mins' ),
readingTime.toLocaleString()
)
: __( '< 1 min' ),
}
);
}
return details;
}
import getPageDetails from './get-page-details';

export default function SidebarNavigationScreenPage() {
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
Expand Down