Merge pull request #1491 from dolanmiu/feat/fix-frame-option-type

#1487 Create union type for IFrameOptions
This commit is contained in:
Dolan
2022-04-25 10:04:28 +01:00
committed by GitHub
3 changed files with 92 additions and 12 deletions

2
package-lock.json generated
View File

@ -3571,7 +3571,7 @@
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"integrity": "sha1-UWbihkV/AzBgZL5Ul+jbsMPTIIM=",
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"

View File

@ -82,5 +82,75 @@ describe("FrameProperties", () => {
},
});
});
it("should create without x and y", () => {
const currentFrameProperties = new FrameProperties({
width: 4000,
height: 1000,
anchor: {
horizontal: FrameAnchorType.MARGIN,
vertical: FrameAnchorType.MARGIN,
},
alignment: {
x: HorizontalPositionAlign.CENTER,
y: VerticalPositionAlign.TOP,
},
space: {
horizontal: 100,
vertical: 200,
},
});
const tree = new Formatter().format(currentFrameProperties);
expect(tree).to.deep.equal({
"w:framePr": {
_attr: {
"w:h": 1000,
"w:hAnchor": "margin",
"w:vAnchor": "margin",
"w:w": 4000,
"w:xAlign": "center",
"w:yAlign": "top",
"w:hSpace": 100,
"w:vSpace": 200,
},
},
});
});
it("should create without alignments", () => {
const currentFrameProperties = new FrameProperties({
position: {
x: 1000,
y: 3000,
},
width: 4000,
height: 1000,
anchor: {
horizontal: FrameAnchorType.MARGIN,
vertical: FrameAnchorType.MARGIN,
},
space: {
horizontal: 100,
vertical: 200,
},
});
const tree = new Formatter().format(currentFrameProperties);
expect(tree).to.deep.equal({
"w:framePr": {
_attr: {
"w:h": 1000,
"w:hAnchor": "margin",
"w:vAnchor": "margin",
"w:w": 4000,
"w:x": 1000,
"w:y": 3000,
"w:hSpace": 100,
"w:vSpace": 200,
},
},
});
});
});
});

View File

@ -24,15 +24,11 @@ export enum FrameWrap {
TIGHT = "tight",
}
export interface IFrameOptions {
interface IBaseFrameOptions {
readonly anchorLock?: boolean;
readonly dropCap?: DropCapType;
readonly width: number;
readonly height: number;
readonly position: {
readonly x: number;
readonly y: number;
};
readonly wrap?: FrameWrap;
readonly lines?: number;
readonly anchor: {
@ -44,19 +40,33 @@ export interface IFrameOptions {
readonly vertical: number;
};
readonly rule?: HeightRule;
}
export interface IXYFrameOptions extends IBaseFrameOptions {
readonly position: {
readonly x: number;
readonly y: number;
};
}
export interface IAlignmentFrameOptions extends IBaseFrameOptions {
readonly alignment: {
readonly x: HorizontalPositionAlign;
readonly y: VerticalPositionAlign;
};
}
// Be wary of Typescript's Open types:
// https://stackoverflow.com/q/46370222/3481582
export type IFrameOptions = IXYFrameOptions | IAlignmentFrameOptions;
export class FramePropertiesAttributes extends XmlAttributeComponent<{
readonly anchorLock?: boolean;
readonly dropCap?: DropCapType;
readonly width: number;
readonly height: number;
readonly x: number;
readonly y: number;
readonly x?: number;
readonly y?: number;
readonly wrap?: FrameWrap;
readonly lines?: number;
readonly anchorHorizontal?: FrameAnchorType;
@ -95,15 +105,15 @@ export class FrameProperties extends XmlComponent {
dropCap: options.dropCap,
width: options.width,
height: options.height,
x: options.position.x,
y: options.position.y,
x: (options as IXYFrameOptions).position ? (options as IXYFrameOptions).position.x : undefined,
y: (options as IXYFrameOptions).position ? (options as IXYFrameOptions).position.y : undefined,
anchorHorizontal: options.anchor.horizontal,
anchorVertical: options.anchor.vertical,
spaceHorizontal: options.space?.horizontal,
spaceVertical: options.space?.vertical,
rule: options.rule,
alignmentX: options.alignment.x,
alignmentY: options.alignment.y,
alignmentX: (options as IAlignmentFrameOptions).alignment ? (options as IAlignmentFrameOptions).alignment.x : undefined,
alignmentY: (options as IAlignmentFrameOptions).alignment ? (options as IAlignmentFrameOptions).alignment.y : undefined,
lines: options.lines,
wrap: options.wrap,
}),