2016-08-18 09:09:54 -03:00
|
|
|
import { Component, PropTypes, Children } from 'react';
|
|
|
|
import { List, Record } from 'immutable';
|
|
|
|
import _ from 'lodash';
|
2016-08-17 09:52:17 -03:00
|
|
|
|
2016-08-18 09:09:54 -03:00
|
|
|
const plugins = { editor: List() };
|
2016-08-17 09:52:17 -03:00
|
|
|
|
2016-08-18 09:09:54 -03:00
|
|
|
const catchesNothing = /.^/;
|
|
|
|
const EditorComponent = Record({
|
|
|
|
id: null,
|
|
|
|
label: 'unnamed component',
|
|
|
|
icon: 'exclamation-triangle',
|
|
|
|
fields: [],
|
|
|
|
pattern: catchesNothing,
|
|
|
|
fromBlock: function(match) { return {}; },
|
|
|
|
toBlock: function(attributes) { return 'Plugin'; },
|
|
|
|
toPreview: function(attributes) { return 'Plugin'; }
|
|
|
|
});
|
2016-08-17 09:52:17 -03:00
|
|
|
|
2016-08-18 09:09:54 -03:00
|
|
|
|
|
|
|
class Plugin extends Component {
|
2016-10-03 14:33:48 +02:00
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.element.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
plugins: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2016-08-18 09:09:54 -03:00
|
|
|
getChildContext() {
|
|
|
|
return { plugins: plugins };
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return Children.only(this.props.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 11:14:21 +02:00
|
|
|
export function newEditorPlugin(config) {
|
|
|
|
const configObj = new EditorComponent({
|
|
|
|
id: config.id || config.label.replace(/[^A-Z0-9]+/ig, '_'),
|
|
|
|
label: config.label,
|
|
|
|
icon: config.icon,
|
|
|
|
fields: config.fields,
|
|
|
|
pattern: config.pattern,
|
|
|
|
fromBlock: _.isFunction(config.fromBlock) ? config.fromBlock.bind(null) : null,
|
|
|
|
toBlock: _.isFunction(config.toBlock) ? config.toBlock.bind(null) : null,
|
|
|
|
toPreview: _.isFunction(config.toPreview) ? config.toPreview.bind(null) : config.toBlock.bind(null)
|
|
|
|
});
|
2016-08-18 09:09:54 -03:00
|
|
|
|
2016-09-12 11:14:21 +02:00
|
|
|
|
|
|
|
return configObj;
|
|
|
|
}
|