Skip to content

Commit

Permalink
PostFormat logic/clarity improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill committed Oct 19, 2020
1 parent 0130246 commit ffa57e9
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions packages/editor/src/components/post-format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,35 @@ export default function PostFormat() {
const instanceId = useInstanceId( PostFormat );
const postFormatSelectorId = `post-format-selector-${ instanceId }`;

const { currentFormat, suggestedFormat, supportedFormats } = useSelect(
const { currentFormat, listedFormats, suggestedFormat } = useSelect(
( select ) => {
const themeSupportedFormats =
const supportedFormats =
select( 'core' ).getThemeSupports().formats ?? [];
const { getEditedPostAttribute, getSuggestedPostFormat } = select(
'core/editor'
);
const _currentFormat =
getEditedPostAttribute( 'format' ) ?? 'standard';

// Ensure current format is always in the set.
// The current format may not be a format supported by the theme.
const _supportedFormats = union(
[ _currentFormat ],
themeSupportedFormats
);

// The suggested format may not be supported by the theme, so we have
// to check to make sure.
const potentialSuggestedFormat = getSuggestedPostFormat();

// If the suggested format isn't null, isn't already applied, and is
// supported by the theme, return it. Otherwise, return null.
let supportedSuggestedFormat = null;

if (
const suggestionIsValid =
potentialSuggestedFormat &&
potentialSuggestedFormat !== _currentFormat &&
_supportedFormats.includes( potentialSuggestedFormat )
) {
supportedSuggestedFormat = potentialSuggestedFormat;
}
supportedFormats.includes( potentialSuggestedFormat );

const _suggestedFormat = suggestionIsValid
? potentialSuggestedFormat
: null;

return {
currentFormat: _currentFormat,
supportedFormats: _supportedFormats,
suggestedFormat: supportedSuggestedFormat,
// The current format may not be supported by the theme.
// Ensure it is always shown in the select control.
listedFormats: union( [ _currentFormat ], supportedFormats ),
suggestedFormat: _suggestedFormat,
};
},
[]
Expand All @@ -90,7 +82,7 @@ export default function PostFormat() {
value={ currentFormat }
onChange={ updatePostFormat }
id={ postFormatSelectorId }
options={ supportedFormats.map( ( format ) => ( {
options={ listedFormats.map( ( format ) => ( {
label: POST_FORMAT_TITLES[ format ],
value: format,
} ) ) }
Expand Down

0 comments on commit ffa57e9

Please sign in to comment.