-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Simplify api for link UI abstraction to use a single prop for the value #46189
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -146,6 +162,12 @@ export function LinkUI( props ) { | |
}; | ||
} | ||
|
||
const link = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a lot of instances of props.link.whatever here. It might be worth destructuring that variable first so that we can just reference the variables directly. This will mean if we ever want to change the props we pass again we'll have less places to change. Happy to do that in a followup though. |
||
url: props.link.url, | ||
opensInNewTab: props.link.opensInNewTab, | ||
title: props.link.label && navStripHTML( props.link.label ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MaggieCabrera and I noticed that the implementation in the offcanvas does not strip the HTML prior to utilising in the Link UI. We need to apply the same logic there. Also we sholud check whether we can then move both over to using the standard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found this comment which suggests moving to the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it also looks like the off-canvas editor is not offering the option to create a new page when it doesn't exist. I don't know if that's intended or not. If not, then that's definitely something to follow up on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's intentional in the sense that I deliberately removed it. Why? Because including that functionality requires There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created #46193 to track. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should we create an issue for this too? |
||
}; | ||
|
||
return ( | ||
<Popover | ||
placement="bottom" | ||
|
@@ -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 { | ||
|
@@ -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 } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously you needed to know about the structure of this object. Now you just pass all the attributes.