Skip to content

Commit 41bac3e

Browse files
committed
fix: RT-292 image conversions
1 parent bd4692b commit 41bac3e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/fromRedactor.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
4949
const assetName = splittedUrl[splittedUrl?.length - 1]
5050
return { type: 'reference', attrs: { "asset-name": assetName,"content-type-uid" : "sys_assets", "asset-link": el.getAttribute('src'), "asset-type": `image/${imageType}`, "display-type": "display", "type": "asset", "asset-uid": assetUid } }
5151
}
52-
return { type: 'img', attrs: { url: el.getAttribute('src') } }
52+
return { type: 'img', attrs: { url: el.getAttribute('src'), width: el.getAttribute('width') } }
5353
},
5454
LI: () => ({ type: 'li', attrs: {} }),
5555
OL: () => ({ type: 'ol', attrs: {} }),
@@ -631,11 +631,12 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
631631
const { href, target } = newChildren[0].attrs?.["redactor-attributes"]
632632
extraAttrs['anchorLink'] = href;
633633
if (target && target !== '') {
634-
extraAttrs['target'] = true;
634+
extraAttrs['target'] = target;
635635
}
636636
const imageAttrs = newChildren[0].children;
637637

638638
if(imageAttrs[0].type === 'img'){
639+
sizeAttrs.width = imageAttrs[0].attrs.width
639640
elementAttrs = getFinalImageAttributes({elementAttrs, newChildren : imageAttrs, extraAttrs, sizeAttrs})
640641

641642
}
@@ -675,6 +676,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
675676
if (elementAttrs?.attrs?.["redactor-attributes"]?.inline) {
676677
elementAttrs.attrs.inline = Boolean(elementAttrs?.attrs?.["redactor-attributes"]?.inline)
677678
}
679+
if(elementAttrs.attrs.width){
680+
elementAttrs.attrs.style.width = `${elementAttrs.attrs.width}px`
681+
elementAttrs.attrs.style['max-width'] = `${elementAttrs.attrs.width}px`
682+
}
678683
return jsx('element', elementAttrs, [{ text: '' }])
679684
}
680685
if (nodeName === 'BLOCKQUOTE') {
@@ -933,14 +938,27 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
933938
sizeAttrs.width = newChildren[0].attrs.width.toString();
934939
}
935940

941+
if(!isNaN(parseInt(sizeAttrs.width))){
942+
sizeAttrs.style = {
943+
width: `${sizeAttrs.width}px`,
944+
"max-width": `${sizeAttrs.width}px`
945+
}
946+
}
947+
936948
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style?.['text-align'] }, caption: extraAttrs['caption'] }
937949
extraAttrs = { ...extraAttrs, ...sizeAttrs }
938950

939951
if (!childAttrs.caption) {
940952
delete childAttrs.caption
941953
}
942954

943-
const imageAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
955+
const imageAttrs = getImageAttributes(elementAttrs, {... childAttrs, ...extraAttrs}, extraAttrs);
956+
if(imageAttrs.attrs.link){
957+
imageAttrs.attrs.anchorLink = imageAttrs.attrs.link;
958+
delete imageAttrs.attrs.link;
959+
}
960+
delete imageAttrs?.attrs?.['redactor-attributes']?.['anchorlink'];
961+
delete imageAttrs?.attrs?.['redactor-attributes']?.['style'];
944962

945963
return imageAttrs
946964
}

src/toRedactor.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
482482
}
483483
if(jsonValue['type'] === 'img'){
484484
attrsJson['src'] = allattrs['url']
485+
486+
if(allattrs['caption']) figureStyles.caption = allattrs['caption']
487+
if(allattrs['position']) figureStyles.position = allattrs['position']
488+
if(allattrs['anchorLink']) figureStyles.anchorLink = `href="${allattrs['anchorLink']}"`
489+
if(allattrs['target']){
490+
figureStyles.anchorLink += ` target="${allattrs['target']}"`
491+
}
492+
figureStyles.fieldsEdited.push(figureStyles.caption)
485493
}
486494
if(!(options?.customElementTypes && !isEmpty(options.customElementTypes) && options.customElementTypes[jsonValue['type']])) {
487495
delete attrsJson['url']

0 commit comments

Comments
 (0)