@@ -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 = {
@@ -156,7 +161,7 @@ const traverseChildAndWarpChild = (children: Array<Object>, allowNonStandardTags
156
161
if ( child . hasOwnProperty ( 'type' ) ) {
157
162
if ( isInline . includes ( child . type ) ) {
158
163
if ( child . type === "reference" ) {
159
- if ( child . attrs && ( child . attrs [ 'display-type' ] === "inline" || child . attrs [ 'display-type' ] === "link" ) ) {
164
+ if ( child . attrs && ( child . attrs [ 'display-type' ] === "inline" || child . attrs [ 'display-type' ] === "link" || child . attrs [ 'inline' ] ) ) {
160
165
inlineElementIndex . push ( index )
161
166
} else {
162
167
hasBlockElement = true
@@ -437,7 +442,15 @@ 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
+ if ( redactor . inline ) {
450
+ assetAttrs . inline = true
451
+ delete redactor . inline
452
+ }
453
+ return jsx ( 'element' , { attrs : assetAttrs , type : "reference" , uid : generateId ( ) } , children )
441
454
}
442
455
}
443
456
if ( nodeName === 'FIGCAPTION' ) {
@@ -626,11 +639,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
626
639
const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
627
640
extraAttrs [ 'anchorLink' ] = href ;
628
641
if ( target && target !== '' ) {
629
- extraAttrs [ 'target' ] = true ;
642
+ extraAttrs [ 'target' ] = target === "_blank" ;
630
643
}
631
644
const imageAttrs = newChildren [ 0 ] . children ;
632
645
633
646
if ( imageAttrs [ 0 ] . type === 'img' ) {
647
+ if ( imageAttrs [ 0 ] . attrs . width ) {
648
+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
649
+ }
634
650
elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
635
651
636
652
}
@@ -655,6 +671,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
655
671
elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
656
672
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
657
673
}
674
+ if ( newChildren [ 0 ] ?. type === 'img' ) {
675
+ let extraAttrs : { [ key : string ] : any } = { }
676
+ const imageAttrs = newChildren [ 0 ]
677
+ elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
678
+ elementAttrs . attrs [ 'anchorLink' ] = el . getAttribute ( 'href' )
679
+ if ( el . getAttribute ( 'target' ) )
680
+ elementAttrs . attrs [ 'target' ] = el . getAttribute ( 'target' )
681
+ return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
682
+
683
+ }
658
684
}
659
685
if ( nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO' ) {
660
686
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. width ) {
@@ -670,6 +696,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
670
696
if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
671
697
elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
672
698
}
699
+ if ( nodeName === "IMG" && elementAttrs . attrs . width ) {
700
+ elementAttrs . attrs . style . width = `${ elementAttrs . attrs . width } px`
701
+ elementAttrs . attrs . style [ 'max-width' ] = `${ elementAttrs . attrs . width } px`
702
+ }
673
703
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
674
704
}
675
705
if ( nodeName === 'BLOCKQUOTE' ) {
@@ -843,6 +873,12 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
843
873
}
844
874
}
845
875
876
+ if ( nodeName === "DIV" ) {
877
+ if ( el . style ?. overflow === 'hidden' && children . find ( ( child : any ) => child . type === 'reference' ) ) {
878
+ elementAttrs = { ...elementAttrs , type : 'p' , attrs : { } }
879
+ }
880
+ }
881
+
846
882
if ( children . length === 0 ) {
847
883
children = [ { text : '' } ]
848
884
}
@@ -928,14 +964,24 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
928
964
sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( ) ;
929
965
}
930
966
967
+ if ( ! isNaN ( parseInt ( sizeAttrs . width ) ) ) {
968
+ sizeAttrs . style = {
969
+ width : `${ sizeAttrs . width } px` ,
970
+ "max-width" : `${ sizeAttrs . width } px`
971
+ }
972
+ }
973
+
931
974
const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style ?. [ 'text-align' ] } , caption : extraAttrs [ 'caption' ] }
932
975
extraAttrs = { ...extraAttrs , ...sizeAttrs }
933
976
934
977
if ( ! childAttrs . caption ) {
935
978
delete childAttrs . caption
936
979
}
937
980
938
- const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
981
+ const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
982
+
983
+ delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'anchorlink' ] ;
984
+ delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'style' ] ;
939
985
940
986
return imageAttrs
941
987
}
0 commit comments