tagify icon indicating copy to clipboard operation
tagify copied to clipboard

Issue with injectAtCaret - Adding outside the textarea (Mixed mode)

Open raikow opened this issue 6 years ago • 5 comments

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:

  1. Click in the textarea
  2. Add a tag with the button (It will be displayed in the area => OK)
  3. Now click on the "-----" of the <p> tag
  4. Click the button again => The tag will be added within the <p> tag instead of the textarea

RESULT image

raikow avatar Jul 01 '20 14:07 raikow

@yairEO Has that been fixed?

raikow avatar Jul 22 '20 07:07 raikow

I made the test on the new version and still the same issue

raikow avatar Jul 22 '20 12:07 raikow

Still the same issue, happens only in Firefox

popoulos avatar Oct 05 '21 14:10 popoulos

I think this is caused by this issue in Firefox... opened 21 years ago :) https://bugzilla.mozilla.org/show_bug.cgi?id=85686

popoulos avatar Oct 05 '21 15:10 popoulos

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:

mexomagno avatar Aug 23 '22 22:08 mexomagno