Skip to content

Commit

Permalink
Update to handle mailto and tel protocols and internal links
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Oct 23, 2019
1 parent d0a348b commit da212f0
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isFunction, noop } from 'lodash';
import { isFunction, noop, startsWith } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -19,7 +19,13 @@ import {
useState,
} from '@wordpress/element';

import { safeDecodeURI, filterURLForDisplay, isURL, prependHTTP } from '@wordpress/url';
import {
safeDecodeURI,
filterURLForDisplay,
isURL,
prependHTTP,
getProtocol,
} from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -61,19 +67,35 @@ function LinkControl( { currentLink, fetchSearchSuggestions, onLinkChange, onSet
setIsEditingLink( true );
};

const handleURLSearch = async ( value ) => {
const handleDirectEntry = async ( value ) => {
let type = 'URL';

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

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

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

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

return [ {
id: '1',
title: value,
type: 'URL',
url: prependHTTP( value ),
url: type === 'URL' ? prependHTTP( value ) : value,
type,
} ];
};

const handleEntitySearch = async ( value ) => {
const results = await Promise.all( [
fetchSearchSuggestions( value ),
handleURLSearch( value ),
handleDirectEntry( value ),
] );

const couldBeURL = ! value.includes( ' ' );
Expand All @@ -86,8 +108,15 @@ function LinkControl( { currentLink, fetchSearchSuggestions, onLinkChange, onSet

// Effects
const getSearchHandler = useCallback( ( value ) => {
return ( isURL( value ) || value.includes( 'www.' ) ) ? handleURLSearch( value ) : handleEntitySearch( value );
}, [ handleURLSearch, fetchSearchSuggestions ] );
const protocol = getProtocol( value ) || '';
const isMailto = protocol.includes( 'mailto' );
const isInternal = startsWith( value, '#' );
const isTel = protocol.includes( 'tel' );

const handleManualEntry = isInternal || isMailto || isTel || isURL( value ) || value.includes( 'www.' );

return ( handleManualEntry ) ? handleDirectEntry( value ) : handleEntitySearch( value );
}, [ handleDirectEntry, fetchSearchSuggestions ] );

// Render Components
const renderSearchResults = ( { suggestionsListProps, buildSuggestionItemProps, suggestions, selectedSuggestion, isLoading } ) => {
Expand Down

0 comments on commit da212f0

Please sign in to comment.