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 draft entity creation in Nav block offcanvas #52166

Merged
merged 1 commit into from
Jun 30, 2023
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
16 changes: 1 addition & 15 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ import {
} from '@wordpress/dom';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
import {
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { store as coreStore } from '@wordpress/core-data';
import { useMergeRefs } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -184,9 +181,6 @@ export default function NavigationLinkEdit( {
const itemLabelPlaceholder = __( 'Add label…' );
const ref = useRef();

const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );

const {
innerBlocks,
isAtMaxNesting,
Expand Down Expand Up @@ -322,13 +316,6 @@ export default function NavigationLinkEdit( {
setIsLinkOpen( false );
}

let userCanCreate = false;
if ( ! type || type === 'page' ) {
userCanCreate = pagesPermissions.canCreate;
} else if ( type === 'post' ) {
userCanCreate = postsPermissions.canCreate;
}

const {
textColor,
customTextColor,
Expand Down Expand Up @@ -589,7 +576,6 @@ export default function NavigationLinkEdit( {
link={ attributes }
onClose={ () => setIsLinkOpen( false ) }
anchor={ popoverAnchor }
hasCreateSuggestion={ userCanCreate }
onRemove={ removeLink }
onChange={ ( updatedValue ) => {
updateAttributes(
Expand Down
16 changes: 14 additions & 2 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { createInterpolateElement, useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import {
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { switchToBlockType } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -125,6 +128,8 @@ function LinkControlTransforms( { clientId } ) {

export function LinkUI( props ) {
const { saveEntityRecord } = useDispatch( coreStore );
const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );

async function handleCreate( pageTitle ) {
const postType = props.link.type || 'page';
Expand Down Expand Up @@ -155,6 +160,13 @@ export function LinkUI( props ) {

const { label, url, opensInNewTab, type, kind } = props.link;

let userCanCreate = false;
if ( ! type || type === 'page' ) {
userCanCreate = pagesPermissions.canCreate;
} else if ( type === 'post' ) {
userCanCreate = postsPermissions.canCreate;
}

// Memoize link value to avoid overriding the LinkControl's internal state.
// This is a temporary fix. See https://github.com/WordPress/gutenberg/issues/50976#issuecomment-1568226407.
const link = useMemo(
Expand All @@ -179,7 +191,7 @@ export function LinkUI( props ) {
className={ props.className }
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ props.hasCreateSuggestion }
withCreateSuggestion={ userCanCreate }
createSuggestion={ handleCreate }
createSuggestionButtonText={ ( searchTerm ) => {
let format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ function AdditionalBlockContent( { block, insertedBlock, setInsertedBlock } ) {
onClose={ () => {
setInsertedBlock( null );
} }
hasCreateSuggestion={ false }
onChange={ ( updatedValue ) => {
updateAttributes(
updatedValue,
Expand Down