static-cms/website/content/docs/customization.md

148 lines
5.7 KiB
Markdown
Raw Normal View History

2022-09-30 11:39:35 -04:00
---
title: Creating Custom Previews
weight: 50
group: Customization
---
2022-10-02 20:06:20 -04:00
The Static CMS exposes a `window.CMS` global object that you can use to register custom widgets, previews and editor plugins. The available customization methods are:
2022-09-30 11:39:35 -04:00
2022-10-20 11:57:30 -04:00
- **registerPreviewTemplate:** Registers a template for a collection.
2022-09-30 11:39:35 -04:00
### React Components inline interaction
2022-10-02 20:06:20 -04:00
Static CMS is a collection of React components and exposes two constructs globally to allow you to create components inline: createClass and h (alias for React.createElement).
2022-09-30 11:39:35 -04:00
## `registerPreviewTemplate`
Registers a template for a folder collection or an individual file in a file collection.
`CMS.registerPreviewTemplate(name, react_component);`
**Params:**
2022-10-20 11:57:30 -04:00
- name: The name of the collection (or file for file collections) which this preview component will be used for.
- Folder collections: Use the name of the collection
- File collections: Use the name of the file
- react_component: A React component that renders the collection data. Six props will be passed to your component during render:
- entry: Immutable collection containing the entry data.
- widgetFor: Returns the appropriate widget preview component for a given field.
- [widgetsFor](#lists-and-objects): Returns an array of objects with widgets and associated field data. For use with list and object type entries.
- getAsset: Returns the correct filePath or in-memory preview for uploaded images.
2022-09-30 11:39:35 -04:00
**Example:**
```html
2022-10-02 20:06:20 -04:00
<script src="https://unpkg.com/@staticcms/core@%5E0.1.0/dist/static-cms-core.js"></script>
2022-09-30 11:39:35 -04:00
<script>
var PostPreview = createClass({
2022-10-20 11:57:30 -04:00
render: function () {
2022-09-30 11:39:35 -04:00
var entry = this.props.entry;
2022-10-20 11:57:30 -04:00
var image = entry.data.image;
2022-09-30 11:39:35 -04:00
var bg = this.props.getAsset(image);
2022-10-20 11:57:30 -04:00
return h(
'div',
{},
h('h1', {}, entry.data.title),
h('img', { src: bg.toString() }),
h('div', { className: 'text' }, this.props.widgetFor('body')),
2022-09-30 11:39:35 -04:00
);
2022-10-20 11:57:30 -04:00
},
2022-09-30 11:39:35 -04:00
});
2022-10-20 11:57:30 -04:00
CMS.registerPreviewTemplate('posts', PostPreview);
2022-09-30 11:39:35 -04:00
</script>
```
2022-10-20 11:57:30 -04:00
- document: The preview pane iframe's [document instance](https://github.com/ryanseddon/react-frame-component/tree/9f8f06e1d3fc40da7122f0a57c62f7dec306e6cb#accessing-the-iframes-window-and-document).
- window: The preview pane iframe's [window instance](https://github.com/ryanseddon/react-frame-component/tree/9f8f06e1d3fc40da7122f0a57c62f7dec306e6cb#accessing-the-iframes-window-and-document).
2022-09-30 11:39:35 -04:00
### Lists and Objects
2022-10-20 11:57:30 -04:00
The API for accessing the individual fields of list- and object-type entries is similar to the API for accessing fields in standard entries, but there are a few key differences. Access to these nested fields is facilitated through the `widgetsFor` function, which is passed to the preview template component during render.
**Note**: as is often the case with the Static CMS API, arrays and objects are created with Immutable.js. If some of the methods that we use are unfamiliar, such as `getIn`, check out [their docs](https://facebook.github.io/immutable-js/docs/#/) to get a better understanding.
**List Example:**
```html
<script>
2022-09-30 11:39:35 -04:00
var AuthorsPreview = createClass({
// For list fields, the widgetFor function returns an array of objects
// that you can map over in your template. If our field is a list of
// authors containing two entries, with fields `name` and `description`,
// the return value of `widgetsFor` would look like this:
//
// [{
// data: { name: 'Mathias', description: 'Co-Founder'},
// widgets: { name: (<WidgetComponent>), description: (WidgetComponent>)}
// },
// {
// data: { name: 'Chris', description: 'Co-Founder'},
// widgets: { name: (<WidgetComponent>), description: (WidgetComponent>)}
// }]
//
// Templating would look something like this:
2022-10-20 11:57:30 -04:00
render: function () {
return h(
'div',
{},
2022-09-30 11:39:35 -04:00
// This is a static header that would only be rendered once for the entire list
h('h1', {}, 'Authors'),
// Here we provide a simple mapping function that will be applied to each
// object in the array of authors
2022-10-20 11:57:30 -04:00
this.props.widgetsFor('authors').map(function (author, index) {
return h(
'div',
{ key: index },
2022-09-30 11:39:35 -04:00
h('hr', {}),
2022-10-20 11:57:30 -04:00
h('strong', {}, author.data.name),
author.widgets.description,
2022-09-30 11:39:35 -04:00
);
2022-10-20 11:57:30 -04:00
}),
2022-09-30 11:39:35 -04:00
);
2022-10-20 11:57:30 -04:00
},
2022-09-30 11:39:35 -04:00
});
2022-10-20 11:57:30 -04:00
CMS.registerPreviewTemplate('authors', AuthorsPreview);
</script>
```
**Object Example:**
```html
<script>
2022-09-30 11:39:35 -04:00
var GeneralPreview = createClass({
// Object fields are simpler than lists - instead of `widgetsFor` returning
// an array of objects, it returns a single object. Accessing the shape of
// that object is the same as the shape of objects returned for list fields:
//
// {
// data: { front_limit: 0, author: 'Chris' },
// widgets: { front_limit: (<WidgetComponent>), author: (WidgetComponent>)}
// }
2022-10-20 11:57:30 -04:00
render: function () {
2022-09-30 11:39:35 -04:00
var entry = this.props.entry;
2022-10-20 11:57:30 -04:00
var title = entry.data.site_title;
var posts = entry.data.posts;
2022-09-30 11:39:35 -04:00
2022-10-20 11:57:30 -04:00
return h(
'div',
{},
2022-09-30 11:39:35 -04:00
h('h1', {}, title),
2022-10-20 11:57:30 -04:00
h(
'dl',
{},
2022-09-30 11:39:35 -04:00
h('dt', {}, 'Posts on Frontpage'),
2022-10-20 11:57:30 -04:00
h('dd', {}, this.props.widgetsFor('posts').widgets.front_limit || 0),
2022-09-30 11:39:35 -04:00
h('dt', {}, 'Default Author'),
2022-10-20 11:57:30 -04:00
h('dd', {}, this.props.widgetsFor('posts').data.author || 'None'),
),
2022-09-30 11:39:35 -04:00
);
2022-10-20 11:57:30 -04:00
},
2022-09-30 11:39:35 -04:00
});
2022-10-20 11:57:30 -04:00
CMS.registerPreviewTemplate('general', GeneralPreview);
</script>
```