github/text-expander-element
关于
Activates a suggestion menu to expand text snippets as you type.
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文<text-expander> element
Activates a suggestion menu to expand text snippets as you type.
Installation
$ npm install --save @github/text-expander-element
Usage
Script
Import as ES modules:
import '@github/text-expander-element'
With a script tag:
<script type="module" src="./node_modules/@github/text-expander-element/dist/bundle.js">
Markup
<text-expander keys=": @ #" multiword="#">
<textarea></textarea>
</text-expander>
Attributes
keysis a space separated list of menu activation keysmultiworddefines whether the expansion should use several words or not- you can provide a space separated list of activation keys that should support multi-word matching
suffixis a string that is appended to the value during expansion, default is a single space character
Events
text-expander-change is fired when a key is matched. In event.detail you can find:
key: The matched key; for example::.text: The matched text; for example:cat, for:cat.- If the
keyis specified in themultiwordattribute then the matched text can contain multiple words; for examplecat and dogfor:cat and dog.
- If the
provide: A function to be called when you have the menu results. Takes aPromisewith{matched: boolean, fragment: HTMLElement}wherematchedtells the element whether a suggestion is available, andfragmentis the menu content to be displayed on the page.
const expander = document.querySelector('text-expander')
expander.addEventListener('text-expander-change', function(event) {
const {key, provide, text} = event.detail
if (key !== ':') return
const suggestions = document.querySelector('.emoji-suggestions').cloneNode(true)
suggestions.hidden = false
for (const suggestion of suggestions.children) {
if (!suggestion.textContent.match(text)) {
suggestion.remove()
}
}
provide(Promise.resolve({matched: suggestions.childElementCount > 0, fragment: suggestions}))
})
The returned fragment should be consisted of filtered [role=option] items to be selected. For example:
<ul class="emoji-suggestions" hidden>
<li role="option" data-value="🐈">🐈 :cat2:</li>
<li role="option" data-value="🐕">🐕 :dog:</li>
</ul>
text-expander-value is fired when an item is selected. In event.detail you can find:
key: The matched key; for example::.item: The selected item. This would be one of the[role=option]. Use this to work out thevalue.value: A null value placeholder to replace the query. To replace the text query, simply re-assign this value.continue: A boolean value to specify whether to continue autocompletion after inserting a value. Defaults tofalse. If set totrue, will not add a space after inserted value and will keep firing thetext-expander-changeevent.
const expander = document.querySelector('text-expander')
expander.addEventListener('text-expander-value', function(event) {
const {key, item} = event.detail
if (key === ':') {
event.detail.value = item.getAttribute('data-value')
}
})
text-expander-committed is fired after the underlying input value has been updated in the DOM. In event.detail you can find:
input: TheHTMLInputElementorHTMLTextAreaElementthat just hadvaluechanges committed to the DOM.
const expander = document.querySelector('text-expander')
expander.addEventListener('text-expander-committed', function(event) {
const {input} = event.detail
})
text-expander-activate is fired just after the menu has been assigned and appended to the DOM, and just before it is about to be positioned near the text to expand. This is useful for assigning classes or calling imperative methods to show the menu, such as .showPopover().
text-expander-deactivate is fired just before the menu is going to be unassigned and removed from the DOM. This is useful for removing classes or running cleanup like removing from caches.
Browser support
Browsers without native custom element support require a polyfill.
- Chrome
- Firefox
- Safari
- Microsoft Edge
Development
npm install
npm test
License
Distributed under the MIT license. See LICENSE for details.
探索相关项目
加载中…