Skip to content

Commit 787ce3c

Browse files
committed
feat: added support for inline image type asset
1 parent 65713b9 commit 787ce3c

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/toRedactor.tsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
4949
return `<iframe${attrs}></iframe>`
5050
},
5151
p: (attrs: any, child: any) => {
52+
if(child.includes("<figure"))
53+
return `<div${attrs} style="overflow: hidden"><span>${child}</span></div>`
5254
return `<p${attrs}>${child}</p>`
5355
},
5456
ol: (attrs: any, child: any) => {
@@ -140,22 +142,21 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
140142

141143
if (caption || (position && position !== "none")) {
142144
const figcaption = `<figcaption style="text-align:center">${caption}</figcaption>`;
143-
let figureAttrs = ``;
144-
145+
const figureStyles = {
146+
margin: '0'
147+
}
148+
if(inline && position !== 'right' && position !== 'left') {
149+
figureStyles["display"] = "inline-block"
150+
}
145151
if (position && position !== "none") {
146-
const style = Object.entries(jsonBlock["attrs"]["style"])
147-
.map((entry) => entry.join(":"))
148-
.join(";");
149-
150-
if (style) figureAttrs = ` style="${style}"`;
152+
figureStyles[ inline ? "float": "text-align"]= position
151153
}
152-
img = `<figure${figureAttrs ? figureAttrs : ""}>${img}${
154+
155+
156+
img = `<figure style="${getStyleStringFromObject(figureStyles)}">${img}${
153157
caption ? figcaption : ""
154158
}</figure>`;
155159
}
156-
if(inline){
157-
img = `<span>${img}</span>`
158-
}
159160
return `${img}`;
160161
}
161162
return `<span${attrs}>${child}</span>`
@@ -402,7 +403,18 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
402403
delete attrsJson['content-type-uid']
403404
attrsJson['sys-style-type'] = allattrs['display-type']
404405
delete attrsJson['display-type']
405-
} else if (attrsJson['type'] === "asset") {
406+
}
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+
}
417+
else if (attrsJson['type'] === "asset") {
406418
attrsJson['data-sys-asset-filelink'] = allattrs['asset-link']
407419
delete attrsJson['asset-link']
408420
attrsJson['data-sys-asset-uid'] = allattrs['asset-uid']
@@ -508,3 +520,10 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
508520

509521
return children
510522
}
523+
524+
525+
function getStyleStringFromObject(styleObj: { [key: string]: string }) {
526+
return Object.keys(styleObj)
527+
.map((key) => `${key}: ${styleObj[key]}`)
528+
.join("; ");
529+
}

0 commit comments

Comments
 (0)