use crate::MergeTextFormatDto;
use anyhow::{Result, anyhow};
use common::database::CommandUnitOfWork;
use common::direct_access::block::block_repository::BlockRelationshipField;
use common::direct_access::document::document_repository::DocumentRelationshipField;
use common::direct_access::frame::frame_repository::FrameRelationshipField;
use common::direct_access::root::root_repository::RootRelationshipField;
use common::entities::{Block, Document, Frame, InlineContent, InlineElement, Root};
use common::snapshot::EntityTreeSnapshot;
use common::types::{EntityId, ROOT_ENTITY_ID};
use common::undo_redo::UndoRedoCommand;
use std::any::Any;
pub trait MergeTextFormatUnitOfWorkFactoryTrait: Send + Sync {
fn create(&self) -> Box<dyn MergeTextFormatUnitOfWorkTrait>;
}
#[macros::uow_action(entity = "Root", action = "Get")]
#[macros::uow_action(entity = "Root", action = "GetRelationship")]
#[macros::uow_action(entity = "Document", action = "Get")]
#[macros::uow_action(entity = "Document", action = "GetRelationship")]
#[macros::uow_action(entity = "Document", action = "Snapshot")]
#[macros::uow_action(entity = "Document", action = "Restore")]
#[macros::uow_action(entity = "Frame", action = "Get")]
#[macros::uow_action(entity = "Frame", action = "GetRelationship")]
#[macros::uow_action(entity = "Block", action = "Get")]
#[macros::uow_action(entity = "Block", action = "GetMulti")]
#[macros::uow_action(entity = "Block", action = "GetRelationship")]
#[macros::uow_action(entity = "InlineElement", action = "Get")]
#[macros::uow_action(entity = "InlineElement", action = "GetMulti")]
#[macros::uow_action(entity = "InlineElement", action = "Update")]
#[macros::uow_action(entity = "InlineElement", action = "Create")]
pub trait MergeTextFormatUnitOfWorkTrait: CommandUnitOfWork {}
fn element_char_len(elem: &InlineElement) -> i64 {
match &elem.content {
InlineContent::Text(s) => s.chars().count() as i64,
InlineContent::Image { .. } => 1,
InlineContent::Empty => 0,
}
}
fn apply_merge_format(elem: &mut InlineElement, dto: &MergeTextFormatDto) {
if let Some(ref family) = dto.font_family
&& !family.is_empty()
{
elem.fmt_font_family = Some(family.clone());
}
if let Some(bold) = dto.font_bold {
elem.fmt_font_bold = Some(bold);
}
if let Some(italic) = dto.font_italic {
elem.fmt_font_italic = Some(italic);
}
if let Some(underline) = dto.font_underline {
elem.fmt_font_underline = Some(underline);
}
if let Some(strikeout) = dto.font_strikeout {
elem.fmt_font_strikeout = Some(strikeout);
}
elem.updated_at = chrono::Utc::now();
}
fn execute_merge_text_format(
uow: &mut Box<dyn MergeTextFormatUnitOfWorkTrait>,
dto: &MergeTextFormatDto,
) -> Result<EntityTreeSnapshot> {
let root = uow
.get_root(&ROOT_ENTITY_ID)?
.ok_or_else(|| anyhow!("Root entity not found"))?;
let doc_ids = uow.get_root_relationship(&root.id, &RootRelationshipField::Document)?;
let doc_id = *doc_ids
.first()
.ok_or_else(|| anyhow!("Root has no document"))?;
let _document = uow
.get_document(&doc_id)?
.ok_or_else(|| anyhow!("Document not found"))?;
let snapshot = uow.snapshot_document(&[doc_id])?;
let frame_ids = uow.get_document_relationship(&doc_id, &DocumentRelationshipField::Frames)?;
let mut all_block_ids = Vec::new();
for fid in &frame_ids {
let block_ids = uow.get_frame_relationship(fid, &FrameRelationshipField::Blocks)?;
all_block_ids.extend(block_ids);
}
let blocks_opt = uow.get_block_multi(&all_block_ids)?;
let mut blocks: Vec<Block> = blocks_opt.into_iter().flatten().collect();
blocks.sort_by_key(|b| b.document_position);
let range_start = std::cmp::min(dto.position, dto.anchor);
let range_end = std::cmp::max(dto.position, dto.anchor);
if range_start == range_end {
return Ok(snapshot);
}
for block in &blocks {
let block_start = block.document_position;
let block_end = block_start + block.text_length;
if block_end <= range_start || block_start >= range_end {
continue;
}
let element_ids =
uow.get_block_relationship(&block.id, &BlockRelationshipField::Elements)?;
let elements_opt = uow.get_inline_element_multi(&element_ids)?;
let elements: Vec<InlineElement> = elements_opt.into_iter().flatten().collect();
let mut elem_doc_pos = block_start;
for elem in &elements {
let elem_len = element_char_len(elem);
let elem_start = elem_doc_pos;
let elem_end = elem_start + elem_len;
if elem_end <= range_start || elem_start >= range_end {
elem_doc_pos += elem_len;
continue;
}
if elem_start >= range_start && elem_end <= range_end {
let mut updated = elem.clone();
apply_merge_format(&mut updated, dto);
uow.update_inline_element(&updated)?;
}
else if let InlineContent::Text(ref text) = elem.content {
let local_start = std::cmp::max(0, range_start - elem_start) as usize;
let local_end = std::cmp::min(elem_len, range_end - elem_start) as usize;
let chars: Vec<char> = text.chars().collect();
if local_start > 0 && local_end < chars.len() {
let before_text: String = chars[..local_start].iter().collect();
let middle_text: String = chars[local_start..local_end].iter().collect();
let after_text: String = chars[local_end..].iter().collect();
let mut updated_orig = elem.clone();
updated_orig.content = InlineContent::Text(before_text);
updated_orig.updated_at = chrono::Utc::now();
uow.update_inline_element(&updated_orig)?;
let mut middle_elem = elem.clone();
middle_elem.id = 0;
middle_elem.content = InlineContent::Text(middle_text);
apply_merge_format(&mut middle_elem, dto);
uow.create_inline_element(&middle_elem, block.id, -1)?;
let mut after_elem = elem.clone();
after_elem.id = 0;
after_elem.content = InlineContent::Text(after_text);
after_elem.updated_at = chrono::Utc::now();
uow.create_inline_element(&after_elem, block.id, -1)?;
} else if local_start > 0 {
let before_text: String = chars[..local_start].iter().collect();
let rest_text: String = chars[local_start..].iter().collect();
let mut updated_orig = elem.clone();
updated_orig.content = InlineContent::Text(before_text);
updated_orig.updated_at = chrono::Utc::now();
uow.update_inline_element(&updated_orig)?;
let mut new_elem = elem.clone();
new_elem.id = 0;
new_elem.content = InlineContent::Text(rest_text);
apply_merge_format(&mut new_elem, dto);
uow.create_inline_element(&new_elem, block.id, -1)?;
} else {
let formatted_text: String = chars[..local_end].iter().collect();
let rest_text: String = chars[local_end..].iter().collect();
let mut updated_orig = elem.clone();
updated_orig.content = InlineContent::Text(formatted_text);
apply_merge_format(&mut updated_orig, dto);
uow.update_inline_element(&updated_orig)?;
let mut new_elem = elem.clone();
new_elem.id = 0;
new_elem.content = InlineContent::Text(rest_text);
new_elem.updated_at = chrono::Utc::now();
uow.create_inline_element(&new_elem, block.id, -1)?;
}
}
elem_doc_pos += elem_len;
}
}
Ok(snapshot)
}
pub struct MergeTextFormatUseCase {
uow_factory: Box<dyn MergeTextFormatUnitOfWorkFactoryTrait>,
undo_snapshot: Option<EntityTreeSnapshot>,
last_dto: Option<MergeTextFormatDto>,
}
impl MergeTextFormatUseCase {
pub fn new(uow_factory: Box<dyn MergeTextFormatUnitOfWorkFactoryTrait>) -> Self {
MergeTextFormatUseCase {
uow_factory,
undo_snapshot: None,
last_dto: None,
}
}
pub fn execute(&mut self, dto: &MergeTextFormatDto) -> Result<()> {
let mut uow = self.uow_factory.create();
uow.begin_transaction()?;
let snapshot = execute_merge_text_format(&mut uow, dto)?;
self.undo_snapshot = Some(snapshot);
self.last_dto = Some(dto.clone());
uow.commit()?;
Ok(())
}
}
impl UndoRedoCommand for MergeTextFormatUseCase {
fn undo(&mut self) -> Result<()> {
let snapshot = self
.undo_snapshot
.as_ref()
.ok_or_else(|| anyhow!("No snapshot available for undo"))?
.clone();
let mut uow = self.uow_factory.create();
uow.begin_transaction()?;
uow.restore_document(&snapshot)?;
uow.commit()?;
Ok(())
}
fn redo(&mut self) -> Result<()> {
let dto = self
.last_dto
.as_ref()
.ok_or_else(|| anyhow!("No DTO available for redo"))?
.clone();
let mut uow = self.uow_factory.create();
uow.begin_transaction()?;
let snapshot = execute_merge_text_format(&mut uow, &dto)?;
self.undo_snapshot = Some(snapshot);
uow.commit()?;
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
}