@@ -15,7 +15,7 @@ const isInline = ['span', 'a', 'inlineCode', 'reference']
15
15
const isVoid = [ 'img' , 'embed' ]
16
16
17
17
18
- const ELEMENT_TAGS : IHtmlToJsonElementTags = {
18
+ export const ELEMENT_TAGS : IHtmlToJsonElementTags = {
19
19
A : ( el : HTMLElement ) => {
20
20
const attrs : Record < string , string > = { }
21
21
const target = el . getAttribute ( 'target' ) ;
@@ -49,7 +49,11 @@ 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
+ 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 : { } } ) ,
@@ -98,7 +102,8 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
98
102
SCRIPT : ( el : HTMLElement ) => {
99
103
return { type : 'script' , attrs : { } }
100
104
} ,
101
- HR : ( ) => ( { type : 'hr' , attrs : { } } )
105
+ HR : ( ) => ( { type : 'hr' , attrs : { } } ) ,
106
+ FIGCAPTION : ( ) => ( { type : 'figcaption' , attrs : { } } ) ,
102
107
}
103
108
104
109
const TEXT_TAGS : IHtmlToJsonTextTags = {
@@ -437,7 +442,11 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
437
442
}
438
443
}
439
444
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 )
441
450
}
442
451
}
443
452
if ( nodeName === 'FIGCAPTION' ) {
@@ -626,11 +635,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
626
635
const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
627
636
extraAttrs [ 'anchorLink' ] = href ;
628
637
if ( target && target !== '' ) {
629
- extraAttrs [ 'target' ] = true ;
638
+ extraAttrs [ 'target' ] = target === "_blank" ;
630
639
}
631
640
const imageAttrs = newChildren [ 0 ] . children ;
632
641
633
642
if ( imageAttrs [ 0 ] . type === 'img' ) {
643
+ if ( imageAttrs [ 0 ] . attrs . width ) {
644
+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
645
+ }
634
646
elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
635
647
636
648
}
@@ -655,6 +667,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
655
667
elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
656
668
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
657
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
+ }
658
680
}
659
681
if ( nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO' ) {
660
682
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. width ) {
@@ -670,6 +692,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
670
692
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
671
693
elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
672
694
}
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
+ }
673
699
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
674
700
}
675
701
if ( nodeName === 'BLOCKQUOTE' ) {
@@ -928,14 +954,24 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
928
954
sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( ) ;
929
955
}
930
956
957
+ if ( ! isNaN ( parseInt ( sizeAttrs . width ) ) ) {
958
+ sizeAttrs . style = {
959
+ width : `${ sizeAttrs . width } px` ,
960
+ "max-width" : `${ sizeAttrs . width } px`
961
+ }
962
+ }
963
+
931
964
const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style ?. [ 'text-align' ] } , caption : extraAttrs [ 'caption' ] }
932
965
extraAttrs = { ...extraAttrs , ...sizeAttrs }
933
966
934
967
if ( ! childAttrs . caption ) {
935
968
delete childAttrs . caption
936
969
}
937
970
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' ] ;
939
975
940
976
return imageAttrs
941
977
}
0 commit comments