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 1 commit
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
Next Next commit
Link Control: Add content and media filtering tabs
  • Loading branch information
Infinite-Null committed Dec 31, 2024
commit 775ac1342a44f6641f2f46b522705cf57a8dced9
4 changes: 4 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,8 @@ function LinkControl( {
</InputControlSuffixWrapper>
)
}
tabs={ tabs }
showTabs={ showTabs }
/>
</div>
{ errorMessage && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import deprecated from '@wordpress/deprecated';
import { URLInput } from '../';
import LinkControlSearchResults from './search-results';
import { CREATE_TYPE } from './constants';
import LinkControlSearchResults from './search-results';
import useSearchHandler from './use-search-handler';
import deprecated from '@wordpress/deprecated';

// Must be a function as otherwise URLInput will default
// to the fetchLinkSuggestions passed in block editor settings
Expand Down Expand Up @@ -44,6 +44,8 @@ const LinkControlSearchInput = forwardRef(
createSuggestionButtonText,
hideLabelFromVision = false,
suffix,
tabs = [],
showTabs = false,
},
ref
) => {
Expand Down Expand Up @@ -149,6 +151,9 @@ const LinkControlSearchInput = forwardRef(
} }
ref={ ref }
suffix={ suffix }
showTabs={ showTabs }
tabClassName="block-editor-tab-control__field"
tabs={ tabs }
/>
{ children }
</div>
Expand Down
10 changes: 9 additions & 1 deletion packages/block-editor/src/components/link-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ $block-editor-link-control-number-of-actions: 1;
}

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

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

.block-editor-tab-control__field {
margin-bottom: $grid-unit-20;
margin-left: $grid-unit-20;
margin-right: $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