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

compose image attribute into NotusDocument lose image information #45

Closed
ttlg opened this issue Nov 9, 2018 · 2 comments
Closed

compose image attribute into NotusDocument lose image information #45

ttlg opened this issue Nov 9, 2018 · 2 comments

Comments

@ttlg
Copy link

ttlg commented Nov 9, 2018

Hi,
I am not sure this is bug or spec.
But when I tried to compose embed image attribute into NotusDocument, the image information was lost.

Here is the code I wrote.

 test('compose embed image', () {
      final imageDelta = Delta.fromJson([
        {
          "insert": "",
          "attributes": {
            "embed": {"type": "image", "source": "http://img.jpg"},
          }
        }
      ])
      ..insert('\n');
      final doc = NotusDocument();
      doc.compose(imageDelta, ChangeSource.local);
      expect(doc.toDelta(), imageDelta);
});
  Expected: Delta:<insert⟨  ⟩ + {embed: {type: image, source: http://img.jpg}}
            insert⟨ ⏎ ⟩>
    Actual: Delta:<insert⟨ ⏎⏎ ⟩>
@pulyaevskiy
Copy link
Contributor

Good question. There are a few things here.

First, I'd advise not to use compose() method unless you're absolutely sure what you are doing is correct. Especially if you need to apply a local change. See dartdoc for NotusDocument.compose for more details.

Second, your imageDelta is not a valid image embed, this is why you don't see it in the result. This is another reason I do not recommend using compose. Normally you should never try to compose hand-written Deltas into Notus documents. You risk corrupting the document state this way.

A better (and recommended) way to apply changes to a document is using all the other methods on NotusDocument. So for your case:

final doc = NotusDocument();
final delta = doc.format(0, 0, NotusAttribute.embed.image('http://img.jpg'));
// format() method returns actual Delta composed into this document, 
// so you can observe it if needed
print(delta);

Hope this helps.

@ttlg
Copy link
Author

ttlg commented Nov 10, 2018

Thank you for your explanation!
I understood right way to apply changes to NotusDocument, also why my code failed.
I needed zero width space (8203) for insert.

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

2 participants