Skip to content

Commit

Permalink
Ad xml correct creator image 5 (#116)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Svensson <[email protected]>
  • Loading branch information
Kinatzo and Kinatzo authored Oct 8, 2023
1 parent 7f972d9 commit a66fa5e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
TextRun,
TocSeparator,
Types,
ImageResource as ADImageResource,
TextStyle,
} from "../../abstract-document/index";
import {
Expand All @@ -35,12 +34,11 @@ import {
ImageRowProps,
ImageParagraph,
ImageRow,
ImageResource,
} from "./custom-elements";

export type ADCreatorFn = (props?: Record<string, unknown>, children?: ReadonlyArray<unknown>) => unknown;

export type ImageResource = ADImageResource.ImageResource & { readonly width?: number; readonly height?: number };

export type TextRunProps = {
readonly text: string;
readonly styleName?: string;
Expand All @@ -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<string, unknown>) => {
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<string, unknown>) => Image.create(imageProps(images, props) as unknown as Image.ImageProps),
Table: (props, children: ReadonlyArray<TableRow.TableRow>) =>
Table.create(props as unknown as Table.TableProps, children),
TableRow: (props, children: ReadonlyArray<TableCell.TableCell>) => TableRow.create(props, children),
Expand Down Expand Up @@ -267,22 +254,24 @@ export const propsCreators: Record<string, ADCreatorFn> = {
},
};

function mutateImageProps(images: Record<string, ImageResource>, props: Record<string, unknown>): void {
const image = images[(props.src as string) ?? ""];
function imageProps(images: Record<string, ImageResource>, props: Record<string, unknown>): Record<string, unknown> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
TextStyle,
ParagraphStyle,
TableCellStyle,
ImageResource,
TextRun,
Paragraph,
TableCell,
TableRow,
Image,
ImageResource as ADImageResource,
} from "../../abstract-document/index";

type StyleProps = {
Expand Down Expand Up @@ -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<string, string>): TableRow.TableRow {
Expand All @@ -92,9 +94,9 @@ export type ImageCellProps = {
} & ImageParagraphProps;

export function ImageCell(props: ImageCellProps, styleNameTypes: Record<string, string>): 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])
Expand All @@ -103,7 +105,7 @@ export function ImageCell(props: ImageCellProps, styleNameTypes: Record<string,
}

export type ImageParagraphProps = {
readonly image: ImageResource.ImageResource | undefined;
readonly imageResource: ImageResource;
readonly width: number;
readonly height: number;
readonly paragraphStyle?: ParagraphStyle.ParagraphStyle;
Expand All @@ -114,9 +116,9 @@ export function ImageParagraph(
props: ImageParagraphProps,
styleNameTypes: Record<string, string>
): 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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const textRow = `<xs:complexType name="TextRow">
<xs:element name="paragraphStyle" type="ParagraphStyle" />
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="style" type="TextStyle" />
<xs:element name="textStyle" type="TextStyle" />
</xs:choice>
<xs:attribute name="styleNames" type="xs:string" >
<xs:annotation>
Expand All @@ -29,13 +29,13 @@ export const textCell = `<xs:complexType name="TextCell">
<xs:documentation>Shortcut to create a \\<TableCell> \\<Paragraph> \\<TextRun text="..." /> \\</Paragraph> \\</TableCell></xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="cellStyle" type="TableCellStyle" />
<xs:element name="style" type="TableCellStyle" />
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="paragraphStyle" type="ParagraphStyle" />
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="style" type="TextStyle" />
<xs:element name="textStyle" type="TextStyle" />
</xs:choice>
<xs:attribute name="styleNames" type="xs:string" >
<xs:annotation>
Expand All @@ -54,10 +54,10 @@ export const textParagraph = `<xs:complexType name="TextParagraph">
<xs:documentation>Shortcut to create \\<Paragraph> \\<TextRun text="..." /> \\</Paragraph></xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="paragraphStyle" type="ParagraphStyle" />
<xs:element name="style" type="ParagraphStyle" />
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="style" type="TextStyle" />
<xs:element name="textStyle" type="TextStyle" />
</xs:choice>
<xs:attribute name="styleNames" type="xs:string" >
<xs:annotation>
Expand Down Expand Up @@ -99,7 +99,7 @@ export const imageCell = `<xs:complexType name="ImageCell">
<xs:attribute name="width" type="xs:decimal" />
<xs:attribute name="height" type="xs:decimal" />
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="cellStyle" type="TableCellStyle" />
<xs:element name="style" type="TableCellStyle" />
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="paragraphStyle" type="ParagraphStyle" />
Expand All @@ -116,7 +116,7 @@ export const imageParagraph = `<xs:complexType name="ImageParagraph">
<xs:attribute name="width" type="xs:decimal" />
<xs:attribute name="height" type="xs:decimal" />
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="paragraphStyle" type="ParagraphStyle" />
<xs:element name="style" type="ParagraphStyle" />
</xs:choice>
</xs:complexType>`;
export const imageParagraphElement = `<xs:element name="ImageParagraph" type="ImageParagraph" maxOccurs="unbounded" />`;

0 comments on commit a66fa5e

Please sign in to comment.