Skip to content

Commit 6d0a1f0

Browse files
committed
feat: support for images and asset as links
1 parent 2ee2a85 commit 6d0a1f0

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

src/fromRedactor.tsx

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -543,31 +543,28 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
543543

544544
}
545545
if (newChildren[0]?.type === 'img') {
546-
if (newChildren[0].attrs.width) {
547-
sizeAttrs.width = newChildren[0].attrs.width.toString()
548-
}
549-
elementAttrs = getImageAttributes(
550-
elementAttrs,
551-
{ ...newChildren[0].attrs, ...sizeAttrs, caption: extraAttrs['caption'], style: {'text-align': style['text-align']} },
552-
{ ...extraAttrs, ...sizeAttrs });
546+
elementAttrs = getFinalImageAttributes({elementAttrs, newChildren, extraAttrs, sizeAttrs})
547+
}
548+
if (newChildren[0]?.type === 'reference') {
549+
elementAttrs = getReferenceAttributes({elementAttrs, newChildren, extraAttrs, sizeAttrs})
553550
}
554551
if (newChildren[0]?.type === 'a') {
555-
const { link, target } = newChildren[0].attrs?.["redactor-attributes"]
556-
extraAttrs['link'] = link
552+
const { href, target } = newChildren[0].attrs?.["redactor-attributes"]
553+
extraAttrs['anchorLink'] = href;
557554
if (target && target !== '') {
558-
extraAttrs['target'] = true
555+
extraAttrs['target'] = true;
556+
}
557+
const imageAttrs = newChildren[0].children;
558+
559+
if(imageAttrs[0].type === 'img'){
560+
elementAttrs = getFinalImageAttributes({elementAttrs, newChildren : imageAttrs, extraAttrs, sizeAttrs})
561+
562+
}
563+
if(imageAttrs[0].type === 'reference'){
564+
elementAttrs = getReferenceAttributes({elementAttrs, newChildren: imageAttrs, extraAttrs, sizeAttrs})
559565
}
560-
const imageAttrs = newChildren[0].children[0]
561-
elementAttrs = getImageAttributes(elementAttrs, imageAttrs.attrs || {}, { ...extraAttrs, ...sizeAttrs })
562-
}
563-
if (newChildren[0]?.type === 'reference') {
564-
extraAttrs['asset-caption'] = extraAttrs['caption']
565-
elementAttrs = getImageAttributes(
566-
elementAttrs,
567-
{ ...newChildren[0].attrs, ...sizeAttrs, position: extraAttrs.position, style: {'text-align': style['text-align']} },
568-
{ ...extraAttrs, ...sizeAttrs });
569-
elementAttrs.type = "reference"
570566
}
567+
571568
return jsx('element', elementAttrs, [{ text: '' }])
572569
}
573570

@@ -755,3 +752,43 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any)
755752
}
756753
return elementAttrs
757754
}
755+
756+
const getReferenceAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttrs} : any) => {
757+
758+
let { style } = elementAttrs.attrs;
759+
760+
extraAttrs['asset-caption'] = extraAttrs['caption'];
761+
762+
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style['text-align'] }, position: extraAttrs.position }
763+
extraAttrs = { ...extraAttrs, ...sizeAttrs }
764+
765+
if (!childAttrs.position) {
766+
delete childAttrs.position
767+
}
768+
769+
const referenceAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
770+
771+
referenceAttrs.type = "reference";
772+
773+
return referenceAttrs
774+
}
775+
776+
const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttrs} : any) => {
777+
778+
let { style } = elementAttrs.attrs;
779+
780+
if (newChildren[0].attrs.width) {
781+
sizeAttrs.width = newChildren[0].attrs.width.toString();
782+
}
783+
784+
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style['text-align'] }, caption: extraAttrs['caption'] }
785+
extraAttrs = { ...extraAttrs, ...sizeAttrs }
786+
787+
if (!childAttrs.caption) {
788+
delete childAttrs.caption
789+
}
790+
791+
const imageAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
792+
793+
return imageAttrs
794+
}

0 commit comments

Comments
 (0)