use crate::commands::{EditorCommand, EffectOperation, EventEffectCommand, ToggleEventTypeCommand};
use crate::core::{EditorDocument, Result};
#[cfg(not(feature = "std"))]
use alloc::{string::ToString, vec, vec::Vec};
pub struct EventToggler<'a> {
document: &'a mut EditorDocument,
event_indices: Vec<usize>,
}
impl<'a> EventToggler<'a> {
pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
Self {
document,
event_indices: Vec::new(), }
}
pub fn events(mut self, indices: Vec<usize>) -> Self {
self.event_indices = indices;
self
}
pub fn event(mut self, index: usize) -> Self {
self.event_indices = vec![index];
self
}
pub fn apply(self) -> Result<&'a mut EditorDocument> {
let command = ToggleEventTypeCommand::new(self.event_indices);
command.execute(self.document)?;
Ok(self.document)
}
}
pub struct EventEffector<'a> {
document: &'a mut EditorDocument,
event_indices: Vec<usize>,
}
impl<'a> EventEffector<'a> {
pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
Self {
document,
event_indices: Vec::new(), }
}
pub fn events(mut self, indices: Vec<usize>) -> Self {
self.event_indices = indices;
self
}
pub fn event(mut self, index: usize) -> Self {
self.event_indices = vec![index];
self
}
pub fn set(self, effect: &str) -> Result<&'a mut EditorDocument> {
let command = EventEffectCommand::set_effect(self.event_indices, effect.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn clear(self) -> Result<&'a mut EditorDocument> {
let command = EventEffectCommand::clear_effect(self.event_indices);
command.execute(self.document)?;
Ok(self.document)
}
pub fn append(self, effect: &str) -> Result<&'a mut EditorDocument> {
let command = EventEffectCommand::append_effect(self.event_indices, effect.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn prepend(self, effect: &str) -> Result<&'a mut EditorDocument> {
let command = EventEffectCommand::new(
self.event_indices,
effect.to_string(),
EffectOperation::Prepend,
);
command.execute(self.document)?;
Ok(self.document)
}
}