-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
image position is lost when converting from html to EditorState #101
Comments
I have the same problem.Have you fixed this bug now? |
//富文本html格式转换
const htmlFormat = (data) => {
const json = html2json(data)
Array.isArray(json.child) &&
json.child.forEach((item) => {
if (item.tag === 'div') {
item.tag = 'img'
if (
item.attr &&
Array.isArray(item.child) &&
item.child.length === 1 &&
item.child[0].attr &&
Array.isArray(item.child[0].attr.style)
) {
const style = item.attr.style + item.child[0].attr.style.join('')
item.attr = {
...item.child[0].attr,
}
item.attr.style = style
}
}
})
const html = json2html(json)
return html
}
//解决image位置无法保存的bug
const html = value.text ? htmlFormat(value.text) : ''
const contentBlock = htmlToDraft(
html,
(nodeName, node) => {
if (nodeName === 'img' && node instanceof HTMLImageElement) {
const entityConfig = {}
entityConfig.src = node.getAttribute
? node.getAttribute('src') || node.src
: node.src
entityConfig.alt = node.alt
entityConfig.height = node.style.height
entityConfig.width = node.style.width
if (node.style.float) {
entityConfig.alignment = node.style.float
} else {
if (node.style.textAlign) {
entityConfig.alignment = node.style.textAlign
}
}
return {
type: 'IMAGE',
mutability: 'MUTABLE',
data: entityConfig,
}
}
}
) |
I was facing the same issue, and since each image has a div parent with align-text property, the idea behind my solution is to find any matches (e.g. find any image with a div parent ), take the property from the div element and set it to the image
|
Hello Guys ! It is wired, but I think that I've got a workaround (it is related to textAlign:none instead of center bug #989
|
is the PR that is fixing the none to center going to be merged any time soon? this could help alot instead of doing our own work around |
when saving the image in react-draft-wysiwyg I get the following output as html:
<div style="text-align:right;"><img src="srcHere" alt="" style="height: auto;width: auto"/></div>
after converting this back to the EditorState and logging out html again I receive:
<p style="text-align:right;"></p> <img src="srcHere" alt="" style="height: auto;width: auto"/>
The image position is lost and wrapping div is converted into adjacent empty p element with same styling as div.
the code that converts the html back to EditorState:
I imagine this might be similar if not the same issue as #98
The text was updated successfully, but these errors were encountered: