import React, { PropTypes } from 'react'; import { List, Map } from 'immutable'; import ImmutablePropTypes from 'react-immutable-proptypes'; import Frame from 'react-frame-component'; import { ScrollSyncPane } from '../ScrollSync'; import registry from '../../lib/registry'; import { resolveWidget } from '../Widgets'; import { selectTemplateName, selectInferedField } from '../../reducers/collections'; import { INFERABLE_FIELDS } from '../../constants/fieldInference'; import Preview from './Preview'; import styles from './PreviewPane.css'; export default class PreviewPane extends React.Component { getWidget = (field, value, props) => { const { fieldsMetaData, getAsset, entry } = props; const widget = resolveWidget(field.get('widget')); return !widget.preview ? null : React.createElement(widget.preview, { field, key: field.get('name'), value: value && Map.isMap(value) ? value.get(field.get('name')) : value, metadata: fieldsMetaData && fieldsMetaData.get(field.get('name')), getAsset, entry, fieldsMetaData, }); }; inferedFields = {}; inferFields() { const titleField = selectInferedField(this.props.collection, 'title'); const shortTitleField = selectInferedField(this.props.collection, 'shortTitle'); const authorField = selectInferedField(this.props.collection, 'author'); this.inferedFields = {}; if (titleField) this.inferedFields[titleField] = INFERABLE_FIELDS.title; if (shortTitleField) this.inferedFields[shortTitleField] = INFERABLE_FIELDS.shortTitle; if (authorField) this.inferedFields[authorField] = INFERABLE_FIELDS.author; } /** * Returns the widget component for a named field, and makes recursive calls * to retrieve components for nested and deeply nested fields, which occur in * object and list type fields. Used internally to retrieve widgets, and also * exposed for use in custom preview templates. */ widgetFor = (name, fields = this.props.fields, values = this.props.entry.get('data')) => { // We retrieve the field by name so that this function can also be used in // custom preview templates, where the field object can't be passed in. let field = fields && fields.find(f => f.get('name') === name); let value = values && values.get(field.get('name')); let nestedFields = field.get('fields'); if (nestedFields) { field = field.set('fields', this.getNestedWidgets(nestedFields, value)); } const labelledWidgets = ['string', 'text', 'number']; if (Object.keys(this.inferedFields).indexOf(name) !== -1) { value = this.inferedFields[name].defaultPreview(value); } else if (value && labelledWidgets.indexOf(field.get('widget')) !== -1 && value.toString().length < 50) { value =