Add images, using the rich text editor

Hello,
How can I upload images from the visual editor in an input of type “richText”? I was able to add one, by manually adding an img html tag, using the “Edit as code” button; then, when I came back to the rich text editor, the image was there. My goal is NOT to use that approach and use the rich text editor only. I need the attached images to be uploaded to the CDN.

Here is my component:

Builder.registerComponent(MyComponent, {
  name: "MyComponent",
  inputs: [
    {
       name: "description",
       type: "richText",
       defaultValue: "",
    }
  ]
});

Here is how I was able to upload the image, by manually typing the img html tag:

  1. I clicked on the “Edit as code” button

  2. Added the following code
    <img src="https://d38b044pevnwc9.cloudfront.net/cutout-nuxt/enhancer/2.jpg" />
    Screenshot 2024-05-15 at 16.15.43

  3. Clicked the “Edit as code” button again to return back to the rich text editor

  4. The image was embedded in the rich text editor

Screen recoding: https://file.io/31c8h5WevuBB

Update: I was also able to drag and drop an image, but then the image was stored as a base64, which is NOT my goal. I want to upload the image to the CDN.

To upload and insert images directly within the rich text editor in Builder.io, you can use the built-in toolbar features of the rich text editor. This will allow you to embed images without needing to switch to the “Edit as code” mode.

Builder.io’s rich text editor includes tools for inserting images, but these need to be enabled and configured correctly. Here’s a step-by-step guide on how to set up and use this functionality:

Step-by-Step Guide:

  1. Enable Image Uploads in Builder’s Visual Editor:

    • The image upload button is included by default in Builder.io’s rich text editor toolbar. Simply click the button to upload images.
  2. Register the Component with the Rich Text Input:

    • Ensure that your component is registered correctly with the rich text input.

Example Component Registration:

Here’s how to register a component with a rich text input that allows image embedding:

import { Builder } from '@builder.io/react';

const MyComponent = (props) => {
  return (
    <div>
      <h1>My Component</h1>
      <div dangerouslySetInnerHTML={{ __html: props.description }} />
    </div>
  );
};

Builder.registerComponent(MyComponent, {
  name: 'MyComponent',
  inputs: [
    {
      name: 'description',
      type: 'richText',
      defaultValue: '',
      helperText: 'Use this field to add rich text with images.',
    },
  ],
});

Using the Visual Editor to Upload Images:

  1. Open the Visual Editor:

    • Go to the Builder.io editor and select the content where you want to use MyComponent.
  2. Edit the Rich Text:

    • Click on the rich text area where you want to insert the image.
  3. Insert Image:

    • Use the toolbar provided in the rich text editor. Click on the image icon/button in the toolbar to upload or insert an image.
    • Upload an image from your local machine or provide a URL for an image.

Example as Images:

  1. Click on the Rich Text Area:

    • You should see a toolbar with various options.
  2. Select the Image Icon:

    • In the toolbar, there will be an icon that looks like an image or a picture.
  3. Upload or Insert Image:

    • Clicking the image icon will open a dialog to either upload an image or insert an image URL.
  4. Complete Insertion:

    • After uploading or providing the URL, the image will be embedded directly within the rich text editor.

Alternative Approach - Custom Toolbar Configuration:

If you need more control over the toolbar, you can configure the toolbar options for the rich text editor:

Builder.registerComponent(MyComponent, {
  name: 'MyComponent',
  inputs: [
    {
      name: 'description',
      type: 'richText',
      defaultValue: '',
      toolbar: ['bold', 'italic', 'underline', 'image', 'link'], // Example custom toolbar configuration
    },
  ],
});

Summary

By following these steps, you can easily upload and insert images directly within the rich text editor in Builder.io. The built-in toolbar makes it straightforward to add images alongside your rich text content. If you need even more customization, you can configure the toolbar options to suit your specific needs.

For more detailed information, refer to the Builder.io documentation on custom components and rich text editing.

To upload images to the CDN instead of storing them as base64 within the rich text editor in Builder.io, you need to make sure that the images are uploaded to Builder.io’s media library. Once uploaded, the images are served via a CDN.

Steps to Ensure Images are Uploaded to the CDN

  1. Upload Images to Builder.io Media Library:

    • Drag and Drop Images: When you drag and drop images into the rich text editor, ensure they are uploaded to Builder.io’s media library, not embedded as base64.
    • Use the Image Toolbar Feature: Use the image upload feature in the rich text editor toolbar to ensure images are uploaded to Builder.io’s media library.
  2. Configure the Rich Text Editor:

    • Configure the rich text editor to utilize Builder.io’s media library for images.

Example: Configuring the Rich Text Editor with CDN Upload

Here’s how you can register a component with a rich text input that uploads images to the Builder.io CDN:

import { Builder } from '@builder.io/react';

const MyComponent = (props) => {
  return (
    <div>
      <h1>My Component</h1>
      <div dangerouslySetInnerHTML={{ __html: props.description }} />
    </div>
  );
};

Builder.registerComponent(MyComponent, {
  name: 'MyComponent',
  inputs: [
    {
      name: 'description',
      type: 'richText',
      defaultValue: '',
      helperText: 'Use this field to add rich text with images.',
      toolbar: ['bold', 'italic', 'underline', 'image', 'link'], // Ensure the image tool is included
    },
  ],
});

Uploading Images through the Visual Editor

  1. Open the Visual Editor:

    • Navigate to the Builder.io editor and select the content where you want to use MyComponent.
  2. Edit the Rich Text:

    • Click on the rich text area where you want to insert the image.
  3. Insert Image using the Toolbar:

    • Use the toolbar provided in the rich text editor. Click the image icon to upload an image.
    • Upload an image from your local machine, and it will get uploaded to Builder.io’s media library and served via the CDN.

Manually Ensuring CDN Uploads

If the drag-and-drop method still results in base64 images, follow these alternative steps:

  1. Upload Image to Builder.io Media Library First:

    • Before inserting the image into the rich text field, manually upload the image to the Builder.io media library.
    • Go to the Builder.io dashboard and navigate to “Media”.
  2. Copy the Image URL:

    • After uploading the image, copy the URL of the image from the media library.
  3. Embed Image Using URL in Rich Text:

    • In the rich text editor, use the image insertion feature and paste the URL of the uploaded image.

Custom Code to Handle Image Uploads (Advanced)

If you need more control over the upload process, you can implement custom code to handle image uploads to the CDN. Here’s how to integrate custom image upload handling:

  1. Implement a Custom Image Upload Handler:
const handleImageUpload = async (file) => {
  const formData = new FormData();
  formData.append('file', file);

  const response = await fetch('https://cdn.builder.io/api/v1/upload?apiKey=YOUR_API_KEY', {
    method: 'POST',
    body: formData,
  });

  const data = await response.json();
  return data.url;
};
  1. Add Custom Code in the Rich Text Editor (if supported):
Builder.registerComponent(MyComponent, {
  name: 'MyComponent',
  inputs: [
    {
      name: 'description',
      type: 'richText',
      defaultValue: '',
      helperText: 'Use this field to add rich text with images.',
      toolbar: ['bold', 'italic', 'underline', 'image', 'link'],
      imageHandler: handleImageUpload, // Custom handler for image uploads
    },
  ],
});

Following these steps will ensure that images are uploaded to Builder.io’s CDN instead of being embedded as base64. The key is to use the image toolbar feature correctly and ensure the images go through Builder.io’s media upload process. This gives you optimized images served via the CDN.