Add json if (#217)

* fix: json

* fix: specs

* 0.0.127

* fix if

* fix: if

* 0.1.129
main
bokuweb 2020-12-21 18:44:31 +09:00 committed by GitHub
parent 2e5a22fc53
commit 727045c3be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 29 deletions

View File

@ -50,12 +50,12 @@ import { saveAs } from "file-saver";
// // Note that a dynamic `import` statement here is required due to webpack/webpack#6615, // // Note that a dynamic `import` statement here is required due to webpack/webpack#6615,
import("docx-wasm").then(w => { import("docx-wasm").then(w => {
const buf = new w.Docx() const { buffer } = new w.Docx()
.addParagraph( .addParagraph(
new w.Paragraph().addRun(new w.Run().addText("Hello world!!")) new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))
) )
.build(); .build();
saveAs(new Blob([buf]), "hello.docx"); saveAs(new Blob([buffer]), "hello.docx");
}); });
``` ```
@ -65,11 +65,11 @@ import("docx-wasm").then(w => {
const w = require("docx-wasm"); const w = require("docx-wasm");
const { writeFileSync } = require("fs"); const { writeFileSync } = require("fs");
const buf = new w.Docx() const { buffer } = new w.Docx()
.addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))) .addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!")))
.build(); .build();
writeFileSync("hello.docx", buf); writeFileSync("hello.docx", buffer);
``` ```
### More examples ### More examples

View File

@ -262,7 +262,7 @@ impl Docx {
} }
} }
pub fn json(&mut self) -> String { pub fn json(&self) -> String {
serde_json::to_string_pretty(&self).unwrap() serde_json::to_string_pretty(&self).unwrap()
} }

View File

@ -563,7 +563,7 @@ export class Docx {
return level; return level;
} }
build() { createDocx(): wasm.Docx {
let docx = wasm.createDocx(); let docx = wasm.createDocx();
this.children.forEach((child) => { this.children.forEach((child) => {
@ -665,6 +665,18 @@ export class Docx {
} }
} }
return docx;
}
json() {
const docx = this.createDocx();
const json = docx.json();
docx.free();
return json;
}
build() {
const docx = this.createDocx();
const buf = docx.build(this.hasNumberings); const buf = docx.build(this.hasNumberings);
docx.free(); docx.free();
return buf; return buf;

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.125", "version": "0.0.129",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -90,4 +90,8 @@ impl Docx {
} }
Ok(cur.into_inner()) Ok(cur.into_inner())
} }
pub fn json(&self) -> String {
self.0.json()
}
} }

View File

@ -4,28 +4,28 @@ const Zip = require("adm-zip");
describe("reader", () => { describe("reader", () => {
test("should read lvlOverride docx", () => { test("should read lvlOverride docx", () => {
const buf = readFileSync("../fixtures/lvl_override/override.docx"); const buffer = readFileSync("../fixtures/lvl_override/override.docx");
const json = w.readDocx(buf); const json = w.readDocx(buffer);
expect(json).toMatchSnapshot(); expect(json).toMatchSnapshot();
}); });
test("should read gridAfter docx", () => { test("should read gridAfter docx", () => {
const buf = readFileSync("../fixtures/grid_after/grid_after.docx"); const buffer = readFileSync("../fixtures/grid_after/grid_after.docx");
const json = w.readDocx(buf); const json = w.readDocx(buffer);
expect(json).toMatchSnapshot(); expect(json).toMatchSnapshot();
}); });
test("should read table style docx", () => { test("should read table style docx", () => {
const buf = readFileSync("../fixtures/table_style/table_style.docx"); const buffer = readFileSync("../fixtures/table_style/table_style.docx");
const json = w.readDocx(buf); const json = w.readDocx(buffer);
expect(json).toMatchSnapshot(); expect(json).toMatchSnapshot();
}); });
test("should read extended comments docx", () => { test("should read extended comments docx", () => {
const buf = readFileSync( const buffer = readFileSync(
"../fixtures/extended_comments/extended_comments.docx" "../fixtures/extended_comments/extended_comments.docx"
); );
const json = w.readDocx(buf); const json = w.readDocx(buffer);
expect(json).toMatchSnapshot(); expect(json).toMatchSnapshot();
}); });
@ -41,8 +41,8 @@ describe("reader", () => {
describe("writer", () => { describe("writer", () => {
test("should write hello", () => { test("should write hello", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
const buf = new w.Docx().addParagraph(p).build(); const buffer = new w.Docx().addParagraph(p).build();
const z = new Zip(Buffer.from(buf)); const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();
@ -60,13 +60,13 @@ describe("writer", () => {
new w.Level(0, 3, "decimal", "%1", "left") new w.Level(0, 3, "decimal", "%1", "left")
) )
); );
const buf = new w.Docx() const buffer = new w.Docx()
.addParagraph(p) .addParagraph(p)
.addAbstractNumbering(new w.AbstractNumbering(0)) .addAbstractNumbering(new w.AbstractNumbering(0))
.addNumbering(num) .addNumbering(num)
.build(); .build();
const z = new Zip(Buffer.from(buf)); const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();
@ -76,8 +76,8 @@ describe("writer", () => {
test("should write page size", () => { test("should write page size", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
const buf = new w.Docx().addParagraph(p).pageSize(400, 800).build(); const buffer = new w.Docx().addParagraph(p).pageSize(400, 800).build();
const z = new Zip(Buffer.from(buf)); const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();
@ -87,11 +87,11 @@ describe("writer", () => {
test("should write page margin", () => { test("should write page margin", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
const buf = new w.Docx() const buffer = new w.Docx()
.addParagraph(p) .addParagraph(p)
.pageMargin({ top: 1000, left: 2000 }) .pageMargin({ top: 1000, left: 2000 })
.build(); .build();
const z = new Zip(Buffer.from(buf)); const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();
@ -105,13 +105,13 @@ describe("writer", () => {
.eastAsia("Arial") .eastAsia("Arial")
.ascii("Arial") .ascii("Arial")
.hiAnsi("Arial"); .hiAnsi("Arial");
const buf = new w.Docx() const buffer = new w.Docx()
.addParagraph(p) .addParagraph(p)
.defaultSize(40) .defaultSize(40)
.defaultFonts(fonts) .defaultFonts(fonts)
.build(); .build();
writeFileSync("../output/default_font.docx", buf); writeFileSync("../output/default_font.docx", buffer);
const z = new Zip(Buffer.from(buf)); const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();
@ -121,9 +121,12 @@ describe("writer", () => {
test("should write doc vars", () => { test("should write doc vars", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!")); const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
const buf = new w.Docx().addParagraph(p).addDocVar("foo", "bar").build(); const buffer = new w.Docx()
writeFileSync("../output/doc_vars.docx", buf); .addParagraph(p)
const z = new Zip(Buffer.from(buf)); .addDocVar("foo", "bar")
.build();
writeFileSync("../output/doc_vars.docx", buffer);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) { for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) { if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot(); expect(z.readAsText(e)).toMatchSnapshot();