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

Link Control: Add content and media filtering tabs #68439

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ function LinkControl( {
hasRichPreviews = false,
hasTextControl = false,
renderControlBottom = null,
tabs = [],
showTabs,
} ) {
if ( withCreateSuggestion === undefined && createSuggestion ) {
withCreateSuggestion = true;
Expand Down Expand Up @@ -422,6 +424,9 @@ function LinkControl( {
</InputControlSuffixWrapper>
)
}
tabs={ tabs }
showTabs={ showTabs }
tabClassName="block-editor-tab-control__field"
/>
</div>
{ errorMessage && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const LinkControlSearchInput = forwardRef(
children,
currentLink = {},
className = null,
tabClassName = null,
placeholder = null,
withCreateSuggestion = false,
onCreateSuggestion = noop,
Expand All @@ -44,6 +45,8 @@ const LinkControlSearchInput = forwardRef(
createSuggestionButtonText,
hideLabelFromVision = false,
suffix,
tabs = [],
showTabs = false,
},
ref
) => {
Expand Down Expand Up @@ -149,6 +152,9 @@ const LinkControlSearchInput = forwardRef(
} }
ref={ ref }
suffix={ suffix }
showTabs={ showTabs }
tabClassName={ tabClassName }
tabs={ tabs }
/>
{ children }
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $block-editor-link-control-number-of-actions: 1;
}

.block-editor-link-control__field {
margin: $grid-unit-20; // allow margin collapse for vertical spacing.
margin: $grid-unit-20 $grid-unit-20 0 $grid-unit-20; // allow margin collapse for vertical spacing.

.components-base-control__label {
color: $gray-900;
Expand Down Expand Up @@ -120,6 +120,10 @@ $block-editor-link-control-number-of-actions: 1;
}
}

.block-editor-tab-control__field {
margin: 0 $grid-unit-20 $grid-unit-20 $grid-unit-20;
}

.block-editor-link-control__search-item {

&.components-button.components-menu-item__button {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ export default function useSearchHandler(
: handleNoop;

return useCallback(
( val, { isInitialSuggestions } ) => {
( val, { isInitialSuggestions, type = 'post' } ) => {
return isURLLike( val )
? directEntryHandler( val, { isInitialSuggestions } )
: handleEntitySearch(
val,
{ ...suggestionsQuery, isInitialSuggestions },
{
...suggestionsQuery,
isInitialSuggestions,
type,
},
fetchSearchSuggestions,
withCreateSuggestion,
pageOnFront,
Expand Down
38 changes: 34 additions & 4 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Button,
__experimentalInputControl as InputControl,
Spinner,
TabPanel,
withSpokenMessages,
Popover,
} from '@wordpress/components';
Expand Down Expand Up @@ -69,11 +70,12 @@ class URLInput extends Component {
selectedSuggestion: null,
suggestionsListboxId: '',
suggestionOptionIdPrefix: '',
selectedTab: 'post',
};
}

componentDidUpdate( prevProps ) {
const { showSuggestions, selectedSuggestion } = this.state;
componentDidUpdate( prevProps, prevState ) {
const { showSuggestions, selectedSuggestion, selectedTab } = this.state;
const { value, __experimentalShowInitialSuggestions = false } =
this.props;

Expand All @@ -91,11 +93,15 @@ class URLInput extends Component {
} );
}

if ( prevState.selectedTab !== selectedTab ) {
this.updateSuggestions( value, selectedTab );
}

// Update suggestions when the value changes.
if ( prevProps.value !== value && ! this.props.disableSuggestions ) {
if ( value?.length ) {
// If the new value is not empty we need to update with suggestions for it.
this.updateSuggestions( value );
this.updateSuggestions( value, selectedTab );
} else if ( __experimentalShowInitialSuggestions ) {
// If the new value is empty and we can show initial suggestions, then show initial suggestions.
this.updateSuggestions();
Expand Down Expand Up @@ -128,7 +134,7 @@ class URLInput extends Component {
);
}

updateSuggestions( value = '' ) {
updateSuggestions( value = '', selectedTab = null ) {
const {
__experimentalFetchLinkSuggestions: fetchLinkSuggestions,
__experimentalHandleURLSuggestions: handleURLSuggestions,
Expand Down Expand Up @@ -175,6 +181,7 @@ class URLInput extends Component {

const request = fetchLinkSuggestions( value, {
isInitialSuggestions,
type: selectedTab || 'post',
} );

request
Expand Down Expand Up @@ -409,11 +416,34 @@ class URLInput extends Component {
return (
<>
{ this.renderControl() }
{ this.renderTabPanel() }
{ this.renderSuggestions() }
</>
);
}

renderTabPanel() {
const { tabClassName, tabs = [], showTabs = false } = this.props;

if ( ! showTabs ) {
return null;
}

return (
<TabPanel
className={ tabClassName }
tabs={ tabs }
onSelect={ ( tabName ) => {
this.setState( {
selectedTab: tabName,
} );
} }
>
{ () => null }
</TabPanel>
);
}

renderControl() {
const {
label = null,
Expand Down
13 changes: 13 additions & 0 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ function InlineLinkUI( {
);
}

const tabs = [
{
name: 'post',
title: 'Content',
},
{
name: 'attachment',
title: 'Media',
},
];

return (
<Popover
anchor={ popoverAnchor }
Expand All @@ -262,6 +273,8 @@ function InlineLinkUI( {
constrainTabbing
>
<LinkControl
tabs={ tabs }
showTabs
value={ linkValue }
onChange={ onChangeLink }
onRemove={ removeLink }
Expand Down
Loading