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

Sever Nav Link item entity link on URL change if ID already exists #68143

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
18 changes: 14 additions & 4 deletions packages/block-library/src/navigation-link/update-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const updateAttributes = (
title: newLabel = '', // the title of any provided Post.
url: newUrl = '',
opensInNewTab,
id,
id: newID,
kind: newKind = originalKind,
type: newType = originalType,
} = updatedValue;
Expand Down Expand Up @@ -86,13 +86,23 @@ export const updateAttributes = (
( ! newKind && ! isBuiltInType ) || newKind === 'custom';
const kind = isCustomLink ? 'custom' : newKind;

setAttributes( {
const attributes = {
// Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
...( newUrl && { url: encodeURI( safeDecodeURI( newUrl ) ) } ),
...( label && { label } ),
...( undefined !== opensInNewTab && { opensInNewTab } ),
...( id && Number.isInteger( id ) && { id } ),
...( kind && { kind } ),
...( type && type !== 'URL' && { type } ),
} );
};

// If the block's id is set then the menu item is linking to an entity.
// Therefore, if the URL is set but a new ID is not provided, the ID
// should be removed because the URL was manually changed by the User.
if ( newUrl && ! newID && blockAttributes.id ) {
attributes.id = undefined; // explicity "unset" the ID.
} else if ( newID && Number.isInteger( newID ) ) {
attributes.id = newID;
}

setAttributes( attributes );
};
Loading