support: num style link (#43)
* support: num style link * chore: update README * chore: update snapmain
parent
c7c6584d7e
commit
ff44dfb7fb
|
@ -106,3 +106,7 @@ writeFileSync("hello.docx", buf);
|
||||||
- [x] HIstory
|
- [x] HIstory
|
||||||
- [ ] Table of contents
|
- [ ] Table of contents
|
||||||
- [ ] Section
|
- [ ] Section
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- wasm-pack 0.9.1+
|
||||||
|
|
|
@ -7,18 +7,35 @@ use serde::Serialize;
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct AbstractNumbering {
|
pub struct AbstractNumbering {
|
||||||
id: usize,
|
id: usize,
|
||||||
|
style_link: Option<String>,
|
||||||
|
num_style_link: Option<String>,
|
||||||
levels: Vec<Level>,
|
levels: Vec<Level>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractNumbering {
|
impl AbstractNumbering {
|
||||||
pub fn new(id: usize) -> Self {
|
pub fn new(id: usize) -> Self {
|
||||||
Self { id, levels: vec![] }
|
Self {
|
||||||
|
id,
|
||||||
|
style_link: None,
|
||||||
|
num_style_link: None,
|
||||||
|
levels: vec![],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_level(mut self, level: Level) -> Self {
|
pub fn add_level(mut self, level: Level) -> Self {
|
||||||
self.levels.push(level);
|
self.levels.push(level);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn num_style_link(mut self, link: impl Into<String>) -> Self {
|
||||||
|
self.num_style_link = Some(link.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn style_link(mut self, link: impl Into<String>) -> Self {
|
||||||
|
self.style_link = Some(link.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuildXML for AbstractNumbering {
|
impl BuildXML for AbstractNumbering {
|
||||||
|
@ -58,4 +75,22 @@ mod tests {
|
||||||
r#"<w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl></w:abstractNum>"#
|
r#"<w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl></w:abstractNum>"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_numbering_json() {
|
||||||
|
let mut c = AbstractNumbering::new(0);
|
||||||
|
c = c
|
||||||
|
.add_level(Level::new(
|
||||||
|
1,
|
||||||
|
Start::new(1),
|
||||||
|
NumberFormat::new("decimal"),
|
||||||
|
LevelText::new("%4."),
|
||||||
|
LevelJc::new("left"),
|
||||||
|
))
|
||||||
|
.num_style_link("style1");
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_string(&c).unwrap(),
|
||||||
|
r#"{"id":0,"styleLink":null,"numStyleLink":"style1","levels":[{"level":1,"start":1,"format":"decimal","text":"%4.","jc":"left","pstyle":null,"paragraphProperty":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":"Normal","numberingProperty":null,"alignment":null,"indent":null}}]}"#
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,19 @@ impl FromXML for Numberings {
|
||||||
attributes, name, ..
|
attributes, name, ..
|
||||||
}) => {
|
}) => {
|
||||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||||
if let XMLElement::Level = e {
|
match e {
|
||||||
let l = Level::read(&mut parser, &attributes)?;
|
XMLElement::Level => {
|
||||||
abs_num = abs_num.add_level(l);
|
let l = Level::read(&mut parser, &attributes)?;
|
||||||
|
abs_num = abs_num.add_level(l);
|
||||||
|
}
|
||||||
|
XMLElement::StyleLink => {
|
||||||
|
abs_num = abs_num.style_link(&attributes[0].value)
|
||||||
|
}
|
||||||
|
XMLElement::NumStyleLink => {
|
||||||
|
abs_num =
|
||||||
|
abs_num.num_style_link(&attributes[0].value)
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(XmlEvent::EndElement { name, .. }) => {
|
Ok(XmlEvent::EndElement { name, .. }) => {
|
||||||
|
@ -112,8 +122,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_numberings_from_xml() {
|
fn test_numberings_from_xml() {
|
||||||
let xml =
|
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||||
r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
||||||
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
|
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
|
||||||
<w:abstractNum w:abstractNumId="0" w15:restartNumberingAfterBreak="0">
|
<w:abstractNum w:abstractNumId="0" w15:restartNumberingAfterBreak="0">
|
||||||
<w:multiLevelType w:val="hybridMultilevel"></w:multiLevelType>
|
<w:multiLevelType w:val="hybridMultilevel"></w:multiLevelType>
|
||||||
|
@ -150,4 +159,44 @@ mod tests {
|
||||||
.add_numbering(Numbering::new(1, 0));
|
.add_numbering(Numbering::new(1, 0));
|
||||||
assert_eq!(n, nums)
|
assert_eq!(n, nums)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_numberings_from_xml_with_num_style_link() {
|
||||||
|
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||||
|
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
|
||||||
|
<w:abstractNum w:abstractNumId="0">
|
||||||
|
<w:multiLevelType w:val="hybridMultilevel"/>
|
||||||
|
<w:numStyleLink w:val="style1"/>
|
||||||
|
</w:abstractNum>
|
||||||
|
<w:num w:numId="1">
|
||||||
|
<w:abstractNumId w:val="0"></w:abstractNumId>
|
||||||
|
</w:num>
|
||||||
|
</w:numbering>"#;
|
||||||
|
let n = Numberings::from_xml(xml.as_bytes()).unwrap();
|
||||||
|
let mut nums = Numberings::new();
|
||||||
|
nums = nums
|
||||||
|
.add_abstract_numbering(AbstractNumbering::new(0).num_style_link("style1"))
|
||||||
|
.add_numbering(Numbering::new(1, 0));
|
||||||
|
assert_eq!(n, nums)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_numberings_from_xml_with_style_link() {
|
||||||
|
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||||
|
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
|
||||||
|
<w:abstractNum w:abstractNumId="0">
|
||||||
|
<w:multiLevelType w:val="hybridMultilevel"/>
|
||||||
|
<w:styleLink w:val="style1"/>
|
||||||
|
</w:abstractNum>
|
||||||
|
<w:num w:numId="1">
|
||||||
|
<w:abstractNumId w:val="0"></w:abstractNumId>
|
||||||
|
</w:num>
|
||||||
|
</w:numbering>"#;
|
||||||
|
let n = Numberings::from_xml(xml.as_bytes()).unwrap();
|
||||||
|
let mut nums = Numberings::new();
|
||||||
|
nums = nums
|
||||||
|
.add_abstract_numbering(AbstractNumbering::new(0).style_link("style1"))
|
||||||
|
.add_numbering(Numbering::new(1, 0));
|
||||||
|
assert_eq!(n, nums)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,8 @@ pub enum XMLElement {
|
||||||
NumberFormat,
|
NumberFormat,
|
||||||
LevelText,
|
LevelText,
|
||||||
LevelJustification,
|
LevelJustification,
|
||||||
|
StyleLink,
|
||||||
|
NumStyleLink,
|
||||||
Unsupported,
|
Unsupported,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,6 +153,8 @@ impl FromStr for XMLElement {
|
||||||
"numFmt" => Ok(XMLElement::NumberFormat),
|
"numFmt" => Ok(XMLElement::NumberFormat),
|
||||||
"lvlText" => Ok(XMLElement::LevelText),
|
"lvlText" => Ok(XMLElement::LevelText),
|
||||||
"lvlJc" => Ok(XMLElement::LevelJustification),
|
"lvlJc" => Ok(XMLElement::LevelJustification),
|
||||||
|
"numStyleLink" => Ok(XMLElement::NumStyleLink),
|
||||||
|
"styleLink" => Ok(XMLElement::StyleLink),
|
||||||
_ => Ok(XMLElement::Unsupported),
|
_ => Ok(XMLElement::Unsupported),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -13,6 +13,8 @@ export type LevelJSON = {
|
||||||
export type AbstractNumberingJSON = {
|
export type AbstractNumberingJSON = {
|
||||||
id: number;
|
id: number;
|
||||||
levels: LevelJSON[];
|
levels: LevelJSON[];
|
||||||
|
numStyleLink: string | null;
|
||||||
|
styleLink: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type NumberingJSON = {
|
export type NumberingJSON = {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "docx-wasm",
|
"name": "docx-wasm",
|
||||||
"version": "0.0.27",
|
"version": "0.0.35",
|
||||||
"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>",
|
||||||
|
|
Loading…
Reference in New Issue