1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Integration between Dioxus and the RealDom

use std::str::FromStr;

use dioxus_core::{
    AttributeValue,
    ElementId,
    TemplateNode,
    WriteMutations,
};
use rustc_hash::{
    FxHashMap,
    FxHashSet,
};
use shipyard::Component;

use crate::{
    node::{
        ElementNode,
        FromAnyValue,
        NodeType,
        OwnedAttributeValue,
    },
    prelude::*,
    real_dom::NodeTypeMut,
    tags::TagName,
    tree::TreeMut,
    NodeId,
};

#[derive(Component)]
struct ElementIdComponent(ElementId);

/// The state of the Dioxus integration with the RealDom
pub struct DioxusState {
    templates: FxHashMap<String, Vec<NodeId>>,
    stack: Vec<NodeId>,
    node_id_mapping: Vec<Option<NodeId>>,
}

impl DioxusState {
    /// Initialize the DioxusState in the RealDom
    pub fn create<V: FromAnyValue + Send + Sync>(rdom: &mut RealDom<V>) -> Self {
        let root_id = rdom.root_id();
        let mut root = rdom.get_mut(root_id).unwrap();
        root.insert(ElementIdComponent(ElementId(0)));
        Self {
            templates: FxHashMap::default(),
            stack: vec![root_id],
            node_id_mapping: vec![Some(root_id)],
        }
    }

    /// Convert an ElementId to a NodeId
    pub fn element_to_node_id(&self, element_id: ElementId) -> NodeId {
        self.try_element_to_node_id(element_id).unwrap()
    }

    /// Attempt to convert an ElementId to a NodeId. This will return None if the ElementId is not in the RealDom.
    pub fn try_element_to_node_id(&self, element_id: ElementId) -> Option<NodeId> {
        self.node_id_mapping.get(element_id.0).copied().flatten()
    }

    /// Create a mutation writer for the RealDom
    pub fn create_mutation_writer<'a, V: FromAnyValue + Send + Sync>(
        &'a mut self,
        rdom: &'a mut RealDom<V>,
    ) -> DioxusNativeCoreMutationWriter<'a, V> {
        DioxusNativeCoreMutationWriter { rdom, state: self }
    }

    fn set_element_id<V: FromAnyValue + Send + Sync>(
        &mut self,
        mut node: NodeMut<V>,
        element_id: ElementId,
    ) {
        let node_id = node.id();
        node.insert(ElementIdComponent(element_id));
        if self.node_id_mapping.len() <= element_id.0 {
            self.node_id_mapping.resize(element_id.0 + 1, None);
        } else if let Some(mut node) =
            self.node_id_mapping[element_id.0].and_then(|id| node.real_dom_mut().get_mut(id))
        {
            node.remove();
        }

        self.node_id_mapping[element_id.0] = Some(node_id);
    }

    fn load_child<V: FromAnyValue + Send + Sync>(&self, rdom: &RealDom<V>, path: &[u8]) -> NodeId {
        let mut current = rdom.get(*self.stack.last().unwrap()).unwrap();
        for i in path {
            let new_id = current.child_ids()[*i as usize];
            current = rdom.get(new_id).unwrap();
        }
        current.id()
    }
}

/// A writer for mutations that can be used with the RealDom.
pub struct DioxusNativeCoreMutationWriter<'a, V: FromAnyValue + Send + Sync = ()> {
    /// The realdom associated with this writer
    pub rdom: &'a mut RealDom<V>,

    /// The state associated with this writer
    pub state: &'a mut DioxusState,
}

