import cms, { useMediaAsset } from "@staticcms/core";
// Register all the things
cms.init();
const PostPreview = ({ entry, widgetFor }) => {
return (
{entry.data.title}
{widgetFor("image")}
Written {entry.data.date}
{widgetFor("body")}
);
};
const PostPreviewCard = ({ entry, widgetFor, viewStyle }) => {
return (
{viewStyle === "grid" ? widgetFor("image") : null}
{entry.data.title}
{entry.data.date}
{entry.data.draft === true ? "Draft" : "Published"}
);
};
const PostDateFieldPreview = ({ value }) => {
const date = new Date(value);
const month = date.getMonth() + 1;
const day = date.getDate();
return h(
'div',
{},
`${date.getFullYear()}-${month < 10 ? `0${month}` : month}-${day < 10 ? `0${day}` : day}`,
);
};
const PostDraftFieldPreview = ({ value }) => {
return h(
'div',
{
style: {
backgroundColor: value === true ? 'rgb(37 99 235)' : 'rgb(22 163 74)',
color: 'white',
border: 'none',
padding: '2px 6px',
textAlign: 'center',
textDecoration: 'none',
display: 'inline-block',
cursor: 'pointer',
borderRadius: '4px',
fontSize: '14px',
},
},
value === true ? 'Draft' : 'Published',
);
};
const GeneralPreview = ({ widgetsFor, entry }) => {
const title = entry.data.site_title;
const posts = entry.data.posts;
const thumb = posts && posts.thumb;
const thumbUrl = useMediaAsset(thumb);
return (
{title}
Posts on Frontpage
{widgetsFor("posts").widgets.front_limit ?? 0}
Default Author
{widgetsFor("posts").data?.author ?? "None"}
Default Thumbnail
{thumb && }
);
};
const AuthorsPreview = ({ widgetsFor }) => {
return (
Authors
{widgetsFor("authors").map((author, index) => (
{author.data.name}
{author.widgets.description}
))}
);
};
const RelationKitchenSinkPostPreview = ({ fieldsMetaData }) => {
// When a post is selected from the relation field, all of it's data
// will be available in the field's metadata nested under the collection
// name, and then further nested under the value specified in `value_field`.
// In this case, the post would be nested under "posts" and then under
// the title of the selected post, since our `value_field` in the config
// is "title".
const post = fieldsMetaData && fieldsMetaData.posts.value;
if (!post) {
return null;
}
return (
Related Post
{post.title}
{(post.body ?? "").slice(0, 100) + "..."}
);
};
const CustomPage = () => {
return I am a custom page!
;
};
cms.registerPreviewTemplate("posts", PostPreview);
CMS.registerPreviewCard("posts", PostPreviewCard);
CMS.registerFieldPreview('posts', 'date', PostDateFieldPreview);
CMS.registerFieldPreview('posts', 'draft', PostDraftFieldPreview);
cms.registerPreviewTemplate("general", GeneralPreview);
cms.registerPreviewTemplate("authors", AuthorsPreview);
// Pass the name of a registered control to reuse with a new widget preview.
cms.registerWidget("relationKitchenSinkPost", "relation", RelationKitchenSinkPostPreview);
cms.registerAdditionalLink({
id: "docs",
title: "Static CMS Docs",
data: "https://staticjsCMS.netlify.app/",
options: {
icon: "page",
},
});
cms.registerAdditionalLink({
id: "config",
title: "Demo config.yml",
data: "https://github.com/StaticJsCMS/static-cms/blob/main/packages/demo/public/config.yml",
options: {
icon: "page",
},
});
cms.registerAdditionalLink({
id: "custom-page",
title: "Custom Page",
data: CustomPage,
options: {
icon: "page",
},
});
cms.registerShortcode("youtube", {
label: "YouTube",
openTag: "[",
closeTag: "]",
separator: "|",
toProps: (args) => {
if (args.length > 0) {
return { src: args[0] };
}
return { src: "" };
},
toArgs: ({ src }) => {
return [src];
},
control: ({ src, onChange }) => {
return (
{
onChange({ src: event.target.value });
}}
/>
VIDEO
);
},
preview: ({ src }) => {
return (
VIDEO
);
},
});