pemedia

payload visual editor

by pemedia
Publishing workflow
v2
^2.0.13
MIT

Overview

Summary generated from the README

Adds a visual editor to the edit view of selected collections and globals in the Payload admin. It shows your frontend in an iframe or popup and sends the current document data to it through window messages. Preview URLs can be set globally or per collection and global, with locale support. Version 2.x targets Payload 2.x and 0.x targets Payload 1.x.

Features

  • Preview shown as an iframe, popup or hidden, with the choice saved in local storage
  • Live document data sent to the frontend via postMessage as fields change
  • Global and per-collection or per-global preview URL functions
  • Locale passed to the preview URL function for localized frontends
  • Configurable preview width as a percentage
  • Relation fallbacks for incomplete required relationship or upload data

Use cases

  • Show editors a live frontend preview while they edit page content and blocks
  • Preview localized pages by switching the preview URL with the selected locale
  • Render incomplete documents safely when required relations are not yet chosen

From the author

Payload CMS plugin which provides a visual live editor directly in the Admin UI.

Installation

npm i payload-visual-editor

315

Stars

18

Forks

2

Open Issues

2 years ago

Last Updated

61

Weekly Downloads

v2.0.6

npm Version

93.2 KB

Unpacked Size

1

Dependencies

GitHub stars

315+7 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 2026315
Dec 4, 2026315
Dec 3, 2026315
Dec 2, 2026315
Dec 1, 2026315
Nov 30, 2026315
Nov 29, 2026315
Nov 28, 2026315
Nov 27, 2026315
Nov 26, 2026315
Nov 25, 2026315
Nov 24, 2026316
Nov 23, 2026316
Nov 22, 2026316
Nov 21, 2026316
Nov 20, 2026316
Nov 19, 2026317
Nov 18, 2026317
Nov 17, 2026317
Nov 16, 2026318
Nov 15, 2026318
Nov 14, 2026318
Nov 13, 2026317
Nov 12, 2026317
Nov 11, 2026316
Nov 10, 2026316
Nov 9, 2026316
Nov 8, 2026316
Nov 7, 2026315
Nov 6, 2026315
Nov 5, 2026315
Nov 4, 2026315
Nov 3, 2026314
Nov 2, 2026314
Nov 1, 2026314
Oct 31, 2026314
Oct 30, 2026314
Oct 29, 2026314
Oct 28, 2026314
Oct 27, 2026314
Oct 26, 2026314
Oct 25, 2026314
Oct 24, 2026314
Oct 23, 2026314
Oct 22, 2026314
Oct 21, 2026312
Oct 20, 2026312
Oct 19, 2026312
Oct 18, 2026312
Oct 17, 2026312
Oct 16, 2026311
Oct 15, 2026311
Oct 14, 2026311
Oct 13, 2026311
Oct 12, 2026311
Oct 11, 2026311
Oct 10, 2026311
Oct 9, 2026311
Oct 8, 2026311
Oct 7, 2026311
Oct 6, 2026311
Oct 5, 2026311
Oct 4, 2026310
Oct 3, 2026310
Oct 2, 2026310
Oct 1, 2026310
Sep 30, 2026310
Sep 29, 2026310
Sep 28, 2026310
Sep 27, 2026310
Sep 26, 2026310
Sep 25, 2026310
Sep 24, 2026310
Sep 23, 2026310
Sep 22, 2026310
Sep 21, 2026310
Sep 20, 2026310
Sep 19, 2026310
Sep 18, 2026310
Sep 17, 2026310
Sep 16, 2026310
Sep 15, 2026310
Sep 14, 2026310
Sep 13, 2026309
Sep 12, 2026309
Sep 11, 2026309
Sep 10, 2026309
Sep 9, 2026309
Sep 8, 2026309
Sep 7, 2026308
Sep 6, 2026308
npm weekly downloads

