2021-03-14 17:00:42 +00:00
|
|
|
// http://officeopenxml.com/WPparagraph-textFrames.php
|
2022-06-26 23:26:42 +01:00
|
|
|
import { HorizontalPositionAlign, VerticalPositionAlign } from "@file/shared/alignment";
|
|
|
|
import { HeightRule } from "@file/table";
|
2023-12-29 15:07:42 +00:00
|
|
|
import { BuilderElement, XmlComponent } from "@file/xml-components";
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
export const DropCapType = {
|
|
|
|
NONE: "none",
|
|
|
|
DROP: "drop",
|
|
|
|
MARGIN: "margin",
|
|
|
|
} as const;
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
export const FrameAnchorType = {
|
|
|
|
MARGIN: "margin",
|
|
|
|
PAGE: "page",
|
|
|
|
TEXT: "text",
|
|
|
|
} as const;
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
export const FrameWrap = {
|
|
|
|
AROUND: "around",
|
|
|
|
AUTO: "auto",
|
|
|
|
NONE: "none",
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
NOT_BESIDE: "notBeside",
|
|
|
|
THROUGH: "through",
|
|
|
|
TIGHT: "tight",
|
|
|
|
} as const;
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2022-04-21 21:27:01 +01:00
|
|
|
interface IBaseFrameOptions {
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly anchorLock?: boolean;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly dropCap?: (typeof DropCapType)[keyof typeof DropCapType];
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly width: number;
|
|
|
|
readonly height: number;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly wrap?: (typeof FrameWrap)[keyof typeof FrameWrap];
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly lines?: number;
|
|
|
|
readonly anchor: {
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly horizontal: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
|
|
|
|
readonly vertical: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
|
2021-03-14 17:00:42 +00:00
|
|
|
};
|
|
|
|
readonly space?: {
|
|
|
|
readonly horizontal: number;
|
|
|
|
readonly vertical: number;
|
|
|
|
};
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly rule?: (typeof HeightRule)[keyof typeof HeightRule];
|
2022-04-21 21:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IXYFrameOptions extends IBaseFrameOptions {
|
2023-12-29 15:07:42 +00:00
|
|
|
readonly type: "absolute";
|
2022-04-21 21:27:01 +01:00
|
|
|
readonly position: {
|
|
|
|
readonly x: number;
|
|
|
|
readonly y: number;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IAlignmentFrameOptions extends IBaseFrameOptions {
|
2023-12-29 15:07:42 +00:00
|
|
|
readonly type: "alignment";
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly alignment: {
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly x: (typeof HorizontalPositionAlign)[keyof typeof HorizontalPositionAlign];
|
|
|
|
readonly y: (typeof VerticalPositionAlign)[keyof typeof VerticalPositionAlign];
|
2021-03-14 17:00:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 21:27:01 +01:00
|
|
|
// Be wary of Typescript's Open types:
|
|
|
|
// https://stackoverflow.com/q/46370222/3481582
|
|
|
|
export type IFrameOptions = IXYFrameOptions | IAlignmentFrameOptions;
|
|
|
|
|
2023-12-29 15:07:42 +00:00
|
|
|
// <xsd:complexType name="CT_FramePr">
|
|
|
|
// <xsd:attribute name="dropCap" type="ST_DropCap" use="optional"/>
|
|
|
|
// <xsd:attribute name="lines" type="ST_DecimalNumber" use="optional"/>
|
|
|
|
// <xsd:attribute name="w" type="s:ST_TwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="h" type="s:ST_TwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="vSpace" type="s:ST_TwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="hSpace" type="s:ST_TwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="wrap" type="ST_Wrap" use="optional"/>
|
|
|
|
// <xsd:attribute name="hAnchor" type="ST_HAnchor" use="optional"/>
|
|
|
|
// <xsd:attribute name="vAnchor" type="ST_VAnchor" use="optional"/>
|
|
|
|
// <xsd:attribute name="x" type="ST_SignedTwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="xAlign" type="s:ST_XAlign" use="optional"/>
|
|
|
|
// <xsd:attribute name="y" type="ST_SignedTwipsMeasure" use="optional"/>
|
|
|
|
// <xsd:attribute name="yAlign" type="s:ST_YAlign" use="optional"/>
|
|
|
|
// <xsd:attribute name="hRule" type="ST_HeightRule" use="optional"/>
|
|
|
|
// <xsd:attribute name="anchorLock" type="s:ST_OnOff" use="optional"/>
|
|
|
|
// </xsd:complexType>
|
|
|
|
type FramePropertiesAttributes = {
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly anchorLock?: boolean;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly dropCap?: (typeof DropCapType)[keyof typeof DropCapType];
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly width: number;
|
|
|
|
readonly height: number;
|
2022-04-21 21:27:01 +01:00
|
|
|
readonly x?: number;
|
|
|
|
readonly y?: number;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly wrap?: (typeof FrameWrap)[keyof typeof FrameWrap];
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly lines?: number;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly anchorHorizontal?: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
|
|
|
|
readonly anchorVertical?: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly spaceHorizontal?: number;
|
|
|
|
readonly spaceVertical?: number;
|
2023-12-22 10:25:00 +09:00
|
|
|
readonly rule?: (typeof HeightRule)[keyof typeof HeightRule];
|
|
|
|
readonly alignmentX?: (typeof HorizontalPositionAlign)[keyof typeof HorizontalPositionAlign];
|
|
|
|
readonly alignmentY?: (typeof VerticalPositionAlign)[keyof typeof VerticalPositionAlign];
|
2023-12-29 15:07:42 +00:00
|
|
|
};
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2023-12-29 15:07:42 +00:00
|
|
|
export const createFrameProperties = (options: IFrameOptions): XmlComponent =>
|
|
|
|
new BuilderElement<FramePropertiesAttributes>({
|
|
|
|
name: "w:framePr",
|
|
|
|
attributes: {
|
|
|
|
anchorLock: {
|
|
|
|
key: "w:anchorLock",
|
|
|
|
value: options.anchorLock,
|
|
|
|
},
|
|
|
|
dropCap: {
|
|
|
|
key: "w:dropCap",
|
|
|
|
value: options.dropCap,
|
|
|
|
},
|
|
|
|
width: {
|
|
|
|
key: "w:w",
|
|
|
|
value: options.width,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
key: "w:h",
|
|
|
|
value: options.height,
|
|
|
|
},
|
|
|
|
x: {
|
|
|
|
key: "w:x",
|
|
|
|
value: (options as IXYFrameOptions).position ? (options as IXYFrameOptions).position.x : undefined,
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
key: "w:y",
|
|
|
|
value: (options as IXYFrameOptions).position ? (options as IXYFrameOptions).position.y : undefined,
|
|
|
|
},
|
|
|
|
anchorHorizontal: {
|
|
|
|
key: "w:hAnchor",
|
|
|
|
value: options.anchor.horizontal,
|
|
|
|
},
|
|
|
|
anchorVertical: {
|
|
|
|
key: "w:vAnchor",
|
|
|
|
value: options.anchor.vertical,
|
|
|
|
},
|
|
|
|
spaceHorizontal: {
|
|
|
|
key: "w:hSpace",
|
|
|
|
value: options.space?.horizontal,
|
|
|
|
},
|
|
|
|
spaceVertical: {
|
|
|
|
key: "w:vSpace",
|
|
|
|
value: options.space?.vertical,
|
|
|
|
},
|
|
|
|
rule: {
|
|
|
|
key: "w:hRule",
|
|
|
|
value: options.rule,
|
|
|
|
},
|
|
|
|
alignmentX: {
|
|
|
|
key: "w:xAlign",
|
|
|
|
value: (options as IAlignmentFrameOptions).alignment ? (options as IAlignmentFrameOptions).alignment.x : undefined,
|
|
|
|
},
|
|
|
|
alignmentY: {
|
|
|
|
key: "w:yAlign",
|
|
|
|
value: (options as IAlignmentFrameOptions).alignment ? (options as IAlignmentFrameOptions).alignment.y : undefined,
|
|
|
|
},
|
|
|
|
lines: {
|
|
|
|
key: "w:lines",
|
|
|
|
value: options.lines,
|
|
|
|
},
|
|
|
|
wrap: {
|
|
|
|
key: "w:wrap",
|
|
|
|
value: options.wrap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|