-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a link completer for inline links to posts (#29172)
* adds a link completer for inline link to post creation * adds icons and removes blank title items * updated icon for post and added shortcut to modal * update snapshot and place the shortcut near other link shortcuts Co-authored-by: Andrei Draganescu <github@andreidraganescu.info>
- Loading branch information
1 parent
b4ed630
commit 6933f93
Showing
5 changed files
with
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { Icon, page, post } from '@wordpress/icons'; | ||
|
||
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: async ( letters ) => { | ||
let options = await apiFetch( { | ||
path: addQueryArgs( '/wp/v2/search', { | ||
per_page: SHOWN_SUGGESTIONS, | ||
search: letters, | ||
type: 'post', | ||
order_by: 'menu_order', | ||
} ), | ||
} ); | ||
|
||
options = options.filter( ( option ) => option.title !== '' ); | ||
|
||
return options; | ||
}, | ||
getOptionKeywords( item ) { | ||
const expansionWords = item.title.split( /\s+/ ); | ||
return [ ...expansionWords ]; | ||
}, | ||
getOptionLabel( item ) { | ||
return ( | ||
<> | ||
<Icon | ||
key="icon" | ||
icon={ item.subtype === 'page' ? page : post } | ||
/> | ||
{ 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
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