@github/text-expander-element
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -174,4 +174,8 @@ const ctrlBindings = !!navigator.userAgent.match(/Macintosh/); | ||
const boundary = /\s|\(|\[/; | ||
function query(text, key, cursor, { multiWord, lookBackIndex } = { multiWord: false, lookBackIndex: 0 }) { | ||
const keyIndex = text.lastIndexOf(key, cursor - 1); | ||
function query(text, key, cursor, { multiWord, lookBackIndex, lastMatchPosition } = { | ||
multiWord: false, | ||
lookBackIndex: 0, | ||
lastMatchPosition: null | ||
}) { | ||
let keyIndex = text.lastIndexOf(key, cursor - 1); | ||
if (keyIndex === -1) | ||
@@ -182,4 +186,9 @@ return; | ||
if (multiWord) { | ||
if (lastMatchPosition != null) { | ||
if (lastMatchPosition === keyIndex) | ||
return; | ||
keyIndex = lastMatchPosition - 1; | ||
} | ||
const charAfterKey = text[keyIndex + 1]; | ||
if (charAfterKey === ' ') | ||
if (charAfterKey === ' ' && cursor >= keyIndex + 2) | ||
return; | ||
@@ -401,2 +410,3 @@ const newLineIndex = text.lastIndexOf('\n', cursor - 1); | ||
this.lookBackIndex = cursor; | ||
this.match = null; | ||
} | ||
@@ -440,6 +450,10 @@ onBlur() { | ||
if (cursor <= this.lookBackIndex) { | ||
this.lookBackIndex = 0; | ||
this.lookBackIndex = cursor - 1; | ||
} | ||
for (const { key, multiWord } of this.expander.keys) { | ||
const found = query(text, key, cursor, { multiWord, lookBackIndex: this.lookBackIndex }); | ||
const found = query(text, key, cursor, { | ||
multiWord, | ||
lookBackIndex: this.lookBackIndex, | ||
lastMatchPosition: this.match ? this.match.position : null | ||
}); | ||
if (found) { | ||
@@ -465,2 +479,3 @@ return { text: found.text, key, position: found.position }; | ||
if (event.key === 'Escape') { | ||
this.match = null; | ||
if (this.deactivate()) { | ||
@@ -467,0 +482,0 @@ this.lookBackIndex = this.input.selectionEnd || this.lookBackIndex; |
import Combobox from '@github/combobox-nav'; | ||
const boundary = /\s|\(|\[/; | ||
function query(text, key, cursor, { multiWord, lookBackIndex } = { multiWord: false, lookBackIndex: 0 }) { | ||
const keyIndex = text.lastIndexOf(key, cursor - 1); | ||
function query(text, key, cursor, { multiWord, lookBackIndex, lastMatchPosition } = { | ||
multiWord: false, | ||
lookBackIndex: 0, | ||
lastMatchPosition: null | ||
}) { | ||
let keyIndex = text.lastIndexOf(key, cursor - 1); | ||
if (keyIndex === -1) | ||
@@ -11,4 +15,9 @@ return; | ||
if (multiWord) { | ||
if (lastMatchPosition != null) { | ||
if (lastMatchPosition === keyIndex) | ||
return; | ||
keyIndex = lastMatchPosition - 1; | ||
} | ||
const charAfterKey = text[keyIndex + 1]; | ||
if (charAfterKey === ' ') | ||
if (charAfterKey === ' ' && cursor >= keyIndex + 2) | ||
return; | ||
@@ -230,2 +239,3 @@ const newLineIndex = text.lastIndexOf('\n', cursor - 1); | ||
this.lookBackIndex = cursor; | ||
this.match = null; | ||
} | ||
@@ -269,6 +279,10 @@ onBlur() { | ||
if (cursor <= this.lookBackIndex) { | ||
this.lookBackIndex = 0; | ||
this.lookBackIndex = cursor - 1; | ||
} | ||
for (const { key, multiWord } of this.expander.keys) { | ||
const found = query(text, key, cursor, { multiWord, lookBackIndex: this.lookBackIndex }); | ||
const found = query(text, key, cursor, { | ||
multiWord, | ||
lookBackIndex: this.lookBackIndex, | ||
lastMatchPosition: this.match ? this.match.position : null | ||
}); | ||
if (found) { | ||
@@ -294,2 +308,3 @@ return { text: found.text, key, position: found.position }; | ||
if (event.key === 'Escape') { | ||
this.match = null; | ||
if (this.deactivate()) { | ||
@@ -296,0 +311,0 @@ this.lookBackIndex = this.input.selectionEnd || this.lookBackIndex; |
@@ -8,4 +8,5 @@ declare type Query = { | ||
multiWord: boolean; | ||
lastMatchPosition: number | null; | ||
}; | ||
export default function query(text: string, key: string, cursor: number, { multiWord, lookBackIndex }?: QueryOptions): Query | void; | ||
export default function query(text: string, key: string, cursor: number, { multiWord, lookBackIndex, lastMatchPosition }?: QueryOptions): Query | void; | ||
export {}; |
const boundary = /\s|\(|\[/; | ||
export default function query(text, key, cursor, { multiWord, lookBackIndex } = { multiWord: false, lookBackIndex: 0 }) { | ||
const keyIndex = text.lastIndexOf(key, cursor - 1); | ||
export default function query(text, key, cursor, { multiWord, lookBackIndex, lastMatchPosition } = { | ||
multiWord: false, | ||
lookBackIndex: 0, | ||
lastMatchPosition: null | ||
}) { | ||
let keyIndex = text.lastIndexOf(key, cursor - 1); | ||
if (keyIndex === -1) | ||
@@ -9,4 +13,9 @@ return; | ||
if (multiWord) { | ||
if (lastMatchPosition != null) { | ||
if (lastMatchPosition === keyIndex) | ||
return; | ||
keyIndex = lastMatchPosition - 1; | ||
} | ||
const charAfterKey = text[keyIndex + 1]; | ||
if (charAfterKey === ' ') | ||
if (charAfterKey === ' ' && cursor >= keyIndex + 2) | ||
return; | ||
@@ -13,0 +22,0 @@ const newLineIndex = text.lastIndexOf('\n', cursor - 1); |
@@ -91,2 +91,3 @@ import Combobox from '@github/combobox-nav'; | ||
this.lookBackIndex = cursor; | ||
this.match = null; | ||
} | ||
@@ -130,6 +131,10 @@ onBlur() { | ||
if (cursor <= this.lookBackIndex) { | ||
this.lookBackIndex = 0; | ||
this.lookBackIndex = cursor - 1; | ||
} | ||
for (const { key, multiWord } of this.expander.keys) { | ||
const found = query(text, key, cursor, { multiWord, lookBackIndex: this.lookBackIndex }); | ||
const found = query(text, key, cursor, { | ||
multiWord, | ||
lookBackIndex: this.lookBackIndex, | ||
lastMatchPosition: this.match ? this.match.position : null | ||
}); | ||
if (found) { | ||
@@ -155,2 +160,3 @@ return { text: found.text, key, position: found.position }; | ||
if (event.key === 'Escape') { | ||
this.match = null; | ||
if (this.deactivate()) { | ||
@@ -157,0 +163,0 @@ this.lookBackIndex = this.input.selectionEnd || this.lookBackIndex; |
{ | ||
"name": "@github/text-expander-element", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Activates a suggestion menu to expand text snippets as you type.", | ||
@@ -5,0 +5,0 @@ "repository": "github/text-expander-element", |
@@ -47,2 +47,3 @@ # <text-expander> element | ||
- `text`: The matched text; for example: `cat`, for `:cat`. | ||
- If the `key` is specified in the `multiword` attribute then the matched text can contain multiple words; for example `cat and dog` for `:cat and dog`. | ||
- `provide`: A function to be called when you have the menu results. Takes a `Promise` with `{matched: boolean, fragment: HTMLElement}` where `matched` tells the element whether a suggestion is available, and `fragment` is the menu content to be displayed on the page. | ||
@@ -49,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48217
1237
116