63+49 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202663
Dec 4, 202628
Dec 3, 202631
Dec 2, 202632
Dec 1, 202634
Nov 30, 202637
Nov 29, 202657
Nov 28, 202659
Nov 27, 202677
Nov 26, 202682
Nov 25, 202684
Nov 24, 202689
Nov 23, 202690
Nov 22, 202676
Nov 21, 202671
Nov 20, 202650
Nov 19, 202653
Nov 18, 202663
Nov 17, 202651
Nov 16, 202649
Nov 15, 202661
Nov 14, 202666
Nov 13, 202669
Nov 12, 202673
Nov 11, 202659
Nov 10, 202663
Nov 9, 202661
Nov 8, 202637
Nov 7, 202643
Nov 6, 202650
Nov 5, 202648
Nov 4, 202659
Nov 3, 202672
Nov 2, 202680
Nov 1, 202693
Oct 31, 202695
Oct 30, 202689
Oct 29, 202676
Oct 28, 202661
Oct 27, 202646
Oct 26, 202643
Oct 25, 202630
Oct 24, 202625
Oct 23, 202618
Oct 22, 202622
Oct 21, 202632
Oct 20, 202638
Oct 19, 202635
Oct 18, 202641
Oct 17, 202641
Oct 16, 202643
Oct 15, 202641
Oct 14, 202650
Oct 13, 202643
Oct 12, 202640
Oct 11, 202637
Oct 10, 202631
Oct 9, 202632
Oct 8, 202630
Oct 7, 202615
Oct 6, 202616
Oct 5, 202617
Oct 4, 202615
Oct 3, 202615
Oct 2, 202613
Oct 1, 202615
Sep 30, 202612
Sep 29, 202628
Sep 28, 202630
Sep 27, 202630
Sep 26, 202630
Sep 25, 202638
Sep 24, 202636
Sep 23, 202637
Sep 22, 202636
Sep 21, 202635
Sep 20, 202638
Sep 19, 202636
Sep 18, 202628
Sep 17, 202630
Sep 16, 202629
Sep 15, 202612
Sep 14, 202614
Sep 13, 202616
Sep 12, 202616
Sep 11, 202618
Sep 10, 202616
Sep 9, 202618
Sep 8, 202620
Sep 7, 202620
Sep 6, 202614

Each point is npm's rolling 7-day download count.

Health Score

