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

when I enter the first character. calling the getSelection() gets the index is 0 #4513

Open
ngd-b opened this issue Dec 3, 2024 · 8 comments

Comments

@ngd-b
Copy link

ngd-b commented Dec 3, 2024

create a base example for the quill. when I enter the first character. calling the getSelection() gets the index is 0

the code is simple such as:

editor = new Quill(root.value!, {
    modules: {
      toolbar:true
    },
   theme: "snow",
})

editor.on(
    "text-change",
    (delta: Delta, _oldContents: Delta, source: string) => {
       // call getSelection()
       console.log(editor!.getSelection())
})

But. when enter the second character. I can get it's index is 2.

However. when I delete the second character , I can get it's index is 1

This is confusing.

image

Steps for Reproduction

  1. Visit base example

add some code of the text-change event

const quill = new Quill('#editor', {
  modules: {
    toolbar: [
      [{ header: [1, 2, false] }],
      ['bold', 'italic', 'underline'],
      ['image', 'code-block'],
    ],
  },
  placeholder: 'Compose an epic...',
  theme: 'snow', // or 'bubble'
});
quill.on('text-change',()=>{
  console.log(quill.getSelection())
})

Expected behavior:

enter the first character. calling getSelection() is 1

Actual behavior:

now is 0. But when delete rest one character is 1

Platforms:

mac m2 . chrome 131

Version:

quill@2.0.2

@ngd-b
Copy link
Author

ngd-b commented Dec 11, 2024

Each line is incorrect after entering the first character to calling getSelection().

So. I'm dealing with this temporarily

  cursorIndex.value -= 1;
  // 第一行第一个字符光标位置 hack
  if (cursorIndex.value < 0) cursorIndex.value = 0;
  // 其他行第一个字符光标位置 hack
  if (editor!.getText(cursorIndex.value).startsWith("\n")) {
    cursorIndex.value += 1;
  }

@frantsyan
Copy link

+1
The same problem in 2.0.3

@shareefalis
Copy link

Use delta to determine the location of the character

@remyBerrebi-fi
Copy link

Use delta to determine the location of the character

What do you means "use delta" ?

@shareefalis
Copy link

you can actually derive the location from retain and insert's length

@remyBerrebi-fi
Copy link

Sorry i'm not sure to clearly understand as i'm not fully expert on quill. Do you have any example to share ?
@ngd-b i try to understand your hak but i don't see where to put all this :-(
thanks for your help, must appreciate !

@ngd-b
Copy link
Author

ngd-b commented Dec 31, 2024

Sorry i'm not sure to clearly understand as i'm not fully expert on quill. Do you have any example to share ? @ngd-b i try to understand your hak but i don't see where to put all this :-( thanks for your help, must appreciate !

@remyBerrebi-fi I'm sorry. I don't understand what you mean . Do you mean ask about my usage scenario? I customize the content block format. display the section when entering /. and replace / after selecting.

So if I get the wrong selection position. I can't accurately delete the previously entered /

@shareefalis
Copy link

shareefalis commented Dec 31, 2024

Try something like this

 (delta: Delta, _oldContents: Delta, source: string) => {
      //get the location of the current position based on delta
      console.log(delta.ops[0].retain ??0+(delta.ops[0].insert?.length??0))
}

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

No branches or pull requests

4 participants