fix firefox and selectionchange issues

This commit is contained in:
LenAnderson 2024-06-20 08:54:37 -04:00
parent 377f3892f7
commit 6ff1d6a9b0

View File

@ -102,10 +102,15 @@ export class AutoComplete {
this.updateDetailsPositionDebounced = debounce(this.updateDetailsPosition.bind(this), 10); this.updateDetailsPositionDebounced = debounce(this.updateDetailsPosition.bind(this), 10);
this.updateFloatingPositionDebounced = debounce(this.updateFloatingPosition.bind(this), 10); this.updateFloatingPositionDebounced = debounce(this.updateFloatingPosition.bind(this), 10);
textarea.addEventListener('input', ()=>this.text != this.textarea.value && this.show(true, this.wasForced)); textarea.addEventListener('input', ()=>{
this.selectionStart = this.textarea.selectionStart;
if (this.text != this.textarea.value) this.show(true, this.wasForced);
});
textarea.addEventListener('keydown', (evt)=>this.handleKeyDown(evt)); textarea.addEventListener('keydown', (evt)=>this.handleKeyDown(evt));
textarea.addEventListener('click', ()=>this.isActive ? this.show() : null); textarea.addEventListener('click', ()=>{
textarea.addEventListener('selectionchange', ()=>this.show()); this.selectionStart = this.textarea.selectionStart;
if (this.isActive) this.show();
});
textarea.addEventListener('blur', ()=>this.hide()); textarea.addEventListener('blur', ()=>this.hide());
if (isFloating) { if (isFloating) {
textarea.addEventListener('scroll', ()=>this.updateFloatingPositionDebounced()); textarea.addEventListener('scroll', ()=>this.updateFloatingPositionDebounced());
@ -768,30 +773,16 @@ export class AutoComplete {
// ignore keydown on modifier keys // ignore keydown on modifier keys
return; return;
} }
switch (evt.key) { // await keyup to see if cursor position or text has changed
case 'ArrowUp': const oldText = this.textarea.value;
case 'ArrowDown': await new Promise(resolve=>{
case 'ArrowRight': window.addEventListener('keyup', resolve, { once:true });
case 'ArrowLeft': { });
if (this.isActive) { if (this.selectionStart != this.textarea.selectionStart) {
// keyboard navigation, wait for keyup to complete cursor move this.selectionStart = this.textarea.selectionStart;
const oldText = this.textarea.value; this.show(this.isReplaceable || oldText != this.textarea.value);
await new Promise(resolve=>{ } else if (this.isActive) {
window.addEventListener('keyup', resolve, { once:true }); this.text != this.textarea.value && this.show(this.isReplaceable);
});
if (this.selectionStart != this.textarea.selectionStart) {
this.selectionStart = this.textarea.selectionStart;
this.show(this.isReplaceable || oldText != this.textarea.value);
}
}
break;
}
default: {
if (this.isActive) {
this.text != this.textarea.value && this.show(this.isReplaceable);
}
break;
}
} }
} }
} }