Merge branch 'master' into feat/tslint-to-eslint

# Conflicts:
#	src/file/numbering/level.spec.ts
#	src/file/numbering/level.ts
This commit is contained in:
Dolan Miu
2022-09-15 18:04:48 +01:00
11 changed files with 355 additions and 199 deletions

View File

@ -122,4 +122,52 @@ describe("Packer", () => {
(Packer as any).compiler.compile.restore();
});
});
describe("#toStream()", () => {
it("should create a standard docx file", async () => {
// tslint:disable-next-line: no-any
stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
generateNodeStream: () => ({
on: (event: string, cb: () => void) => {
if (event === "end") {
cb();
}
},
}),
}));
const stream = await Packer.toStream(file);
return new Promise((resolve, reject) => {
stream.on("error", () => {
reject();
});
stream.on("end", () => {
resolve();
});
});
});
it("should handle exception if it throws any", async () => {
// tslint:disable-next-line:no-any
const compiler = stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
on: (event: string, cb: () => void) => {
if (event === "error") {
cb();
}
},
}));
compiler.throwsException();
return Packer.toStream(file).catch((error) => {
assert.isDefined(error);
});
});
afterEach(() => {
// tslint:disable-next-line:no-any
(Packer as any).compiler.compile.restore();
});
});
});

View File

@ -1,4 +1,5 @@
import { File } from "@file/file";
import { Stream } from "stream";
import { Compiler } from "./next-compiler";
@ -57,5 +58,17 @@ export class Packer {
return zipData;
}
public static async toStream(file: File, prettify?: boolean | PrettifyType): Promise<Stream> {
const zip = this.compiler.compile(file, prettify);
const zipData = zip.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
return zipData;
}
private static readonly compiler = new Compiler();
}

View File

@ -1,8 +1,9 @@
import { expect } from "chai";
import { AlignmentType } from "..";
import { Formatter } from "@export/formatter";
import { Level, LevelFormat, LevelSuffix } from "./level";
import { AlignmentType } from "..";
describe("Level", () => {
describe("#constructor", () => {
@ -21,4 +22,41 @@ describe("Level", () => {
).to.throw();
});
});
describe("isLegalNumberingStyle", () => {
it("should work", () => {
const concreteNumbering = new Level({
level: 9,
isLegalNumberingStyle: true,
});
const tree = new Formatter().format(concreteNumbering);
expect(tree).to.deep.equal({
"w:lvl": [
{
"w:start": {
_attr: {
"w:val": 1,
},
},
},
{
"w:isLgl": {},
},
{
"w:lvlJc": {
_attr: {
"w:val": "start",
},
},
},
{
_attr: {
"w15:tentative": 1,
"w:ilvl": 9,
},
},
],
});
});
});
});

View File

@ -87,6 +87,7 @@ export interface ILevelsOptions {
readonly alignment?: AlignmentType;
readonly start?: number;
readonly suffix?: LevelSuffix;
readonly isLegalNumberingStyle?: boolean;
readonly style?: {
readonly run?: IRunStylePropertiesOptions;
readonly paragraph?: ILevelParagraphStylePropertiesOptions;
@ -114,6 +115,14 @@ class Suffix extends XmlComponent {
}
}
// http://officeopenxml.com/WPnumbering-isLgl.php
// https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.islegalnumberingstyle?view=openxml-2.8.1
class IsLegalNumberingStyle extends XmlComponent {
constructor() {
super("w:isLgl");
}
}
// <xsd:complexType name="CT_Lvl">
// <xsd:sequence>
// <xsd:element name="start" type="CT_DecimalNumber" minOccurs="0"/>
@ -137,7 +146,16 @@ export class LevelBase extends XmlComponent {
private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties;
public constructor({ level, format, text, alignment = AlignmentType.START, start = 1, style, suffix }: ILevelsOptions) {
public constructor({
level,
format,
text,
alignment = AlignmentType.START,
start = 1,
style,
suffix,
isLegalNumberingStyle,
}: ILevelsOptions) {
super("w:lvl");
this.root.push(new NumberValueElement("w:start", decimalNumber(start)));
@ -150,6 +168,10 @@ export class LevelBase extends XmlComponent {
this.root.push(new Suffix(suffix));
}
if (isLegalNumberingStyle) {
this.root.push(new IsLegalNumberingStyle());
}
if (text) {
this.root.push(new LevelText(text));
}

View File

@ -5,7 +5,6 @@ import { EMPTY_OBJECT } from "@file/xml-components";
import * as defaultStyles from "./default-styles";
describe("Default Styles", () => {
it("HeadingStyle#constructor", () => {
const style = new defaultStyles.HeadingStyle({

View File

@ -5,7 +5,6 @@ import { Formatter } from "@export/formatter";
import { GridCol, TableGrid } from "./grid";
describe("GridCol", () => {
describe("#constructor", () => {
it("sets the width attribute to the value given", () => {

View File

@ -39,7 +39,10 @@ export enum TableCellMarginElementType {
}
export class TableCellMargin extends IgnoreIfEmptyXmlComponent {
public constructor(type: TableCellMarginElementType, { marginUnitType = WidthType.DXA, top, left, bottom, right }: ITableCellMarginOptions) {
public constructor(
type: TableCellMarginElementType,
{ marginUnitType = WidthType.DXA, top, left, bottom, right }: ITableCellMarginOptions,
) {
super(type);
if (top !== undefined) {