@@ -49,7 +49,11 @@ 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' ) , width : el . getAttribute ( 'width' ) } }
52
+ const imageAttrs : any = { type : 'img' , attrs : { url : el . getAttribute ( 'src' ) } }
53
+ if ( el . getAttribute ( 'width' ) ) {
54
+ imageAttrs . attrs [ 'width' ] = el . getAttribute ( 'width' )
55
+ }
56
+ return imageAttrs
53
57
} ,
54
58
LI : ( ) => ( { type : 'li' , attrs : { } } ) ,
55
59
OL : ( ) => ( { type : 'ol' , attrs : { } } ) ,
@@ -631,12 +635,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
631
635
const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
632
636
extraAttrs [ 'anchorLink' ] = href ;
633
637
if ( target && target !== '' ) {
634
- extraAttrs [ 'target' ] = target ;
638
+ extraAttrs [ 'target' ] = target === "_blank" ;
635
639
}
636
640
const imageAttrs = newChildren [ 0 ] . children ;
637
641
638
642
if ( imageAttrs [ 0 ] . type === 'img' ) {
639
- sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
643
+ if ( imageAttrs [ 0 ] . attrs . width ) {
644
+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
645
+ }
640
646
elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
641
647
642
648
}
@@ -661,6 +667,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
661
667
elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
662
668
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
663
669
}
670
+ if ( newChildren [ 0 ] ?. type === 'img' ) {
671
+ let extraAttrs : { [ key : string ] : any } = { }
672
+ const imageAttrs = newChildren [ 0 ]
673
+ elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
674
+ elementAttrs . attrs [ 'anchorLink' ] = el . getAttribute ( 'href' )
675
+ if ( el . getAttribute ( 'target' ) )
676
+ elementAttrs . attrs [ 'target' ] = el . getAttribute ( 'target' )
677
+ return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
678
+
679
+ }
664
680
}
665
681
if ( nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO' ) {
666
682
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. width ) {
@@ -676,7 +692,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
676
692
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
677
693
elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
678
694
}
679
- if ( elementAttrs . attrs . width ) {
695
+ if ( nodeName === "IMG" && elementAttrs . attrs . width ) {
680
696
elementAttrs . attrs . style . width = `${ elementAttrs . attrs . width } px`
681
697
elementAttrs . attrs . style [ 'max-width' ] = `${ elementAttrs . attrs . width } px`
682
698
}
@@ -952,11 +968,8 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
952
968
delete childAttrs . caption
953
969
}
954
970
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
- }
971
+ const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
972
+
960
973
delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'anchorlink' ] ;
961
974
delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'style' ] ;
962
975
0 commit comments