Make media return a picture run instead

This commit is contained in:
Dolan
2019-06-23 22:36:01 +01:00
parent 427dc86915
commit dfe986331d
6 changed files with 31 additions and 15 deletions

View File

@ -12,7 +12,7 @@ const table = doc.createTable({
table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
table.getCell(1, 1).addParagraph(image.Paragraph);
table.getCell(1, 1).addParagraph(new Paragraph(image));
const packer = new Packer();

View File

@ -1,7 +1,7 @@
// Add image to table cell
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Media, Packer, Table } from "../build";
import { Document, Media, Packer, Paragraph, Table } from "../build";
const doc = new Document();
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
@ -10,7 +10,7 @@ const table = new Table({
rows: 2,
columns: 2,
});
table.getCell(1, 1).addParagraph(image.Paragraph);
table.getCell(1, 1).addParagraph(new Paragraph(image));
doc.addTable(table);

View File

@ -141,11 +141,12 @@ export class File {
width?: number,
height?: number,
drawingOptions?: IDrawingOptions,
): Image {
): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
this.document.addParagraph(image.Paragraph);
const paragraph = new Paragraph(image);
this.document.addParagraph(paragraph);
return image;
return paragraph;
}
public createHyperlink(link: string, text?: string): Hyperlink {

View File

@ -5,6 +5,7 @@ import { stub } from "sinon";
import { Formatter } from "export/formatter";
import { File } from "../file";
import { Paragraph } from "../paragraph";
import { Media } from "./media";
describe("Media", () => {
@ -13,10 +14,10 @@ describe("Media", () => {
const file = new File();
const image = Media.addImage(file, "");
let tree = new Formatter().format(image.Paragraph);
let tree = new Formatter().format(new Paragraph(image));
expect(tree["w:p"]).to.be.an.instanceof(Array);
tree = new Formatter().format(image.Run);
tree = new Formatter().format(image);
expect(tree["w:r"]).to.be.an.instanceof(Array);
});
@ -26,7 +27,7 @@ describe("Media", () => {
const file = new File();
const image1 = Media.addImage(file, "test");
const tree = new Formatter().format(image1.Paragraph);
const tree = new Formatter().format(new Paragraph(image1));
const inlineElements = tree["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
const graphicData = inlineElements.find((x) => x["a:graphic"]);
@ -38,7 +39,7 @@ describe("Media", () => {
});
const image2 = Media.addImage(file, "test");
const tree2 = new Formatter().format(image2.Paragraph);
const tree2 = new Formatter().format(new Paragraph(image2));
const inlineElements2 = tree2["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
const graphicData2 = inlineElements2.find((x) => x["a:graphic"]);

View File

@ -1,8 +1,8 @@
import { IDrawingOptions } from "../drawing";
import { File } from "../file";
import { ImageParagraph } from "../paragraph";
import { PictureRun } from "../paragraph";
import { IMediaData } from "./data";
import { Image } from "./image";
// import { Image } from "./image";
export class Media {
public static addImage(
@ -11,10 +11,10 @@ export class Media {
width?: number,
height?: number,
drawingOptions?: IDrawingOptions,
): Image {
): PictureRun {
// Workaround to expose id without exposing to API
const mediaData = file.Media.addMedia(buffer, width, height);
return new Image(new ImageParagraph(mediaData, drawingOptions));
return new PictureRun(mediaData, drawingOptions);
}
private static generateId(): string {

View File

@ -55,12 +55,13 @@ export interface IParagraphOptions {
readonly level: number;
readonly custom?: boolean;
};
readonly children?: Array<TextRun | PictureRun | Hyperlink>;
}
export class Paragraph extends XmlComponent {
private readonly properties: ParagraphProperties;
constructor(options: string | IParagraphOptions) {
constructor(options: string | PictureRun | IParagraphOptions) {
super("w:p");
if (typeof options === "string") {
@ -70,6 +71,13 @@ export class Paragraph extends XmlComponent {
return;
}
if (options instanceof PictureRun) {
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(options);
return;
}
this.properties = new ParagraphProperties({
border: options.border,
});
@ -163,6 +171,12 @@ export class Paragraph extends XmlComponent {
this.root.push(run);
}
}
if (options.children) {
for (const child of options.children) {
this.root.push(child);
}
}
}
public addRun(run: Run): Paragraph {