use super::memo_query::{MemoError, MemoErrorCode};
use crate::document_core::DocumentCore;
use crate::model::control::Control;
use crate::model::memo_shape::{
color_from_hex, color_hex, memo_shapes, set_memo_shapes, MemoShape,
};
fn invalid(message: &str) -> MemoError {
MemoError::new(MemoErrorCode::InvalidRange, message)
}
impl DocumentCore {
pub fn memo_shape(&self, section_idx: usize) -> Option<MemoShape> {
let section = self.document.sections.get(section_idx)?;
let id = usize::from(section.section_def.memo_shape_id);
Some(
id.checked_sub(1)
.and_then(|index| memo_shapes(&self.document.doc_info).get(index).copied())
.unwrap_or_default(),
)
}
pub fn get_memo_shape_json(&self, section_idx: usize) -> Result<String, MemoError> {
let shape = self
.memo_shape(section_idx)
.ok_or_else(|| invalid("구역 위치를 찾을 수 없음"))?;
Ok(format!(
r#"{{"width":{},"lineType":{},"lineWidth":{},"lineColor":"{}","fillColor":"{}","activeColor":"{}"}}"#,
shape.width,
shape.line_type,
shape.line_width,
color_hex(shape.line_color),
color_hex(shape.fill_color),
color_hex(shape.active_color),
))
}
pub fn set_memo_shape_json(
&mut self,
section: Option<usize>,
json: &str,
) -> Result<String, MemoError> {
let value: serde_json::Value =
serde_json::from_str(json).map_err(|_| invalid("메모 모양은 JSON 객체여야 함"))?;
let number = |key: &str, max: u64| {
value
.get(key)
.and_then(serde_json::Value::as_u64)
.filter(|n| *n <= max)
.ok_or_else(|| invalid(&format!("메모 모양 {key} 값이 맞지 않음")))
};
let color = |key: &str| {
value
.get(key)
.and_then(serde_json::Value::as_str)
.and_then(color_from_hex)
.ok_or_else(|| invalid(&format!("메모 모양 {key}는 #RRGGBB여야 함")))
};
let width = number("width", u64::from(i32::MAX as u32))?;
if width == 0 {
return Err(invalid("메모 모양 width 값이 맞지 않음"));
}
let previous = self
.memo_shape(section.unwrap_or(0))
.ok_or_else(|| invalid("구역 위치를 찾을 수 없음"))?;
let shape = MemoShape {
width: width as u32,
line_type: number("lineType", 13)? as u8,
line_width: number("lineWidth", 15)? as u8,
line_color: color("lineColor")?,
fill_color: color("fillColor")?,
active_color: color("activeColor")?,
memo_type: previous.memo_type,
};
let targets: Vec<usize> = match section {
Some(index) => vec![index],
None => (0..self.document.sections.len()).collect(),
};
let mut shapes = memo_shapes(&self.document.doc_info);
let refs: Vec<u16> = self
.document
.sections
.iter()
.map(|section| section.section_def.memo_shape_id)
.collect();
let target_ref = refs[targets[0]];
let shared = targets.iter().all(|&index| refs[index] == target_ref)
&& refs
.iter()
.enumerate()
.all(|(index, id)| *id != target_ref || targets.contains(&index));
let id = if target_ref != 0 && usize::from(target_ref) <= shapes.len() && shared {
shapes[usize::from(target_ref) - 1] = shape;
target_ref
} else {
shapes.push(shape);
u16::try_from(shapes.len()).map_err(|_| invalid("메모 모양이 너무 많음"))?
};
set_memo_shapes(&mut self.document.doc_info, &shapes);
for index in targets {
let section = &mut self.document.sections[index];
section.section_def.memo_shape_id = id;
for ctrl in section
.paragraphs
.first_mut()
.map(|para| para.controls.iter_mut())
.into_iter()
.flatten()
{
if let Control::SectionDef(def) = ctrl {
def.memo_shape_id = id;
}
}
section.raw_stream = None;
self.mark_section_dirty(index);
}
Ok(r#"{"ok":true}"#.to_string())
}
}