-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Expose autocomplete triggers and text insertion utility #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9b6ca18
feat: Observe `@` autocomplete triggers
dcalhoun df4ee9b
feat: Observe `+` autocomplete triggers
dcalhoun 141b778
feat: Notify native host on autocomplete trigger
dcalhoun 99f77d8
feat: Expose `appendTextAtCursor` bridge method
dcalhoun c0b8d77
fix: Avoid restarting autocompletion after selecting option
dcalhoun daaeb29
feat: iOS apps receive autocompletion events
dcalhoun 2c3e6bd
feat: iOS apps append text at cursor
dcalhoun a0e854e
fix: Ensure hook registration occurs in global `wp.hooks` namespace
dcalhoun 3201e11
feat: Android support for autocompleter events and text insertion
dcalhoun 9d995c0
fix: Only trigger autocompleter on initial trigger insertion
dcalhoun f94ca59
fix: Prevent autocompletion unless white space surrounds prefix trigger
dcalhoun 81dcf29
fix: Authenticate editor assets request for Android
dcalhoun 1868728
docs: Improve documentation
dcalhoun 63ba2d4
refactor: Rely upon shorthand property name
dcalhoun 07241a7
build: Simplify side-effect module externalization
dcalhoun 3b742b5
fix: Avoid loading duplicate CSS files
dcalhoun 64f6556
refactor: Reduce log noise
dcalhoun f5e0a7f
fix: Expand iOS string encoding to avoid stripping content
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter, removeFilter } from '@wordpress/hooks'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { onAutocompleterTriggered } from '../../utils/bridge'; | ||
|
||
/** | ||
* Adds a filter for the Autocomplete completers to show an alert when @ is typed. | ||
* | ||
* @return {void} | ||
*/ | ||
export function useAtAutocompleter() { | ||
useEffect( () => { | ||
// Avoid conflicts with core's default autocompleter | ||
removeFilter( | ||
'editor.Autocomplete.completers', | ||
'editor/autocompleters/set-default-completers' | ||
); | ||
|
||
addFilter( | ||
'editor.Autocomplete.completers', | ||
'GutenbergKit/at-symbol-alert', | ||
addAtSymbolCompleter | ||
); | ||
|
||
return () => { | ||
removeFilter( | ||
'editor.Autocomplete.completers', | ||
'GutenbergKit/at-symbol-alert' | ||
); | ||
}; | ||
}, [] ); | ||
} | ||
|
||
/** | ||
* Adds the @ symbol autocompleter to the completers array. | ||
* | ||
* @param {Array} completers Existing completers. | ||
* @return {Array} Updated completers array. | ||
*/ | ||
function addAtSymbolCompleter( completers = [] ) { | ||
const atSymbolCompleter = { | ||
name: 'at-symbol', | ||
triggerPrefix: '@', | ||
options: ( filterValue ) => { | ||
// Only trigger when cursor is directly after @ (no characters typed yet) | ||
if ( filterValue === '' ) { | ||
onAutocompleterTriggered( 'at-symbol' ); | ||
} | ||
// Return empty array since we're not providing actual completion options | ||
return []; | ||
}, | ||
allowContext: ( before, after ) => { | ||
const beforeEmptyOrWhitespace = /^$|\s$/.test( before ); | ||
const afterEmptyOrWhitespace = /^$|^\s/.test( after ); | ||
return beforeEmptyOrWhitespace && afterEmptyOrWhitespace; | ||
}, | ||
isDebounced: true, | ||
}; | ||
|
||
return [ ...completers, atSymbolCompleter ]; | ||
} |
This file contains hidden or 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 hidden or 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,61 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter, removeFilter } from '@wordpress/hooks'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { onAutocompleterTriggered } from '../../utils/bridge'; | ||
|
||
/** | ||
* Adds a filter for the Autocomplete completers to show an alert when + is typed. | ||
* | ||
* @return {void} | ||
*/ | ||
export function usePlusAutocompleter() { | ||
useEffect( () => { | ||
addFilter( | ||
'editor.Autocomplete.completers', | ||
'GutenbergKit/plus-symbol-alert', | ||
addPlusSymbolCompleter | ||
); | ||
|
||
return () => { | ||
removeFilter( | ||
'editor.Autocomplete.completers', | ||
'GutenbergKit/plus-symbol-alert' | ||
); | ||
}; | ||
}, [] ); | ||
} | ||
|
||
/** | ||
* Adds the + symbol autocompleter to the completers array. | ||
* | ||
* @param {Array} completers Existing completers. | ||
* @return {Array} Updated completers array. | ||
*/ | ||
function addPlusSymbolCompleter( completers = [] ) { | ||
const plusSymbolCompleter = { | ||
name: 'plus-symbol', | ||
triggerPrefix: '+', | ||
options: ( filterValue ) => { | ||
// Only trigger when cursor is directly after + (no characters typed yet) | ||
if ( filterValue === '' ) { | ||
onAutocompleterTriggered( 'plus-symbol' ); | ||
} | ||
// Return empty array since we're not providing actual completion options | ||
return []; | ||
}, | ||
allowContext: ( before, after ) => { | ||
const beforeEmptyOrWhitespace = /^$|\s$/.test( before ); | ||
const afterEmptyOrWhitespace = /^$|^\s/.test( after ); | ||
return beforeEmptyOrWhitespace && afterEmptyOrWhitespace; | ||
}, | ||
isDebounced: true, | ||
}; | ||
|
||
return [ ...completers, plusSymbolCompleter ]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.