@@ -49,7 +49,7 @@ export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
49
49
const assetName = splittedUrl [ splittedUrl ?. length - 1 ]
50
50
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 } }
51
51
}
52
- return { type : 'img' , attrs : { url : el . getAttribute ( 'src' ) } }
52
+ return { type : 'img' , attrs : { url : el . getAttribute ( 'src' ) , width : el . getAttribute ( 'width' ) } }
53
53
} ,
54
54
LI : ( ) => ( { type : 'li' , attrs : { } } ) ,
55
55
OL : ( ) => ( { type : 'ol' , attrs : { } } ) ,
@@ -631,11 +631,12 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
631
631
const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
632
632
extraAttrs [ 'anchorLink' ] = href ;
633
633
if ( target && target !== '' ) {
634
- extraAttrs [ 'target' ] = true ;
634
+ extraAttrs [ 'target' ] = target ;
635
635
}
636
636
const imageAttrs = newChildren [ 0 ] . children ;
637
637
638
638
if ( imageAttrs [ 0 ] . type === 'img' ) {
639
+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
639
640
elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
640
641
641
642
}
@@ -675,6 +676,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
675
676
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
676
677
elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
677
678
}
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
+ }
678
683
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
679
684
}
680
685
if ( nodeName === 'BLOCKQUOTE' ) {
@@ -933,14 +938,27 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
933
938
sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( ) ;
934
939
}
935
940
941
+ if ( ! isNaN ( parseInt ( sizeAttrs . width ) ) ) {
942
+ sizeAttrs . style = {
943
+ width : `${ sizeAttrs . width } px` ,
944
+ "max-width" : `${ sizeAttrs . width } px`
945
+ }
946
+ }
947
+
936
948
const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style ?. [ 'text-align' ] } , caption : extraAttrs [ 'caption' ] }
937
949
extraAttrs = { ...extraAttrs , ...sizeAttrs }
938
950
939
951
if ( ! childAttrs . caption ) {
940
952
delete childAttrs . caption
941
953
}
942
954
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' ] ;
944
962
945
963
return imageAttrs
946
964
}
0 commit comments