2016-06-16 19:20:36 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-09-09 17:31:59 +02:00
|
|
|
import { render } from 'react-dom';
|
2016-06-16 19:20:36 -03:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-09-11 23:08:18 +02:00
|
|
|
import registry from '../lib/registry';
|
|
|
|
import { resolveWidget } from './Widgets';
|
2016-09-11 17:53:44 +02:00
|
|
|
import styles from './PreviewPane.css';
|
2016-05-30 16:55:32 -07:00
|
|
|
|
2016-09-09 17:31:59 +02:00
|
|
|
class Preview extends React.Component {
|
2016-09-29 22:17:29 +02:00
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
previewFor(field) {
|
2016-06-10 00:16:01 -03:00
|
|
|
const { entry, getMedia } = this.props;
|
2016-09-11 23:08:18 +02:00
|
|
|
const widget = resolveWidget(field.get('widget'));
|
|
|
|
return React.createElement(widget.preview, {
|
2016-05-30 16:55:32 -07:00
|
|
|
field: field,
|
2016-06-10 00:16:01 -03:00
|
|
|
value: entry.getIn(['data', field.get('name')]),
|
|
|
|
getMedia: getMedia,
|
2016-05-30 16:55:32 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { collection } = this.props;
|
2016-09-29 22:17:29 +02:00
|
|
|
if (!collection) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-30 16:55:32 -07:00
|
|
|
|
|
|
|
return <div>
|
2016-09-29 22:17:29 +02:00
|
|
|
{
|
|
|
|
collection.get('fields').map(field => (
|
|
|
|
<div
|
|
|
|
key={field.get('name')}
|
|
|
|
>
|
|
|
|
{this.previewFor(field)}
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
}
|
2016-05-30 16:55:32 -07:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 19:20:36 -03:00
|
|
|
|
2016-09-11 17:53:44 +02:00
|
|
|
Preview.propTypes = {
|
|
|
|
collection: ImmutablePropTypes.map.isRequired,
|
|
|
|
entry: ImmutablePropTypes.map.isRequired,
|
|
|
|
getMedia: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2016-09-09 17:31:59 +02:00
|
|
|
export default class PreviewPane extends React.Component {
|
|
|
|
|
2016-09-29 22:17:29 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
// Update scroll position of the iframe
|
|
|
|
const { scrollTop, scrollHeight, offsetHeight, ...rest } = this.props;
|
|
|
|
const frameHeight = this.iframeBody.scrollHeight - offsetHeight;
|
|
|
|
this.iframeBody.scrollTop = frameHeight * scrollTop / (scrollHeight - offsetHeight);
|
|
|
|
|
|
|
|
// We don't want to re-render on scroll
|
|
|
|
if (prevProps.collection !== this.props.collection || prevProps.entry !== this.props.entry) {
|
|
|
|
this.renderPreview(rest);
|
|
|
|
}
|
2016-09-09 17:31:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 22:17:29 +02:00
|
|
|
widgetFor = name => {
|
2016-09-11 17:53:44 +02:00
|
|
|
const { collection, entry, getMedia } = this.props;
|
2016-09-29 22:17:29 +02:00
|
|
|
const field = collection.get('fields').find((field) => field.get('name') === name);
|
2016-09-11 23:08:18 +02:00
|
|
|
const widget = resolveWidget(field.get('widget'));
|
|
|
|
return React.createElement(widget.preview, {
|
2016-09-11 17:53:44 +02:00
|
|
|
field: field,
|
|
|
|
value: entry.getIn(['data', field.get('name')]),
|
|
|
|
getMedia: getMedia,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:17:29 +02:00
|
|
|
renderPreview(props) {
|
2016-09-11 23:08:18 +02:00
|
|
|
const component = registry.getPreviewTemplate(props.collection.get('name')) || Preview;
|
2016-09-29 22:17:29 +02:00
|
|
|
const previewProps = {
|
|
|
|
...props,
|
|
|
|
widgetFor: this.widgetFor
|
|
|
|
};
|
|
|
|
render(React.createElement(component, previewProps), this.previewEl);
|
2016-09-09 17:31:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 22:17:29 +02:00
|
|
|
handleIframeRef = ref => {
|
2016-09-09 17:31:59 +02:00
|
|
|
if (ref) {
|
2016-09-11 23:08:18 +02:00
|
|
|
registry.getPreviewStyles().forEach((style) => {
|
2016-09-11 17:53:44 +02:00
|
|
|
const linkEl = document.createElement('link');
|
|
|
|
linkEl.setAttribute('rel', 'stylesheet');
|
|
|
|
linkEl.setAttribute('href', style);
|
|
|
|
ref.contentDocument.head.appendChild(linkEl);
|
|
|
|
});
|
2016-09-09 17:31:59 +02:00
|
|
|
this.previewEl = document.createElement('div');
|
2016-09-29 22:17:29 +02:00
|
|
|
this.iframeBody = ref.contentDocument.body;
|
|
|
|
this.iframeBody.appendChild(this.previewEl);
|
|
|
|
this.renderPreview(this.props);
|
2016-09-09 17:31:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { collection } = this.props;
|
2016-09-29 22:17:29 +02:00
|
|
|
if (!collection) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-09 17:31:59 +02:00
|
|
|
|
2016-09-11 23:08:18 +02:00
|
|
|
return <iframe className={styles.frame} ref={this.handleIframeRef}></iframe>;
|
2016-09-09 17:31:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:20:36 -03:00
|
|
|
PreviewPane.propTypes = {
|
|
|
|
collection: ImmutablePropTypes.map.isRequired,
|
|
|
|
entry: ImmutablePropTypes.map.isRequired,
|
|
|
|
getMedia: PropTypes.func.isRequired,
|
2016-09-29 22:17:29 +02:00
|
|
|
scrollTop: PropTypes.number,
|
|
|
|
scrollHeight: PropTypes.number,
|
|
|
|
offsetHeight: PropTypes.number,
|
2016-06-16 19:20:36 -03:00
|
|
|
};
|