Skip to content

Commit 449962b

Browse files
committed
fix: added optional chaning and wrapper div for caption
1 parent 787ce3c commit 449962b

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/toRedactor.tsx

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,20 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
141141
}
142142

143143
if (caption || (position && position !== "none")) {
144-
const figcaption = `<figcaption style="text-align:center">${caption}</figcaption>`;
144+
const figcaption = caption
145+
? `<figcaption style="text-align:center">${caption}</figcaption>`
146+
: "";
145147
const figureStyles = {
146-
margin: '0'
147-
}
148-
if(inline && position !== 'right' && position !== 'left') {
149-
figureStyles["display"] = "inline-block"
148+
margin: "0",
149+
};
150+
if (inline && position !== "right" && position !== "left") {
151+
figureStyles["display"] = "inline-block";
150152
}
151153
if (position && position !== "none") {
152-
figureStyles[ inline ? "float": "text-align"]= position
154+
figureStyles[inline ? "float" : "text-align"] = position;
153155
}
154-
155-
156-
img = `<figure style="${getStyleStringFromObject(figureStyles)}">${img}${
157-
caption ? figcaption : ""
158-
}</figure>`;
156+
157+
img = `<figure style="${getStyleStringFromObject(figureStyles)}"><div style="display: inline-block">${img}${figcaption}</div></figure>`;
159158
}
160159
return `${img}`;
161160
}
@@ -404,16 +403,7 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
404403
attrsJson['sys-style-type'] = allattrs['display-type']
405404
delete attrsJson['display-type']
406405
}
407-
else if (attrsJson["display-type"]) {
408-
const styleObj = jsonValue["attrs"]["style"];
409-
if(jsonValue["attrs"]["position"] === 'center'){
410-
styleObj['object-fit'] = "contain"
411-
}
412-
delete styleObj['float']
413-
attrsJson["style"] = getStyleStringFromObject(styleObj);
414-
console.dir({jsonValue, attrsJson, figureStyles, styleObj}, {depth:null})
415-
416-
}
406+
417407
else if (attrsJson['type'] === "asset") {
418408
attrsJson['data-sys-asset-filelink'] = allattrs['asset-link']
419409
delete attrsJson['asset-link']
@@ -451,7 +441,14 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
451441
if (!attrsJson['sys-style-type']) {
452442
attrsJson['sys-style-type'] = String(allattrs['asset-type']).indexOf('image') > -1 ? 'display' : 'download'
453443
}
454-
444+
if (attrsJson?.["display-type"] === "display") {
445+
const styleObj = jsonValue?.["attrs"]?.["style"] ?? {}
446+
if (!styleObj["width"]) {
447+
styleObj["width"] = "auto";
448+
}
449+
delete styleObj["float"];
450+
attrsJson["style"] = attrsJson["style"] + "; " + getStyleStringFromObject(styleObj);
451+
}
455452
delete attrsJson['display-type']
456453
}
457454
}

0 commit comments

Comments
 (0)