singerdmx / flutter-quill

Rich text editor for Flutter
https://pub.dev/packages/flutter_quill
MIT License
2.52k stars 808 forks source link

[Desktop] Loading delta to controller throws exception #1118

Closed mmasdivins closed 5 days ago

mmasdivins commented 1 year ago

Hello, I was trying to load a delta to the document. The delta is generated using node-quill-converter that converts an HTML to delta. I have two problems loading the delta generated. The first one: Failed assertion: line 357 pos 12: '(doc.last.data as String).endsWith('\n')': is not true.

It expects that the last operation of the delta is '\n' if I add it, it works fine, but I don't know why it have to end with a '\n' to work.

The second error: Failed assertion: line 30 pos 12: 'value.isInline || value.isIgnored || value.isEmpty': Unable to apply Style to leaf: {Attribute{key: list, scope: AttributeScope.BLOCK, value: ordered}, Attribute{key: color, scope: AttributeScope.INLINE, value: #800080}} On leaf.dart if I comment the lines on the applyStyle function:

  @override
  void applyStyle(Style value) {
    // assert(value.isInline || value.isIgnored || value.isEmpty,
    //     'Unable to apply Style to leaf: $value');
    super.applyStyle(value);
  }

Works fine and the document is loaded without a problem. It throws an error because the attribute scope is BLOCK, why does only assert if is INLINE?

Here is the delta that I was using:

{
   "ops":[
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":"una ampliacióde prova"
      },
      {
         "insert":"\n·     "
      },
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":"sense"
      },
      {
         "insert":"\n·     "
      },
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":"ordre"
      },
      {
         "insert":"\n"
      },
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":"aaa"
      },
      {
         "insert":"\n"
      },
      {
         "attributes":{
            "list":"ordered",
            "italic":true,
            "color":"#800080"
         },
         "insert":"llista numerada 1"
      },
      {
         "attributes":{
            "list":"ordered"
         },
         "insert":"\n"
      },
      {
         "attributes":{
            "list":"ordered",
            "italic":true,
            "color":"#800080"
         },
         "insert":"llista numerada 2"
      },
      {
         "attributes":{
            "list":"ordered"
         },
         "insert":"\n"
      },
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":" "
      },
      {
         "insert":"\n"
      },
      {
         "attributes":{
            "link":"https://github.com/kenyog/rtf-to-quill-delta/blob/master/rtf-to-delta.js",
            "italic":true,
            "color":"#0000ff"
         },
         "insert":"un link"
      },
      {
         "insert":"\n"
      },
      {
         "attributes":{
            "italic":true,
            "color":"#800080"
         },
         "insert":"\n"
      }
   ]
}

Note that I added the last '\n'.

spChief commented 1 year ago

Same problem with ordered. node-quill-converter. I have delta generated by this lib:

[
    {
        "insert": "asd as dasd asd\nasd asd "
    },
    {
        "insert": "dfg",
        "attributes": {
            "bold": true
        }
    },
    {
        "insert": " dsfg sdfg sdfg \ngsdfg sdfg "
    },
    {
        "insert": "dfgdfg",
        "attributes": {
            "link": "https://singularity-app.com/"
        }
    },
    {
        "insert": "\ndf\n\n"
    },
    {
        "insert": "sgdf gdsf gsdfg sd\nfg \ndsfg sdfg \ndsfg\n sdf\ng sdfg\n",
        "attributes": {
            "list": "ordered"
        }
    }
]

And when I try to insert it to flutter-quill I receive error: 'package:flutter_quill/src/models/documents/nodes/leaf.dart': Failed assertion: line 30 pos 12: 'value.isInline || value.isIgnored || value.isEmpty': Unable to apply Style to leaf: {Attribute{key: list, scope: AttributeScope.BLOCK, value: ordered}}

sobotkami commented 10 months ago

Hi @freshtechtips Is there any news for this issue?

Aruljebaraj commented 2 months ago

any fixes for this issue ? (doc.last.data as String).endsWith('\n')': is not true

MODE83-PROJET-DEV commented 1 month ago

Indeed there is a problem with version 10.0.0, I have the stable version of flutter 3.24.0, I didn't try too hard to understand. I did this in document.dart in line 419 :

before :

assert((doc.last.data as String).endsWith('\n'));

After :

try { assert((doc.last.data as String).endsWith('\n')); } catch (Ex) { print("Problème : $Ex"); }

The essential works on my side

AtlasAutocode commented 3 weeks ago

I can make a couple of comments:

  1. Flutter Quill documents must end in '\n'. Even an empty document must contain that final newline character. This is the reason for the assert. (You may feel it is not necessary, but there is a lot of code that assumes that final newline, so ignoring it may cause unexpected problems when subsequent operations are applied.)
  2. block attributes are expected to be attached to a single '\n' delta.
     {
         "attributes":{
            "list":"ordered",   <- causes problem - remove this line, it is applied in the next delta
            "italic":true,
            "color":"#800080"
         },
         "insert":"llista numerada 1"
      },
      {
         "attributes":{
            "list":"ordered"
         },
         "insert":"\n"
      },

I think a basic question is: When should errors be reported by assert and when should errors simply be ignored?

In this case, deltas are being generated by an outside source that is not generating the deltas correctly for flutter-quill.

I am not an expert in deltas but reading: Quill delta format

To solve this, Quill "adds" a newline to all documents and always ends Deltas with "\n".

// Hello World on two lines
const content = [
  { text: "Hello" },
  { text: "\n", attributes: { align: "center" } },
  { text: "World" },
  { text: "\n", attributes: { align: "right" } }   // Deltas must end with newline
];

and other parts of the document show that applying block attributes to inline text causes problems.