Skip to content

fix match validation to prevent stale matches #78

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 1 commit into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,51 @@ <h2>Multiword text-expander element</h2>
<textarea autofocus rows="10" cols="40"></textarea>
</text-expander>

<h2>Multiword and multikey text-expander element</h2>
<text-expander keys=": #" multiword="#">
<textarea autofocus rows="10" cols="40"></textarea>
</text-expander>

<h2>Multiword and multikey text-expander element with random delay</h2>
<text-expander keys=": #" multiword="#" _random_delay="">
<textarea autofocus rows="10" cols="40"></textarea>
</text-expander>

<script type="text/javascript">
const emojis = [
{ emoji: "😀", names: ["smile", "happy"] },
{ emoji: "❤️", names: ["heart", "love"] },
{ emoji: "🔥", names: ["fire", "hot"] },
{ emoji: "⭐", names: ["star", "favorite"] },
{ emoji: "🚀", names: ["rocket", "fast"] }
];

const expanders = document.querySelectorAll('text-expander')
for (const expander of expanders) {
expander.addEventListener('text-expander-change', event => {
const {key, provide, text} = event.detail
if (key === '#') {
if (key === ':') {
const menu = document.createElement('ul')
menu.classList.add('menu')
menu.role = 'listbox'
for (const {emoji, names} of emojis) {
if (names.some(name => name.includes(text.toLowerCase()))) {
const item = document.createElement('li')
item.setAttribute('role', 'option')
item.textContent = `${emoji} ${names[0]}`
item.setAttribute('data-value', emoji)
menu.append(item)
}
}
// Async test with random delay
if (expander.hasAttribute('_random_delay')) {
provide(new Promise(resolve => {
setTimeout(() => resolve({matched: true, fragment: menu}), Math.random() * 1000)
}))
} else {
provide(Promise.resolve({matched: true, fragment: menu}))
}
} else if (key === '#') {
const menu = document.createElement('ul')
menu.classList.add('menu')
menu.role = 'listbox'
Expand All @@ -55,13 +94,25 @@ <h2>Multiword text-expander element</h2>
menu.append(item)
}
}
provide(Promise.resolve({matched: true, fragment: menu}))
// Async test with random delay
if (expander.hasAttribute('_random_delay')) {
provide(new Promise(resolve => {
setTimeout(() => resolve({matched: true, fragment: menu}), Math.random() * 1000)
}))
} else {
// For normal expander - synchronous response
provide(Promise.resolve({matched: true, fragment: menu}))
}
}
})

expander.addEventListener('text-expander-value', function(event) {
const {key, item} = event.detail
if (key === '#') event.detail.value = item.getAttribute('data-value') || item.textContent
if (key === '#') {
event.detail.value = item.getAttribute('data-value') || item.textContent
} else if (key === ':') {
event.detail.value = item.getAttribute('data-value')
}
})
}
</script>
Expand Down
10 changes: 9 additions & 1 deletion src/text-expander-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class TextExpander {
this.justPasted = true
}

private isMatchStillValid(match: TextExpanderMatch): boolean {
return match.position <= this.input.value.length
}

async onInput() {
if (this.justPasted) {
this.justPasted = false
Expand All @@ -213,7 +217,11 @@ class TextExpander {
const menu = await this.notifyProviders(match)

// Text was cleared while waiting on async providers.
if (!this.match) return
if (!this.match || !this.isMatchStillValid(match)) {
this.match = null
this.deactivate()
return
}

if (menu) {
this.activate(match, menu)
Expand Down