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
use sauron_vdom::Callback;
use sauron_vdom::{self, diff};
use std::ops::Deref;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::{self, Element, EventTarget, Node, Text};

use apply_patches::patch;
use std::collections::HashMap;
use std::sync::Mutex;
use web_sys::{Event, KeyboardEvent, MouseEvent};
use web_sys::{HtmlInputElement, HtmlTextAreaElement};

mod apply_patches;

// Used to uniquely identify elements that contain closures so that the DomUpdater can
// look them up by their unique id.
// When the DomUpdater sees that the element no longer exists it will drop all of it's
// Rc'd Closures for those events.
use lazy_static::lazy_static;
lazy_static! {
    static ref ELEM_UNIQUE_ID: Mutex<u32> = Mutex::new(0);
}

/// A node along with all of the closures that were created for that
/// node's events and all of it's child node's events.
pub struct CreatedNode<T> {
    /// A `Node` or `Element` that was created from a `Node`
    pub node: T,
    closures: ActiveClosure,
}

/// Used for keeping a real DOM node up to date based on the current Node
/// and a new incoming Node that represents our latest DOM state.
pub struct DomUpdater {
    current_vdom: crate::Node,
    root_node: Node,

    /// The closures that are currently attached to elements in the page.
    ///
    /// We keep these around so that they don't get dropped (and thus stop working);
    ///
    /// FIXME: Drop them when the element is no longer in the page. Need to figure out
    /// a good strategy for when to do this.
    pub active_closures: ActiveClosure,
}

pub type ActiveClosure = HashMap<u32, Vec<Closure<FnMut(Event)>>>;

impl<T> CreatedNode<T> {
    pub fn without_closures<N: Into<T>>(node: N) -> Self {
        CreatedNode {
            node: node.into(),
            closures: HashMap::with_capacity(0),
        }
    }

    pub fn create_text_node(text: &sauron_vdom::Text) -> Text {
        let document = web_sys::window().unwrap().document().unwrap();
        document.create_text_node(&text.text)
    }

    /// Create and return a `CreatedNode` instance (containing a DOM `Node`
    /// together with potentially related closures) for this virtual node.
    pub fn create_dom_node(vnode: &crate::Node) -> CreatedNode<Node> {
        match vnode {
            crate::Node::Text(text_node) => {
                CreatedNode::without_closures(Self::create_text_node(text_node))
            }
            crate::Node::Element(element_node) => {
                let created_element: CreatedNode<Node> =
                    Self::create_element_node(element_node).into();
                created_element
            }
        }
    }

    /// Build a DOM element by recursively creating DOM nodes for this element and it's
    /// children, it's children's children, etc.
    pub fn create_element_node(velem: &crate::Element) -> CreatedNode<Element> {
        let document = web_sys::window().unwrap().document().unwrap();

        let element = if let Some(ref namespace) = velem.namespace {
            document
                .create_element_ns(Some(namespace), &velem.tag)
                .unwrap()
        } else {
            document.create_element(&velem.tag).unwrap()
        };

        let mut closures = HashMap::new();

        velem.attrs.iter().for_each(|(name, value)| {
            element
                .set_attribute(name, &value.to_string())
                .expect("Set element attribute in create element");
        });

        if !velem.events.is_empty() {
            let unique_id = create_unique_identifier();

            element
                .set_attribute("data-sauron_vdom-id", &unique_id.to_string())
                .expect("Could not set attribute on element");

            closures.insert(unique_id, vec![]);

            velem.events.iter().for_each(
                |(event_str, callback): (&String, &Callback<sauron_vdom::Event>)| {
                    let current_elem: &EventTarget = element.dyn_ref().unwrap();

                    let callback_clone = callback.clone();
                    let event_name_str = event_str.to_string();

                    let closure_wrap: Closure<FnMut(Event)> =
                        Closure::wrap(Box::new(move |event: Event| {
                            let mouse_event: Option<&MouseEvent> = event.dyn_ref();
                            let key_event: Option<&KeyboardEvent> = event.dyn_ref();
                            let target: Option<EventTarget> = event.target();

                            if let Some(mouse_event) = mouse_event {
                                if event_name_str == "click" {
                                    callback_clone.emit(sauron_vdom::Event::MouseEvent(
                                        sauron_vdom::MouseEvent::Press(
                                            sauron_vdom::MouseButton::Left,
                                            mouse_event.x() as u16,
                                            mouse_event.y() as u16,
                                        ),
                                    ));
                                }
                            } else if let Some(key_event) = key_event {
                                callback_clone.emit(sauron_vdom::Event::KeyEvent(
                                    sauron_vdom::KeyEvent {
                                        key: key_event.key(),
                                        ctrl: key_event.ctrl_key(),
                                        alt: key_event.alt_key(),
                                        shift: key_event.shift_key(),
                                        meta: key_event.meta_key(),
                                    },
                                ));
                            } else if let Some(target) = target {
                                let input: Option<&HtmlInputElement> = target.dyn_ref();
                                let textarea: Option<&HtmlTextAreaElement> = target.dyn_ref();
                                if let Some(input) = input {
                                    callback_clone.emit(sauron_vdom::Event::InputEvent(
                                        sauron_vdom::InputEvent {
                                            value: input.value(),
                                        },
                                    ));
                                } else if let Some(textarea) = textarea {
                                    callback_clone.emit(sauron_vdom::Event::InputEvent(
                                        sauron_vdom::InputEvent {
                                            value: textarea.value(),
                                        },
                                    ));
                                } else {
                                    callback_clone.emit(sauron_vdom::Event::Generic(event.type_()));
                                }
                            }
                        }));

                    current_elem
                        .add_event_listener_with_callback(
                            event_str,
                            closure_wrap.as_ref().unchecked_ref(),
                        )
                        .unwrap();

                    closures.get_mut(&unique_id).unwrap().push(closure_wrap);
                },
            );
        }

        let mut previous_node_was_text = false;

        velem.children.iter().for_each(|child| {
            match child {
                crate::Node::Text(text_node) => {
                    let current_node = element.as_ref() as &web_sys::Node;

                    // We ensure that the text siblings are patched by preventing the browser from merging
                    // neighboring text nodes. Originally inspired by some of React's work from 2016.
                    //  -> https://reactjs.org/blog/2016/04/07/react-v15.html#major-changes
                    //  -> https://github.com/facebook/react/pull/5753
                    //
                    // `ptns` = Percy text node separator
                    if previous_node_was_text {
                        let separator = document.create_comment("ptns");
                        current_node
                            .append_child(separator.as_ref() as &web_sys::Node)
                            .unwrap();
                    }

                    current_node
                        .append_child(&Self::create_text_node(&text_node))
                        .unwrap();

                    previous_node_was_text = true;
                }
                crate::Node::Element(element_node) => {
                    previous_node_was_text = false;

                    let child = Self::create_element_node(element_node);
                    let child_elem: Element = child.node;
                    closures.extend(child.closures);

                    element.append_child(&child_elem).unwrap();
                }
            }
        });

        CreatedNode {
            node: element,
            closures,
        }
    }
}

