Skip to content

Commit 0a55c01

Browse files
committed
feat: style support for image and asset images
1 parent fdbcf01 commit 0a55c01

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/fromRedactor.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,21 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
510510
sizeAttrs.width = el.style.width
511511
if (sizeAttrs.width[sizeAttrs.width.length - 1] === '%') {
512512
sizeAttrs.width = Number(sizeAttrs.width.slice(0, sizeAttrs.width.length - 1))
513-
} else if (sizeAttrs.width.slice(sizeAttrs.width.length - 2) === 'px') {
514-
sizeAttrs.width = (Number(sizeAttrs.width.slice(0, sizeAttrs.width.length - 2)) / window?.screen?.width || 1920) * 100
513+
}
514+
515+
else if (sizeAttrs.width.slice(sizeAttrs.width.length - 2) === 'px') {
516+
sizeAttrs.width = (Number(sizeAttrs.width.slice(0, sizeAttrs.width.length - 2)) / 1920) * 100
515517
}
516518
}
517519
if (el.style?.['max-width']) {
518520
sizeAttrs['max-width'] = el.style['max-width']
519521
if (sizeAttrs['max-width'][sizeAttrs['max-width'].length - 1] === '%') {
520522
sizeAttrs['max-width'] = Number(sizeAttrs['max-width'].slice(0, sizeAttrs['max-width'].length - 1))
521-
} else if (sizeAttrs['max-width'].slice(sizeAttrs['max-width'].length - 2) === 'px') {
523+
}
524+
525+
else if (sizeAttrs['max-width'].slice(sizeAttrs['max-width'].length - 2) === 'px') {
522526
sizeAttrs['max-width'] =
523-
(Number(sizeAttrs['max-width'].slice(0, sizeAttrs['max-width'].length - 2)) / window?.screen?.width || 1920) * 100
527+
(Number(sizeAttrs['max-width'].slice(0, sizeAttrs['max-width'].length - 2)) / 1920) * 100
524528
}
525529
}
526530
let captionElements = el.getElementsByTagName("FIGCAPTION")
@@ -536,7 +540,6 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
536540
}
537541
extraAttrs['captionAttrs'] = captionAttrs
538542
extraAttrs['caption'] = captionElements?.[0]?.textContent
539-
// console.log(extraAttrs);
540543

541544
}
542545
if (newChildren[0]?.type === 'img') {
@@ -545,9 +548,8 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
545548
}
546549
elementAttrs = getImageAttributes(
547550
elementAttrs,
548-
{ ...newChildren[0].attrs, ...sizeAttrs },
549-
{ ...extraAttrs, ...sizeAttrs }
550-
)
551+
{ ...newChildren[0].attrs, ...sizeAttrs, caption: extraAttrs['caption'], style: {'text-align': style['text-align']} },
552+
{ ...extraAttrs, ...sizeAttrs });
551553
}
552554
if (newChildren[0]?.type === 'a') {
553555
const { link, target } = newChildren[0].attrs?.["redactor-attributes"]
@@ -558,12 +560,13 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
558560
const imageAttrs = newChildren[0].children[0]
559561
elementAttrs = getImageAttributes(elementAttrs, imageAttrs.attrs || {}, { ...extraAttrs, ...sizeAttrs })
560562
}
561-
if (newChildren[0]?.type === 'reference' && newChildren[0]?.attrs?.default) {
563+
if (newChildren[0]?.type === 'reference') {
564+
extraAttrs['asset-caption'] = extraAttrs['caption']
562565
elementAttrs = getImageAttributes(
563-
elementAttrs,
564-
{ ...newChildren[0].attrs, ...sizeAttrs },
565-
{ ...extraAttrs, ...sizeAttrs }
566-
)
566+
elementAttrs,
567+
{ ...newChildren[0].attrs, ...sizeAttrs, position: extraAttrs.position, style: {'text-align': style['text-align']} },
568+
{ ...extraAttrs, ...sizeAttrs });
569+
elementAttrs.type = "reference"
567570
}
568571
return jsx('element', elementAttrs, [{ text: '' }])
569572
}

0 commit comments

Comments
 (0)