@@ -15,7 +15,7 @@ const isInline = ['span', 'a', 'inlineCode', 'reference']
1515const isVoid = [ 'img' , 'embed' ]
1616
1717
18- const ELEMENT_TAGS : IHtmlToJsonElementTags = {
18+ export const ELEMENT_TAGS : IHtmlToJsonElementTags = {
1919 A : ( el : HTMLElement ) => {
2020 const attrs : Record < string , string > = { }
2121 const target = el . getAttribute ( 'target' ) ;
@@ -49,7 +49,11 @@ 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+ 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
5357 } ,
5458 LI : ( ) => ( { type : 'li' , attrs : { } } ) ,
5559 OL : ( ) => ( { type : 'ol' , attrs : { } } ) ,
@@ -98,7 +102,8 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
98102 SCRIPT : ( el : HTMLElement ) => {
99103 return { type : 'script' , attrs : { } }
100104 } ,
101- HR : ( ) => ( { type : 'hr' , attrs : { } } )
105+ HR : ( ) => ( { type : 'hr' , attrs : { } } ) ,
106+ FIGCAPTION : ( ) => ( { type : 'figcaption' , attrs : { } } ) ,
102107}
103108
104109const TEXT_TAGS : IHtmlToJsonTextTags = {
@@ -437,7 +442,11 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
437442 }
438443 }
439444 elementAttrs . attrs [ "redactor-attributes" ] = redactor
440- return jsx ( 'element' , { attrs : { ...elementAttrs ?. attrs , type, "asset-caption" : caption , "link" : link , "asset-alt" : alt , target, position, "asset-link" : fileLink , "asset-uid" : uid , "display-type" : displayType , "asset-name" : fileName , "asset-type" : contentType , "content-type-uid" : contentTypeUid } , type : "reference" , uid : generateId ( ) } , children )
445+ const assetAttrs = { ...elementAttrs ?. attrs , type, "asset-caption" : caption , "link" : link , "asset-alt" : alt , target, position, "asset-link" : fileLink , "asset-uid" : uid , "display-type" : displayType , "asset-name" : fileName , "asset-type" : contentType , "content-type-uid" : contentTypeUid }
446+ if ( assetAttrs . target === "_self" ) {
447+ delete assetAttrs . target
448+ }
449+ return jsx ( 'element' , { attrs : assetAttrs , type : "reference" , uid : generateId ( ) } , children )
441450 }
442451 }
443452 if ( nodeName === 'FIGCAPTION' ) {
@@ -626,11 +635,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
626635 const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
627636 extraAttrs [ 'anchorLink' ] = href ;
628637 if ( target && target !== '' ) {
629- extraAttrs [ 'target' ] = true ;
638+ extraAttrs [ 'target' ] = target === "_blank" ;
630639 }
631640 const imageAttrs = newChildren [ 0 ] . children ;
632641
633642 if ( imageAttrs [ 0 ] . type === 'img' ) {
643+ if ( imageAttrs [ 0 ] . attrs . width ) {
644+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
645+ }
634646 elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
635647
636648 }
@@ -655,6 +667,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
655667 elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
656668 return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
657669 }
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+ }
658680 }
659681 if ( nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO' ) {
660682 if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. width ) {
@@ -670,6 +692,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
670692 if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
671693 elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
672694 }
695+ if ( nodeName === "IMG" && elementAttrs . attrs . width ) {
696+ elementAttrs . attrs . style . width = `${ elementAttrs . attrs . width } px`
697+ elementAttrs . attrs . style [ 'max-width' ] = `${ elementAttrs . attrs . width } px`
698+ }
673699 return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
674700 }
675701 if ( nodeName === 'BLOCKQUOTE' ) {
@@ -928,14 +954,24 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
928954 sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( ) ;
929955 }
930956
957+ if ( ! isNaN ( parseInt ( sizeAttrs . width ) ) ) {
958+ sizeAttrs . style = {
959+ width : `${ sizeAttrs . width } px` ,
960+ "max-width" : `${ sizeAttrs . width } px`
961+ }
962+ }
963+
931964 const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style ?. [ 'text-align' ] } , caption : extraAttrs [ 'caption' ] }
932965 extraAttrs = { ...extraAttrs , ...sizeAttrs }
933966
934967 if ( ! childAttrs . caption ) {
935968 delete childAttrs . caption
936969 }
937970
938- const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
971+ const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
972+
973+ delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'anchorlink' ] ;
974+ delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'style' ] ;
939975
940976 return imageAttrs
941977}
0 commit comments