Sjors van Dongen
Sjors van Dongen

Reputation: 490

ProseMirror/vue-tiptap replace nbsp to space

I've been trying to automatically replace   to a normal space when someone is pasting in some text or HTML. The preserveWhitespace option doesn't do much for me.

Does anyone have an idea how to automatically replace   with ProseMirror?

Upvotes: 2

Views: 1685

Answers (2)

HazAnwar
HazAnwar

Reputation: 440

This is normally caused due to a browser's handling of clipboard data.

You can try the above recommended by henk, by matching the character, alternatively failing that, you can try using the Unicode character number directly to make the change as per this ProseMirror discussion

editorProps: {
  transformPastedText(text) {
    return text.replace(/\u00A0/g, " ");
  },
  transformPastedHTML: function(html) {
    return html.replace(/\u00A0/g, " ");
  }
}

Upvotes: 2

henk
henk

Reputation: 2848

In ProseMirror you have EditorProps for that, especially transformPastedText, transformPastedHTML. There you can target the   with a regular expression RegEx and replace it.

Here is a working codesandbox. I forked it off from a VueJS example, you will have to adjust it to fit your environment but the general editor settings should be more or less the same. Just copy the input text and paste in into the input again to see it in action.

Disclosure: in the codesandbox example the   is replaced by a '*' for demonstration.

new Editor({
  editorProps: {
    transformPastedText(text) {
      return text.replace(/ /g, ' ');
    },
    transformPastedHTML(html) {
      return html.replace(/ /g, ' ');
    },
  },...

Upvotes: 5

Related Questions