@automattic/editor-autocompleter
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

Editor Autocompleter

Autocompleters for the Gutenberg editor. Provides:

  • User completor (@name)
  • Tag completer (#tag)
  • Cross-post completer (+xpost)

The completers require Gutenberg to function but are independant of WordPress. A data source (local or remote) needs to be supplied.

Matches are highlighted within the autocompleter.

Usage

npm install @automattic/isolated-block-editor" --save

Then include in your code with something like this:

import { tagCompleter, xpostCompleter, userCompleter } from '@automattic/editor-autocompleter';
import '@automattic/editor-autocompleter/dist/style.scss';
import apiFetch from '@wordpress/api-fetch';

function tagFetcher( search: string ) {
	return apiFetch( { path: '/api/tags?s=' + encodeURIComponent( search ) } );
}

function userFetcher( search: string ) {
	return apiFetch( { path: '/api/user?s=' + encodeURIComponent( search ) } );
}

function xpostFetcher( search: string ) {
	return apiFetch( { path: '/api/xpost?s=' + encodeURIComponent( search ) } );
}

function addCompleters( completers = [] ) {
	// Add tag completer
	completers.push( tagCompleter( tagFetcher, true ) );

	// Add xpost completer
	completers.push( xpostCompleter( xpostFetcher ) );

	// Override the standard user completer with a custom one
	return completers
		.filter( ( filter ) => filter.name !== 'users' )
		.concat( [ userCompleter( useUserFetcher ) ] );
}

addFilter(
	'editor.Autocomplete.completers',
	'my-program/autocompleters',
	addCompleters
);

Each autocompleter takes a 'fetcher' and an optional isRemote flag. The fetcher is a function that returns either a promise (i.e. from a remote API call), or a plain array of values.

If the isRemote flag is set to true then a loading indicator will be shown if the returned data is not immediate.

Releasing

To make a release, ensure you are on the trunk branch. Do not update the version number in package.json - the release process will do this for you. Then run:

GITHUB_TOKEN=<TOKEN> yarn dist

Readme

Keywords

none

Package Sidebar

Install

npm i @automattic/editor-autocompleter

Weekly Downloads

4

Version

1.4.0

License

GPL-2.0-or-later

Unpacked Size

177 kB

Total Files

89

Last publish

Collaborators

  • porada
  • imranh920
  • gmjuhasz
  • rcarvalho
  • briowill
  • bgrgicak
  • dhenridev
  • daledupreez-a8c
  • jeherve
  • yuliyan
  • micbosia8c
  • jeremy.yip
  • wpvip-bot
  • etobiesen
  • kzoschke
  • brunobasto
  • kat3samsin
  • fmfernandes
  • newspack
  • robertsreberski_a8c
  • msurdi-a8c
  • chihsuan
  • manzoorwanijk
  • rjchow
  • andrea-sdl
  • scjr
  • spsiddarthan
  • t2dw4t
  • ehg_
  • wwa
  • sirreal
  • elazzabi
  • royho
  • luismulinari
  • macbre
  • mjangda
  • matticbot
  • a8c
  • blowery
  • noahtallen
  • hanifn
  • sgomes
  • tyxla
  • saroshaga
  • parkcityj
  • nejclovrencic
  • sirbrillig
  • chriszarate
  • robersongomes
  • johngodley