Skip to content

Commit

Permalink
feat: set horizontal alignment and font color
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJeremyHe committed Jan 21, 2025
1 parent 5823315 commit 4169af6
Show file tree
Hide file tree
Showing 27 changed files with 529 additions and 107 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ num = "0.4.0"
num-bigint = "0.4"
num-traits = "0.2"
quick-xml = { workspace = true, features = ["serialize"] }
xmlserde = { workspace = true }
rand = "0.8.4"
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand All @@ -37,6 +38,9 @@ logisheets_astchecker = { path = "./ast_checker", version = "0.7.0" }
gents = { workspace = true, optional = true }
gents_derives = { workspace = true, optional = true }

[dev-dependencies]
serde_json = { workspace = true }

[features]
gents = ["dep:gents", "dep:gents_derives"]
sequencer = []
Expand Down
6 changes: 1 addition & 5 deletions crates/controller/src/container/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use logisheets_base::{
SheetId, TextId,
};

use crate::{
cell::Cell,
edit_action::{EditPayload, StyleUpdateType},
Error,
};
use crate::{cell::Cell, edit_action::EditPayload, Error};

use super::{ctx::ContainerExecCtx, DataContainer};

Expand Down
43 changes: 39 additions & 4 deletions crates/controller/src/controller/style.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::style_manager::RawStyle;
use crate::edit_action::{HorizontalAlignment, VerticalAlignment};
use crate::theme_manager::ThemeManager;
use crate::{edit_action::Alignment, style_manager::RawStyle};
use logisheets_workbook::prelude::{
CtBorder, CtBorderPr, CtCellAlignment, CtCellProtection, CtColor, CtFill, CtFont, CtFontFamily,
CtFontName, CtFontScheme, CtUnderlineProperty, CtVerticalAlignFontProperty, StBorderStyle,
StGradientType, StPatternType,
StGradientType, StHorizontalAlignment, StPatternType, StVerticalAlignment,
};

#[derive(Debug, Clone)]
Expand All @@ -12,7 +13,7 @@ pub struct Style {
pub font: Font,
pub fill: Fill,
pub border: Border,
pub alignment: Option<CtCellAlignment>,
pub alignment: Option<Alignment>,
pub protection: Option<CtCellProtection>,
pub formatter: String,
}
Expand Down Expand Up @@ -127,7 +128,7 @@ impl<'a> StyleConverter<'a> {
font: self.convert_font(raw_style.font),
fill: self.convert_fill(raw_style.fill),
border: self.convert_border(raw_style.border),
alignment: raw_style.alignment,
alignment: raw_style.alignment.map(|v| self.convert_alignment(v)),
protection: raw_style.protection,
formatter: raw_style.formatter,
}
Expand Down Expand Up @@ -179,6 +180,40 @@ impl<'a> StyleConverter<'a> {
}
}

fn convert_alignment(&self, raw: CtCellAlignment) -> Alignment {
let horizontal = if let Some(h) = raw.horizontal {
match h {
StHorizontalAlignment::General => Some(HorizontalAlignment::General),
StHorizontalAlignment::Left => Some(HorizontalAlignment::Left),
StHorizontalAlignment::Center => Some(HorizontalAlignment::Center),
StHorizontalAlignment::Right => Some(HorizontalAlignment::Right),
StHorizontalAlignment::Fill => Some(HorizontalAlignment::Fill),
StHorizontalAlignment::Justify => Some(HorizontalAlignment::Justify),
StHorizontalAlignment::CenterContinuous => {
Some(HorizontalAlignment::CenterContinuous)
}
StHorizontalAlignment::Distributed => Some(HorizontalAlignment::Distributed),
}
} else {
None
};
let vertical = if let Some(v) = raw.vertical {
match v {
StVerticalAlignment::Top => Some(VerticalAlignment::Top),
StVerticalAlignment::Center => Some(VerticalAlignment::Center),
StVerticalAlignment::Bottom => Some(VerticalAlignment::Bottom),
StVerticalAlignment::Justify => Some(VerticalAlignment::Justify),
StVerticalAlignment::Distributed => Some(VerticalAlignment::Distributed),
}
} else {
None
};
Alignment {
horizontal,
vertical,
}
}

