UNPKG

@20minutes/draft-convert

Version:

Extensibly serialize & deserialize Draft.js ContentState

102 lines (101 loc) 5.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); const _draftjs = /*#__PURE__*/ _interop_require_default(require("draft-js")); const _react = /*#__PURE__*/ _interop_require_default(require("react")); const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server")); const _tinyinvariant = /*#__PURE__*/ _interop_require_default(require("tiny-invariant")); const _blockEntities = /*#__PURE__*/ _interop_require_default(require("./blockEntities.js")); const _blockInlineStyles = /*#__PURE__*/ _interop_require_default(require("./blockInlineStyles.js")); const _defaultBlockHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultBlockHTML.js")); const _encodeBlock = /*#__PURE__*/ _interop_require_default(require("./encodeBlock.js")); const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction.js")); const _blockTypeObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/blockTypeObjectFunction.js")); const _getBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getBlockTags.js")); const _getNestedBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getNestedBlockTags.js")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const { convertToRaw } = _draftjs.default; const defaultEntityToHTML = (_entity, originalText)=>originalText; const defaultValidateHTML = (_html)=>true; const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{ (0, _tinyinvariant.default)(contentState !== null && contentState !== undefined, 'Expected contentState to be non-null'); let getBlockHTML; if (blockToHTML.__isMiddleware === true) { getBlockHTML = blockToHTML((0, _blockTypeObjectFunction.default)(_defaultBlockHTML.default)); } else { getBlockHTML = (0, _accumulateFunction.default)((0, _blockTypeObjectFunction.default)(blockToHTML), (0, _blockTypeObjectFunction.default)(_defaultBlockHTML.default)); } const rawState = convertToRaw(contentState); let listStack = []; let result = rawState.blocks.map((block)=>{ const { type, depth } = block; let closeNestTags = ''; let openNestTags = ''; const blockHTMLResult = getBlockHTML(block); if (!blockHTMLResult) { throw new Error(`convertToHTML: missing HTML definition for block with type ${block.type}`); } if (!blockHTMLResult.nest) { // this block can't be nested, so reset all nesting if necessary closeNestTags = listStack.reduceRight((string, nestedBlock)=>string + (0, _getNestedBlockTags.default)(getBlockHTML(nestedBlock), depth).nestEnd, ''); listStack = []; } else { while(depth + 1 !== listStack.length || type !== listStack[depth].type){ if (depth + 1 === listStack.length) { // depth is right but doesn't match type const blockToClose = listStack[depth]; closeNestTags += (0, _getNestedBlockTags.default)(getBlockHTML(blockToClose), depth).nestEnd; openNestTags += (0, _getNestedBlockTags.default)(getBlockHTML(block), depth).nestStart; listStack[depth] = block; } else if (depth + 1 < listStack.length) { const blockToClose = listStack[listStack.length - 1]; closeNestTags += (0, _getNestedBlockTags.default)(getBlockHTML(blockToClose), depth).nestEnd; listStack = listStack.slice(0, -1); } else { openNestTags += (0, _getNestedBlockTags.default)(getBlockHTML(block), depth).nestStart; listStack.push(block); } } } const innerHTML = (0, _blockInlineStyles.default)((0, _blockEntities.default)((0, _encodeBlock.default)(block), rawState.entityMap, entityToHTML), styleToHTML); const blockHTML = (0, _getBlockTags.default)(getBlockHTML(block)); let html; if (typeof blockHTML === 'string') { html = blockHTML; } else { html = blockHTML.start + innerHTML + blockHTML.end; } if (innerHTML.length === 0 && Object.hasOwn(blockHTML, 'empty')) { if (/*#__PURE__*/ _react.default.isValidElement(blockHTML.empty)) { html = _server.default.renderToStaticMarkup(blockHTML.empty); } else { html = blockHTML.empty; } } const finalHtml = closeNestTags + openNestTags + html; if (!validateHTML(finalHtml)) { return ''; } return finalHtml; }).join(''); result = listStack.reduce((res, nestBlock)=>res + (0, _getNestedBlockTags.default)(getBlockHTML(nestBlock), nestBlock.depth).nestEnd, result); return result; }; const _default = (...args)=>{ if (args.length === 1 && Object.hasOwn(args[0], '_map') && args[0].getBlockMap != null) { // skip higher-order function and use defaults return convertToHTML({})(...args); } return convertToHTML(...args); };