update json (#458)

main
bokuweb 2022-03-29 13:32:14 +09:00 committed by GitHub
parent 9d58871287
commit 6f787af9af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 139 additions and 83 deletions

View File

@ -1,22 +1,56 @@
use super::*; use super::*;
use serde::Serialize; use serde::{ser::*, Serialize};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug, Clone, Serialize, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct Drawing { pub struct Drawing {
pub data: Option<DrawingData>, pub data: Option<DrawingData>,
} }
#[derive(Debug, Clone, Serialize, PartialEq)] impl Serialize for Drawing {
#[serde(rename_all = "camelCase")] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.data {
Some(DrawingData::Pic(ref pic)) => {
let mut t = serializer.serialize_struct("Drawing", 2)?;
t.serialize_field("type", "pic")?;
t.serialize_field("data", pic)?;
t.end()
}
_ => {
let t = serializer.serialize_struct("Drawing", 2)?;
t.end()
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum DrawingData { pub enum DrawingData {
Pic(Pic), Pic(Pic),
} }
impl Serialize for DrawingData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
DrawingData::Pic(ref pic) => {
let mut t = serializer.serialize_struct("Pic", 2)?;
t.serialize_field("type", "pic")?;
t.serialize_field("data", pic)?;
t.end()
}
}
}
}
impl Drawing { impl Drawing {
pub fn new() -> Drawing { pub fn new() -> Drawing {
Default::default() Default::default()

View File

@ -5,23 +5,8 @@ use crate::documents::*;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug, Clone, Copy, Serialize, PartialEq)] #[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[serde(rename_all = "camelCase")] #[ts(export)]
pub enum PicAlign {
Left,
Right,
Bottom,
Top,
}
#[derive(Debug, Clone, Copy, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum DrawingPosition {
Offset(i32),
Align(PicAlign),
}
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Pic { pub struct Pic {
pub id: String, pub id: String,
@ -62,8 +47,7 @@ pub struct Pic {
} }
impl Pic { impl Pic {
pub fn pub fn new(buf: Vec<u8>) -> Pic {
new(buf: Vec<u8>) -> Pic {
let id = create_pic_rid(generate_pic_id()); let id = create_pic_rid(generate_pic_id());
let dimg = image::load_from_memory(&buf).unwrap(); let dimg = image::load_from_memory(&buf).unwrap();
let size = dimg.dimensions(); let size = dimg.dimensions();

View File

@ -6,6 +6,7 @@ use std::str::FromStr;
use xml::attribute::OwnedAttribute; use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent}; use xml::reader::{EventReader, XmlEvent};
use crate::types::*;
use crate::{DrawingPositionType, RelativeFromHType, RelativeFromVType}; use crate::{DrawingPositionType, RelativeFromHType, RelativeFromVType};
use super::*; use super::*;

View File

@ -3,9 +3,29 @@ use serde::Serialize;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, PartialEq)] #[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum DrawingPositionType { pub enum DrawingPositionType {
Anchor, Anchor,
Inline, Inline,
} }
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub enum PicAlign {
Left,
Right,
Bottom,
Top,
}
#[derive(Debug, Clone, Copy, Serialize, PartialEq, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub enum DrawingPosition {
Offset(i32),
Align(PicAlign),
}

View File

@ -8,7 +8,8 @@ use std::str::FromStr;
// @See: 20.4.3.4 ST_RelFromH (Horizontal Relative Positioning) // @See: 20.4.3.4 ST_RelFromH (Horizontal Relative Positioning)
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum RelativeFromHType { pub enum RelativeFromHType {
/// Specifies that the horizontal positioning shall be /// Specifies that the horizontal positioning shall be
@ -80,7 +81,8 @@ impl FromStr for RelativeFromHType {
} }
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum RelativeFromVType { pub enum RelativeFromVType {
BottomMargin, BottomMargin,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,3 @@
import type { PicAlign } from "./PicAlign";
export type DrawingPosition = { offset: number } | { align: PicAlign };

View File

@ -0,0 +1,2 @@
export type DrawingPositionType = "anchor" | "inline";

View File

@ -0,0 +1,6 @@
import type { DrawingPosition } from "./DrawingPosition";
import type { DrawingPositionType } from "./DrawingPositionType";
import type { RelativeFromHType } from "./RelativeFromHType";
import type { RelativeFromVType } from "./RelativeFromVType";
export interface Pic { id: string, image: Array<number>, size: [number, number], positionType: DrawingPositionType, simplePos: boolean, simplePosX: number, simplePosY: number, layoutInCell: boolean, relativeHeight: number, allowOverlap: boolean, positionH: DrawingPosition, positionV: DrawingPosition, relativeFromH: RelativeFromHType, relativeFromV: RelativeFromVType, distT: number, distB: number, distL: number, distR: number, }

View File

@ -0,0 +1,2 @@
export type PicAlign = "left" | "right" | "bottom" | "top";

View File

@ -0,0 +1,2 @@
export type RelativeFromHType = "character" | "column" | "insideMargin" | "leftMargin" | "margin" | "outsizeMargin" | "page" | "rightMargin";

View File

@ -0,0 +1,2 @@
export type RelativeFromVType = "bottomMargin" | "insideMargin" | "line" | "margin" | "outsizeMargin" | "page" | "paragraph" | "topMargin";

View File

@ -3,12 +3,10 @@ import { TextBoxContentJSON } from "./textbox-content";
export type DrawingJSON = { export type DrawingJSON = {
type: "drawing"; type: "drawing";
data: { data: {
children: DrawingChildJSON[]; type: "pic";
}; };
}; };
export type DrawingChildJSON = WpAnchorJSON;
export type WpAnchorJSON = { export type WpAnchorJSON = {
type: "anchor"; type: "anchor";
data: { data: {

View File

@ -47,6 +47,8 @@ export type DocxJSON = {
webSettings: WebSettingsJSON; webSettings: WebSettingsJSON;
fontTable: {}; fontTable: {};
themes: ThemeJSON[]; themes: ThemeJSON[];
// base64 encoded iamges
images: string[];
}; };
export type SettingsJSON = { export type SettingsJSON = {

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.249", "version": "0.0.250",
"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>",

View File

@ -11724,33 +11724,32 @@ Object {
Object { Object {
"data": Object { "data": Object {
"data": Object { "data": Object {
"pic": Object { "allowOverlap": true,
"allowOverlap": true, "distB": 0,
"distB": 0, "distL": 0,
"distL": 0, "distR": 0,
"distR": 0, "distT": 0,
"distT": 0, "id": "rId4",
"id": "rId4", "layoutInCell": true,
"layoutInCell": true, "positionH": Object {
"positionH": Object { "offset": 0,
"offset": 0,
},
"positionType": "inline",
"positionV": Object {
"offset": 0,
},
"relativeFromH": "margin",
"relativeFromV": "margin",
"relativeHeight": 0,
"simplePos": false,
"simplePosX": 0,
"simplePosY": 0,
"size": Array [
1275711,
1699947,
],
}, },
"positionType": "inline",
"positionV": Object {
"offset": 0,
},
"relativeFromH": "margin",
"relativeFromV": "margin",
"relativeHeight": 0,
"simplePos": false,
"simplePosX": 0,
"simplePosY": 0,
"size": Array [
1275711,
1699947,
],
}, },
"type": "pic",
}, },
"type": "drawing", "type": "drawing",
}, },
@ -11794,33 +11793,32 @@ Object {
Object { Object {
"data": Object { "data": Object {
"data": Object { "data": Object {
"pic": Object { "allowOverlap": true,
"allowOverlap": true, "distB": 0,
"distB": 0, "distL": 114300,
"distL": 114300, "distR": 114300,
"distR": 114300, "distT": 0,
"distT": 0, "id": "rId5",
"id": "rId5", "layoutInCell": true,
"layoutInCell": true, "positionH": Object {
"positionH": Object { "offset": 798195,
"offset": 798195,
},
"positionType": "anchor",
"positionV": Object {
"offset": 2510155,
},
"relativeFromH": "margin",
"relativeFromV": "margin",
"relativeHeight": 251658240,
"simplePos": false,
"simplePosX": 798195,
"simplePosY": 2510155,
"size": Array [
1264920,
1685925,
],
}, },
"positionType": "anchor",
"positionV": Object {
"offset": 2510155,
},
"relativeFromH": "margin",
"relativeFromV": "margin",
"relativeHeight": 251658240,
"simplePos": false,
"simplePosX": 798195,
"simplePosY": 2510155,
"size": Array [
1264920,
1685925,
],
}, },
"type": "pic",
}, },
"type": "drawing", "type": "drawing",
}, },