fn convert_border(&self, border: CtBorder) -> Border {
Border {
left: border
Expand Down
51 changes: 51 additions & 0 deletions crates/controller/src/edit_action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,45 @@ pub struct LineStyleUpdate {
pub type Color = String;

#[derive(Debug, Clone)]
#[cfg_attr(
feature = "gents",
gents_derives::gents_header(file_name = "alignment.ts")
)]
pub struct Alignment {
pub horizontal: Option<HorizontalAlignment>,
pub vertical: Option<VerticalAlignment>,
}

#[derive(Debug, Clone)]
#[cfg_attr(
feature = "gents",
gents_derives::gents_header(file_name = "vertical_alignment.ts")
)]
pub enum VerticalAlignment {
Center,
Top,
Bottom,
Justify,
Distributed,
}

#[derive(Debug, Clone)]
#[cfg_attr(
feature = "gents",
gents_derives::gents_header(file_name = "horizontal_alignment.ts")
)]
pub enum HorizontalAlignment {
General,
Left,
Center,
Right,
Fill,
Justify,
CenterContinuous,
Distributed,
}

#[derive(Debug, Clone, Default)]
#[cfg_attr(
feature = "gents",
gents_derives::gents_header(file_name = "style_update_type.ts")
Expand Down Expand Up @@ -507,6 +546,7 @@ pub struct StyleUpdateType {
pub set_border_giagonal_down: Option<bool>,
pub set_border_outline: Option<bool>,
pub set_pattern_fill: Option<PatternFill>,
pub set_alignment: Option<Alignment>,
}

impl From<BlockStyleUpdate> for EditPayload {
Expand Down Expand Up @@ -643,3 +683,14 @@ impl Payload for InsertColsInBlock {}
impl Payload for InsertRowsInBlock {}
impl Payload for DeleteColsInBlock {}
impl Payload for DeleteRowsInBlock {}

#[cfg(test)]
mod tests {
use super::VerticalAlignment;

#[test]
fn test_should_have_double_quote() {
let s1 = "\"center\"".to_string();
let _: VerticalAlignment = serde_json::from_str(&s1).unwrap();
}
}
45 changes: 44 additions & 1 deletion crates/controller/src/style_manager/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::{errors::StyleError, StyleManager};
use crate::{edit_action::StyleUpdateType, Error};
use crate::{
edit_action::{HorizontalAlignment, StyleUpdateType, VerticalAlignment},
Error,
};
use logisheets_base::StyleId;
use logisheets_workbook::prelude::{CtCellAlignment, StHorizontalAlignment, StVerticalAlignment};

pub fn execute_style_payload(
sm: &mut StyleManager,
Expand All @@ -27,6 +31,45 @@ pub fn execute_style_payload(
xf.fill_id = Some(new_fill_id);
xf.apply_fill = Some(true)
}
if let Some(alignment) = update_type.set_alignment {
let mut result = CtCellAlignment::default();
if let Some(v) = alignment.vertical {
match v {
VerticalAlignment::Center => result.vertical = Some(StVerticalAlignment::Center),
VerticalAlignment::Top => result.vertical = Some(StVerticalAlignment::Top),
VerticalAlignment::Bottom => result.vertical = Some(StVerticalAlignment::Bottom),
VerticalAlignment::Justify => result.vertical = Some(StVerticalAlignment::Justify),
VerticalAlignment::Distributed => {
result.vertical = Some(StVerticalAlignment::Distributed)
}
}
}
if let Some(v) = alignment.horizontal {
match v {
HorizontalAlignment::General => {
result.horizontal = Some(StHorizontalAlignment::General)
}
HorizontalAlignment::Left => result.horizontal = Some(StHorizontalAlignment::Left),
HorizontalAlignment::Center => {
result.horizontal = Some(StHorizontalAlignment::Center)
}
HorizontalAlignment::Right => {
result.horizontal = Some(StHorizontalAlignment::Right)
}
HorizontalAlignment::Fill => result.horizontal = Some(StHorizontalAlignment::Fill),
HorizontalAlignment::Justify => {
result.horizontal = Some(StHorizontalAlignment::Justify)
}
HorizontalAlignment::CenterContinuous => {
result.horizontal = Some(StHorizontalAlignment::CenterContinuous)
}
HorizontalAlignment::Distributed => {
result.horizontal = Some(StHorizontalAlignment::Distributed)
}
}
}
xf.alignment = Some(result);
}
let new_id = cell_xfs_manager.get_id(&xf);
Ok(new_id)
}
9 changes: 9 additions & 0 deletions crates/controller/src/style_manager/font_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ impl FontManager {
if let Some(b) = update_type.set_font_condense {
font.condense = b;
}
if let Some(c) = &update_type.set_font_color {
font.color = Some(CtColor {
auto: None,
indexed: None,
rgb: Some(c.clone()),
theme: None,
tint: 0.,
})
}

let new_id = self.get_id(&font);
if new_id != id {
Expand Down
81 changes: 77 additions & 4 deletions crates/wasms/server/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use logisheets_controller::controller::display::{CellPosition, DisplaySheetRequest};
use logisheets_controller::edit_action::{
AsyncFuncResult, BlockInput, CellClear, CellInput, CellStyleUpdate, CreateBlock, CreateSheet,
DeleteCols, DeleteColsInBlock, DeleteRows, DeleteRowsInBlock, DeleteSheet, EditAction,
EditPayload, InsertCols, InsertColsInBlock, InsertRows, InsertRowsInBlock, LineStyleUpdate,
MoveBlock, PayloadsAction, SetColWidth, SetRowHeight, SheetRename, StyleUpdateType,
Alignment, AsyncFuncResult, BlockInput, CellClear, CellInput, CellStyleUpdate, CreateBlock,
CreateSheet, DeleteCols, DeleteColsInBlock, DeleteRows, DeleteRowsInBlock, DeleteSheet,
EditAction, EditPayload, HorizontalAlignment, InsertCols, InsertColsInBlock, InsertRows,
InsertRowsInBlock, LineStyleUpdate, MoveBlock, PayloadsAction, SetColWidth, SetRowHeight,
SheetRename, StyleUpdateType, VerticalAlignment,
};
use logisheets_controller::{AsyncCalcResult, AsyncErr, RowInfo, SaveFileResult, Workbook};
use logisheets_controller::{ColInfo, ErrorMessage};
Expand Down Expand Up @@ -449,6 +450,7 @@ pub fn set_line_font(
set_border_giagonal_down: None,
set_border_outline: None,
set_pattern_fill: None,
set_alignment: None,
},
});
MANAGER.get_mut().add_payload(id, p);
Expand Down Expand Up @@ -498,11 +500,81 @@ pub fn set_font(
set_border_giagonal_down: None,
set_border_outline: None,
set_pattern_fill: None,
set_alignment: None,
},
});
MANAGER.get_mut().add_payload(id, p);
}

