Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom button insertText() resets cursor position #731

Closed
KameSama opened this issue Jun 7, 2016 · 5 comments
Closed

Custom button insertText() resets cursor position #731

KameSama opened this issue Jun 7, 2016 · 5 comments

Comments

@KameSama
Copy link

KameSama commented Jun 7, 2016

I'm trying to insert a custom button to add scientific symbols to quilljs ( v1.0.0-beta.4) and when the insertion of the symbol happens in the "middle" of a line, it resets the cursor to the beginning of the line. The problem does not occur when the cursor is at the end of the line.

Steps for Reproduction
I reproduced it on the http://beta.quilljs.com/playground/

  1. Add this code to the javascript
    $('.add-micro').on("click",function(){ var range = quill.getSelection(!quill.hasFocus()); quill.insertText(range.index,$(this).val(),'','',null,'api'); });
  2. Add this HTML to add the toolbar button
    <span class="ql-formats"> <button class="add-micro" value="&#181;" >&#181;</button> </span>
  3. Press the "µ" button a few times, then add some whatever text, move the cursor to the text then click the button again.

Expected behavior:
The cursor should increment its position by the size of the inserted text
Actual behavior:
The cursor moves back to the first position of the line
Platforms:
Chrome 51

@benbro
Copy link
Contributor

benbro commented Jun 8, 2016

In a collaborative editor the cursor should stay at the same place when the delta adds content after the cursor.

@pietrop
Copy link

pietrop commented Sep 1, 2017

Having the same problem,

var length = quill.getLength() -1;
quill.insertText(length, a_variable_with_some_test_here );

when I insert text at the end, I need to do -1 to avoid it ending on a new line, but once the text is added the cursor/caret position jumps to top of the document.

Any ideas on how to figure this out?

@jhchen
Copy link
Member

jhchen commented Sep 4, 2017

setTimeout(function() { quill.insertText('Hello', quill.getLength() - 1) }, 5000); and then placing the cursor anywhere in the quilljs.com editor before the 5000ms is working for me. If you are having issues please file a separate Github Issue and fill out all the details.

@pietrop
Copy link

pietrop commented Sep 4, 2017

As specified in the API for insertText, the parameters are first the position and then the text. This is more likely to work:

setTimeout(function() {
    quill.insertText( quill.getLength() - 1, 'Hello')  
}, 5000);

In any case, I've tried it and it seems ok, going to do some more tests to see if I need to raise a separate issue.

Thanks!

@jhchen
Copy link
Member

jhchen commented Sep 4, 2017

Yes that was a typo on my part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants