Merge pull request #118 from dolanmiu/feat/add-image-to-run

Add image to run
This commit is contained in:
Dolan
2018-08-09 23:06:05 +01:00
committed by GitHub
10 changed files with 50 additions and 23 deletions

View File

@ -7,9 +7,9 @@ import { SectionPropertiesOptions } from "./document/body/section-properties/sec
import { FooterWrapper } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
import { HeaderWrapper } from "./header-wrapper";
import { Media } from "./media";
import { Image, Media } from "./media";
import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, Image, Paragraph } from "./paragraph";
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
@ -126,19 +126,19 @@ export class File {
public createImage(filePath: string): Image {
const image = Media.addImage(this, filePath);
this.document.addParagraph(image);
this.document.addParagraph(image.Paragraph);
return image;
}
public addImage(image: Image): File {
this.document.addParagraph(image);
this.document.addParagraph(image.Paragraph);
return this;
}
public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image {
const image = Media.addImageFromBuffer(this, buffer, width, height);
this.document.addParagraph(image);
this.document.addParagraph(image.Paragraph);
return image;
}

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components";
import { Footer } from "./footer/footer";
import { Media } from "./media";
import { Image, Paragraph } from "./paragraph";
import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -43,11 +43,11 @@ export class FooterWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addImage(new Image(mediaData));
this.addImage(new Image(new ImageParagraph(mediaData)));
}
public addImage(image: Image): FooterWrapper {
this.footer.addParagraph(image);
this.footer.addParagraph(image.Paragraph);
return this;
}

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components";
import { Header } from "./header/header";
import { Media } from "./media";
import { Image, Paragraph } from "./paragraph";
import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -43,11 +43,11 @@ export class HeaderWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addImage(new Image(mediaData));
this.addImage(new Image(new ImageParagraph(mediaData)));
}
public addImage(image: Image): HeaderWrapper {
this.header.addParagraph(image);
this.header.addParagraph(image.Paragraph);
return this;
}

17
src/file/media/image.ts Normal file
View File

@ -0,0 +1,17 @@
import { ImageParagraph, PictureRun } from "../paragraph";
export class Image {
constructor(private readonly paragraph: ImageParagraph) {}
public get Paragraph(): ImageParagraph {
return this.paragraph;
}
public get Run(): PictureRun {
return this.paragraph.Run;
}
public scale(factorX: number, factorY?: number): void {
this.paragraph.Run.scale(factorX, factorY);
}
}

View File

@ -1,2 +1,3 @@
export * from "./media";
export * from "./data";
export * from "./image";

View File

@ -3,8 +3,9 @@ import * as sizeOf from "image-size";
import * as path from "path";
import { File } from "../file";
import { Image } from "../paragraph";
import { ImageParagraph } from "../paragraph";
import { IMediaData } from "./data";
import { Image } from "./image";
interface IHackedFile {
currentRelationshipId: number;
@ -20,7 +21,7 @@ export class Media {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return new Image(mediaData);
return new Image(new ImageParagraph(mediaData));
}
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image {
@ -39,7 +40,7 @@ export class Media {
`media/${mediaData.fileName}`,
);
return new Image(mediaData);
return new Image(new ImageParagraph(mediaData));
}
private static generateId(): string {

View File

@ -2,13 +2,13 @@
import { assert, expect } from "chai";
import { Formatter } from "../../export/formatter";
import { Image } from "./image";
import { ImageParagraph } from "./image";
describe("Image", () => {
let image: Image;
let image: ImageParagraph;
beforeEach(() => {
image = new Image({
image = new ImageParagraph({
referenceId: 0,
stream: new Buffer(""),
path: "",

View File

@ -3,7 +3,7 @@ import { IMediaData } from "../media";
import { Paragraph } from "./paragraph";
import { PictureRun } from "./run";
export class Image extends Paragraph {
export class ImageParagraph extends Paragraph {
private readonly pictureRun: PictureRun;
constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) {
@ -15,4 +15,8 @@ export class Image extends Paragraph {
public scale(factorX: number, factorY?: number): void {
this.pictureRun.scale(factorX, factorY);
}
public get Run(): PictureRun {
return this.pictureRun;
}
}

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPparagraph.php
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { IMediaData } from "file/media";
import { Image } from "file/media";
import { Num } from "file/numbering/num";
import { XmlComponent } from "file/xml-components";
@ -54,9 +54,10 @@ export class Paragraph extends XmlComponent {
return run;
}
public createPictureRun(imageData: IMediaData): PictureRun {
const run = new PictureRun(imageData);
public addImage(image: Image): PictureRun {
const run = image.Run;
this.addRun(run);
return run;
}