At.js icon indicating copy to clipboard operation
At.js copied to clipboard

Unexpected behavior when typing '@' after a space AND just before a word

Open Fluffette opened this issue 9 years ago • 5 comments

Hey !

I am trying tu use At.js combined with Quill.js. Thanks to this topic, I managed to make insertion work as I wanted.

Here is my code. I voluntarily removed some parts so as to clarify, but you can still use it :

<!DOCTYPE html>
<html>
    <head>
        <link href="https://cdn.quilljs.com/latest/quill.snow.css" rel="stylesheet">
        <link href="https://cdn.bootcss.com/at.js/1.5.2/css/jquery.atwho.css" rel="stylesheet">
    </head>

    <body>
        <div id="editor"></div>
    </body>

    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="https://cdn.quilljs.com/latest/quill.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/caret/1.0.0/jquery.caret.min.js"></script>
    <script src="https://cdn.bootcss.com/at.js/1.5.2/js/jquery.atwho.min.js"></script>

    <script>

        // global variables usefull for atwho insertion
        // ...

        var quill = new Quill('#editor', {
            debug: 'warn',
            placeholder: 'Write something...',
            theme: 'snow'
        });

        $(quill.root).atwho({
            at: '@',
            data:['Peter', 'Tom', 'Anne'],
            functionOverrides: {
                insert: function (content, $li) {
                    // do some stuff using global variables and quill binding for <ENTER>
                    // so that insertion works well
                }
            }
        });

    </script>
</html>

As I said, the insertion is working. The problem is :

Everytime I type @ after a space AND just before a character (e.g. a word), the cursor moves directly to the end of the line, and the list of data appears and disappears instantly. After that, if I try to go back with the left arrow key, the cursor is automatically replaced at the end of the line.

For example, with the text : Hello world if I type @ just before the w, the cursor will move behind the d.

I don't understand this behavior. Is it a bug, or am I missing something ? Is there a way to avoid it and properly display the list of data ?

Beside, I tried to print the matched.atwho(event, flag, query) event, which is triggered twice during the process :

  • first time with flag = @ and query empty
  • second time with flag = @ and query = world

Thanks for your help !

Fluffette avatar Feb 02 '17 09:02 Fluffette

Removing the attribute [contenteditable=false] from .atwho-inserted after insert seems work.

cesiya23 avatar Apr 08 '17 05:04 cesiya23

I have this same issue, but I cannot find what is changing the cursor position :( and @cesiya23 your solution is not working in my case :(

nitaskenderi avatar Jul 17 '17 12:07 nitaskenderi

@veloce48 did you happen to find a solution?

nitaskenderi avatar Jul 17 '17 14:07 nitaskenderi

@nitaskenderi I guess it may lead by this line https://github.com/ichord/At.js/blob/master/dist/js/jquery.atwho.js#L623

Try to set config of suffix to ''

const config = {
  at: '@',
  suffix: ''
};

cesiya23 avatar Jul 18 '17 08:07 cesiya23

This is what I've done. If you are in a hurry and if you want to remove last inserted line.

const Parchment = Quill.import('parchment');
const Query = new Parchment.Attributor.Class('custom', 'atwho', {
    scope: Parchment.Scope.INLINE_BLOT,
    whitelist: ['query', 'inserted', 'last-at-js']
});
Quill.register(Query, true);

editor = new Quill('#editor', {});

$(editor.root).atwho({
    at: "@",
    displayTpl: '<li>${name}</li>',
    insertTpl: '<span class="atwho-query atwho-last-at-js">&nbsp;</span>${atwho-at}${username}',
    data: null,
    callbacks:{
        remoteFilter: function(query, callback) {
            $.ajax({
                'type': 'GET',
                'url': '/suggest-users',
                'data': 'keyword=' + query,
                dataType: 'json',
                success: function (json) {
                     callback(json);
                }
           });
        }
    }
}).on('inserted.atwho', function (e) {
    var ljs = $(e.currentTarget).find('.atwho-last-at-js');

    element = ljs.closest('p, blockquote, li, h1, h2, h3, h4, h5, h6');
    if(element.next().has('.ql-cursor')) {
        element.next().remove();
    }

    // remove helper
    ljs.remove();
});

electrize avatar Sep 12 '17 07:09 electrize