@@ -543,31 +543,28 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
543
543
544
544
}
545
545
if ( newChildren [ 0 ] ?. type === 'img' ) {
546
- if ( newChildren [ 0 ] . attrs . width ) {
547
- sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( )
548
- }
549
- elementAttrs = getImageAttributes (
550
- elementAttrs ,
551
- { ...newChildren [ 0 ] . attrs , ...sizeAttrs , caption : extraAttrs [ 'caption' ] , style : { 'text-align' : style [ 'text-align' ] } } ,
552
- { ...extraAttrs , ...sizeAttrs } ) ;
546
+ elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren, extraAttrs, sizeAttrs} )
547
+ }
548
+ if ( newChildren [ 0 ] ?. type === 'reference' ) {
549
+ elementAttrs = getReferenceAttributes ( { elementAttrs, newChildren, extraAttrs, sizeAttrs} )
553
550
}
554
551
if ( newChildren [ 0 ] ?. type === 'a' ) {
555
- const { link , target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
556
- extraAttrs [ 'link ' ] = link
552
+ const { href , target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
553
+ extraAttrs [ 'anchorLink ' ] = href ;
557
554
if ( target && target !== '' ) {
558
- extraAttrs [ 'target' ] = true
555
+ extraAttrs [ 'target' ] = true ;
556
+ }
557
+ const imageAttrs = newChildren [ 0 ] . children ;
558
+
559
+ if ( imageAttrs [ 0 ] . type === 'img' ) {
560
+ elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
561
+
562
+ }
563
+ if ( imageAttrs [ 0 ] . type === 'reference' ) {
564
+ elementAttrs = getReferenceAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
559
565
}
560
- const imageAttrs = newChildren [ 0 ] . children [ 0 ]
561
- elementAttrs = getImageAttributes ( elementAttrs , imageAttrs . attrs || { } , { ...extraAttrs , ...sizeAttrs } )
562
- }
563
- if ( newChildren [ 0 ] ?. type === 'reference' ) {
564
- extraAttrs [ 'asset-caption' ] = extraAttrs [ 'caption' ]
565
- elementAttrs = getImageAttributes (
566
- elementAttrs ,
567
- { ...newChildren [ 0 ] . attrs , ...sizeAttrs , position : extraAttrs . position , style : { 'text-align' : style [ 'text-align' ] } } ,
568
- { ...extraAttrs , ...sizeAttrs } ) ;
569
- elementAttrs . type = "reference"
570
566
}
567
+
571
568
return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
572
569
}
573
570
@@ -755,3 +752,43 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any)
755
752
}
756
753
return elementAttrs
757
754
}
755
+
756
+ const getReferenceAttributes = ( { elementAttrs, newChildren, extraAttrs, sizeAttrs} : any ) => {
757
+
758
+ let { style } = elementAttrs . attrs ;
759
+
760
+ extraAttrs [ 'asset-caption' ] = extraAttrs [ 'caption' ] ;
761
+
762
+ const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style [ 'text-align' ] } , position : extraAttrs . position }
763
+ extraAttrs = { ...extraAttrs , ...sizeAttrs }
764
+
765
+ if ( ! childAttrs . position ) {
766
+ delete childAttrs . position
767
+ }
768
+
769
+ const referenceAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
770
+
771
+ referenceAttrs . type = "reference" ;
772
+
773
+ return referenceAttrs
774
+ }
775
+
776
+ const getFinalImageAttributes = ( { elementAttrs, newChildren, extraAttrs, sizeAttrs} : any ) => {
777
+
778
+ let { style } = elementAttrs . attrs ;
779
+
780
+ if ( newChildren [ 0 ] . attrs . width ) {
781
+ sizeAttrs . width = newChildren [ 0 ] . attrs . width . toString ( ) ;
782
+ }
783
+
784
+ const childAttrs = { ...newChildren [ 0 ] . attrs , ...sizeAttrs , style : { 'text-align' : style [ 'text-align' ] } , caption : extraAttrs [ 'caption' ] }
785
+ extraAttrs = { ...extraAttrs , ...sizeAttrs }
786
+
787
+ if ( ! childAttrs . caption ) {
788
+ delete childAttrs . caption
789
+ }
790
+
791
+ const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
792
+
793
+ return imageAttrs
794
+ }
0 commit comments