draft-convert-greger
Version:
Extensibly serialize & deserialize Draft.js ContentState
31 lines (24 loc) • 1.17 kB
JavaScript
import invariant from 'invariant';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import splitReactElement from './splitReactElement';
function hasChildren(element) {
return React.isValidElement(element) && React.Children.count(element.props.children) > 0;
}
export default function getBlockTags(blockHTML, noChild) {
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
if (typeof blockHTML === 'string') {
return blockHTML;
}
if (React.isValidElement(blockHTML)) {
if (hasChildren(blockHTML) || noChild) {
return ReactDOMServer.renderToStaticMarkup(blockHTML);
}
return splitReactElement(blockHTML);
}
if (Object.prototype.hasOwnProperty.call(blockHTML, 'element') && React.isValidElement(blockHTML.element)) {
return Object.assign({}, blockHTML, splitReactElement(blockHTML.element));
}
invariant(Object.prototype.hasOwnProperty.call(blockHTML, 'start') && Object.prototype.hasOwnProperty.call(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
return blockHTML;
}