Skip to content

Commit

Permalink
adds a link completer for inline link to post creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Draganescu committed Feb 19, 2021
1 parent 30b2c14 commit d6c47ed
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@babel/runtime": "^7.12.5",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/blob": "file:../blob",
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
Expand Down
49 changes: 49 additions & 0 deletions packages/block-editor/src/autocompleters/link.js
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();
8 changes: 8 additions & 0 deletions packages/block-editor/src/autocompleters/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
margin-right: $grid-unit-10;
}
}

.block-editor-autocompleters__link {
white-space: nowrap;

.block-editor-block-icon {
margin-right: $grid-unit-10;
}
}
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getDefaultBlockName } from '@wordpress/blocks';
*/
import { useBlockEditContext } from '../block-edit/context';
import blockAutocompleter from '../../autocompleters/block';
import linkAutocompleter from '../../autocompleters/link';

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand All @@ -42,6 +43,7 @@ function BlockEditorAutocomplete( props ) {
if ( name === getDefaultBlockName() ) {
filteredCompleters = filteredCompleters.concat( [
blockAutocompleter,
linkAutocompleter,
] );
}

Expand Down

0 comments on commit d6c47ed

Please sign in to comment.