49/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/648494502-root.svg)](https://payloaddirectory.dev/plugins/648494502-root)
HTML
<a href="https://payloaddirectory.dev/plugins/648494502-root"><img src="https://payloaddirectory.dev/badge/648494502-root.svg" alt="Payload Directory"></a>
Updated Mar 14, 2024Created Jun 2, 2023Published Mar 14, 2024

README

Payload Visual Editor Plugin

Note This plugin provides a visual live preview, including a nice UI, for Payload

Version 0.x.x is compatible with Payload 1.x.x
Version 2.x.x is compatible with Payload 2.x.x

Core features:

  • Adds a visual editor component to your collections and globals:
    • Creates the visual editor UI in the Admin UIs edit view
    • Handles the live data exchange with your frontend

image

Installation

  yarn add payload-visual-editor
  # OR
  npm i payload-visual-editor

Basic Usage

In the plugins array of your Payload config, call the plugin with options:

// import plugin
import { visualEditor } from "payload-visual-editor";

// import styles
import "payload-visual-editor/dist/styles.scss";

const config = buildConfig({
  collections: [...],
  plugins: [
    visualEditor({
      previewUrl: () => `http://localhost:3001/pages/preview`,
      previewWidthInPercentage: 60,
      collections: {
        [COLLECTION_SLUG]: {
          previewUrl: () => `...` // optional individual preview url for each collection
        },
      },
      globals: {
        [GLOBAL_SLUG]: {
          previewUrl: () => `...` // optional individual preview url for each global
        },
      },
    }),
  ],
});

Options

  • previewUrl : ({ locale: string; }) => string | mandatory

    A function returning a string of the URL to your frontend preview route (e.g. https://localhost:3001/pages/preview). The locale property can be used if needed for preview localization.

  • defaultPreviewMode : "iframe" | "popup" | "none"

    Preferred preview mode while opening an edit page the first time. After toggling, the state will be saved in localStore. Default: "iframe"

  • previewWidthInPercentage : number

    Width of the iframe preview in percentage. Default: 50

  • collections / globals : Record<string, { previewUrl?: ({ locale: string; }) => string; }>

    An object with configs for all collections / globals which should enable the live preview. Use the collection / global slug as the key. If you don't want to override the previewUrl, just pass an empty object.

Localization

If you are using Localization with multiple locales, it can be very handy, to be able to adjust the preview URL based on the selected/current locale. You can pass locale to the previewUrl function in your payload config an place it, where your frontend needs it to be:

const config = buildConfig({
  collections: [...],
  plugins: [
    visualEditor({
      previewUrl: params => `https://localhost:3001/${params.locale}/pages/preview`
      ...
    }),
  ],
});

Relation Fallbacks

When adding blocks or editing relationship / upload fields, you will often encounter the issue that the data is incomplete. For instance, because no relation has been selected yet. However, when such fields are marked as required and there is no check for undefined values in the frontend, it can lead to unexpected errors in the rendering process.
To address this problem, fallbacks can be set up for the collections / globals. In cases where a field is required but no value has been selected, the fallback of the respective collection will be returned.

import { CollectionWithFallbackConfig } from "payload-visual-editor";

export const Tags: CollectionWithFallbackConfig<Tag> = {
    slug: "tags",
    fields: [
        {
            name: "name",
            type: "text",
            required: true,
        },
    ],
    custom: {
        fallback: {
            id: "",
            name: "Fallback Tag",
            createdAt: "",
            updatedAt: "",
        },
    },
};

Frontend Integration in React / Next.js

In the next.js route which will handle your life preview use this code snippet to get the live post data of your collection directly from payload. In this case it"s a collection with he name page.

const [page, setPage] = useState<Page | null>(null);

useEffect(() => {
    const listener = (event: MessageEvent) => {
        if (event.data.cmsLivePreviewData) {
            setPage(event.data.cmsLivePreviewData);
        }
    };

    window.addEventListener("message", listener, false);

    return () => {
        window.removeEventListener("message", listener);
    };
}, []);

You can now pass this to your render function and you can use all your payload collection data in there. For example like this:

return (
    <div>
        <header>
            <h1>{page.title}</h1>
        </header>
        <main>
            <RenderBlocks blocks={page.content} />
        </main>
    </div>
);

Since the document will only be send to the frontend after a field has been changed the preview page wouldn"t show any data on first render. To inform the cms to send the current document state to the frontend, send a ready message to the parent window, as soon as the DOM / react app is ready:

// react
useEffect(() => {
    (opener ?? parent).postMessage("ready", "*");
}, []);

// vanilla js
window.addEventListener("DOMContentLoaded", () => {
    (opener ?? parent).postMessage("ready", "*");
});

Development

This repo includes a demo project with payload as the backend and a simple website written in plain TypeScript. To start the demo, follow these steps:

  1. Start docker and wait until the containers are up:
docker-compose up
  1. Open another terminal and install root dependencies:
yarn docker:plugin:yarn
  1. Install dependencies of the payload example:
yarn docker:example:cms:yarn
  1. Run the payload dev server:
yarn docker:example:cms:dev
  1. Open another terminal and install dependencies of the frontend example:
yarn docker:example:website:yarn
  1. Start the dev server for the frontend:
yarn docker:example:website:dev
  • After changing collections, fields, etc., you can use yarn docker:example:cms:generate-types to create an updated interface file.
  • To connect with the node container, run yarn docker:shell.
  • To connect with the database container, run yarn docker:mongo.

Similar plugins