[feature request]: convertFromHTML support custom entity.
See original GitHub issueDuring this loop, draft-js should create a LINK
typed entity if this child has href
attribute and has valid link text.
We would like to handle the condition and create a custom entity (e.g., convert <img />
tag to an IMAGE
entity ), just like :
while (child) {
if (this.props.handleParseHTMLElement
&& entity = this.props.handleParseHTMLElement(child)
) {
newEntityMap = addEntityToEntityMap(
newEntityMap,
entity
);
entityId = newEntityMap.keySeq().last();
} else {
// original logic...
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Bountysource
[feature request]: convertFromHTML support custom entity. ... Help and Information. Frequently Asked Questions · Bugs and Feature Requests · Fees ...
Read more >Does convertFromHTML support custom block types in Draft.js?
Yes it does. It's not well documented, but you need to pass the same blockRenderMap that you passed to the Editor , to...
Read more >Does convertFromHTML support custom block types in Draft.js?
convertToHtml is a custom function which returns convertFromHTML(html) let htmlTranscript = convertToHtml(paragraphsArray) // important part ...
Read more >Change Log | GreenArrow Studio
Fixed an issue in Studio's content editor where the “Convert from HTML” button for text ... Permit List-Help in custom headers in both...
Read more >Add Export to Word Support [#2733781] | Drupal.org
Entity Print. Version: 8.x-2.x-dev. Component: Code. Priority: Normal. Category: Feature request. Assigned: Unassigned. Issue tags:.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gbbr thanks for the heads up. Didn’t follow DraftJS for a while, good to know and get ready for API changes. Need to study API changes to give a good answer, but the quick answer to your question is (also used this approach in our code) following: don’t use
Entity
, but useContentBlock
’sdata
prop in combination with custom block type. So, for example, this part from mentioned gist:Should be replaced with this:
Of course, in
createContentBlock
you will also no longer needEntity
-related code. So, as you see we store parsed data indata
prop of theContentBlock
, which is part ofContentBlock
API, also we settype
to custom_type_name_it_whatever. And since we added custom type, we need to do 2 more things to get all working.blockRenderMap
Editor
’s prop with our custom typeblockRendererFn
Editor
’s prop which will know how to render our custom (in this case it’s<image />
) block with help ofReact
.It can be something like this:
And then you pass both custom
renderMap
andblockRendererFn
to DraftJS Editor:<Editor blockRenderMap={ updatedRenderMap } blockRendererFn={ blockRendererFn } />
This is very primitive example, but the general idea should be clear and should work with 0.11 as I didn’t mentioned any notification about changing
ContentBlock
API and editor component API. Otherwise that will have to be revised 😃@michelson, hard to say why Draft doesn’t handle
<video />
blocks in your case. It’s rather ‘special’ element whose children are not text nodes, maybe that causes Draft to fall back tounstyled
blocks. Never the less a dirty hack I used might be helpful in your case. It’s pretty dump, but it works. Please refer to this gist. I ‘transpiled’ it from our project code which is written incoffeescript
, so there can be typos/minor mistakes, just debug it in that case. It works for us - we convert images and custom tables (tables generated based on internal data with custom attributes) and it works fine.The idea is to collect all required attributes from target element, e.g.
src
attribute of<img />
element (which would enable us to render it later with React component) to JSON, then stringify it and save as simpletextContent
within some “temporary” text block element which is recognizable by Draft, in mentioned gist it is<blockquote />
. It has to be some element which you don’t normally use in your editor implementation to avoid collisions. I took<blockquote />
because we don’t use such element in our editor and it is recognizable by Draft out of the box, so no need to bother with customblockRenderMap
. Then afterconvertFromHTML
successfully converted your content you replace<blockquote />
blocks withatomic
ones, you parse JSON string into object and inject that data in Entity (or in block meta data - it’s implementation details). Later you render suchatomic
blocks with React component (<img />
src you will get from Entity data. Hope that helped.