Add more run properties and Universal measure

This commit is contained in:
Dolan Miu
2022-12-29 09:57:15 +00:00
parent 5907132062
commit 4513bb529b
15 changed files with 342 additions and 190 deletions

View File

@ -1,25 +1,23 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
export interface IColumnAttributes {
readonly width: number | string;
readonly space?: number | string;
}
// <xsd:complexType name="CT_Column">
// <xsd:attribute name="w" type="s:ST_TwipsMeasure" use="optional" />
// <xsd:attribute name="space" type="s:ST_TwipsMeasure" use="optional" default="0" />
// </xsd:complexType>
export class ColumnAttributes extends XmlAttributeComponent<IColumnAttributes> {
protected readonly xmlKeys = {
width: "w:w",
space: "w:space",
};
}
type IColumnAttributes = {
readonly width: number | PositiveUniversalMeasure;
readonly space?: number | PositiveUniversalMeasure;
};
export class Column extends XmlComponent {
public constructor({ width, space }: IColumnAttributes) {
super("w:col");
this.root.push(
new ColumnAttributes({
width: twipsMeasureValue(width),
space: space === undefined ? undefined : twipsMeasureValue(space),
new NextAttributeComponent<IColumnAttributes>({
width: { key: "w:w", value: twipsMeasureValue(width) },
space: { key: "w:space", value: space === undefined ? undefined : twipsMeasureValue(space) },
}),
);
}

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
import { Column } from "./column";
@ -12,32 +12,23 @@ import { Column } from "./column";
// <xsd:attribute name="num" type="ST_DecimalNumber" use="optional" default="1"/>
// <xsd:attribute name="sep" type="s:ST_OnOff" use="optional"/>
// </xsd:complexType>
export interface IColumnsAttributes {
readonly space?: number | string;
export type IColumnsAttributes = {
readonly space?: number | PositiveUniversalMeasure;
readonly count?: number;
readonly separate?: boolean;
readonly equalWidth?: boolean;
readonly children?: readonly Column[];
}
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
protected readonly xmlKeys = {
space: "w:space",
count: "w:num",
separate: "w:sep",
equalWidth: "w:equalWidth",
};
}
};
export class Columns extends XmlComponent {
public constructor({ space, count, separate, equalWidth, children }: IColumnsAttributes) {
super("w:cols");
this.root.push(
new ColumnsAttributes({
space: space === undefined ? undefined : twipsMeasureValue(space),
count: count === undefined ? undefined : decimalNumber(count),
separate,
equalWidth,
new NextAttributeComponent<Omit<IColumnsAttributes, "children">>({
space: { key: "w:space", value: space === undefined ? undefined : twipsMeasureValue(space) },
count: { key: "w:num", value: count === undefined ? undefined : decimalNumber(count) },
separate: { key: "w:sep", value: separate },
equalWidth: { key: "w:equalWidth", value: equalWidth },
}),
);

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPsectionLineNumbering.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:simpleType name="ST_LineNumberRestart">
// <xsd:restriction base="xsd:string">
@ -26,27 +26,23 @@ export interface ILineNumberAttributes {
readonly countBy?: number;
readonly start?: number;
readonly restart?: LineNumberRestartFormat;
readonly distance?: number | string;
}
export class LineNumberAttributes extends XmlAttributeComponent<ILineNumberAttributes> {
protected readonly xmlKeys = {
countBy: "w:countBy",
start: "w:start",
restart: "w:restart",
distance: "w:distance",
};
readonly distance?: number | PositiveUniversalMeasure;
}
export class LineNumberType extends XmlComponent {
public constructor({ countBy, start, restart, distance }: ILineNumberAttributes) {
super("w:lnNumType");
this.root.push(
new LineNumberAttributes({
countBy: countBy === undefined ? undefined : decimalNumber(countBy),
start: start === undefined ? undefined : decimalNumber(start),
restart,
distance: distance === undefined ? undefined : twipsMeasureValue(distance),
new NextAttributeComponent<{
readonly countBy?: number;
readonly start?: number;
readonly restart?: LineNumberRestartFormat;
readonly distance?: number | PositiveUniversalMeasure;
}>({
countBy: { key: "w:countBy", value: countBy === undefined ? undefined : decimalNumber(countBy) },
start: { key: "w:start", value: start === undefined ? undefined : decimalNumber(start) },
restart: { key: "w:restart", value: restart },
distance: { key: "w:distance", value: distance === undefined ? undefined : twipsMeasureValue(distance) },
}),
);
}

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values";
// <xsd:complexType name="CT_PageMar">
// <xsd:attribute name="top" type="ST_SignedTwipsMeasure" use="required"/>
@ -10,48 +10,36 @@ import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";
// <xsd:attribute name="footer" type="s:ST_TwipsMeasure" use="required"/>
// <xsd:attribute name="gutter" type="s:ST_TwipsMeasure" use="required"/>
// </xsd:complexType>
export interface IPageMarginAttributes {
readonly top?: number | string;
readonly right?: number | string;
readonly bottom?: number | string;
readonly left?: number | string;
readonly header?: number | string;
readonly footer?: number | string;
readonly gutter?: number | string;
}
export class PageMarginAttributes extends XmlAttributeComponent<IPageMarginAttributes> {
protected readonly xmlKeys = {
top: "w:top",
right: "w:right",
bottom: "w:bottom",
left: "w:left",
header: "w:header",
footer: "w:footer",
gutter: "w:gutter",
};
}
export type IPageMarginAttributes = {
readonly top?: number | UniversalMeasure;
readonly right?: number | PositiveUniversalMeasure;
readonly bottom?: number | UniversalMeasure;
readonly left?: number | PositiveUniversalMeasure;
readonly header?: number | PositiveUniversalMeasure;
readonly footer?: number | PositiveUniversalMeasure;
readonly gutter?: number | PositiveUniversalMeasure;
};
export class PageMargin extends XmlComponent {
public constructor(
top: number | string,
right: number | string,
bottom: number | string,
left: number | string,
header: number | string,
footer: number | string,
gutter: number | string,
top: number | UniversalMeasure,
right: number | PositiveUniversalMeasure,
bottom: number | UniversalMeasure,
left: number | PositiveUniversalMeasure,
header: number | PositiveUniversalMeasure,
footer: number | PositiveUniversalMeasure,
gutter: number | PositiveUniversalMeasure,
) {
super("w:pgMar");
this.root.push(
new PageMarginAttributes({
top: signedTwipsMeasureValue(top),
right: twipsMeasureValue(right),
bottom: signedTwipsMeasureValue(bottom),
left: twipsMeasureValue(left),
header: twipsMeasureValue(header),
footer: twipsMeasureValue(footer),
gutter: twipsMeasureValue(gutter),
new NextAttributeComponent<IPageMarginAttributes>({
top: { key: "w:top", value: signedTwipsMeasureValue(top) },
right: { key: "w:right", value: twipsMeasureValue(right) },
bottom: { key: "w:bottom", value: signedTwipsMeasureValue(bottom) },
left: { key: "w:left", value: twipsMeasureValue(left) },
header: { key: "w:header", value: twipsMeasureValue(header) },
footer: { key: "w:footer", value: twipsMeasureValue(footer) },
gutter: { key: "w:gutter", value: twipsMeasureValue(gutter) },
}),
);
}

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:simpleType name="ST_PageOrientation">
// <xsd:restriction base="xsd:string">
@ -18,22 +18,14 @@ export enum PageOrientation {
// <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/>
// <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType>
export interface IPageSizeAttributes {
readonly width?: number | string;
readonly height?: number | string;
export type IPageSizeAttributes = {
readonly width?: number | PositiveUniversalMeasure;
readonly height?: number | PositiveUniversalMeasure;
readonly orientation?: PageOrientation;
}
export class PageSizeAttributes extends XmlAttributeComponent<IPageSizeAttributes> {
protected readonly xmlKeys = {
width: "w:w",
height: "w:h",
orientation: "w:orient",
};
}
};
export class PageSize extends XmlComponent {
public constructor(width: number | string, height: number | string, orientation: PageOrientation) {
public constructor(width: number | PositiveUniversalMeasure, height: number | PositiveUniversalMeasure, orientation: PageOrientation) {
super("w:pgSz");
const flip = orientation === PageOrientation.LANDSCAPE;
@ -42,10 +34,10 @@ export class PageSize extends XmlComponent {
const heightTwips = twipsMeasureValue(height);
this.root.push(
new PageSizeAttributes({
width: flip ? heightTwips : widthTwips,
height: flip ? widthTwips : heightTwips,
orientation: orientation,
new NextAttributeComponent<IPageSizeAttributes>({
width: { key: "w:w", value: flip ? heightTwips : widthTwips },
height: { key: "w:h", value: flip ? widthTwips : heightTwips },
orientation: { key: "w:orient", value: orientation },
}),
);
}

View File

@ -17,6 +17,7 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-num
import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size";
import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction";
import { SectionType, Type } from "./properties/section-type";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
export interface IHeaderFooterGroup<T> {
readonly default?: T;
@ -76,10 +77,10 @@ export interface ISectionPropertiesOptions {
// </xsd:group>
export const sectionMarginDefaults = {
TOP: "1in",
RIGHT: "1in",
BOTTOM: "1in",
LEFT: "1in",
TOP: "1in" as UniversalMeasure,
RIGHT: "1in" as PositiveUniversalMeasure,
BOTTOM: "1in" as UniversalMeasure,
LEFT: "1in" as PositiveUniversalMeasure,
HEADER: 708,
FOOTER: 708,
GUTTER: 0,