Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1529 - Word wrap feature #1738

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions demo/72-word-wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,31 @@ const doc = new Document({
sections: [
{
children: [
new Paragraph({
wordWrap: true,
children: [
new TextRun("我今天遛狗去公园"),
new TextRun({
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
}),
],
}),
new Paragraph({
wordWrap: true,
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",
}),
],
}),
new Paragraph({
children: [
new TextRun("我今天遛狗去公园"),
new TextRun({
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
space: SpaceType.PRESERVE,
}),
],
}),
Expand All @@ -23,7 +42,6 @@ const doc = new Document({
),
new TextRun({
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
space: SpaceType.PRESERVE,
}),
],
}),
Expand Down
20 changes: 20 additions & 0 deletions src/file/paragraph/formatting/word-wrap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect } from "chai";

import { Formatter } from "@export/formatter";

import { WordWrap } from "./word-wrap";

describe("WordWrap", () => {
it("should create", () => {
const wordWrap = new WordWrap();
const tree = new Formatter().format(wordWrap);

expect(tree).to.deep.equal({
"w:wordWrap": {
_attr: {
"w:val": 0,
},
},
});
});
});
14 changes: 14 additions & 0 deletions src/file/paragraph/formatting/word-wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// http://officeopenxml.com/WPalignment.php
// http://officeopenxml.com/WPtableAlignment.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";

export class WordWrapAttributes extends XmlAttributeComponent<{ readonly val: 0 }> {
protected readonly xmlKeys = { val: "w:val" };
}

export class WordWrap extends XmlComponent {
public constructor() {
super("w:wordWrap");
this.root.push(new WordWrapAttributes({ val: 0 }));
}
}
19 changes: 19 additions & 0 deletions src/file/paragraph/properties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,24 @@ describe("ParagraphProperties", () => {
],
});
});

it("should create with the wordWrap property", () => {
const properties = new ParagraphProperties({
wordWrap: true,
});
const tree = new Formatter().format(properties);

expect(tree).to.deep.equal({
"w:pPr": [
{
"w:wordWrap": {
_attr: {
"w:val": 0,
},
},
},
],
});
});
});
});
6 changes: 6 additions & 0 deletions src/file/paragraph/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style";
import { TabStop, TabStopDefinition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { WordWrap } from "./formatting/word-wrap";
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
import { OutlineLevel } from "./links";

Expand Down Expand Up @@ -50,6 +51,7 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp
readonly widowControl?: boolean;
readonly frame?: IFrameOptions;
readonly suppressLineNumbers?: boolean;
readonly wordWrap?: boolean;
}

export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
Expand Down Expand Up @@ -128,6 +130,10 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Shading(options.shading));
}

if (options.wordWrap) {
this.push(new WordWrap());
}

/**
* FIX: Multitab support for Libre Writer
* Ensure there is only one w:tabs tag with multiple w:tab
Expand Down
2 changes: 0 additions & 2 deletions src/file/paragraph/run/properties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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";

Expand Down Expand Up @@ -46,7 +45,6 @@ export interface IRunStylePropertiesOptions {
readonly imprint?: boolean;
readonly revision?: IRunPropertiesChangeOptions;
readonly border?: IBorderOptions;
readonly space?: SpaceType;
}

export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
Expand Down
17 changes: 0 additions & 17 deletions src/file/paragraph/run/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { expect } from "chai";
import { Formatter } from "@export/formatter";
import { BorderStyle } from "@file/border";
import { ShadingType } from "@file/shading";
import { SpaceType } from "@file/space-type";

import { EmphasisMarkType } from "./emphasis-mark";
import { PageNumber, Run } from "./run";
Expand Down Expand Up @@ -520,20 +519,4 @@ 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",
},
},
});
});
});
});
5 changes: 0 additions & 5 deletions src/file/paragraph/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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?: readonly (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
Expand Down Expand Up @@ -37,10 +36,6 @@ 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