use dioxus_core::{WriteMutations, AttributeValue, Template};
impl WriteMutations for CustomDom {
fn append_children(&mut self, id: ElementId, m: usize) {
let children: Vec<_> = self.stack.drain(self.stack.len() - m..).collect();
if let Some(parent) = self.nodes.get_mut(&id.0) {
parent.children.extend(children);
}
}
fn assign_node_id(&mut self, path: &'static [u8], id: ElementId) {
// Navigate path to find node in template and assign ID
let template_root = self.stack.last().copied().unwrap();
let target = self.navigate_path(template_root, path);
// Clone the node at target and give it the new ID
if let Some(node) = self.nodes.get(&target.0).cloned() {
self.nodes.insert(id.0, node);
}
}
fn create_placeholder(&mut self, id: ElementId) {
let node = CustomNode {
tag: "placeholder".to_string(),
text: None,
attributes: HashMap::new(),
children: Vec::new(),
};
self.nodes.insert(id.0, node);
self.stack.push(id);
}
fn create_text_node(&mut self, value: &str, id: ElementId) {
let node = CustomNode {
tag: "text".to_string(),
text: Some(value.to_string()),
attributes: HashMap::new(),
children: Vec::new(),
};
self.nodes.insert(id.0, node);
self.stack.push(id);
}
fn load_template(&mut self, template: Template, index: usize, id: ElementId) {
// Templates are pre-compiled DOM structures
// Clone the template and assign it the given ID
let template_node = self.clone_template(&template, index);
self.nodes.insert(id.0, template_node);
self.stack.push(id);
}
fn replace_node_with(&mut self, id: ElementId, m: usize) {
let new_nodes: Vec<_> = self.stack.drain(self.stack.len() - m..).collect();
// Find parent and replace old node with new nodes
if let Some((parent_id, idx)) = self.find_parent(id) {
if let Some(parent) = self.nodes.get_mut(&parent_id.0) {
parent.children.splice(idx..idx + 1, new_nodes);
}
}
self.nodes.remove(&id.0);
}
fn set_attribute(
&mut self,
name: &'static str,
_ns: Option<&'static str>,
value: &AttributeValue,
id: ElementId,
) {
if let Some(node) = self.nodes.get_mut(&id.0) {
match value {
AttributeValue::Text(s) => {
node.attributes.insert(name.to_string(), s.clone());
}
AttributeValue::Bool(b) => {
node.attributes.insert(name.to_string(), b.to_string());
}
AttributeValue::Int(i) => {
node.attributes.insert(name.to_string(), i.to_string());
}
AttributeValue::Float(f) => {
node.attributes.insert(name.to_string(), f.to_string());
}
AttributeValue::None => {
node.attributes.remove(name);
}
_ => {}
}
}
}
fn set_node_text(&mut self, value: &str, id: ElementId) {
if let Some(node) = self.nodes.get_mut(&id.0) {
node.text = Some(value.to_string());
}
}
fn create_event_listener(&mut self, name: &'static str, id: ElementId) {
// Register event listener for this node
// Implementation depends on your event system
}
fn remove_event_listener(&mut self, name: &'static str, id: ElementId) {
// Unregister event listener
}
fn remove_node(&mut self, id: ElementId) {
self.nodes.remove(&id.0);
}
fn push_root(&mut self, id: ElementId) {
self.stack.push(id);
}
}