#[wasm_bindgen]
pub fn set_cell_alignment(
id: usize,
sheet_idx: usize,
row: usize,
col: usize,
h_align: Option<String>,
v_align: Option<String>,
) {
let v: Option<VerticalAlignment> = if let Some(value) = v_align {
serde_json::from_str(&format!("\"{}\"", value)).unwrap()
} else {
None
};
let h: Option<HorizontalAlignment> = if let Some(value) = h_align {
serde_json::from_str(&&format!("\"{}\"", value)).unwrap()
} else {
None
};
let mut ty = StyleUpdateType::default();
ty.set_alignment = Some(Alignment {
vertical: v,
horizontal: h,
});
let p = EditPayload::CellStyleUpdate(CellStyleUpdate {
sheet_idx,
row,
col,
ty,
});

MANAGER.get_mut().add_payload(id, p);
}

#[wasm_bindgen]
pub fn set_line_alignment(
id: usize,
sheet_idx: usize,
row: bool,
from: usize,
to: usize,
h_align: Option<String>,
v_align: Option<String>,
) {
let v: Option<VerticalAlignment> = if let Some(v_align) = v_align {
serde_json::from_str(&v_align).unwrap()
} else {
None
};
let h: Option<HorizontalAlignment> = if let Some(h_align) = h_align {
serde_json::from_str(&h_align).unwrap()
} else {
None
};
let mut ty = StyleUpdateType::default();
ty.set_alignment = Some(Alignment {
vertical: v,
horizontal: h,
});
let p = EditPayload::LineStyleUpdate(LineStyleUpdate {
sheet_idx,
row,
from,
to,
ty,
});
MANAGER.get_mut().add_payload(id, p);
}

#[wasm_bindgen]
pub fn set_border(
id: usize,
Expand Down Expand Up @@ -551,6 +623,7 @@ pub fn set_border(
set_border_giagonal_down: diagonal_down,
set_border_outline: outline,
set_pattern_fill: None,
set_alignment: None,
},
});
MANAGER.get_mut().add_payload(id, p);
Expand Down
2 changes: 1 addition & 1 deletion crates/workbook/src/ooxml/complex_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl crate::XmlDeserialize for CtFill {
}
}

#[derive(Debug, XmlSerialize, XmlDeserialize, PartialEq, Eq, Clone, Hash)]
#[derive(Debug, XmlSerialize, XmlDeserialize, PartialEq, Eq, Clone, Hash, Default)]
#[cfg_attr(
feature = "gents",
gents_derives::gents_header(file_name = "cell_alignment.ts")
Expand Down
Loading

0 comments on commit 4169af6

Please sign in to comment.