Payload Visual Editor Plugin
This plugin provides a visual editor, including a nice UI, for Payload.
Note
This plugin is currently under active development. Please check back periodically for updates.
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
Note
For the collections in which you use the visual editor, fields in the {admin: {position:'sidebar'}}
area will be rendered below all other fields, in the "main" area or, if you are using tabs, in an extra tab called "More".
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`,
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 | mandatoryA function returning a string of the URL to your frontend preview route (e.g.
https://localhost:3001/pages/preview
). Thelocale
property can be used if needed for preview localization. -
showPreview
: booleanShow or hide preview while opening an edit page the first time. After toggling, the state will be saved in localStore. Default: true
-
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`
...
}),
],
});
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>
<div>
<RenderBlocks blocks={page.content} />
</div>
</main>
</div>
)
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:
- Start docker and wait until the containers are up:
docker-compose up
- Open another terminal and install root dependencies:
yarn docker:plugin:yarn
- Install dependencies of the payload example:
yarn docker:example:cms:yarn
- Run the payload dev server:
yarn docker:example:cms:dev
- Open another terminal and install dependencies of the frontend example:
yarn docker:example:website:yarn
- 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
.