@@ -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}
0 commit comments