Tiptap: Tiptap leaves empty <p> tag, how to remove it?

Created on 4 Jan 2019  路  6Comments  路  Source: ueberdosis/tiptap

I am doing client side validation, and want to notice user when content is empty, but Tiptap leaves <p></p> even if there's no content in editor. Make no sense. How to remove everything when user deletes all content in editor?

Most helpful comment

You don't need an empty paragraph to type in. My inital value is null and it works as it should. It automatically adds p or any other selected tags. Same issue happens with any other tags (headings, anchor, span, ...) when you delete text. There is no textual value, and all it's left are empty tags <p></p> <h1></h1> and so on... Solution from @bdrtsky is hadrcoding and such solution can only work as temporary fix

Edit:
This is my fix (it clears empty paragraphs, headings and lists as tested):

onUpdate: ({ getJSON, getHTML }) => {
  let content = getHTML(),
      json = getJSON().content;

  if (Array.isArray(json) && json.length === 1 && !json[0].hasOwnProperty("content")) {
    content = null; // or any other default value
  }

  this.$emit("input", content);
}

All 6 comments

For now fixed it with

onUpdate: ({ getJSON, getHTML }) => {
          this.html = getHTML()
          if (this.html === '<p></p>') this.html = ''
          this.$emit('input', this.html)
        }

Still wonder about this behavior? Not sure if there can be situations where Tiptap will leave something else empty, but paragraph.

That's the behavior of most editors I think. It's because you need an empty paragraph to type in. I don't plan to change this at the moment.

Also, Tiptap puts <p> tags inside list elements for some reason

@ieglonew01f see #118

You don't need an empty paragraph to type in. My inital value is null and it works as it should. It automatically adds p or any other selected tags. Same issue happens with any other tags (headings, anchor, span, ...) when you delete text. There is no textual value, and all it's left are empty tags <p></p> <h1></h1> and so on... Solution from @bdrtsky is hadrcoding and such solution can only work as temporary fix

Edit:
This is my fix (it clears empty paragraphs, headings and lists as tested):

onUpdate: ({ getJSON, getHTML }) => {
  let content = getHTML(),
      json = getJSON().content;

  if (Array.isArray(json) && json.length === 1 && !json[0].hasOwnProperty("content")) {
    content = null; // or any other default value
  }

  this.$emit("input", content);
}

@bdrtsky @kikky7 both solution works, but when I send content: this.editor.getHTML(), in axios, I see content: "<p></p>"... empty paragraph is still here. I use server-side validation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bernhardh picture bernhardh  路  3Comments

afwn90cj93201nixr2e1re picture afwn90cj93201nixr2e1re  路  3Comments

klaasgeldof picture klaasgeldof  路  3Comments

nekooee picture nekooee  路  3Comments

jameswragg picture jameswragg  路  3Comments