To implement autocomplete, follow these three steps:
- Create an editor object: Encapsulate an HTML element (like a
<textarea>) using the Textarea editor. - Initialize Textcomplete: Create a new
Textcomplete instance by passing the editor and an optional Textcomplete~Options object. - Register strategies: Provide an array of
Strategy~Properties to define how text is matched, searched, and replaced.
Note: You can pass an index property to a strategy to specify which match group from the match regex should be used as the query. The default value is 2.
// 1. Create the editor
var textareaElement = document.getElementById('your-textarea-element')
var editor = new Textarea(textareaElement);
// 2. Initialize Textcomplete
var textcomplete = new Textcomplete(editor, {
dropdown: {
maxCount: Infinity
}
});
// 3. Register strategies
textcomplete.register([{
// Emoji strategy
match: /(^|\s):(\w+)$/,
search: function (term, callback) {
callback(emojies.filter(emoji => { return emoji.startsWith(term); }));
},
replace: function (value) {
return '$1:' + value + ': ';
}
}]);