From f414b4ee9a5d8d2e32ef8bd974e429dbb5fa3a62 Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 15 Jun 2022 00:07:12 +0100 Subject: [PATCH] #1529 Add word break feature --- demo/72-word-wrap.ts | 37 +++++++++++++++++++ src/file/index.ts | 1 + .../links/pageref-field-instruction.ts | 8 ++-- src/file/paragraph/run/page-number.ts | 6 +-- src/file/paragraph/run/properties.ts | 6 ++- src/file/paragraph/run/run-components/text.ts | 6 +-- src/file/paragraph/run/run.spec.ts | 17 +++++++++ src/file/paragraph/run/run.ts | 6 +++ .../run/sequential-identifier-instruction.ts | 6 +-- src/file/paragraph/run/text-attributes.ts | 6 +++ .../table-of-contents/field-instruction.ts | 8 ++-- .../deleted-page-number.ts | 7 +--- .../track-revision-components/deleted-text.ts | 7 +--- 13 files changed, 87 insertions(+), 34 deletions(-) create mode 100644 demo/72-word-wrap.ts create mode 100644 src/file/paragraph/run/text-attributes.ts diff --git a/demo/72-word-wrap.ts b/demo/72-word-wrap.ts new file mode 100644 index 0000000000..95b8b41d01 --- /dev/null +++ b/demo/72-word-wrap.ts @@ -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); +}); diff --git a/src/file/index.ts b/src/file/index.ts index 1ab2a79cfd..c001054d72 100644 --- a/src/file/index.ts +++ b/src/file/index.ts @@ -19,3 +19,4 @@ export * from "./shared"; export * from "./border"; export * from "./values"; export * from "./vertical-align"; +export * from "./space-type"; diff --git a/src/file/paragraph/links/pageref-field-instruction.ts b/src/file/paragraph/links/pageref-field-instruction.ts index 99be950d28..af15635256 100644 --- a/src/file/paragraph/links/pageref-field-instruction.ts +++ b/src/file/paragraph/links/pageref-field-instruction.ts @@ -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 = {}) { diff --git a/src/file/paragraph/run/page-number.ts b/src/file/paragraph/run/page-number.ts index ab5284f3c9..0a652f3d09 100644 --- a/src/file/paragraph/run/page-number.ts +++ b/src/file/paragraph/run/page-number.ts @@ -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() { diff --git a/src/file/paragraph/run/properties.ts b/src/file/paragraph/run/properties.ts index 29e67479a8..77aec7ae81 100644 --- a/src/file/paragraph/run/properties.ts +++ b/src/file/paragraph/run/properties.ts @@ -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"; @@ -45,6 +46,7 @@ export interface IRunStylePropertiesOptions { readonly imprint?: boolean; readonly revision?: IRunPropertiesChangeOptions; readonly border?: IBorderOptions; + readonly space?: SpaceType; } export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { diff --git a/src/file/paragraph/run/run-components/text.ts b/src/file/paragraph/run/run-components/text.ts index f730d37d3b..f25c39c1ad 100644 --- a/src/file/paragraph/run/run-components/text.ts +++ b/src/file/paragraph/run/run-components/text.ts @@ -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) { diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 09af5a524d..e356173b0b 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -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"; @@ -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", + }, + }, + }); + }); + }); }); diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 02b97ec47f..8f9dc65011 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -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)[]; @@ -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") { diff --git a/src/file/paragraph/run/sequential-identifier-instruction.ts b/src/file/paragraph/run/sequential-identifier-instruction.ts index aad320cbdb..eb830096a6 100644 --- a/src/file/paragraph/run/sequential-identifier-instruction.ts +++ b/src/file/paragraph/run/sequential-identifier-instruction.ts @@ -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) { diff --git a/src/file/paragraph/run/text-attributes.ts b/src/file/paragraph/run/text-attributes.ts new file mode 100644 index 0000000000..e380959c40 --- /dev/null +++ b/src/file/paragraph/run/text-attributes.ts @@ -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" }; +} diff --git a/src/file/table-of-contents/field-instruction.ts b/src/file/table-of-contents/field-instruction.ts index e67f35987e..c5ab1144d6 100644 --- a/src/file/table-of-contents/field-instruction.ts +++ b/src/file/table-of-contents/field-instruction.ts @@ -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; diff --git a/src/file/track-revision/track-revision-components/deleted-page-number.ts b/src/file/track-revision/track-revision-components/deleted-page-number.ts index 6ce6266f13..469b9b1700 100644 --- a/src/file/track-revision/track-revision-components/deleted-page-number.ts +++ b/src/file/track-revision/track-revision-components/deleted-page-number.ts @@ -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() { diff --git a/src/file/track-revision/track-revision-components/deleted-text.ts b/src/file/track-revision/track-revision-components/deleted-text.ts index 408b47304d..1311e056d8 100644 --- a/src/file/track-revision/track-revision-components/deleted-text.ts +++ b/src/file/track-revision/track-revision-components/deleted-text.ts @@ -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) {