editable-text: Prevent dropping text into read-only field

This commit is contained in:
Abe Jellinek 2024-03-28 13:13:10 -04:00
parent 489cee6b24
commit f9e25675f0

View file

@ -260,24 +260,25 @@
input.addEventListener('dragover', (event) => {
// If the input is not focused, override the default drop behavior
if ((document.activeElement !== this._input || Services.focus.activeWindow !== window)
&& !this.readOnly
&& event.dataTransfer.getData('text/plain')) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
}
});
input.addEventListener('drop', (event) => {
let text = event.dataTransfer.getData('text/plain');
// If the input is not focused, replace its entire value with the dropped text
// Otherwise, the normal drop effect takes place and the text is inserted at the cursor
if ((document.activeElement !== this._input || Services.focus.activeWindow !== window)
&& text) {
&& !this.readOnly
&& event.dataTransfer.getData('text/plain')) {
event.preventDefault();
document.activeElement?.blur();
// Wait a tick to work around an apparent Firefox bug where the cursor stays inside the old
// input even though the new input becomes visually focused
setTimeout(() => {
this.focus();
this._input.value = text;
this._input.value = event.dataTransfer.getData('text/plain');
handleInput();
});
}