Issue with injectAtCaret - Adding outside the textarea (Mixed mode)
Hi,
I found an issue that is linked to the injectAtCaret function.
I took that example from another issue report https://jsbin.com/figukur/edit?html,css,js,output Then
- I edit the script import (to match the latest tagify version) 3.12.0
- I also add
<p>------------</p>after the textarea
Now:
- Click in the textarea
- Add a tag with the button (It will be displayed in the area => OK)
- Now click on the "-----" of the
<p>tag - Click the button again => The tag will be added within the
<p>tag instead of the textarea
RESULT

@yairEO Has that been fixed?
I made the test on the new version and still the same issue
Still the same issue, happens only in Firefox
I think this is caused by this issue in Firefox... opened 21 years ago :) https://bugzilla.mozilla.org/show_bug.cgi?id=85686
Looks like firefox doesn't restore our input selection when calling focus() on it. It only does it if manually clicking on it or navigating to it with the tab key.
I just spent a good couple of hours on this and finally resorted to monkeypatching the setStateSelection() method (which is the only place where state.selection gets changed).
Before updating the state selection I ask if the anchor node actually belongs to our tagify instance, and if it doesn't I just return the selection currently stored in state.
let myTagify = new Tagify($('#my-tagify')).get(0), {
mode: 'mix',
...options,
})
myTagify.setStateSelection = function(){
var selection = window.getSelection()
// save last selection place to be able to inject anything from outside to that specific place
const parent = selection.anchorNode?.parentElement
if (![this.DOM.input, this.DOM.input.parentElement].includes(parent)) {
return this.state.selection
}
var newSel = {
anchorOffset: selection.anchorOffset,
anchorNode : selection.anchorNode,
range : selection.getRangeAt && selection.rangeCount && selection.getRangeAt(0)
}
this.state.selection = newSel
return newSel
}
It's not perfect but at least it only inserts inside the input again, every single time. Using "caret navigation" in firefox (F7 key) helped me debug a bit.
I hope it helps someone :sparkles: