Get delta at cursor (no selection) without splitting it

1.6k views Asked by At

Here's a delta with an attribute:

Trying to get the delta with

editor.getContents(range.index, range.length);

returns

  Delta: {
    ops: []
  }

which is expected - range.length is 0.

Is there a way of returning the entire delta (from left to right) so it looks like this:

Delta: {
   ops: [
    {
      attributes: { test: '123' },
      insert: 'A selection'
    },
    ...
  ]
}
1

There are 1 answers

1
jhchen On BEST ANSWER

Assuming a slightly more complex example to disambiguate and assuming the test 123 attribute is implemented with a class Attributor, given the document:

<div class="ql-editor">
  <p><strong>ab</strong><span class="ql-test=123">cd<em>ef</em></span></p>
</div>

I think what you are asking however is getting the Delta for the "cdef" text when the user's cursor is between the "e" and "f" and so your range is index: 5.

This is an experimental/undocumented API but quill.scroll.path(5) will get you an array [[blockBlot, 0], [inlineBlot, 2], [italicBlot, 1]] and the blot you want in this case is the second one so by summing the offsets to it you will have 2 (0 + 2) and then you can call quill.getContents(2, blot.length()).

If the class is unique (or you can access the DOM node some other way) you can also do:

const Parchment = Quill.import("parchment");
let node = document.querySelector('.ql-test-123');
let blot = Parchment.find(node);
let offset = quill.scroll.offset(blot);

quill.getContents(offset, blot.length());