Skip to content

Commit

Permalink
Add toc ppr (#785)
Browse files Browse the repository at this point in the history
* feat: add ppr in toc

* feat: add frame property

* fix: add rpr

* fix: add toc paragraphProperty

* support toc paragraph property

* fix: some properties

* 0.4.18-rc20

* fix: toc style

* chore: rc21

* fix

* fix: toc structure

* fix: toc styles
  • Loading branch information
bokuweb authored Nov 22, 2024
1 parent 7400979 commit 11e09de
Show file tree
Hide file tree
Showing 30 changed files with 1,380 additions and 492 deletions.
5 changes: 5 additions & 0 deletions docx-core/src/documents/elements/paragraph_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ impl ParagraphProperty {
self
}

pub fn run_property(mut self, s: RunProperty) -> Self {
self.run_property = s;
self
}

pub fn text_alignment(mut self, s: TextAlignmentType) -> Self {
self.text_alignment = Some(TextAlignment::new(s));
self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ParagraphPropertyDefault {
self
}

pub(crate) fn paragraph_property(mut self, p: ParagraphProperty) -> Self {
pub fn paragraph_property(mut self, p: ParagraphProperty) -> Self {
self.paragraph_property = p;
self
}
Expand Down
5 changes: 5 additions & 0 deletions docx-core/src/documents/elements/run_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ impl RunProperty {
self
}

pub fn character_spacing(mut self, v: i32) -> RunProperty {
self.character_spacing = Some(CharacterSpacing::new(v));
self
}

pub fn text_border(mut self, b: TextBorder) -> Self {
self.text_border = Some(b);
self
Expand Down
9 changes: 9 additions & 0 deletions docx-core/src/documents/elements/table_of_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct TableOfContents {
pub after_contents: Vec<TocContent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub delete: Option<TableOfContentsReviewData>,
#[serde(skip_serializing_if = "Option::is_none")]
pub paragraph_property: Option<ParagraphProperty>,
}

impl TableOfContents {
Expand Down Expand Up @@ -149,6 +151,11 @@ impl TableOfContents {
self.without_sdt = true;
self
}

pub fn paragraph_property(mut self, p: ParagraphProperty) -> Self {
self.paragraph_property = Some(p);
self
}
}

impl BuildXML for TableOfContents {
Expand Down Expand Up @@ -199,9 +206,11 @@ impl BuildXML for TableOfContents {
.add_field_char(FieldCharType::Separate, false),
)
};

b = b.add_child(&p1)?;

let p2 = Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false));

if self.after_contents.is_empty() {
b = b.add_child(&p2)?;
} else {
Expand Down
23 changes: 16 additions & 7 deletions docx-core/src/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,7 @@ impl Docx {
})
.collect();

if !tocs.is_empty() {
for i in 1..=9 {
self.styles = self
.styles
.add_style(crate::documents::preset_styles::toc(i));
}
}
let has_toc = !tocs.is_empty();

for (i, toc) in tocs {
if toc.items.is_empty() && toc.auto {
Expand Down Expand Up @@ -611,6 +605,21 @@ impl Docx {
self.document_rels.has_footnotes = true;
}

if has_toc {
for i in 1..=9 {
if !self
.styles
.styles
.iter()
.any(|s| s.name == Name::new(format!("toc {}", i)))
{
self.styles = self
.styles
.add_style(crate::documents::preset_styles::toc(i));
}
}
}

XMLDocx {
content_type: self.content_type.build(),
rels: self.rels.build(),
Expand Down
3 changes: 1 addition & 2 deletions docx-core/src/documents/paragraph_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ static PARA_ID: AtomicUsize = AtomicUsize::new(1);
pub fn generate_para_id() -> String {
use std::sync::atomic::Ordering;

let id = PARA_ID.load(Ordering::Relaxed);
PARA_ID.store(id.wrapping_add(1), Ordering::Relaxed);
let id = PARA_ID.fetch_add(1, Ordering::Relaxed);
format!("{:08x}", id)
}

Expand Down
4 changes: 2 additions & 2 deletions docx-core/src/documents/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Styles {
doc_defaults: DocDefaults,
styles: Vec<Style>,
pub doc_defaults: DocDefaults,
pub styles: Vec<Style>,
}

impl Styles {
Expand Down
16 changes: 8 additions & 8 deletions docx-wasm/js/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ function buildParagraph(child: Paragraph) {
paragraph = paragraph.style(child.property.styleId);
}

if (child.property.runProperty.del) {
if (child.property.runProperty._del) {
paragraph = paragraph.delete(
child.property.runProperty.del.author,
child.property.runProperty.del.date
child.property.runProperty._del.author,
child.property.runProperty._del.date
);
}

if (child.property.runProperty.ins) {
if (child.property.runProperty._ins) {
paragraph = paragraph.insert(
child.property.runProperty.ins.author,
child.property.runProperty.ins.date
child.property.runProperty._ins.author,
child.property.runProperty._ins.date
);
}

if (child.property.runProperty.characterSpacing != null) {
if (child.property.runProperty._characterSpacing != null) {
paragraph = paragraph.character_spacing(
child.property.runProperty.characterSpacing
child.property.runProperty._characterSpacing
);
}

Expand Down
19 changes: 13 additions & 6 deletions docx-wasm/js/doc-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { LineSpacing, ParagraphProperty } from "./paragraph-property";
import { RunProperty, RunFonts } from "./run";
import {
RunProperty,
RunFonts,
createDefaultRunProperty,
} from "./run-property";

export class DocDefaults {
runProperty: RunProperty;
paragraphProperty: ParagraphProperty;
paragraphProperty: ParagraphProperty = new ParagraphProperty();

size(size: number) {
this.runProperty = { ...this.runProperty, size };
this.runProperty ??= createDefaultRunProperty();
this.runProperty.size(size);
return this;
}

fonts(fonts: RunFonts) {
this.runProperty = { ...this.runProperty, fonts };
this.runProperty ??= createDefaultRunProperty();
this.runProperty.fonts(fonts);
return this;
}

characterSpacing(characterSpacing: number) {
this.runProperty = { ...this.runProperty, characterSpacing };
this.runProperty ??= createDefaultRunProperty();
this.runProperty.spacing(characterSpacing);
return this;
}

lineSpacing(lineSpacing: LineSpacing) {
this.paragraphProperty = { ...this.paragraphProperty, lineSpacing };
this.paragraphProperty.lineSpacing = lineSpacing;
return this;
}
}
41 changes: 21 additions & 20 deletions docx-wasm/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Paragraph } from "./paragraph";
import { LineSpacing, ParagraphProperty } from "./paragraph-property";
import { Table } from "./table";
import { TableOfContents } from "./table-of-contents";
import { RunFonts } from "./run";
import { RunFonts } from "./run-property";
import { AbstractNumbering } from "./abstract-numbering";
import { Numbering } from "./numbering";
import { BookmarkStart } from "./bookmark-start";
Expand Down Expand Up @@ -290,31 +290,31 @@ export class Docx {
level = level.suffix(wasm.LevelSuffixType.Tab);
}

if (l.runProperty.bold) {
if (l.runProperty._bold) {
level = level.bold();
}

if (l.runProperty.italic) {
if (l.runProperty._italic) {
level = level.italic();
}

if (l.runProperty.size) {
level = level.size(l.runProperty.size);
if (l.runProperty._size) {
level = level.size(l.runProperty._size);
}

if (l.runProperty.fonts) {
if (l.runProperty._fonts) {
let f = wasm.createRunFonts();
if (l.runProperty.fonts._ascii) {
f = f.ascii(l.runProperty.fonts._ascii);
if (l.runProperty._fonts._ascii) {
f = f.ascii(l.runProperty._fonts._ascii);
}
if (l.runProperty.fonts._hiAnsi) {
f = f.hi_ansi(l.runProperty.fonts._hiAnsi);
if (l.runProperty._fonts._hiAnsi) {
f = f.hi_ansi(l.runProperty._fonts._hiAnsi);
}
if (l.runProperty.fonts._cs) {
f = f.cs(l.runProperty.fonts._cs);
if (l.runProperty._fonts._cs) {
f = f.cs(l.runProperty._fonts._cs);
}
if (l.runProperty.fonts._eastAsia) {
f = f.east_asia(l.runProperty.fonts._eastAsia);
if (l.runProperty._fonts._eastAsia) {
f = f.east_asia(l.runProperty._fonts._eastAsia);
}
level = level.fonts(f);
}
Expand Down Expand Up @@ -548,20 +548,20 @@ export class Docx {

if (this.styles?.docDefaults) {
if (this.styles.docDefaults.runProperty) {
if (this.styles.docDefaults.runProperty.fonts) {
if (this.styles.docDefaults.runProperty._fonts) {
const fonts = this.buildRunFonts(
this.styles.docDefaults.runProperty.fonts
this.styles.docDefaults.runProperty._fonts
);
docx = docx.default_fonts(fonts);
}

if (this.styles.docDefaults.runProperty.size) {
docx = docx.default_size(this.styles.docDefaults.runProperty.size);
if (this.styles.docDefaults.runProperty._size) {
docx = docx.default_size(this.styles.docDefaults.runProperty._size);
}

if (this.styles.docDefaults.runProperty.characterSpacing) {
if (this.styles.docDefaults.runProperty._characterSpacing) {
docx = docx.default_spacing(
this.styles.docDefaults.runProperty.characterSpacing
this.styles.docDefaults.runProperty._characterSpacing
);
}
}
Expand Down Expand Up @@ -658,6 +658,7 @@ export * from "./table-of-contents";
export * from "./table-of-contents-item";
export * from "./table-row";
export * from "./run";
export * from "./run-property";
export * from "./text";
export * from "./style";
export * from "./styles";
Expand Down
2 changes: 1 addition & 1 deletion docx-wasm/js/json/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DeleteJSONData,
} from "..";
import { BorderType } from "../border";
import { VertAlignType } from "../run";
import { VertAlignType } from "../run-property";
import { FieldChar } from "./bindings/FieldChar";
import { InstrHyperlink } from "./bindings/InstrHyperlink";
import { InstrToC } from "./bindings/InstrToC";
Expand Down
26 changes: 15 additions & 11 deletions docx-wasm/js/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
ParagraphProperty,
SpecialIndentKind,
} from "./paragraph-property";
import { RunFonts, RunProperty } from "./run";
import {
createDefaultRunProperty,
RunFonts,
RunProperty,
} from "./run-property";

export type LevelSuffixType = "nothing" | "tab" | "space";

Expand All @@ -15,7 +19,7 @@ export class Level {
text: string;
jc: string;
paragraphProperty: ParagraphProperty = createDefaultParagraphProperty();
runProperty: RunProperty = {};
runProperty: RunProperty = createDefaultRunProperty();
levelSuffix: LevelSuffixType;

constructor(
Expand Down Expand Up @@ -52,47 +56,47 @@ export class Level {
}

size(size: number) {
this.runProperty = { ...this.runProperty, size };
this.runProperty.size(size);
return this;
}

color(color: string) {
this.runProperty = { ...this.runProperty, color };
this.runProperty.color(color);
return this;
}

highlight(color: string) {
this.runProperty = { ...this.runProperty, highlight: color };
this.runProperty.highlight(color);
return this;
}

bold() {
this.runProperty = { ...this.runProperty, bold: true };
this.runProperty.bold();
return this;
}

italic() {
this.runProperty = { ...this.runProperty, italic: true };
this.runProperty.italic();
return this;
}

underline(type: string) {
this.runProperty = { ...this.runProperty, underline: type };
this.runProperty.underline(type);
return this;
}

vanish() {
this.runProperty = { ...this.runProperty, vanish: true };
this.runProperty.vanish();
return this;
}

fonts(fonts: RunFonts) {
this.runProperty = { ...this.runProperty, fonts };
this.runProperty.fonts(fonts);
return this;
}

characterSpacing(characterSpacing: number) {
this.runProperty = { ...this.runProperty, characterSpacing };
this.runProperty.spacing(characterSpacing);
return this;
}
}
Expand Down
Loading

0 comments on commit 11e09de

Please sign in to comment.