Skip to content

Commit 8f2edcf

Browse files
committed
fix: RT-263 transformation of assets between html and json and vice-versa
1 parent e0ac739 commit 8f2edcf

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/fromRedactor.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
497497
]
498498
)
499499
}
500+
if(el.parentNode?.nodeName === 'FIGURE'){
501+
return children
502+
}
500503
}
501504

502505
if (ELEMENT_TAGS[nodeName]) {
@@ -874,7 +877,7 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any)
874877
...extraAttrs
875878
},
876879
"asset-caption": extraAttrs["asset-caption"],
877-
"link": extraAttrs.link
880+
"link": extraAttrs.link ?? extraAttrs.anchorLink
878881
}
879882
}
880883
if (elementAttrs?.attrs?.["redactor-attributes"]?.link) {
@@ -891,11 +894,16 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any)
891894

892895
const getReferenceAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttrs} : any) => {
893896

894-
let { style } = elementAttrs.attrs;
895-
896897
extraAttrs['asset-caption'] = extraAttrs['caption'];
898+
if(newChildren[0].attrs.width){
899+
delete sizeAttrs.width
900+
}
901+
const style = {}
902+
if (elementAttrs?.attrs?.style?.['text-align']) {
903+
style['text-align'] = elementAttrs?.attrs?.style?.['text-align']
904+
}
897905

898-
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style['text-align'] }, position: extraAttrs.position }
906+
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style , position: extraAttrs.position }
899907
extraAttrs = { ...extraAttrs, ...sizeAttrs }
900908

901909
if (!childAttrs.position) {
@@ -905,7 +913,7 @@ const getReferenceAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttr
905913
const referenceAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
906914

907915
referenceAttrs.type = "reference";
908-
916+
delete referenceAttrs?.attrs?.['redactor-attributes']?.['anchorlink'];
909917
return referenceAttrs
910918
}
911919

src/toRedactor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
125125
}
126126

127127
else if (extraAttrs?.displayType === "display") {
128-
const anchor = jsonBlock?.["attrs"]?.["link"];
128+
const anchor = jsonBlock?.["attrs"]?.["link"] ?? jsonBlock?.["attrs"]?.["anchorLink"];
129129

130130
const caption = jsonBlock?.["attrs"]?.["asset-caption"];
131131
const position = jsonBlock?.["attrs"]?.["position"];
@@ -134,7 +134,9 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
134134
const figureStyles = {
135135
margin: "0",
136136
};
137-
attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
137+
if(!attrs.includes(`src="${jsonBlock?.["attrs"]?.["asset-link"]}`)){
138+
attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
139+
}
138140
let img = `<img${attrs}/>`;
139141

140142
if (anchor) {

0 commit comments

Comments
 (0)