progress on clean up file/paragraph

This commit is contained in:
Tom Hunkapiller
2021-05-24 17:12:10 +03:00
parent 4466145d00
commit 7bfe8f26f0
8 changed files with 80 additions and 396 deletions

View File

@ -1,5 +1,4 @@
import { Run } from "file/paragraph/run";
import { Style } from "file/paragraph/run/style";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class FootNoteReferenceRunAttributes extends XmlAttributeComponent<{
@ -24,9 +23,7 @@ export class FootnoteReference extends XmlComponent {
export class FootnoteReferenceRun extends Run {
constructor(id: number) {
super({});
this.properties.push(new Style("FootnoteReference"));
super({ style: "FootnoteReference" });
this.root.push(new FootnoteReference(id));
}

View File

@ -1,29 +1,57 @@
// http://officeopenxml.com/WPindentation.php
import { signedTwipsMeasureValue, twipsMeasureValue } from "file/values";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface IIndentAttributesProperties {
readonly left?: number;
readonly hanging?: number;
readonly firstLine?: number;
readonly start?: number;
readonly end?: number;
readonly right?: number;
readonly start?: number | string;
readonly end?: number | string;
readonly left?: number | string;
readonly right?: number | string;
readonly hanging?: number | string;
readonly firstLine?: number | string;
}
// <xsd:complexType name="CT_Ind">
// <xsd:attribute name="start" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="startChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="end" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="endChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="left" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="leftChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="right" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="rightChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="hanging" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="hangingChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="firstLine" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="firstLineChars" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType>
class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties> {
protected readonly xmlKeys = {
left: "w:left",
hanging: "w:hanging",
firstLine: "w:firstLine",
start: "w:start",
end: "w:end",
right: "w:end", // alias
left: "w:left",
right: "w:right",
hanging: "w:hanging",
firstLine: "w:firstLine",
};
}
// <xsd:complexType name="CT_PPrBase">
// <xsd:sequence>
// ...
// <xsd:element name="ind" type="CT_Ind" minOccurs="0"/>
export class Indent extends XmlComponent {
constructor(attrs: IIndentAttributesProperties) {
constructor({ start, end, left, right, hanging, firstLine }: IIndentAttributesProperties) {
super("w:ind");
this.root.push(new IndentAttributes(attrs));
this.root.push(
new IndentAttributes({
start: start === undefined ? undefined : signedTwipsMeasureValue(start),
end: end === undefined ? undefined : signedTwipsMeasureValue(end),
left: left === undefined ? undefined : signedTwipsMeasureValue(left),
right: right === undefined ? undefined : signedTwipsMeasureValue(right),
hanging: hanging === undefined ? undefined : twipsMeasureValue(hanging),
firstLine: firstLine === undefined ? undefined : twipsMeasureValue(firstLine),
}),
);
}
}

View File

@ -1,6 +1,30 @@
// http://officeopenxml.com/WPtextSpecialContent-break.php
import { XmlComponent } from "file/xml-components";
// <xsd:group name="EG_RunInnerContent">
// ...
// <xsd:element name="br" type="CT_Br"/>
// <xsd:complexType name="CT_Br">
// <xsd:attribute name="type" type="ST_BrType" use="optional"/>
// <xsd:attribute name="clear" type="ST_BrClear" use="optional"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_BrType">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="page"/>
// <xsd:enumeration value="column"/>
// <xsd:enumeration value="textWrapping"/>
// </xsd:restriction>
// </xsd:simpleType>
// <xsd:simpleType name="ST_BrClear">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="none"/>
// <xsd:enumeration value="left"/>
// <xsd:enumeration value="right"/>
// <xsd:enumeration value="all"/>
// </xsd:restriction>
// </xsd:simpleType>
export class Break extends XmlComponent {
constructor() {
super("w:br");

View File

@ -1,10 +1,9 @@
import { IShadingAttributesProperties, Shading } from "file/shading";
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, XmlComponent } from "file/xml-components";
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "file/xml-components";
import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting";
import { IFontAttributesProperties, RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script";
import { Style } from "./style";
import { Underline, UnderlineType } from "./underline";
interface IFontOptions {
@ -162,7 +161,7 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
}
if (options.style) {
this.push(new Style(options.style));
this.push(new StringValueElement("w:rStyle", options.style));
}
if (options.font) {

View File

@ -1,12 +0,0 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class StyleAttributes extends XmlAttributeComponent<{ readonly val: string }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class Style extends XmlComponent {
constructor(styleId: string) {
super("w:rStyle");
this.root.push(new StyleAttributes({ val: styleId }));
}
}

View File

@ -1,5 +1,11 @@
import { XmlComponent } from "file/xml-components";
// <xsd:group name="EG_RunInnerContent">
// ...
// <xsd:element name="tab" type="CT_Empty" minOccurs="0"/>
//
// TODO: this is unused and undocumented currently.
// I think the intended use was for users to import, and insert as a child of `Run`.
export class Tab extends XmlComponent {
constructor() {
super("w:tab");

View File

@ -2,12 +2,12 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import * as u from "./underline";
import { Underline, UnderlineType } from "./underline";
describe("Underline", () => {
describe("#constructor()", () => {
it("should create a new Underline object with u:u as the rootKey", () => {
const underline = new u.Underline();
const underline = new Underline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
@ -19,7 +19,7 @@ describe("Underline", () => {
});
it("should default to 'single' and no color", () => {
const underline = new u.Underline();
const underline = new Underline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": { _attr: { "w:val": "single" } },
@ -27,7 +27,7 @@ describe("Underline", () => {
});
it("should use the given style type and color", () => {
const underline = new u.Underline(u.UnderlineType.DOUBLE, "FF00CC");
const underline = new Underline(UnderlineType.DOUBLE, "FF00CC");
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": { _attr: { "w:val": "double", "w:color": "FF00CC" } },
@ -35,259 +35,3 @@ describe("Underline", () => {
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DashDotDotHeavyUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashDotDotHeavy",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DashDotHeavyUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashDotHeavy",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DashLongHeavyUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashLongHeavy",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DashLongUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashLong",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DashUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dash",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DotDashUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotDash",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DotDotDashUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotDotDash",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DottedHeavyUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dottedHeavy",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DottedUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotted",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.DoubleUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "double",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.SingleUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "single",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.ThickUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "thick",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.WaveUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wave",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.WavyDoubleUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wavyDouble",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.WavyHeavyUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wavyHeavy",
},
},
});
});
});
});
describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => {
it("should put value in attribute", () => {
const underline = new u.WordsUnderline();
const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "words",
},
},
});
});
});
});

View File

@ -21,116 +21,14 @@ export enum UnderlineType {
WAVYDOUBLE = "wavyDouble",
}
export abstract class BaseUnderline extends XmlComponent {
constructor(underlineType: UnderlineType, color?: string) {
export class Underline extends XmlComponent {
constructor(underlineType: UnderlineType = UnderlineType.SINGLE, color?: string) {
super("w:u");
this.root.push(
new Attributes({
val: underlineType,
color: color === undefined ? color : hexColorValue(color),
color: color === undefined ? undefined : hexColorValue(color),
}),
);
}
}
export class Underline extends BaseUnderline {
constructor(underlineType: UnderlineType = UnderlineType.SINGLE, color?: string) {
super(underlineType, color);
}
}
export class DashUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DASH);
}
}
export class DashDotDotHeavyUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DASHDOTDOTHEAVY);
}
}
export class DashDotHeavyUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DASHDOTHEAVY);
}
}
export class DashLongUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DASHLONG);
}
}
export class DashLongHeavyUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DASHLONGHEAVY);
}
}
export class DotDashUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DOTDASH);
}
}
export class DotDotDashUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DOTDOTDASH);
}
}
export class DottedUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DOTTED);
}
}
export class DottedHeavyUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DOTTEDHEAVY);
}
}
export class DoubleUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.DOUBLE);
}
}
export class SingleUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.SINGLE);
}
}
export class ThickUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.THICK);
}
}
export class WaveUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.WAVE);
}
}
export class WavyDoubleUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.WAVYDOUBLE);
}
}
export class WavyHeavyUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.WAVYHEAVY);
}
}
export class WordsUnderline extends BaseUnderline {
constructor() {
super(UnderlineType.WORDS);
}
}