Pixabay / jQuery-tagEditor

A powerful and lightweight tag editor plugin for jQuery.
501 stars 164 forks source link

Delimiters \n and \t do not work on paste #171

Open MrTomRod opened 3 years ago

MrTomRod commented 3 years ago

Hey!

I want my users to be able to paste entries into the TagEditor. My main delimiter is comma ,, but for technical reasons, some of my users have newlines \n or tabs \t as delimiters. So I tried this:

$(div_name).tagEditor({
    ...
    delimiter: ',\n\t\r'
})

Unfortunately, it does not work. Pasting 1\n2\n3 results in one tag only (1 2 3) and not the expected three tags (1, 2, 3).

I also tried to overwrite the paste event:

$('.tag-editor').bind('paste', (event) => {
    // get pasted data
    let paste = (event.originalEvent.clipboardData || window.clipboardData).getData('text');

    // turn all delimiters into commas
    paste = paste.replaceAll('\n', ',').replaceAll('\t', ',')

    // split into array
    // trim: remove white space, filter: remove empty strings from array
    paste = paste.split(',').map(s => s.trim()).filter(n => n)

    // add each element to tagEditor
    paste.forEach(element => $('#my-tag-editor').tagEditor('addTag', element))

    // prevent default paste event
    event.preventDefault();
})

But this code adds only one additional tag (1\n2\n3 -> 1). Probably a timeout problem?

Can you help me?

MrTomRod commented 3 years ago

I created a workaround in a fork of this repo: https://github.com/MrTomRod/jQuery-tagEditor

It's ugly, but it works for me.

Click here for the difference.