Skip to content

Error is thrown if the first paragraph has no text but has attributes #2

@slak44

Description

@slak44

Basically, for a Delta like this:

{
  "ops": [
    {
      "attributes": {
        "header": 2
      },
      "insert": "\n"
    },
    {
      "insert": "a\n"
    }
  ]
}

I get TypeError: Cannot set properties of undefined (setting 'attributes'), at this line in the insertNewline function:

parsed.paragraphs[parsed.paragraphs.length-1].attributes = op.attributes;

My workaround is to do this, which is fine for my use case:

        const raw = { ...(delta as RawQuillDelta) };
        if (raw.ops?.length > 0) {
          raw.ops = [...raw.ops];
          const first = raw.ops[0];
          if (first.insert === '\n' && first.attributes) {
            raw.ops.unshift({ insert: '\n' });
          }
        }
        const parsed = parseQuillDelta(raw);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions