Skip to content

Commit

Permalink
Simplify api for link UI abstraction to use a single prop for the val…
Browse files Browse the repository at this point in the history
…ue (WordPress#46189)

* Refactor to link prop for all link related data

* Apply same refactor to originalk code in Nav Link block
  • Loading branch information
getdave authored and mpkelly committed Dec 7, 2022
1 parent 3262c99 commit b99a437
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ export const Appender = forwardRef( ( props, ref ) => {
let maybeLinkUI;

if ( insertedBlock ) {
const link = {
url: insertedBlockAttributes.url,
opensInNewTab: insertedBlockAttributes.opensInNewTab,
title: insertedBlockAttributes.label,
};
maybeLinkUI = (
<LinkUI
clientId={ insertedBlock }
value={ link }
linkAttributes={ {
type: insertedBlockAttributes.type,
url: insertedBlockAttributes.url,
kind: insertedBlockAttributes.kind,
} }
link={ insertedBlockAttributes }
onClose={ () => setInsertedBlock( null ) }
hasCreateSuggestion={ false }
onChange={ ( updatedValue ) => {
Expand Down
20 changes: 13 additions & 7 deletions packages/block-editor/src/components/off-canvas-editor/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ function LinkControlTransforms( { clientId } ) {
}

export function LinkUI( props ) {
const link = {
url: props.link.url,
opensInNewTab: props.link.opensInNewTab,
title: props.link.label,
};

return (
<Popover
placement="bottom"
Expand All @@ -131,22 +137,22 @@ export function LinkUI( props ) {
shift
>
<LinkControl
className={ props.className }
hasTextControl
hasRichPreviews
value={ props?.value }
className={ props.className }
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ props?.hasCreateSuggestion }
noDirectEntry={ !! props?.linkAttributes?.type }
noURLSuggestion={ !! props?.linkAttributes?.type }
noDirectEntry={ !! props.link?.type }
noURLSuggestion={ !! props.link?.type }
suggestionsQuery={ getSuggestionsQuery(
props?.linkAttributes?.type,
props?.linkAttributes?.kind
props.link?.type,
props.link?.kind
) }
onChange={ props.onChange }
onRemove={ props.onRemove }
renderControlBottom={
! props?.linkAttributes?.url
! props.link?.url
? () => (
<LinkControlTransforms
clientId={ props?.clientId }
Expand Down
37 changes: 2 additions & 35 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,6 @@ function getMissingText( type ) {
return missingText;
}

/**
* Removes HTML from a given string.
* Note the does not provide XSS protection or otherwise attempt
* to filter strings with malicious intent.
*
* See also: https://github.com/WordPress/gutenberg/pull/35539
*
* @param {string} html the string from which HTML should be removed.
* @return {string} the "cleaned" string.
*/
function navStripHTML( html ) {
const doc = document.implementation.createHTMLDocument( '' );
doc.body.innerHTML = html;
return doc.body.textContent || '';
}

export default function NavigationLinkEdit( {
attributes,
isSelected,
Expand All @@ -237,27 +221,11 @@ export default function NavigationLinkEdit( {
context,
clientId,
} ) {
const {
id,
label,
type,
opensInNewTab,
url,
description,
rel,
title,
kind,
} = attributes;
const { id, label, type, url, description, rel, title, kind } = attributes;

const [ isInvalid, isDraft ] = useIsInvalidLink( kind, type, id );
const { maxNestingLevel } = context;

const link = {
url,
opensInNewTab,
title: label && navStripHTML( label ), // don't allow HTML to display inside the <LinkControl>
};

const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
Expand Down Expand Up @@ -656,8 +624,7 @@ export default function NavigationLinkEdit( {
<LinkUI
className="wp-block-navigation-link__inline-link-input"
clientId={ clientId }
value={ link }
linkAttributes={ { type, url, kind } }
link={ attributes }
onClose={ () => setIsLinkOpen( false ) }
anchor={ popoverAnchor }
hasCreateSuggestion={ userCanCreate }
Expand Down
38 changes: 30 additions & 8 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,27 @@ function LinkControlTransforms( { clientId } ) {
);
}

/**
* Removes HTML from a given string.
* Note the does not provide XSS protection or otherwise attempt
* to filter strings with malicious intent.
*
* See also: https://github.com/WordPress/gutenberg/pull/35539
*
* @param {string} html the string from which HTML should be removed.
* @return {string} the "cleaned" string.
*/
function navStripHTML( html ) {
const doc = document.implementation.createHTMLDocument( '' );
doc.body.innerHTML = html;
return doc.body.textContent || '';
}

export function LinkUI( props ) {
const { saveEntityRecord } = useDispatch( coreStore );

async function handleCreate( pageTitle ) {
const postType = props.linkAttributes.type || 'page';
const postType = props.link.type || 'page';

const page = await saveEntityRecord( 'postType', postType, {
title: pageTitle,
Expand All @@ -146,6 +162,12 @@ export function LinkUI( props ) {
};
}

const link = {
url: props.link.url,
opensInNewTab: props.link.opensInNewTab,
title: props.link.label && navStripHTML( props.link.label ),
};

return (
<Popover
placement="bottom"
Expand All @@ -157,14 +179,14 @@ export function LinkUI( props ) {
hasTextControl
hasRichPreviews
className={ props.className }
value={ props.value }
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ props.hasCreateSuggestion }
createSuggestion={ handleCreate }
createSuggestionButtonText={ ( searchTerm ) => {
let format;

if ( props.linkAttributes.type === 'post' ) {
if ( props.link.type === 'post' ) {
/* translators: %s: search term. */
format = __( 'Create draft post: <mark>%s</mark>' );
} else {
Expand All @@ -179,16 +201,16 @@ export function LinkUI( props ) {
}
);
} }
noDirectEntry={ !! props.linkAttributes.type }
noURLSuggestion={ !! props.linkAttributes.type }
noDirectEntry={ !! props.link.type }
noURLSuggestion={ !! props.link.type }
suggestionsQuery={ getSuggestionsQuery(
props.linkAttributes.type,
props.linkAttributes.kind
props.link.type,
props.link.kind
) }
onChange={ props.onChange }
onRemove={ props.onRemove }
renderControlBottom={
! props.linkAttributes.url
! props.link.url
? () => (
<LinkControlTransforms
clientId={ props.clientId }
Expand Down

0 comments on commit b99a437

Please sign in to comment.