2018-09-06 14:44:58 -04:00
|
|
|
import React from 'react';
|
2020-02-04 09:16:48 +02:00
|
|
|
import CMS from 'netlify-cms-app';
|
2018-09-06 14:44:58 -04:00
|
|
|
import dayjs from 'dayjs';
|
2018-10-01 20:00:57 -04:00
|
|
|
import Prism from 'prismjs';
|
2020-02-19 16:55:10 -05:00
|
|
|
import { CacheProvider } from '@emotion/core';
|
|
|
|
import createCache from '@emotion/cache';
|
2018-09-06 14:44:58 -04:00
|
|
|
import { BlogPostTemplate } from '../templates/blog-post';
|
2020-02-19 16:55:10 -05:00
|
|
|
import { LayoutTemplate as Layout } from '../components/layout';
|
2018-09-06 14:44:58 -04:00
|
|
|
import { DocsTemplate } from '../templates/doc-page';
|
2018-10-01 20:00:57 -04:00
|
|
|
import WidgetDoc from '../components/widget-doc';
|
2018-09-06 14:44:58 -04:00
|
|
|
import WhatsNew from '../components/whats-new';
|
|
|
|
import Notification from '../components/notification';
|
2019-06-26 15:52:47 -04:00
|
|
|
import Community from '../components/community';
|
2020-02-19 16:55:10 -05:00
|
|
|
import siteConfig from '../../site.yml';
|
2018-09-06 14:44:58 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
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;
|
|
|
|
}
|
2018-10-01 20:00:57 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
const PreviewContainer = ({ children, highlight }) => (
|
|
|
|
<CacheProvider value={getEmotionCache()}>
|
2020-02-20 16:38:34 +01:00
|
|
|
<Layout>{highlight ? <Highlight>{children}</Highlight> : children}</Layout>
|
2020-02-19 16:55:10 -05:00
|
|
|
</CacheProvider>
|
|
|
|
);
|
2018-10-01 20:00:57 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
class Highlight extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.ref = React.createRef();
|
|
|
|
}
|
2018-10-01 20:00:57 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
highlight() {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (this.ref.current) {
|
|
|
|
Prism.highlightAllUnder(this.ref.current);
|
|
|
|
}
|
2020-02-20 16:38:34 +01:00
|
|
|
});
|
2020-02-19 16:55:10 -05:00
|
|
|
}
|
2018-10-01 20:00:57 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
componentDidMount() {
|
|
|
|
this.highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-02-20 16:38:34 +01:00
|
|
|
return <div ref={this.ref}>{this.props.children}</div>;
|
2020-02-19 16:55:10 -05:00
|
|
|
}
|
2020-02-20 16:38:34 +01:00
|
|
|
}
|
2018-10-01 20:00:57 -04:00
|
|
|
|
2018-09-06 14:44:58 -04:00
|
|
|
const BlogPostPreview = ({ entry, widgetFor }) => {
|
|
|
|
const data = entry.get('data');
|
|
|
|
return (
|
2020-02-19 16:55:10 -05:00
|
|
|
<PreviewContainer highlight={true}>
|
|
|
|
<BlogPostTemplate
|
|
|
|
title={data.get('title')}
|
|
|
|
author={data.get('author')}
|
|
|
|
date={dayjs(data.get('date')).format('MMMM D, YYYY')}
|
|
|
|
body={widgetFor('body')}
|
|
|
|
/>
|
|
|
|
</PreviewContainer>
|
2018-09-06 14:44:58 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-26 15:52:47 -04:00
|
|
|
const CommunityPreview = ({ entry }) => {
|
|
|
|
const { title, headline, subhead, sections } = entry.get('data').toJS();
|
2020-02-19 16:55:10 -05:00
|
|
|
return (
|
|
|
|
<PreviewContainer>
|
|
|
|
<Community title={title} headline={headline} subhead={subhead} sections={sections} />
|
|
|
|
</PreviewContainer>
|
|
|
|
);
|
2019-06-26 15:52:47 -04:00
|
|
|
};
|
|
|
|
|
2018-09-06 14:44:58 -04:00
|
|
|
const DocsPreview = ({ entry, widgetFor }) => (
|
2020-02-19 16:55:10 -05:00
|
|
|
<PreviewContainer highlight={true}>
|
|
|
|
<DocsTemplate title={entry.getIn(['data', 'title'])} body={widgetFor('body')} />
|
|
|
|
</PreviewContainer>
|
2018-09-06 14:44:58 -04:00
|
|
|
);
|
|
|
|
|
2018-10-01 20:00:57 -04:00
|
|
|
const WidgetDocPreview = ({ entry, widgetFor }) => (
|
2020-02-19 16:55:10 -05:00
|
|
|
<PreviewContainer highlight={true}>
|
|
|
|
<WidgetDoc visible={true} label={entry.get('label')} body={widgetFor('body')} />
|
|
|
|
</PreviewContainer>
|
2018-10-01 20:00:57 -04:00
|
|
|
);
|
|
|
|
|
2018-09-06 14:44:58 -04:00
|
|
|
const ReleasePreview = ({ entry }) => (
|
2020-02-19 16:55:10 -05:00
|
|
|
<PreviewContainer highlight={true}>
|
|
|
|
<WhatsNew
|
|
|
|
updates={entry
|
|
|
|
.getIn(['data', 'updates'])
|
|
|
|
.map(release => ({
|
|
|
|
version: release.get('version'),
|
|
|
|
date: dayjs(release.get('date')).format('MMMM D, YYYY'),
|
|
|
|
description: release.get('description'),
|
|
|
|
}))
|
|
|
|
.toJS()}
|
|
|
|
/>
|
|
|
|
</PreviewContainer>
|
2018-09-06 14:44:58 -04:00
|
|
|
);
|
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
const NotificationPreview = ({ entry }) => (
|
|
|
|
<PreviewContainer>
|
|
|
|
{entry
|
|
|
|
.getIn(['data', 'notifications'])
|
|
|
|
.filter(notif => notif.get('published'))
|
|
|
|
.map((notif, idx) => (
|
|
|
|
<Notification key={idx} url={notif.get('url')} loud={notif.get('loud')}>
|
|
|
|
{notif.get('message')}
|
|
|
|
</Notification>
|
2020-02-20 16:38:34 +01:00
|
|
|
))}
|
2020-02-19 16:55:10 -05:00
|
|
|
</PreviewContainer>
|
|
|
|
);
|
2018-09-06 14:44:58 -04:00
|
|
|
|
2020-02-19 16:55:10 -05:00
|
|
|
CMS.registerPreviewTemplate('blog', BlogPostPreview);
|
|
|
|
siteConfig.menu.docs.forEach(group => {
|
|
|
|
CMS.registerPreviewTemplate(`docs_${group.name}`, DocsPreview);
|
|
|
|
});
|
|
|
|
CMS.registerPreviewTemplate('widget_docs', WidgetDocPreview);
|
2018-09-06 14:44:58 -04:00
|
|
|
CMS.registerPreviewTemplate('releases', ReleasePreview);
|
|
|
|
CMS.registerPreviewTemplate('notifications', NotificationPreview);
|
2019-06-26 15:52:47 -04:00
|
|
|
CMS.registerPreviewTemplate('community', CommunityPreview);
|