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
Merged
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
12 changes: 12 additions & 0 deletions packages/block-editor/src/components/link-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ $preview-image-height: 140px;
}
}

.block-editor-link-control__search-create {
align-items: center; // align text with icon.

.block-editor-link-control__search-item-title {
margin-bottom: 0;
}

.block-editor-link-control__search-item-icon {
top: 0; // cancel compensatory spacing added to default suggestions.
}
}

// Specificity overide
.block-editor-link-control__search-results div[role="menu"] > .block-editor-link-control__search-item.block-editor-link-control__search-item {
padding: 10px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
__experimentalFetchUrlData as fetchUrlData,
} from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -33,6 +34,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
reusableBlocks,
hasUploadPermissions,
canUseUnfilteredHTML,
userCanCreatePages,
getdave marked this conversation as resolved.
Show resolved Hide resolved
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML } = select( editorStore );
const isWeb = Platform.OS === 'web';
Expand Down Expand Up @@ -61,11 +63,30 @@ function useBlockEditorSettings( settings, hasTemplate ) {
),
hasResolvedLocalSiteData: hasFinishedResolvingSiteData,
baseUrl: siteData?.url || '',
userCanCreatePages: canUser( 'create', 'pages' ),
};
}, [] );

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 {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 createPageEntity = ( options ) => {
if ( ! userCanCreatePages ) {
return Promise.reject( {
message: __( 'You do not have permission to create Pages.' ),
} );
}
getdave marked this conversation as resolved.
Show resolved Hide resolved
return saveEntityRecord( 'postType', 'page', options );
};

return useMemo(
() => ( {
...pick( settings, [
Expand Down Expand Up @@ -117,6 +138,8 @@ function useBlockEditorSettings( settings, hasTemplate ) {
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
__experimentalUndo: undo,
outlineMode: hasTemplate,
__experimentalCreatePageEntity: createPageEntity,
__experimentalUserCanCreatePages: userCanCreatePages,
} ),
[
settings,
Expand All @@ -125,6 +148,9 @@ function useBlockEditorSettings( settings, hasTemplate ) {
canUseUnfilteredHTML,
undo,
hasTemplate,

userCanCreatePages,
createPageEntity,
Copy link
Contributor

Choose a reason for hiding this comment

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

This PR introduced a really bad degradation in terms of typing performance. I think this change here has been reverted though in a more recent PR so the degradation is gone but we should be careful with block editor settings. It shouldn't become a basket for any config. https://codehealth.vercel.app

Copy link
Contributor Author

@getdave getdave Oct 7, 2021

Choose a reason for hiding this comment

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

Yes it did. Apologies for that.

Specifically the dependencies we introduced to the useMemo hook that caused the entire memoized object to be recomputed on each render because the references were always different.

It was resolved and it's good that we have tooling to help catch such scenarios. I assume we are to be encouraged to keep an eye on https://codehealth.vercel.app/?

It shouldn't become a basket for any config

I agree and that was considered before we placed it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

It was resolved and it's good that we have tooling to help catch such scenarios. I assume we are to be encouraged to keep an eye on https://codehealth.vercel.app/?

yeah, while not 100% precise, it's a good indicator to give us hints :)

Copy link
Contributor

Choose a reason for hiding this comment

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

From eyeballing, it seems like the problem rooted in the fact that createPageEntity was a different function every time this function ran, therefore useMemo was re-ran on every re-render and possibly triggered another re-render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's right.

Specifically the dependencies we introduced to the useMemo hook that caused the entire memoized object to be recomputed on each render because the references were always different.

]
);
}
Expand Down
49 changes: 46 additions & 3 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* WordPress dependencies
*/
import { useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useState, useRef, createInterpolateElement } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { withSpokenMessages, Popover } from '@wordpress/components';
import { prependHTTP } from '@wordpress/url';
import {
Expand All @@ -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 { createPageEntity, userCanCreatePages } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const _settings = getSettings();

return {
createPageEntity: _settings.__experimentalCreatePageEntity,
userCanCreatePages: _settings.__experimentalUserCanCreatePages,
};
}, [] );

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

async function handleCreate( pageTitle ) {
const page = await createPageEntity( {
title: pageTitle,
status: 'draft',
} );

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

function createButtonText( searchTerm ) {
return createInterpolateElement(
sprintf(
/* translators: %s: search term. */
__( 'Create Page: <mark>%s</mark>' ),
searchTerm
),
{ mark: <mark /> }
);
}

return (
<Popover
anchorRef={ anchorRef }
Expand All @@ -150,6 +190,9 @@ function InlineLinkUI( {
onRemove={ removeLink }
forceIsEditingLink={ addingLink }
hasRichPreviews
createSuggestion={ createPageEntity && handleCreate }
withCreateSuggestion={ userCanCreatePages }
createSuggestionButtonText={ createButtonText }
/>
</Popover>
);
Expand Down