impl<V: FromAnyValue + Send + Sync> WriteMutations for DioxusNativeCoreMutationWriter<'_, V> {
    fn register_template(&mut self, template: dioxus_core::prelude::Template) {
        let mut template_root_ids = Vec::new();
        for root in template.roots {
            let id = create_template_node(self.rdom, root);
            template_root_ids.push(id);
        }
        self.state
            .templates
            .insert(template.name.to_string(), template_root_ids);
    }

    fn append_children(&mut self, id: ElementId, m: usize) {
        let children = self.state.stack.split_off(self.state.stack.len() - m);
        let parent_id = self.state.element_to_node_id(id);
        let mut parent = self.rdom.get_mut(parent_id).unwrap();
        for child in children {
            parent.add_child(child);
        }
    }

    fn assign_node_id(&mut self, path: &'static [u8], id: ElementId) {
        let node_id = self.state.load_child(self.rdom, path);
        self.state
            .set_element_id(self.rdom.get_mut(node_id).unwrap(), id);
    }

    fn create_placeholder(&mut self, id: ElementId) {
        let node = NodeType::Placeholder;
        let node = self.rdom.create_node(node);
        let node_id = node.id();
        self.state.set_element_id(node, id);
        self.state.stack.push(node_id);
    }

    fn create_text_node(&mut self, value: &str, id: ElementId) {
        let node_data = NodeType::Text(value.to_string());
        let node = self.rdom.create_node(node_data);
        let node_id = node.id();
        self.state.set_element_id(node, id);
        self.state.stack.push(node_id);
    }

    fn hydrate_text_node(&mut self, path: &'static [u8], value: &str, id: ElementId) {
        let node_id = self.state.load_child(self.rdom, path);
        let node = self.rdom.get_mut(node_id).unwrap();
        self.state.set_element_id(node, id);
        let mut node = self.rdom.get_mut(node_id).unwrap();
        let node_type_mut = node.node_type_mut();
        if let NodeTypeMut::Text(mut text) = node_type_mut {
            *text.text_mut() = value.to_string();
        } else {
            drop(node_type_mut);
            node.set_type(NodeType::Text(value.to_string()));
        }
    }

    fn load_template(&mut self, name: &'static str, index: usize, id: ElementId) {
        let template_id = self.state.templates[name][index];
        let clone_id = self.rdom.get_mut(template_id).unwrap().clone_node();
        let clone = self.rdom.get_mut(clone_id).unwrap();
        self.state.set_element_id(clone, id);
        self.state.stack.push(clone_id);
    }

    fn replace_node_with(&mut self, id: ElementId, m: usize) {
        let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
        let old_node_id = self.state.element_to_node_id(id);
        for new in new_nodes {
            let mut node = self.rdom.get_mut(new).unwrap();
            node.insert_before(old_node_id);
        }
        self.rdom.get_mut(old_node_id).unwrap().remove();
    }

    fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize) {
        let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
        let old_node_id = self.state.load_child(self.rdom, path);
        for new in new_nodes {
            let mut node = self.rdom.get_mut(new).unwrap();
            node.insert_before(old_node_id);
        }
        self.rdom.get_mut(old_node_id).unwrap().remove();
    }

    fn insert_nodes_after(&mut self, id: ElementId, m: usize) {
        let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
        let old_node_id = self.state.element_to_node_id(id);
        for new in new_nodes.into_iter().rev() {
            let mut node = self.rdom.get_mut(new).unwrap();
            node.insert_after(old_node_id);
        }
    }

    fn insert_nodes_before(&mut self, id: ElementId, m: usize) {
        let new_nodes = self.state.stack.split_off(self.state.stack.len() - m);
        let old_node_id = self.state.element_to_node_id(id);
        for new in new_nodes {
            self.rdom.tree_mut().insert_before(old_node_id, new);
        }
    }

    fn set_attribute(
        &mut self,
        name: &'static str,
        _ns: Option<&'static str>,
        value: &AttributeValue,
        id: ElementId,
    ) {
        let node_id = self.state.element_to_node_id(id);
        let mut node = self.rdom.get_mut(node_id).unwrap();
        let mut node_type_mut = node.node_type_mut();
        if let NodeTypeMut::Element(element) = &mut node_type_mut {
            let attribute = AttributeName::from_str(name).expect("Unexpected");
            if let AttributeValue::None = &value {
                element.remove_attribute(&attribute);
            } else {
                element.set_attribute(attribute, OwnedAttributeValue::from(value));
            }
        }
    }

    fn set_node_text(&mut self, value: &str, id: ElementId) {
        let node_id = self.state.element_to_node_id(id);
        let mut node = self.rdom.get_mut(node_id).unwrap();
        let node_type_mut = node.node_type_mut();
        if let NodeTypeMut::Text(mut text) = node_type_mut {
            *text.text_mut() = value.to_string();
        }
    }

    fn create_event_listener(&mut self, name: &'static str, id: ElementId) {
        let node_id = self.state.element_to_node_id(id);
        let mut node = self.rdom.get_mut(node_id).unwrap();
        node.add_event_listener(EventName::from_str(name).expect("Unexpected."));
    }

    fn remove_event_listener(&mut self, name: &'static str, id: ElementId) {
        let node_id = self.state.element_to_node_id(id);
        let mut node = self.rdom.get_mut(node_id).unwrap();
        node.remove_event_listener(&EventName::from_str(name).expect("Unexpected."));
    }

    fn remove_node(&mut self, id: ElementId) {
        let node_id = self.state.element_to_node_id(id);
        self.rdom.get_mut(node_id).unwrap().remove();
    }

    fn push_root(&mut self, id: ElementId) {
        let node_id = self.state.element_to_node_id(id);
        self.state.stack.push(node_id);
    }
}

fn create_template_node<V: FromAnyValue + Send + Sync>(
    rdom: &mut RealDom<V>,
    node: &TemplateNode,
) -> NodeId {
    match node {
        TemplateNode::Element {
            tag,
            attrs,
            children,
            ..
        } => {
            let node = NodeType::Element(ElementNode {
                tag: TagName::from_str(tag).expect("Unexpected."),
                attributes: attrs
                    .iter()
                    .filter_map(|attr| match attr {
                        dioxus_core::TemplateAttribute::Static { name, value, .. } => Some((
                            AttributeName::from_str(name).expect("Unexpected."),
                            OwnedAttributeValue::Text(value.to_string()),
                        )),
                        dioxus_core::TemplateAttribute::Dynamic { .. } => None,
                    })
                    .collect(),
                listeners: FxHashSet::default(),
            });
            let node_id = rdom.create_node(node).id();
            for child in *children {
                let child_id = create_template_node(rdom, child);
                rdom.get_mut(node_id).unwrap().add_child(child_id);
            }
            node_id
        }
        TemplateNode::Text { text } => rdom.create_node(NodeType::Text(text.to_string())).id(),
        TemplateNode::Dynamic { .. } => rdom.create_node(NodeType::Placeholder).id(),
        // TemplateNode::DynamicText { .. } => {
        //     rdom.create_node(NodeType::Text(String::default())).id()
        // }
    }
}

/// A trait that extends the `NodeImmutable` trait with methods that are useful for dioxus.
pub trait NodeImmutableDioxusExt<V: FromAnyValue + Send + Sync>: NodeImmutable<V> {
    /// Returns the id of the element that this node is mounted to.
    /// Not all nodes are mounted to an element, only nodes with dynamic content that have been renderered will have an id.
    fn mounted_id(&self) -> Option<ElementId> {
        let id = self.get::<ElementIdComponent>();
        id.map(|id| id.0)
    }
}

impl<T: NodeImmutable<V>, V: FromAnyValue + Send + Sync> NodeImmutableDioxusExt<V> for T {}