### Install Textcomplete Core
Source: https://yuku.takahashi.coffee/textcomplete
Install the core Textcomplete library using npm.
```bash
npm install --save @textcomplete/core
```
--------------------------------
### Install Textarea Editor Package
Source: https://yuku.takahashi.coffee/textcomplete
Install the specific editor package for HTML textarea elements.
```bash
npm install --save @textcomplete/textarea
```
--------------------------------
### GitHub-style Emoji Strategy Example
Source: https://yuku.takahashi.coffee/textcomplete
A sample strategy for autocompleting GitHub-style emoji notation. It includes optional context checking, a regular expression for matching, and defines how to search for candidates and replace the matched text.
```javascript
// This is a sample strategy that autocompletes GitHub-style emoji notation.
// This document page is using almost the same strategy for demo.
{
// (Optional) Identifier of the strategy. Will be appear on data-strategy
// attribute of a dropdown element.
id: "mention",
// (Optional) This function is called on every change before matching. The
// first argument is the string from head to cursor. If it returns `false`,
// following matching phase isn't started.
context: (beforeCursor: string) =>
// Return false if the cursor is in code block or inline code notation
// to stop executing the matching phase.
!isInClode(beforeCursor),
// (Required) On every change, the string from head to cursor tests with the
// RegExp. If it matches, the captured substring will be passed to the search
// parameter's first argument.
// See also "index" parameter.
match: /\B:([\-+\w]*)$/,
// (Optional) Specify the index of target capture group. Default to 1.
index: 1,
// (Required) When the current input matches the "match" regexp above, this
// function is called. The first argument is the captured substring.
// You can callback only once for each search.
search: async (
term: string,
callback: (results: ResultType[]) => void,
match: RegExpMatchArray
) => {
callback(await gatherCandidates(term))
},
// (Optional) Whether the search results are cached. Default false.
cache: false,
// (Optional) Specify how to render each search result on the dropdown UI.
// The argument is an element of the search results callbacked in the search
// phase.
template: ([key, url]) =>
` ${key}`,
// (Required) Specify how to update the editor value. The whole substring
// matched in the match phase will be replaced by the returned value.
// Note that it can return a string or an array of two strings. If it returns
// an array, the matched substring will be replaced by the concatenated string
// and the cursor will be set between first and second strings.
replace: (result: ResultType): string => `:${result[0]}: `
}
```
--------------------------------
### Initialize Textcomplete with Editor
Source: https://yuku.takahashi.coffee/textcomplete
Initialize Textcomplete by creating a Textcomplete object with an editor instance and a strategy. Remember to destroy the instance when finished.
```javascript
const { Textcomplete } = require("@textcomplete/core")
const { TextareaEditor } = require("@textcomplete/textarea")
// How to construct the editor object depends on the actual editor class.
// Please read the document of the editor you choose.
const editor = new TextareaEditor(myTextareaElement)
// Start autocompleting
// The strategy and option are described below.
const textcomplete = new Textcomplete(editor, [strategy], option)
// When you finish using it.
// This command also destroys the editor object.
textcomplete.destroy()
```
--------------------------------
### Initialize Textcomplete with CodeMirror Editor
Source: https://yuku.takahashi.coffee/textcomplete
This JavaScript snippet shows how to integrate Textcomplete with CodeMirror. It requires both the 'codemirror' library and the '@textcomplete/codemirror' package. Ensure 'myElement' is a valid CodeMirror instance.
```javascript
const CodeMirror = require("codemirror")
const { CodeMirrorEditor } = require("@textcomplete/codemirror")
const cm = CodeMirror(myElement)
const editor = new CodeMirrorEditor(cm)
const textcomplete = new Textcomplete(editor, strategies, option)
```
--------------------------------
### Default Textcomplete Option Configuration
Source: https://yuku.takahashi.coffee/textcomplete
Configure the appearance and behavior of the dropdown UI used for autocompletion. This includes settings for class names, maximum item count, placement, and rendering of header/footer elements.
```javascript
// Default option. All properties are optional recursively.
{
// Configure a dropdown UI.
dropdown: {
// Class attribute of the dropdown element.
className: "dropdown-menu textcomplete-dropdown",
// The maximum number of items to be rendered.
maxCount: 10,
// Placement of the dropdown. "auto", "top" or "bottom".
placement: "auto",
// Return header and footer elements' content
header: (results: ResultType[]) => "",
footer: (results: ResultType[]) => "",
// Whether activate the opposite side item on pressing up or
// down key when an edge item is active.
rotate: false,
// Configure CSS style of the dropdown element.
style: { display: "none", position: "absolute", zIndex: "1000" },
// The parent node of the dropdown element.
parent: document.body,
item: {
// Class attribute of the each dropdown item element.
className: "textcomplete-item",
// Active item's class attribute.
activeClassName: "textcomplete-item active",
}
}
}
```
--------------------------------
### Initialize Textcomplete with Contenteditable Editor
Source: https://yuku.takahashi.coffee/textcomplete
This JavaScript snippet demonstrates initializing Textcomplete with the ContenteditableEditor for a contenteditable element. Note that this package is experimental and may not trigger automatically in all scenarios.
```javascript
const { ContenteditableEditor } = require("@textcomplete/contenteditable")
const editor = new ContenteditableEditor(myContenteditableElement)
const textcomplete = new Textcomplete(editor, strategies, option)
```
--------------------------------
### Initialize Textcomplete with Textarea Editor
Source: https://yuku.takahashi.coffee/textcomplete
This JavaScript snippet shows how to initialize Textcomplete using the TextareaEditor for a standard HTML textarea element. Ensure you have a textarea element with the ID 'myTextareaElement' and define your strategies and options.
```javascript
const { TextareaEditor } = require("@textcomplete/textarea")
const editor = new TextareaEditor(myTextareaElement)
const textcomplete = new Textcomplete(editor, strategies, option)
```
--------------------------------
### Textcomplete Dropdown UI Structure
Source: https://yuku.takahashi.coffee/textcomplete
This HTML structure defines the basic markup for a Textcomplete dropdown menu, including header, active item, search result, and footer elements.
```html