How can I import a custom Portable text editor into my schema files

51 views Asked by At

Trying to build on text editor and share it among all the schemas that need it in Sanity but cannot figure out how this is done.

I did look through the docs but they just build it to show in the view, I was unable to locate anything that was showing how to share this in the CMS everywhere.

I have a file called richtextEditor.js in my lib file for the moment and am trying to just import it and use it in one of my schemas but cannot get it dialed in. The path is correct in the schema at ../../lib/richtextEditor.js but the syntax for how to place this in the field for the Text editor I cannot get dialed in. what I currently have, that is not working, is

    {
      title: 'Abstract',
      name: 'abstract',
      type: 'array',
      of: editor
    },

I had to place the imported editor into the of: area because it complained about that not being there but now it just complains about the syntax of the file, but it is exactly the same as when it was directly in this file and working so there is not a syntax issue with the file but something getting wonky once it gets imported

1

There are 1 answers

0
Sripad Prabhukumar On

One of the comprehensive approach that I can think of is

1. Project Structure:

  • Create a dedicated components directory within your Sanity project's root to store reusable components. Move your richtextEditor.js file to this components directory.

2. Component Definition:

  • Create the richtextEditor.js file with the following structure:

JavaScript

import React from 'react';
import { PortableText } from 'sanity-portable-text';

// Define your custom rich text components here
const serializers = {
  // ... your custom serializer definitions
};

const RichTextEditor = ({ value }) => {
  return <PortableText value={value} components={serializers} />;
};

export default RichTextEditor;
  • Replace // ... your custom serializer definitions with the actual logic to define your custom rich text components (bold, italic, links, etc.). You can find examples and guidance in the Sanity documentation. I'll leave you the link here (https://www.sanity.io/docs/presenting-block-text)
  • This component takes a value prop containing the Portable Text data to render.

3. Schema Integration:

  • In your schemas, import and use the RichTextEditor component:

JavaScript

export default {
  title: 'Document',
  name: 'document',
  type: 'document',
  fields: [
    {
      title: 'Abstract',
      name: 'abstract',
      type: 'array',
      of: [{ type: 'block' }], // Specify the block types allowed in the editor
    },
    {
      title: 'Body',
      name: 'body',
      type: 'array',
      of: [{ type: 'reference', to: [{ type: 'richTextComponent' }] }], // Reference the rich text component
    },
    // ... other fields
  ],
};

Explanation:

  1. Import RichTextEditor:

JavaScript

import RichTextEditor from '../components/richtextEditor'; // Adjust path as needed
  1. Use the reference type:
  • type: 'reference': This indicates that the field will hold references to a specific document type.
  • to: [{ type: 'richTextComponent' }]: This refers to the document type named richTextComponent (create this in a separate schema file if you haven't already).

4. Rich Text Component Schema (optional tho):

If you need additional control over the rich text editor configuration (allowed blocks, etc.), create a separate document type for it: JavaScript

export default {
  title: 'Rich Text Component',
  name: 'richTextComponent',
  type: 'document',
  fields: [
    {
      title: 'Content',
      name: 'content',
      type: 'array',
      of: [{ type: 'block' }], // Specify the allowed block types here
    },
  ],
};
  • Update the reference field in your main schema to point to this new richTextComponent type:

JavaScript

{
  title: 'Body',
  name: 'body',
  type: 'array',
  of: [{ type: 'reference', to: [{ type: 'richTextComponent' }] }],
},