Skip to content

Commit

Permalink
Merge pull request #1552 from dolanmiu/feat/word-break-asian
Browse files Browse the repository at this point in the history
#1529 Add word break feature
  • Loading branch information
dolanmiu committed Jun 15, 2022
2 parents bdbd15e + c7b68a4 commit a79f839
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 38 deletions.
37 changes: 37 additions & 0 deletions demo/72-word-wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Example on how to preserve word wrap text. Works with all languages.
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun, SpaceType } from "../build";

const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("我今天遛狗去公园"),
new TextRun({
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
space: SpaceType.PRESERVE,
}),
],
}),
new Paragraph({
children: [
new TextRun(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
),
new TextRun({
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
space: SpaceType.PRESERVE,
}),
],
}),
],
},
],
});

Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
50 changes: 46 additions & 4 deletions src/export/packer/next-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ describe("Compiler", () => {
sections: [
{
headers: {
default: new Header(),
default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer(),
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [],
},
{
headers: {
default: new Header(),
default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer(),
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [],
},
Expand Down Expand Up @@ -99,5 +107,39 @@ describe("Compiler", () => {
compiler.compile(file);
expect(spy.callCount).to.equal(12);
});

it("should work with media datas", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
const paragraph = new Paragraph("");
const file = new File({
sections: [
{
properties: {},
children: [paragraph],
},
],
});

// tslint:disable-next-line: no-string-literal
sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
{
stream: Buffer.from(""),
fileName: "test",
transformation: {
pixels: {
x: 100,
y: 100,
},
emus: {
x: 100,
y: 100,
},
},
},
]);

compiler.compile(file);
});
});
});
1 change: 1 addition & 0 deletions src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from "./shared";
export * from "./border";
export * from "./values";
export * from "./vertical-align";
export * from "./space-type";
8 changes: 3 additions & 5 deletions src/file/paragraph/links/pageref-field-instruction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { IPageReferenceOptions } from "./pageref-properties";
import { XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { TextAttributes } from "../run/text-attributes";
import { IPageReferenceOptions } from "./pageref-properties";

export class PageReferenceFieldInstruction extends XmlComponent {
constructor(bookmarkId: string, options: IPageReferenceOptions = {}) {
Expand Down
6 changes: 2 additions & 4 deletions src/file/paragraph/run/page-number.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { TextAttributes } from "./text-attributes";

export class Page extends XmlComponent {
constructor() {
Expand Down
6 changes: 4 additions & 2 deletions src/file/paragraph/run/properties.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ChangeAttributes, IChangedAttributesProperties } from "../../track-revision/track-revision";

import { BorderElement, IBorderOptions } from "file/border";
import { IShadingAttributesProperties, Shading } from "file/shading";
import { SpaceType } from "file/space-type";
import { ChangeAttributes, IChangedAttributesProperties } from "file/track-revision/track-revision";
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "file/xml-components";

import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting";
import { IFontAttributesProperties, RunFonts } from "./run-fonts";
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface IRunStylePropertiesOptions {
readonly imprint?: boolean;
readonly revision?: IRunPropertiesChangeOptions;
readonly border?: IBorderOptions;
readonly space?: SpaceType;
}

export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
Expand Down
6 changes: 2 additions & 4 deletions src/file/paragraph/run/run-components/text.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { TextAttributes } from "../text-attributes";

export class Text extends XmlComponent {
constructor(text: string) {
Expand Down
17 changes: 17 additions & 0 deletions src/file/paragraph/run/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
// import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { ShadingType } from "file/shading";
import { SpaceType } from "file/space-type";

import { Run } from "./";
import { EmphasisMarkType } from "./emphasis-mark";
Expand Down Expand Up @@ -521,4 +522,20 @@ describe("Run", () => {
});
});
});

describe("#space", () => {
it("should correctly set the border", () => {
const run = new Run({
space: SpaceType.PRESERVE,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": {
_attr: {
"xml:space": "preserve",
},
},
});
});
});
});
6 changes: 6 additions & 0 deletions src/file/paragraph/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { XmlComponent } from "file/xml-components";

import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { FieldInstruction } from "file/table-of-contents/field-instruction";

import { Break } from "./break";
import { Begin, End, Separate } from "./field";
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
import { IRunPropertiesOptions, RunProperties } from "./properties";
import { Text } from "./run-components/text";
import { TextAttributes } from "./text-attributes";

export interface IRunOptions extends IRunPropertiesOptions {
readonly children?: (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
Expand Down Expand Up @@ -35,6 +37,10 @@ export class Run extends XmlComponent {
}
}

if (options.space) {
this.root.push(new TextAttributes({ space: options.space }));
}

if (options.children) {
for (const child of options.children) {
if (typeof child === "string") {
Expand Down
6 changes: 2 additions & 4 deletions src/file/paragraph/run/sequential-identifier-instruction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// http://officeopenxml.com/WPfieldInstructions.php
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { TextAttributes } from "./text-attributes";

export class SequentialIdentifierInstruction extends XmlComponent {
constructor(identifier: string) {
Expand Down
6 changes: 6 additions & 0 deletions src/file/paragraph/run/text-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent } from "file/xml-components";

export class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
8 changes: 3 additions & 5 deletions src/file/table-of-contents/field-instruction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// http://officeopenxml.com/WPfieldInstructions.php
import { TextAttributes } from "file/paragraph/run/text-attributes";
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { ITableOfContentsOptions } from "./table-of-contents-properties";
import { XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { ITableOfContentsOptions } from "./table-of-contents-properties";

export class FieldInstruction extends XmlComponent {
private readonly properties: ITableOfContentsOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { TextAttributes } from "file/paragraph/run/text-attributes";
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { XmlComponent } from "file/xml-components";

export class DeletedPage extends XmlComponent {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { TextAttributes } from "file/paragraph/run/text-attributes";
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";

class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
import { XmlComponent } from "file/xml-components";

export class DeletedText extends XmlComponent {
constructor(text: string) {
Expand Down

0 comments on commit a79f839

Please sign in to comment.