UNPKG

@20minutes/draft-convert

Version:

Extensibly serialize & deserialize Draft.js ContentState

28 lines (27 loc) 1.2 kB
import React from 'react'; import ReactDOMServer from 'react-dom/server'; import invariant from 'tiny-invariant'; import splitReactElement from './splitReactElement.mjs'; function hasChildren(element) { return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0; } export default function getBlockTags(blockHTML) { invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null'); if (typeof blockHTML === 'string') { return blockHTML; } if (/*#__PURE__*/ React.isValidElement(blockHTML)) { if (hasChildren(blockHTML)) { return ReactDOMServer.renderToStaticMarkup(blockHTML); } return splitReactElement(blockHTML); } if (Object.hasOwn(blockHTML, 'element') && /*#__PURE__*/ React.isValidElement(blockHTML.element)) { return { ...blockHTML, ...splitReactElement(blockHTML.element) }; } invariant(Object.hasOwn(blockHTML, 'start') && Object.hasOwn(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags'); return blockHTML; }