Skip to content

Commit cb717fa

Browse files
committed
feat: added figure for block level img
1 parent 449962b commit cb717fa

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/toRedactor.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
127127
const caption = jsonBlock?.["attrs"]?.["asset-caption"];
128128
const position = jsonBlock?.["attrs"]?.["position"];
129129
const inline = jsonBlock?.["attrs"]?.["inline"]
130-
130+
let figureAttrs = ""
131+
const figureStyles = {
132+
margin: "0",
133+
};
131134
attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
132135
let img = `<img${attrs}/>`;
133136

@@ -144,19 +147,25 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
144147
const figcaption = caption
145148
? `<figcaption style="text-align:center">${caption}</figcaption>`
146149
: "";
147-
const figureStyles = {
148-
margin: "0",
149-
};
150+
150151
if (inline && position !== "right" && position !== "left") {
151152
figureStyles["display"] = "inline-block";
152153
}
153154
if (position && position !== "none") {
154155
figureStyles[inline ? "float" : "text-align"] = position;
155156
}
156-
157-
img = `<figure style="${getStyleStringFromObject(figureStyles)}"><div style="display: inline-block">${img}${figcaption}</div></figure>`;
157+
158+
if(figcaption){
159+
img = `<div style="display: inline-block">${img}${figcaption}</div>`;
160+
}
161+
}
162+
if(!isEmpty(figureStyles)){
163+
figureAttrs = ` style="${getStyleStringFromObject(figureStyles)}"`
164+
}
165+
if(inline && !caption && (!position ||position==='none')){
166+
return img
158167
}
159-
return `${img}`;
168+
return `<figure${figureAttrs ? figureAttrs : ""}>${img}</figure>`;
160169
}
161170
return `<span${attrs}>${child}</span>`
162171
},
@@ -442,12 +451,14 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
442451
attrsJson['sys-style-type'] = String(allattrs['asset-type']).indexOf('image') > -1 ? 'display' : 'download'
443452
}
444453
if (attrsJson?.["display-type"] === "display") {
445-
const styleObj = jsonValue?.["attrs"]?.["style"] ?? {}
454+
const styleObj = jsonValue?.["attrs"]?.["style"] ?? {};
446455
if (!styleObj["width"]) {
447456
styleObj["width"] = "auto";
448457
}
449458
delete styleObj["float"];
450-
attrsJson["style"] = attrsJson["style"] + "; " + getStyleStringFromObject(styleObj);
459+
attrsJson["style"]
460+
? (attrsJson["style"] += getStyleStringFromObject(styleObj))
461+
: (attrsJson["style"] = getStyleStringFromObject(styleObj));
451462
}
452463
delete attrsJson['display-type']
453464
}

0 commit comments

Comments
 (0)