tiptap icon indicating copy to clipboard operation
tiptap copied to clipboard

[Bug]: Difference in behavior of setContent and insertContent commands

Open ashu12chi opened this issue 1 year ago • 5 comments

Which packages did you experience the bug in?

core

What Tiptap version are you using?

latest

What’s the bug you are facing?

In our application, we are using insertContent and setContent command. The issue we are facing is the final output for the same content is different for the two commands.

Example to this:

const content = `<h1>Welcome to your fresh Tiptap Code Sandbox</h1>
  <p>You can create a demo for your issue inside of this sandbox and share it with us.</p>`

For above content, output of setContent is: image But, for insertContent it is coming with two blank lines in-between the content image if I pass parseOptions in insertContent command as

editor.commands.insertContent(content, {
    parseOptions: { preserveWhitespace: false }
})

It will still come with one blank line like this: image

Ideally, behavior should be same for both the cases.

I have some observations regarding this:

  1. If I modify my content to have no new line characters like this:
const content = `<h1>Welcome to your fresh Tiptap Code Sandbox</h1><p>You can create a demo for your issue inside of this sandbox and share it with us.</p>`

Behavior will be same for both insertContent and setContent.

  1. On taking a cursory at the internal implementation of the two commands, setContent is using parseSlice and insertContent is using parse function from Prosmirror.

Since, external content can possibly have new lines in them, we want behavior of both the commands to be same (Ideally the behavior of setContent)

What browser are you using?

Chrome

Code example

https://codesandbox.io/s/green-monad-surcnd?file=/src/App.js

What did you expect to happen?

setContent and insertContent should produce same output for given content.

Anything to add? (optional)

NA

Did you update your dependencies?

  • [X] Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • [X] Yes, I’m a sponsor. 💖

ashu12chi avatar May 10 '23 12:05 ashu12chi

In order to maintain consistency between the behaviors of "insertContent" and "setContent", this is what I did:

const htmlWithoutNewLine = html.replace(/\n/g, '');

Adonis0123 avatar Jul 03 '23 08:07 Adonis0123

Experiencing the same issue. Even when replacing the newline characters the commands do not produce the same results for more complex html imports.

Seems like quite a core functionality that is important to have reliable import. Is it still on the roadmap? @bdbch

kaikun213 avatar Feb 11 '24 17:02 kaikun213

Thanks for reporting. I'll take a look tomorrow

bdbch avatar Feb 14 '24 21:02 bdbch

Could this have any relation to #4862?

Edit: Oh sorry, probably not given the issue's creation date - although it may be related to what @kaikun213 is saying specifically?

Nantris avatar Feb 14 '24 21:02 Nantris

https://github.com/ueberdosis/tiptap/pull/4895 created a fix here which should bring setContent and insertContent in line with each other. setContent was using a different DOM parsing approach then insertContent.

bdbch avatar Feb 16 '24 16:02 bdbch

Should be resolved with the latest beta, let us know: v2.5.0-beta.5

nperez0111 avatar Jun 25 '24 20:06 nperez0111

Not OP, but I did a quick test in our editor and it looks good to me. It's always possible our custom code is interfering but I think this can be closed unless someone reports otherwise.

Nantris avatar Jun 25 '24 21:06 Nantris

~~I'm a little confused about preserveWhitespace though. I'd expect false NOT to insert the empty line, but instead it does insert it. (at the end, not in the middle as shown in the image) Is that right?~~ Nevermind, bad test on my part. I think we're good here.

Nantris avatar Jun 25 '24 21:06 Nantris