import React from 'react';
import CMS from 'netlify-cms-app';
import dayjs from 'dayjs';
import Prism from 'prismjs';
import { CacheProvider } from '@emotion/core';
import createCache from '@emotion/cache';
import { BlogPostTemplate } from '../templates/blog-post';
import { LayoutTemplate as Layout } from '../components/layout';
import { DocsTemplate } from '../templates/doc-page';
import WidgetDoc from '../components/widget-doc';
import WhatsNew from '../components/whats-new';
import Notification from '../components/notification';
import Community from '../components/community';
import siteConfig from '../../site.yml';
let emotionCache;
function getEmotionCache() {
const previewPaneIframe = document.querySelector('iframe[class*="PreviewPaneFrame"]');
const previewPaneHeadEl = previewPaneIframe.contentWindow.document.querySelector('head');
if (!emotionCache || emotionCache.sheet.container !== previewPaneHeadEl) {
emotionCache = createCache({ container: previewPaneHeadEl });
}
return emotionCache;
}
const PreviewContainer = ({ children, highlight }) => (
{highlight ? {children} : children}
);
class Highlight extends React.Component {
constructor(props) {
super(props);
this.ref = React.createRef();
}
highlight() {
setTimeout(() => {
if (this.ref.current) {
Prism.highlightAllUnder(this.ref.current);
}
});
}
componentDidMount() {
this.highlight();
}
componentDidUpdate() {
this.highlight();
}
render() {
return
{this.props.children}
;
}
}
const BlogPostPreview = ({ entry, widgetFor }) => {
const data = entry.get('data');
return (
);
};
const CommunityPreview = ({ entry }) => {
const { title, headline, subhead, sections } = entry.get('data').toJS();
return (
);
};
const DocsPreview = ({ entry, widgetFor }) => (
);
const WidgetDocPreview = ({ entry, widgetFor }) => (
);
const ReleasePreview = ({ entry }) => (
({
version: release.get('version'),
date: dayjs(release.get('date')).format('MMMM D, YYYY'),
description: release.get('description'),
}))
.toJS()}
/>
);
const NotificationPreview = ({ entry }) => (
{entry
.getIn(['data', 'notifications'])
.filter(notif => notif.get('published'))
.map((notif, idx) => (
{notif.get('message')}
))}
);
CMS.registerPreviewTemplate('blog', BlogPostPreview);
siteConfig.menu.docs.forEach(group => {
CMS.registerPreviewTemplate(`docs_${group.name}`, DocsPreview);
});
CMS.registerPreviewTemplate('widget_docs', WidgetDocPreview);
CMS.registerPreviewTemplate('releases', ReleasePreview);
CMS.registerPreviewTemplate('notifications', NotificationPreview);
CMS.registerPreviewTemplate('community', CommunityPreview);