所以我一直在尝试获取convertFromHTML
将图像转换为原子块的方法,以便它可以与 兼容,draft-js-image-plugin
因为它需要类型的块atomic
给定一个简单的 HTML 结构,其中包含一些文本和开箱即用的图像,convertFromHTML
会产生以下内容contentState
:
{
"blocks": [
{
"key": "82k8",
"text": "‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
},
{
"key": "9jbor",
"text": "On December 29, 2020, 5:20 PM EST txwbi.nrjrtn@gmail.com wrote:",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [{ "offset": 34, "length": 23, "key": 0 }],
"data": {}
},
{
"key": "anq8o",
"text": "A bunch of text here to test out the body",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [
{ "offset": 3, "length": 13, "style": "ITALIC" },
{ "offset": 3, "length": 13, "style": "UNDERLINE" },
{ "offset": 38, "length": 4, "style": "BOLD" }
],
"entityRanges": [{ "offset": 0, "length": 1, "key": 1 }],
"data": {}
}
],
"entityMap": {
"0": {
"type": "LINK",
"mutability": "MUTABLE",
"data": {
"href": "mailto:txwbi.nrjrtn@gmail.com",
"rel": "noreferrer nofollow noopener",
"target": "_blank",
"url": "mailto:txwbi.nrjrtn@gmail.com"
}
},
"1": {
"type": "IMAGE",
"mutability": "IMMUTABLE",
"data": {
"alt": "cory_emoji.png",
"height": "210",
"src": "data:image/png;base64, ...BASE64ENCODEDIMAGE WOULD BE HERE I REMOVED BECAUSE OF CHAR LIMITS",
"width": "173"
}
}
}
}
我们可以看到img
标签占据了一个unstyled
我不想要的块。因此,我创建了以下函数来扩展默认块渲染映射以考虑img
标签:
const {
EditorState,
convertToRaw,
DefaultDraftBlockRenderMap,
ContentState,
convertFromHTML,
getSafeBodyFromHTML
} = require('draft-js');
const Immutable = require('immutable');
module.exports.editorStateFromHTML = htmlBody => {
console.log('HTML ---> EDITOR ::: RAW BODY', htmlBody);
const blockRenderMap = Immutable.Map({
atomic: {
element: 'figure',
aliasedElements: ['img']
}
});
const extendedBlockRenderMap = DefaultDraftBlockRenderMap.merge(
blockRenderMap
);
const blocksFromHTML = convertFromHTML(
htmlBody,
getSafeBodyFromHTML,
extendedBlockRenderMap
);
console.log(blocksFromHTML);
const state = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap
);
console.log(JSON.stringify(convertToRaw(state)));
const newEditor = EditorState.createWithContent(state);
return newEditor;
};
导致此内容状态:
{
"blocks": [
{
"key": "fhgqd",
"text": "‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
},
{
"key": "2nsnk",
"text": "On December 29, 2020, 5:20 PM EST txwbi.nrjrtn@gmail.com wrote:",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [{ "offset": 34, "length": 23, "key": 0 }],
"data": {}
},
{
"key": "7c7cu",
"text": "A bunch of text here to test out the body",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [
{ "offset": 2, "length": 13, "style": "UNDERLINE" },
{ "offset": 2, "length": 13, "style": "ITALIC" },
{ "offset": 37, "length": 4, "style": "BOLD" }
],
"entityRanges": [],
"data": {}
},
{
"key": "f84vb",
"text": "",
"type": "atomic",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
}
],
"entityMap": {
"0": {
"type": "LINK",
"mutability": "MUTABLE",
"data": {
"href": "mailto:txwbi.nrjrtn@gmail.com",
"rel": "noreferrer nofollow noopener",
"target": "_blank",
"url": "mailto:txwbi.nrjrtn@gmail.com"
}
}
}
}
如您所见,entityMap现在只有一个键,并且图像的所有数据都不再存在。如何在 entityMap 中img
创建实体的同时使标签成为原子块???IMAGE