impl DomUpdater {
    /// Create a new `DomUpdater`.
    ///
    /// A root `Node` will be created but not added to your DOM.
    pub fn new(current_vdom: crate::Node) -> DomUpdater {
        let created_node = CreatedNode::<Node>::create_dom_node(&current_vdom);
        DomUpdater {
            current_vdom,
            root_node: created_node.node,
            active_closures: created_node.closures,
        }
    }

    /// Create a new `DomUpdater`.
    ///
    /// A root `Node` will be created and appended (as a child) to your passed
    /// in mount element.
    pub fn new_append_to_mount(current_vdom: crate::Node, mount: &Element) -> DomUpdater {
        let created_node: CreatedNode<Node> = CreatedNode::<Node>::create_dom_node(&current_vdom);
        mount
            .append_child(&created_node.node)
            .expect("Could not append child to mount");
        DomUpdater {
            current_vdom,
            root_node: created_node.node,
            active_closures: created_node.closures,
        }
    }

    /// Create a new `DomUpdater`.
    ///
    /// A root `Node` will be created and it will replace your passed in mount
    /// element.
    pub fn new_replace_mount(current_vdom: crate::Node, mount: Element) -> DomUpdater {
        let created_node = CreatedNode::<Node>::create_dom_node(&current_vdom);
        mount
            .replace_with_with_node_1(&created_node.node)
            .expect("Could not replace mount element");
        DomUpdater {
            current_vdom,
            root_node: created_node.node,
            active_closures: created_node.closures,
        }
    }

    /// Diff the current virtual dom with the new virtual dom that is being passed in.
    ///
    /// Then use that diff to patch the real DOM in the user's browser so that they are
    /// seeing the latest state of the application.
    pub fn update(&mut self, new_vdom: crate::Node) {
        let patches = diff(&self.current_vdom, &new_vdom);
        let active_closures = patch(self.root_node.clone(), &patches).unwrap();
        self.active_closures.extend(active_closures);
        self.current_vdom = new_vdom;
    }

    /// Return the root node of your application, the highest ancestor of all other nodes in
    /// your real DOM tree.
    pub fn root_node(&self) -> Node {
        // Note that we're cloning the `web_sys::Node`, not the DOM element.
        // So we're effectively cloning a pointer here, which is fast.
        self.root_node.clone()
    }
}

fn create_unique_identifier() -> u32 {
    let mut elem_unique_id = ELEM_UNIQUE_ID.lock().unwrap();
    *elem_unique_id += 1;
    *elem_unique_id
}

impl From<CreatedNode<Element>> for CreatedNode<Node> {
    fn from(other: CreatedNode<Element>) -> CreatedNode<Node> {
        CreatedNode {
            node: other.node.into(),
            closures: other.closures,
        }
    }
}

impl<T> Deref for CreatedNode<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.node
    }
}