Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for image type asset #27

Merged
merged 7 commits into from
Nov 2, 2023
Merged
61 changes: 60 additions & 1 deletion src/toRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
return `<iframe${attrs}></iframe>`
},
p: (attrs: any, child: any) => {
if(child.includes("<figure"))
return `<div${attrs} style="overflow: hidden"><span>${child}</span></div>`
return `<p${attrs}>${child}</p>`
},
ol: (attrs: any, child: any) => {
Expand Down Expand Up @@ -118,6 +120,45 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
} else if (extraAttrs?.displayType === 'asset') {
return `<figure${attrs}>${child}</figure>`
}

else if (extraAttrs?.displayType === "display") {
const anchor = jsonBlock?.["attrs"]?.["link"];

const caption = jsonBlock?.["attrs"]?.["asset-caption"];
const position = jsonBlock?.["attrs"]?.["position"];
const inline = jsonBlock?.["attrs"]?.["inline"]

attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
let img = `<img${attrs}/>`;

if (anchor) {
const target = jsonBlock?.["attrs"]?.["target"];
let anchorAttrs = `href="${anchor}"`;
if (target) {
anchorAttrs = `${anchorAttrs} target="${target}"`;
}
img = `<a ${anchorAttrs}>${img}</a>`;
}

if (caption || (position && position !== "none")) {
const figcaption = `<figcaption style="text-align:center">${caption}</figcaption>`;
Jayesh2812 marked this conversation as resolved.
Show resolved Hide resolved
const figureStyles = {
margin: '0'
}
if(inline && position !== 'right' && position !== 'left') {
figureStyles["display"] = "inline-block"
}
if (position && position !== "none") {
figureStyles[ inline ? "float": "text-align"]= position
}


img = `<figure style="${getStyleStringFromObject(figureStyles)}">${img}${
caption ? figcaption : ""
}</figure>`;
}
return `${img}`;
}
return `<span${attrs}>${child}</span>`
},
inlineCode: (attrs: any, child: any) => {
Expand Down Expand Up @@ -362,7 +403,18 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
delete attrsJson['content-type-uid']
attrsJson['sys-style-type'] = allattrs['display-type']
delete attrsJson['display-type']
} else if (attrsJson['type'] === "asset") {
}
else if (attrsJson["display-type"]) {
Jayesh2812 marked this conversation as resolved.
Show resolved Hide resolved
const styleObj = jsonValue["attrs"]["style"];
Jayesh2812 marked this conversation as resolved.
Show resolved Hide resolved
if(jsonValue["attrs"]["position"] === 'center'){
styleObj['object-fit'] = "contain"
}
delete styleObj['float']
attrsJson["style"] = getStyleStringFromObject(styleObj);
console.dir({jsonValue, attrsJson, figureStyles, styleObj}, {depth:null})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this console

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done


}
else if (attrsJson['type'] === "asset") {
attrsJson['data-sys-asset-filelink'] = allattrs['asset-link']
delete attrsJson['asset-link']
attrsJson['data-sys-asset-uid'] = allattrs['asset-uid']
Expand Down Expand Up @@ -468,3 +520,10 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string

return children
}


function getStyleStringFromObject(styleObj: { [key: string]: string }) {
return Object.keys(styleObj)
.map((key) => `${key}: ${styleObj[key]}`)
.join("; ");
}
Loading