From a66fa5e8d284491f67bc5767b3604df9fcbe0d96 Mon Sep 17 00:00:00 2001 From: Kinatzo Date: Sun, 8 Oct 2023 13:50:14 +0200 Subject: [PATCH] Ad xml correct creator image 5 (#116) Co-authored-by: Tim Svensson --- .../abstract-doc-of-xml/creator.ts | 45 +++++++------------ .../abstract-doc-of-xml/custom-elements.tsx | 14 +++--- .../xsd-template/custom-elements.ts | 14 +++--- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/creator.ts b/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/creator.ts index 48925d8b..8808cec6 100644 --- a/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/creator.ts +++ b/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/creator.ts @@ -19,7 +19,6 @@ import { TextRun, TocSeparator, Types, - ImageResource as ADImageResource, TextStyle, } from "../../abstract-document/index"; import { @@ -35,12 +34,11 @@ import { ImageRowProps, ImageParagraph, ImageRow, + ImageResource, } from "./custom-elements"; export type ADCreatorFn = (props?: Record, children?: ReadonlyArray) => unknown; -export type ImageResource = ADImageResource.ImageResource & { readonly width?: number; readonly height?: number }; - export type TextRunProps = { readonly text: string; readonly styleName?: string; @@ -65,22 +63,11 @@ export const creators: ( TextCell: (props: TextCellProps) => TextCell(props, styleNames), TextParagraph: (props: TextParagraphProps) => TextParagraph(props, styleNames), TextRun: (props) => TextRun.create(props as unknown as TextRun.TextRunProps), - ImageRow: (props: ImageRowProps) => { - mutateImageProps(images, props); - return ImageRow(props, styleNames); - }, - ImageCell: (props: ImageCellProps) => { - mutateImageProps(images, props); - return ImageCell(props, styleNames); - }, - ImageParagraph: (props: ImageParagraphProps) => { - mutateImageProps(images, props); - return ImageParagraph(props, styleNames); - }, - Image: (props: Record) => { - mutateImageProps(images, props); - return Image.create(props as unknown as Image.ImageProps); - }, + ImageRow: (props: ImageRowProps) => ImageRow(imageProps(images, props) as unknown as ImageRowProps, styleNames), + ImageCell: (props: ImageCellProps) => ImageCell(imageProps(images, props) as unknown as ImageCellProps, styleNames), + ImageParagraph: (props: ImageParagraphProps) => + ImageParagraph(imageProps(images, props) as unknown as ImageParagraphProps, styleNames), + Image: (props: Record) => Image.create(imageProps(images, props) as unknown as Image.ImageProps), Table: (props, children: ReadonlyArray) => Table.create(props as unknown as Table.TableProps, children), TableRow: (props, children: ReadonlyArray) => TableRow.create(props, children), @@ -267,22 +254,24 @@ export const propsCreators: Record = { }, }; -function mutateImageProps(images: Record, props: Record): void { - const image = images[(props.src as string) ?? ""]; +function imageProps(images: Record, props: Record): Record { + const newProps = { ...props }; + const image = images[(newProps.src as string) ?? ""]; if (image) { if (image.width && image.height) { - props.width = image.width; - props.height = image.height; + newProps.width = image.width; + newProps.height = image.height; } else { - const scaleX = (props.width as number) / image.abstractImage.size.width; - const scaleY = (props.height as number) / image.abstractImage.size.height; + const scaleX = (newProps.width as number) / image.abstractImage.size.width; + const scaleY = (newProps.height as number) / image.abstractImage.size.height; if (scaleX < scaleY) { - props.height = (props.height as number) * (scaleX / scaleY); + newProps.height = (newProps.height as number) * (scaleX / scaleY); } else { - props.width = (props.width as number) * (scaleY / scaleX); + newProps.width = (newProps.width as number) * (scaleY / scaleX); } } - props.imageResource = images[props.src as string]; + newProps.imageResource = images[newProps.src as string]; } + return newProps; } diff --git a/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx b/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx index 8c45d561..a23f3ede 100644 --- a/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx +++ b/packages/abstract-document/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx @@ -2,12 +2,12 @@ import { TextStyle, ParagraphStyle, TableCellStyle, - ImageResource, TextRun, Paragraph, TableCell, TableRow, Image, + ImageResource as ADImageResource, } from "../../abstract-document/index"; type StyleProps = { @@ -79,6 +79,8 @@ export function TextParagraph(props: TextParagraphProps, styleNameTypes: Record< return Paragraph.create({ style: paragraphStyle, styleName: styleNames.ParagraphStyle }, [textRun]); } +export type ImageResource = ADImageResource.ImageResource & { readonly width?: number; readonly height?: number }; + export type ImageRowProps = ImageCellProps & {}; export function ImageRow(props: ImageCellProps, styleNameTypes: Record): TableRow.TableRow { @@ -92,9 +94,9 @@ export type ImageCellProps = { } & ImageParagraphProps; export function ImageCell(props: ImageCellProps, styleNameTypes: Record): TableCell.TableCell { - const { image, width, height, paragraphStyle, cellStyle, columnSpan, rowSpan } = props; + const { imageResource, width, height, paragraphStyle, cellStyle, columnSpan, rowSpan } = props; const styleNames = extractStyleNames(props.styleNames, styleNameTypes); - const imageElement = image && Image.create({ imageResource: image, width, height }); + const imageElement = imageResource && Image.create({ imageResource, width, height }); const pararaphProps = { style: paragraphStyle, styleName: styleNames.ParagraphStyle }; const paragraph = imageElement ? Paragraph.create(pararaphProps, [imageElement]) @@ -103,7 +105,7 @@ export function ImageCell(props: ImageCellProps, styleNameTypes: Record ): Paragraph.Paragraph | undefined { - const { image, width, height, paragraphStyle } = props; + const { imageResource, width, height, paragraphStyle } = props; const styleNames = extractStyleNames(props.styleNames, styleNameTypes); - const imageElement = image && Image.create({ imageResource: image, width, height }); + const imageElement = imageResource && Image.create({ imageResource, width, height }); const pararaphProps = { style: paragraphStyle, styleName: styleNames.ParagraphStyle }; return imageElement ? Paragraph.create(pararaphProps, [imageElement]) diff --git a/packages/abstract-document/src/abstract-document-xml/xsd-template/custom-elements.ts b/packages/abstract-document/src/abstract-document-xml/xsd-template/custom-elements.ts index 36fce26d..3235d6d0 100644 --- a/packages/abstract-document/src/abstract-document-xml/xsd-template/custom-elements.ts +++ b/packages/abstract-document/src/abstract-document-xml/xsd-template/custom-elements.ts @@ -10,7 +10,7 @@ export const textRow = ` - + @@ -29,13 +29,13 @@ export const textCell = ` Shortcut to create a \\ \\ \\ \\ \\ - + - + @@ -54,10 +54,10 @@ export const textParagraph = ` Shortcut to create \\ \\ \\ - + - + @@ -99,7 +99,7 @@ export const imageCell = ` - + @@ -116,7 +116,7 @@ export const imageParagraph = ` - + `; export const imageParagraphElement = ``;