Change image API so that it now sends Images

This commit is contained in:
Dolan
2018-08-03 00:01:42 +01:00
parent 31fdf08c27
commit 5f22950721
8 changed files with 42 additions and 58 deletions

View File

@ -6,8 +6,8 @@ var doc = new docx.Document();
const table = doc.createTable(4, 4);
table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
const imageData = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
table.getCell(1, 1).addContent(new docx.Image(imageData));
const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
table.getCell(1, 1).addContent(image);
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');

View File

@ -1,7 +1,6 @@
// http://officeopenxml.com/WPdocument.php
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components";
import { Image, Paragraph } from "../paragraph";
import { Paragraph } from "../paragraph";
import { Table } from "../table";
import { Body } from "./body";
import { SectionPropertiesOptions } from "./body/section-properties/section-properties";
@ -37,8 +36,9 @@ export class Document extends XmlComponent {
this.root.push(this.body);
}
public addParagraph(paragraph: Paragraph): void {
public addParagraph(paragraph: Paragraph): Document {
this.body.push(paragraph);
return this;
}
public createParagraph(text?: string): Paragraph {
@ -57,13 +57,6 @@ export class Document extends XmlComponent {
return table;
}
public createDrawing(imageData: IMediaData): Image {
const image = new Image(imageData);
this.addParagraph(image);
return image;
}
get Body(): Body {
return this.body;
}

View File

@ -1,4 +1,3 @@
import { IMediaData } from "file/media";
import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
@ -126,18 +125,22 @@ export class File {
}
public createImage(filePath: string): Image {
const mediaData = Media.addImage(this, filePath);
return this.document.createDrawing(mediaData);
const image = Media.addImage(this, filePath);
this.document.addParagraph(image);
return image;
}
public insertImage(mediaData: IMediaData): File {
this.document.createDrawing(mediaData);
public insertImage(image: Image): File {
this.document.addParagraph(image);
return this;
}
public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image {
const mediaData = Media.addImageFromBuffer(this, buffer, width, height);
return this.document.createDrawing(mediaData);
const image = Media.addImageFromBuffer(this, buffer, width, height);
this.document.addParagraph(image);
return image;
}
public createHyperlink(link: string, text?: string): Hyperlink {

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components";
import { Footer } from "./footer/footer";
import { IMediaData, Media } from "./media";
import { Paragraph } from "./paragraph";
import { Media } from "./media";
import { Image, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -32,10 +32,6 @@ export class FooterWrapper {
return this.footer.createTable(rows, cols);
}
public addDrawing(imageData: IMediaData): void {
this.footer.addDrawing(imageData);
}
public addChildElement(childElement: XmlComponent | string): void {
this.footer.addChildElement(childElement);
}
@ -47,7 +43,12 @@ export class FooterWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
this.insertImage(new Image(mediaData));
}
public insertImage(image: Image): FooterWrapper {
this.footer.addParagraph(image);
return this;
}
public get Footer(): Footer {

View File

@ -1,7 +1,6 @@
// http://officeopenxml.com/WPfooters.php
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph";
import { Paragraph } from "../paragraph";
import { Table } from "../table";
import { FooterAttributes } from "./footer-attributes";
@ -36,8 +35,10 @@ export class Footer extends XmlComponent {
return this.refId;
}
public addParagraph(paragraph: Paragraph): void {
public addParagraph(paragraph: Paragraph): Footer {
this.root.push(paragraph);
return this;
}
public createParagraph(text?: string): Paragraph {
@ -55,12 +56,4 @@ export class Footer extends XmlComponent {
this.addTable(table);
return table;
}
public addDrawing(imageData: IMediaData): void {
const paragraph = new Paragraph();
const run = new PictureRun(imageData);
paragraph.addRun(run);
this.root.push(paragraph);
}
}

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components";
import { Header } from "./header/header";
import { IMediaData, Media } from "./media";
import { Paragraph } from "./paragraph";
import { Media } from "./media";
import { Image, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -32,10 +32,6 @@ export class HeaderWrapper {
return this.header.createTable(rows, cols);
}
public addDrawing(imageData: IMediaData): void {
this.header.addDrawing(imageData);
}
public addChildElement(childElement: XmlComponent | string): void {
this.header.addChildElement(childElement);
}
@ -47,7 +43,12 @@ export class HeaderWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
this.insertImage(new Image(mediaData));
}
public insertImage(image: Image): HeaderWrapper {
this.header.addParagraph(image);
return this;
}
public get Header(): Header {

View File

@ -1,7 +1,6 @@
// http://officeopenxml.com/WPheaders.php
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph";
import { Paragraph } from "../paragraph";
import { Table } from "../table";
import { HeaderAttributes } from "./header-attributes";
@ -55,12 +54,4 @@ export class Header extends XmlComponent {
this.addTable(table);
return table;
}
public addDrawing(imageData: IMediaData): void {
const paragraph = new Paragraph();
const run = new PictureRun(imageData);
paragraph.addRun(run);
this.root.push(paragraph);
}
}

View File

@ -3,6 +3,7 @@ import * as sizeOf from "image-size";
import * as path from "path";
import { File } from "../file";
import { Image } from "../paragraph";
import { IMediaData } from "./data";
interface IHackedFile {
@ -10,7 +11,7 @@ interface IHackedFile {
}
export class Media {
public static addImage(file: File, filePath: string): IMediaData {
public static addImage(file: File, filePath: string): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
@ -19,10 +20,10 @@ export class Media {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return mediaData;
return new Image(mediaData);
}
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): IMediaData {
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMediaFromBuffer(
@ -37,7 +38,8 @@ export class Media {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return mediaData;
return new Image(mediaData);
}
private static generateId(): string {