slab / delta

Simple and expressive JSON format for describing rich-text content and their changes
https://quilljs.com/docs/delta
BSD 3-Clause "New" or "Revised" License
914 stars 130 forks source link

How to find the effective length of a Delta #70

Closed AlanObject closed 3 years ago

AlanObject commented 3 years ago

I want to allow the user to paste the contents of one quill editor to another, and the pasted contents is block-quoted with a reference header. The code (typescript in angular) looks like this:

      const header = new Delta().insert('The following is a quote from xxxx\n', 'blockquote');
      const cursor = this.editor.getSelection();
      const start = cursor ? cursor.index : 0;
      const paste = wbe.editor.getContents(wbe.selection.index, wbe.selection.length);
      const delta = new Delta().retain(cursor.index).concat(header).concat(paste);
      this.editor.updateContents(delta, 'api');
      const length = delta.length();    // this is not the right value
      this.editor.setSelection(start, length);
      this.editor.format('blockquote', 'true');

The length() function doesn't return what I want, which is the number of inserted characters. Is there a function that does this or do I have to iterate through the delta to count them?