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

Enable ability to create Pages from the inline Link UI #35083

Merged
merged 8 commits into from
Sep 30, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function useBlockEditorSettings( settings, hasTemplate ) {
reusableBlocks,
hasUploadPermissions,
canUseUnfilteredHTML,
userCanCreatePages,
getdave marked this conversation as resolved.
Show resolved Hide resolved
userCanCreatePosts,
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML } = select( editorStore );
const isWeb = Platform.OS === 'web';
Expand Down Expand Up @@ -61,11 +63,51 @@ function useBlockEditorSettings( settings, hasTemplate ) {
),
hasResolvedLocalSiteData: hasFinishedResolvingSiteData,
baseUrl: siteData?.url || '',
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
),
userCanCreatePosts: select( coreStore ).canUser(
'create',
'posts'
),
getdave marked this conversation as resolved.
Show resolved Hide resolved
};
}, [] );

const { undo } = useDispatch( editorStore );

const { saveEntityRecord } = useDispatch( coreStore );

/**
* Creates a Post entity.
* This is utilised by the Link UI to allow for on-the-fly creation of Posts/Pages.
*
* @param {string} postType the post type of the "post" to be created.
* @param {Object} options parameters for the post being created. These mirror those used on 3rd param of saveEntityRecord.
* @return {Object} the post type object that was created.
*/
const createEntity = ( postType, options ) => {
const postTypeWhitelist = [ 'post', 'page' ];
getdave marked this conversation as resolved.
Show resolved Hide resolved

if ( ! postTypeWhitelist.includes( postType ) ) {
return Promise.reject( {
message: `Only Posts and Pages may be created.`,
} );
}

const permsOnEntity =
postType === 'page' ? userCanCreatePages : userCanCreatePosts;

const pluralType = postType === 'page' ? 'Pages' : 'Posts';

if ( ! permsOnEntity ) {
return Promise.reject( {
message: `You do not have permission to create ${ pluralType }.`,
} );
}
return saveEntityRecord( 'postType', postType, options );
};

return useMemo(
() => ( {
...pick( settings, [
Expand Down Expand Up @@ -117,6 +159,9 @@ function useBlockEditorSettings( settings, hasTemplate ) {
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
__experimentalUndo: undo,
outlineMode: hasTemplate,
__experimentalCreateEntity: createEntity,
getdave marked this conversation as resolved.
Show resolved Hide resolved
__experimentalUserCanCreate:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it appropriate to expose this property? Would __experimentalUserCanCreateEntities be a better name?

userCanCreatePosts && userCanCreatePages,
} ),
[
settings,
Expand All @@ -125,6 +170,9 @@ function useBlockEditorSettings( settings, hasTemplate ) {
canUseUnfilteredHTML,
undo,
hasTemplate,
userCanCreatePosts,
userCanCreatePages,
createEntity,
]
);
}
Expand Down
36 changes: 35 additions & 1 deletion packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
useAnchorRef,
removeFormat,
} from '@wordpress/rich-text';
import { __experimentalLinkControl as LinkControl } from '@wordpress/block-editor';
import {
__experimentalLinkControl as LinkControl,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -41,6 +45,16 @@ function InlineLinkUI( {
*/
const [ nextLinkValue, setNextLinkValue ] = useState();

const { createEntity, userCanCreate } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const _settings = getSettings();

return {
createEntity: _settings.__experimentalCreateEntity,
userCanCreate: _settings.__experimentalUserCanCreate,
};
}, [] );

const linkValue = {
url: activeAttributes.url,
type: activeAttributes.type,
Expand Down Expand Up @@ -137,6 +151,24 @@ function InlineLinkUI( {
// otherwise it causes a render of the content.
const focusOnMount = useRef( addingLink ? 'firstElement' : false );

async function handleCreate( pageTitle ) {
const type = 'page';
const postType = type || 'page';

const page = await createEntity( postType, {
title: pageTitle,
status: 'draft',
} );
getdave marked this conversation as resolved.
Show resolved Hide resolved

return {
id: page.id,
type: postType,
title: page.title.rendered,
url: page.link,
kind: 'post-type',
};
}

return (
<Popover
anchorRef={ anchorRef }
Expand All @@ -150,6 +182,8 @@ function InlineLinkUI( {
onRemove={ removeLink }
forceIsEditingLink={ addingLink }
hasRichPreviews
createSuggestion={ createEntity && handleCreate }
withCreateSuggestion={ userCanCreate }
getdave marked this conversation as resolved.
Show resolved Hide resolved
/>
</Popover>
);
Expand Down