Implementing Draft.js and converting rich text to Sanity portable text

16 replies
Last updated: May 18, 2023
Hey, can any one tell is there a way to implement draft js a rich text editor on the front end and send the data to sanity ?
AI Update

Yes, you can absolutely implement Draft.js as a rich text editor on your frontend and send the data to Sanity! The key is converting Draft.js content to Portable Text, which is Sanity's structured content format.

Here's the general approach:

The Conversion Process

Since Draft.js doesn't directly output Portable Text, you'll need to convert it through HTML as an intermediate format:

  1. Export Draft.js to HTML - Use the draft-js-export-html library to convert your Draft.js content state to HTML
  2. Convert HTML to Portable Text - Use Sanity's @sanity/block-tools package, specifically the htmlToBlocks function

Basic Implementation

import { stateToHTML } from 'draft-js-export-html';
import { htmlToBlocks } from '@sanity/block-tools';
import { Schema } from '@sanity/schema';

// Get your Draft.js content state
const contentState = editorState.getCurrentContent();

// Convert to HTML
const html = stateToHTML(contentState);

// Define your Sanity schema (you'll need your actual block content schema)
const defaultSchema = Schema.compile({
  name: 'myBlog',
  types: [
    /* your schema types */
  ]
});

const blockContentType = defaultSchema
  .get('blockContent')
  .jsonType;

// Convert HTML to Portable Text blocks
const blocks = htmlToBlocks(html, blockContentType, {
  parseHtml: html => new DOMParser().parseFromString(html, 'text/html')
});

Custom Deserialization Rules

If you have custom formatting or blocks in Draft.js, you can define custom deserialization rules when converting HTML to blocks. This lets you handle special elements like custom inline styles, embedded media, or other Draft.js decorators.

Alternative Approach

If you're building a new project, consider whether you really need Draft.js. Sanity Studio comes with its own excellent Portable Text editor that you can configure extensively to support custom blocks, decorators, and behaviors. This eliminates the conversion step entirely.

However, if you need Draft.js for specific frontend requirements (like a public-facing comment system or a custom editing experience), the HTML-to-Portable-Text conversion approach works well!

you need to convert rich-text to portable-text similar to this https://github.com/portabletext/contentful-rich-text-to-portable-text
then it would work. but youd need to map the rich text correctly to sanitys portable text key for key. either that or you can upload the richtext as json but you wouldnt be able to edit it in the admin as easily. why not just use sanitys portable text editor from the sanity ui lib
thank you so much
can i dm u ?
no I dont have time for one on one.
ok
then can u tell what u are telling to use ?
then it would work. but youd need to map the rich text correctly to sanitys portable text key for key. either that or you can upload the richtext as json but you wouldnt be able to edit it in the admin as easily. why not just use sanitys portable text editor from the sanity ui lib
im saying using that repo I posted as a reference build your own draft rich text -> portable text converter
in other words, its possible but you need to use big brain science to make it work.
then is there a simple way ?
like I said you can upload the richtext from draft as json. which accepts any kind of json data(rich-text) to sanity
i just need a text editor on my front end and want to send the data to the admin studio
like I said you can upload the richtext from draft as json. which accepts any kind of json data(rich-text) to sanity
How to do it can u provide any resource ?
User’s answer here is what you’re looking for! You’ll set up your editor on your frontend, convert it to portable text then patch your data in your dataset.
If you don’t want to do that and just want to use the output from Drat directly, you’ll still need to submit a patch but you’ll also need to implement a Draft editor in your Studio.

You can learn more about patches by reading the resources in the documentation.
okey i got it now
thanks every one for help

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?