Deprecate import dotx

This commit is contained in:
Dolan Miu
2023-03-17 00:20:55 +00:00
parent a07d6378e8
commit 0388a564b5
11 changed files with 7 additions and 517 deletions

View File

@ -1,11 +0,0 @@
import { IDocumentTemplate } from "../import-dotx";
export interface IFileProperties {
readonly template?: IDocumentTemplate;
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
/**
* @ignore
*/
export const WORKAROUND = "";

View File

@ -1,12 +1,11 @@
import { expect } from "chai";
import { Formatter } from "@export/formatter";
import { sectionMarginDefaults, sectionPageSizeDefaults } from "./document";
import { sectionMarginDefaults, sectionPageSizeDefaults } from "./document";
import { File } from "./file";
import { Footer, Header } from "./header";
import { Paragraph } from "./paragraph";
import { Media } from "./media";
const PAGE_SIZE_DEFAULTS = {
"w:h": sectionPageSizeDefaults.HEIGHT,
@ -433,29 +432,6 @@ describe("File", () => {
});
});
describe("#templates", () => {
// Test will be deprecated when import-dotx and templates are deprecated
it("should work with template", () => {
const doc = new File(
{
sections: [],
},
{
template: {
currentRelationshipId: 1,
headers: [],
footers: [],
styles: "",
titlePageIsDefined: true,
media: new Media(),
},
},
);
expect(doc).to.not.be.undefined;
});
});
describe("#externalStyles", () => {
it("should work with external styles", () => {
const doc = new File({

View File

@ -4,7 +4,6 @@ import { CoreProperties, IPropertiesOptions } from "./core-properties";
import { CustomProperties } from "./custom-properties";
import { DocumentWrapper } from "./document-wrapper";
import { HeaderFooterReferenceType, ISectionPropertiesOptions } from "./document/body/section-properties";
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootnotesWrapper } from "./footnotes-wrapper";
import { Footer, Header } from "./header";
@ -55,7 +54,7 @@ export class File {
private readonly styles: Styles;
private readonly comments: Comments;
public constructor(options: IPropertiesOptions, fileProperties: IFileProperties = {}) {
public constructor(options: IPropertiesOptions) {
this.coreProperties = new CoreProperties({
...options,
creator: options.creator ?? "Un-named",
@ -80,20 +79,9 @@ export class File {
updateFields: options.features?.updateFields,
});
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
this.media = new Media();
if (fileProperties.template) {
this.currentRelationshipId = fileProperties.template.currentRelationshipId + 1;
}
// set up styles
if (fileProperties.template && options.externalStyles) {
throw Error("can not use both template and external styles");
}
if (fileProperties.template && fileProperties.template.styles) {
const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(fileProperties.template.styles);
} else if (options.externalStyles) {
if (options.externalStyles) {
const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles);
} else if (options.styles) {
@ -110,18 +98,6 @@ export class File {
this.addDefaultRelationships();
if (fileProperties.template && fileProperties.template.headers) {
for (const templateHeader of fileProperties.template.headers) {
this.addHeaderToDocument(templateHeader.header, templateHeader.type);
}
}
if (fileProperties.template && fileProperties.template.footers) {
for (const templateFooter of fileProperties.template.footers) {
this.addFooterToDocument(templateFooter.footer, templateFooter.type);
}
}
for (const section of options.sections) {
this.addSection(section);
}

View File

@ -1,7 +1,6 @@
export * from "./paragraph";
export * from "./table";
export * from "./file";
export * from "./file-properties";
export * from "./numbering";
export * from "./media";
export * from "./drawing";

View File

@ -15,67 +15,13 @@ describe("Media", () => {
(convenienceFunctions.uniqueId as SinonStub).restore();
});
describe("#addMedia", () => {
it("should add media", () => {
const image = new Media().addMedia("", {
width: 100,
height: 100,
});
expect(image.fileName).to.equal("test.png");
expect(image.transformation).to.deep.equal({
pixels: {
x: 100,
y: 100,
},
flip: undefined,
emus: {
x: 952500,
y: 952500,
},
rotation: undefined,
});
});
it("should return UInt8Array if atob is present", () => {
// eslint-disable-next-line functional/immutable-data
global.atob = () => "atob result";
const image = new Media().addMedia("", {
width: 100,
height: 100,
});
expect(image.stream).to.be.an.instanceof(Uint8Array);
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});
it("should use data as is if its not a string", () => {
// eslint-disable-next-line functional/immutable-data
global.atob = () => "atob result";
const image = new Media().addMedia(Buffer.from(""), {
width: 100,
height: 100,
});
expect(image.stream).to.be.an.instanceof(Uint8Array);
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});
});
describe("#addImage", () => {
it("should add media", () => {
describe("#Array", () => {
it("Get images as array", () => {
const media = new Media();
media.addMedia("", {
width: 100,
height: 100,
});
media.addImage("test2.png", {
stream: Buffer.from(""),
fileName: "",
fileName: "test2.png",
transformation: {
pixels: {
x: Math.round(1),
@ -88,23 +34,6 @@ describe("Media", () => {
},
});
expect(media.Array).to.be.lengthOf(2);
});
});
describe("#Array", () => {
it("Get images as array", () => {
const media = new Media();
media.addMedia("", {
width: 100,
height: 100,
flip: {
vertical: true,
horizontal: true,
},
rotation: 90,
});
const array = media.Array;
expect(array).to.be.an.instanceof(Array);
expect(array.length).to.equal(1);

View File

@ -1,5 +1,3 @@
import { uniqueId } from "@util/convenience-functions";
import { IMediaData } from "./data";
export interface IMediaTransformation {
@ -20,34 +18,6 @@ export class Media {
this.map = new Map<string, IMediaData>();
}
// TODO: Unused
public addMedia(data: Buffer | string | Uint8Array | ArrayBuffer, transformation: IMediaTransformation): IMediaData {
const key = `${uniqueId()}.png`;
const newData = typeof data === "string" ? this.convertDataURIToBinary(data) : data;
const imageData: IMediaData = {
stream: newData,
fileName: key,
transformation: {
pixels: {
x: Math.round(transformation.width),
y: Math.round(transformation.height),
},
emus: {
x: Math.round(transformation.width * 9525),
y: Math.round(transformation.height * 9525),
},
flip: transformation.flip,
rotation: transformation.rotation ? transformation.rotation * 60000 : undefined,
},
};
this.map.set(key, imageData);
return imageData;
}
public addImage(key: string, mediaData: IMediaData): void {
this.map.set(key, mediaData);
}
@ -55,24 +25,4 @@ export class Media {
public get Array(): readonly IMediaData[] {
return Array.from(this.map.values());
}
private convertDataURIToBinary(dataURI: string): Uint8Array {
// https://gist.github.com/borismus/1032746
// https://github.com/mafintosh/base64-to-uint8array
const BASE64_MARKER = ";base64,";
const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
if (typeof atob === "function") {
return new Uint8Array(
atob(dataURI.substring(base64Index))
.split("")
.map((c) => c.charCodeAt(0)),
);
} else {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const b = require("buf" + "fer");
return new b.Buffer(dataURI, "base64");
}
}
}