static-cms/src/components/PreviewPane.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
2016-09-09 17:31:59 +02:00
import { render } from 'react-dom';
import ImmutablePropTypes from 'react-immutable-proptypes';
2016-05-30 16:55:32 -07:00
import Widgets from './Widgets';
2016-09-09 17:31:59 +02:00
class Preview extends React.Component {
2016-05-30 16:55:32 -07:00
previewFor(field) {
2016-06-10 00:16:01 -03:00
const { entry, getMedia } = this.props;
2016-05-30 16:55:32 -07:00
const widget = Widgets[field.get('widget')] || Widgets._unknown;
return React.createElement(widget.Preview, {
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;
if (!collection) { return null; }
return <div>
{collection.get('fields').map((field) => <div key={field.get('name')}>{this.previewFor(field)}</div>)}
2016-05-30 16:55:32 -07:00
</div>;
}
}
2016-09-09 17:31:59 +02:00
export default class PreviewPane extends React.Component {
constructor(props) {
super(props);
this.handleIframeRef = this.handleIframeRef.bind(this);
}
componentDidUpdate() {
this.renderPreview();
}
renderPreview() {
const props = this.props;
render(<Preview {...props}/>, this.previewEl);
}
handleIframeRef(ref) {
if (ref) {
this.previewEl = document.createElement('div');
ref.contentDocument.body.appendChild(this.previewEl);
this.renderPreview();
}
}
render() {
const { collection } = this.props;
if (!collection) { return null; }
return <iframe style={{width: "100%", height: "100%", border: "none"}} ref={this.handleIframeRef}></iframe>
}
}
PreviewPane.propTypes = {
collection: ImmutablePropTypes.map.isRequired,
entry: ImmutablePropTypes.map.isRequired,
getMedia: PropTypes.func.isRequired,
};