Expose methods for installations to create custom preview components
This commit is contained in:
parent
c51f42658e
commit
8d63ff0a88
@ -68,6 +68,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bricks.js": "^1.7.0",
|
"bricks.js": "^1.7.0",
|
||||||
"fuzzy": "^0.1.1",
|
"fuzzy": "^0.1.1",
|
||||||
|
"html-to-react": "^1.0.0",
|
||||||
"js-base64": "^2.1.9",
|
"js-base64": "^2.1.9",
|
||||||
"json-loader": "^0.5.4",
|
"json-loader": "^0.5.4",
|
||||||
"localforage": "^1.4.2",
|
"localforage": "^1.4.2",
|
||||||
|
@ -6,14 +6,17 @@ export default class ControlPane extends React.Component {
|
|||||||
controlFor(field) {
|
controlFor(field) {
|
||||||
const { entry, getMedia, onChange, onAddMedia, onRemoveMedia } = this.props;
|
const { entry, getMedia, onChange, onAddMedia, onRemoveMedia } = this.props;
|
||||||
const widget = Widgets[field.get('widget')] || Widgets._unknown;
|
const widget = Widgets[field.get('widget')] || Widgets._unknown;
|
||||||
return React.createElement(widget.Control, {
|
return <div className="cms-control">
|
||||||
|
<label>{ field.get('label') }</label>
|
||||||
|
{React.createElement(widget.Control, {
|
||||||
field: field,
|
field: field,
|
||||||
value: entry.getIn(['data', field.get('name')]),
|
value: entry.getIn(['data', field.get('name')]),
|
||||||
onChange: (value) => onChange(entry.setIn(['data', field.get('name')], value)),
|
onChange: (value) => onChange(entry.setIn(['data', field.get('name')], value)),
|
||||||
onAddMedia: onAddMedia,
|
onAddMedia: onAddMedia,
|
||||||
onRemoveMedia: onRemoveMedia,
|
onRemoveMedia: onRemoveMedia,
|
||||||
getMedia: getMedia
|
getMedia: getMedia
|
||||||
});
|
})}
|
||||||
|
</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
6
src/components/PreviewPane.css
Normal file
6
src/components/PreviewPane.css
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
.frame {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
border: none;
|
||||||
|
background: #fff;
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
import React, { PropTypes } from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import { render } from 'react-dom';
|
import { render } from 'react-dom';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
|
import { getPreviewTemplate, getPreviewStyles } from '../lib/registry';
|
||||||
import Widgets from './Widgets';
|
import Widgets from './Widgets';
|
||||||
|
import styles from './PreviewPane.css';
|
||||||
|
|
||||||
class Preview extends React.Component {
|
class Preview extends React.Component {
|
||||||
previewFor(field) {
|
previewFor(field) {
|
||||||
@ -24,23 +26,49 @@ class Preview extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Preview.propTypes = {
|
||||||
|
collection: ImmutablePropTypes.map.isRequired,
|
||||||
|
entry: ImmutablePropTypes.map.isRequired,
|
||||||
|
getMedia: PropTypes.func.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
export default class PreviewPane extends React.Component {
|
export default class PreviewPane extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.handleIframeRef = this.handleIframeRef.bind(this);
|
this.handleIframeRef = this.handleIframeRef.bind(this);
|
||||||
|
this.widgetFor = this.widgetFor.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate() {
|
componentDidUpdate() {
|
||||||
this.renderPreview();
|
this.renderPreview();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
widgetFor(name) {
|
||||||
|
const { collection, entry, getMedia } = this.props;
|
||||||
|
const field = collection.get('fields').find((field) => field.get('name') === name);
|
||||||
|
const widget = Widgets[field.get('widget')] || Widgets._unknown;
|
||||||
|
return React.createElement(widget.Preview, {
|
||||||
|
field: field,
|
||||||
|
value: entry.getIn(['data', field.get('name')]),
|
||||||
|
getMedia: getMedia,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
renderPreview() {
|
renderPreview() {
|
||||||
const props = this.props;
|
const props = Object.assign({}, this.props, {widgetFor: this.widgetFor});
|
||||||
render(<Preview {...props}/>, this.previewEl);
|
const component = getPreviewTemplate(props.collection.get('name')) || Preview;
|
||||||
|
|
||||||
|
render(React.createElement(component, props), this.previewEl);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleIframeRef(ref) {
|
handleIframeRef(ref) {
|
||||||
if (ref) {
|
if (ref) {
|
||||||
|
getPreviewStyles().forEach((style) => {
|
||||||
|
const linkEl = document.createElement('link');
|
||||||
|
linkEl.setAttribute('rel', 'stylesheet');
|
||||||
|
linkEl.setAttribute('href', style);
|
||||||
|
ref.contentDocument.head.appendChild(linkEl);
|
||||||
|
});
|
||||||
this.previewEl = document.createElement('div');
|
this.previewEl = document.createElement('div');
|
||||||
ref.contentDocument.body.appendChild(this.previewEl);
|
ref.contentDocument.body.appendChild(this.previewEl);
|
||||||
this.renderPreview();
|
this.renderPreview();
|
||||||
@ -51,7 +79,7 @@ export default class PreviewPane extends React.Component {
|
|||||||
const { collection } = this.props;
|
const { collection } = this.props;
|
||||||
if (!collection) { return null; }
|
if (!collection) { return null; }
|
||||||
|
|
||||||
return <iframe style={{width: "100%", height: "100%", border: "none"}} ref={this.handleIframeRef}></iframe>
|
return <iframe className={styles.frame} ref={this.handleIframeRef}></iframe>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +42,7 @@ class VisualEditor extends React.Component {
|
|||||||
let rawJson;
|
let rawJson;
|
||||||
if (props.value !== undefined) {
|
if (props.value !== undefined) {
|
||||||
const content = this.markdown.toContent(props.value);
|
const content = this.markdown.toContent(props.value);
|
||||||
|
console.log('md: %o', content);
|
||||||
rawJson = SlateUtils.encode(content, null, ['mediaproxy'].concat(getPlugins().map(plugin => plugin.id)));
|
rawJson = SlateUtils.encode(content, null, ['mediaproxy'].concat(getPlugins().map(plugin => plugin.id)));
|
||||||
} else {
|
} else {
|
||||||
rawJson = emptyParagraphBlock;
|
rawJson = emptyParagraphBlock;
|
||||||
|
@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
import { render } from 'react-dom';
|
import { render } from 'react-dom';
|
||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
import { Router } from 'react-router';
|
import { Router } from 'react-router';
|
||||||
|
import { registerPreviewStyle,registerPreviewTemplate } from './lib/registry';
|
||||||
import configureStore from './store/configureStore';
|
import configureStore from './store/configureStore';
|
||||||
import routes from './routing/routes';
|
import routes from './routing/routes';
|
||||||
import history, { syncHistory } from './routing/history';
|
import history, { syncHistory } from './routing/history';
|
||||||
@ -29,3 +30,8 @@ render((
|
|||||||
</Plugin>
|
</Plugin>
|
||||||
</Provider>
|
</Provider>
|
||||||
), el);
|
), el);
|
||||||
|
|
||||||
|
window.CMS = {
|
||||||
|
registerPreviewStyle: registerPreviewStyle,
|
||||||
|
registerPreviewTemplate: registerPreviewTemplate
|
||||||
|
};
|
||||||
|
20
src/lib/registry.js
Normal file
20
src/lib/registry.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
const registry = {
|
||||||
|
templates: {},
|
||||||
|
previewStyles: []
|
||||||
|
};
|
||||||
|
|
||||||
|
export function registerPreviewStyle(style) {
|
||||||
|
registry.previewStyles.push(style);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerPreviewTemplate(name, component) {
|
||||||
|
registry.templates[name] = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPreviewTemplate(name) {
|
||||||
|
return registry.templates[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPreviewStyles() {
|
||||||
|
return registry.previewStyles;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user