Skip to content

Commit

Permalink
Link UI - add constants for link entry types (#36490)
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave authored May 18, 2022
1 parent b495580 commit 14327de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
11 changes: 11 additions & 0 deletions packages/block-editor/src/components/link-control/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import { __ } from '@wordpress/i18n';
// Used to help distinguish the "Create" suggestion within the search results in
// order to handle it as a unique case.
export const CREATE_TYPE = '__CREATE__';
export const TEL_TYPE = 'tel';
export const URL_TYPE = 'URL';
export const MAILTO_TYPE = 'mailto';
export const INTERNAL_TYPE = 'internal';

export const LINK_ENTRY_TYPES = [
URL_TYPE,
MAILTO_TYPE,
TEL_TYPE,
INTERNAL_TYPE,
];

export const DEFAULT_LINK_SETTINGS = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createElement, Fragment } from '@wordpress/element';
*/
import LinkControlSearchCreate from './search-create-button';
import LinkControlSearchItem from './search-item';
import { CREATE_TYPE } from './constants';
import { CREATE_TYPE, LINK_ENTRY_TYPES } from './constants';

export default function LinkControlSearchResults( {
instanceId,
Expand All @@ -38,10 +38,9 @@ export default function LinkControlSearchResults( {
}
);

const directLinkEntryTypes = [ 'url', 'mailto', 'tel', 'internal' ];
const isSingleDirectEntryResult =
suggestions.length === 1 &&
directLinkEntryTypes.includes( suggestions[ 0 ].type.toLowerCase() );
LINK_ENTRY_TYPES.includes( suggestions[ 0 ].type );
const shouldShowCreateSuggestion =
withCreateSuggestion &&
! isSingleDirectEntryResult &&
Expand Down Expand Up @@ -127,8 +126,8 @@ export default function LinkControlSearchResults( {
handleSuggestionClick( suggestion );
} }
isSelected={ index === selectedSuggestion }
isURL={ directLinkEntryTypes.includes(
suggestion.type.toLowerCase()
isURL={ LINK_ENTRY_TYPES.includes(
suggestion.type
) }
searchTerm={ currentInputValue }
shouldShowType={ shouldShowSuggestionsTypes }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@ import { startsWith } from 'lodash';
* Internal dependencies
*/
import isURLLike from './is-url-like';
import { CREATE_TYPE } from './constants';
import {
CREATE_TYPE,
TEL_TYPE,
MAILTO_TYPE,
INTERNAL_TYPE,
URL_TYPE,
} from './constants';
import { store as blockEditorStore } from '../../store';

export const handleNoop = () => Promise.resolve( [] );

export const handleDirectEntry = ( val ) => {
let type = 'URL';
let type = URL_TYPE;

const protocol = getProtocol( val ) || '';

if ( protocol.includes( 'mailto' ) ) {
type = 'mailto';
type = MAILTO_TYPE;
}

if ( protocol.includes( 'tel' ) ) {
type = 'tel';
type = TEL_TYPE;
}

if ( startsWith( val, '#' ) ) {
type = 'internal';
type = INTERNAL_TYPE;
}

return Promise.resolve( [
Expand Down

0 comments on commit 14327de

Please sign in to comment.