-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a link completer for inline link to post creation
- Loading branch information
Andrei Draganescu
committed
Feb 19, 2021
1 parent
30b2c14
commit d6c47ed
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
const SHOWN_SUGGESTIONS = 10; | ||
|
||
/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */ | ||
|
||
/** | ||
* Creates a suggestion list for links to posts or pages. | ||
* | ||
* @return {WPCompleter} A links completer. | ||
*/ | ||
function createLinkCompleter() { | ||
return { | ||
name: 'links', | ||
className: 'block-editor-autocompleters__link', | ||
triggerPrefix: '[[', | ||
options( letters ) { | ||
return apiFetch( { | ||
path: addQueryArgs( '/wp/v2/search', { | ||
per_page: SHOWN_SUGGESTIONS, | ||
search: letters, | ||
type: 'post', | ||
order_by: 'menu_order', | ||
} ), | ||
} ); | ||
}, | ||
getOptionKeywords( item ) { | ||
const expansionWords = item.title.split( /\s+/ ); | ||
return [ ...expansionWords ]; | ||
}, | ||
getOptionLabel( item ) { | ||
return item.title; | ||
}, | ||
getOptionCompletion( item ) { | ||
return <a href={ item.url }>{ item.title }</a>; | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a suggestion list for links to posts or pages.. | ||
* | ||
* @return {WPCompleter} A link completer. | ||
*/ | ||
export default createLinkCompleter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters