fix custom attribute value support

This commit is contained in:
Evan Almloff committed 2022-12-09 16:18:37 -06:00
1 parent 937eb1f0f0
commit 8a1c96a68c
28 files changed
+369 -276

No files matched your search

+35 -6
View File
@@ -1,6 +1,7 @@
use crate::{state::State, tree::NodeId};
use dioxus_core::ElementId;
use dioxus_core::{AnyValueBox, AttributeValue, ElementId};
use rustc_hash::{FxHashMap, FxHashSet};
use std::fmt::Debug;
/// The node is stored client side and stores only basic data about the node.
#[derive(Debug, Clone)]
@@ -72,15 +73,43 @@ pub struct OwnedAttributeView<'a> {
pub value: &'a OwnedAttributeValue,
}
#[derive(Clone, Debug)]
#[derive(Clone)]
pub enum OwnedAttributeValue {
Text(String),
Float(f32),
Int(i32),
Float(f64),
Int(i64),
Bool(bool),
Any(AnyValueBox),
None,
}
impl Debug for OwnedAttributeValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
Self::Any(_) => f.debug_tuple("Any").finish(),
Self::None => write!(f, "None"),
}
}
}
impl From<AttributeValue<'_>> for OwnedAttributeValue {
fn from(value: AttributeValue<'_>) -> Self {
match value {
AttributeValue::Text(text) => Self::Text(text.to_string()),
AttributeValue::Float(float) => Self::Float(float),
AttributeValue::Int(int) => Self::Int(int),
AttributeValue::Bool(bool) => Self::Bool(bool),
AttributeValue::Any(any) => Self::Any(any),
AttributeValue::None => Self::None,
_ => Self::None,
}
}
}
impl OwnedAttributeValue {
pub fn as_text(&self) -> Option<&str> {
match self {
@@ -89,14 +118,14 @@ impl OwnedAttributeValue {
}
}
pub fn as_float(&self) -> Option<f32> {
pub fn as_float(&self) -> Option<f64> {
match self {
OwnedAttributeValue::Float(float) => Some(*float),
_ => None,
}
}
pub fn as_int(&self) -> Option<i32> {
pub fn as_int(&self) -> Option<i64> {
match self {
OwnedAttributeValue::Int(int) => Some(*int),
_ => None,
+11 -43
View File
@@ -102,11 +102,7 @@ impl<S: State> RealDom<S> {
self.tree.add_child(node_id, child_id);
}
fn create_template_node(
&mut self,
node: &TemplateNode,
mutations_vec: &mut FxHashMap<RealNodeId, NodeMask>,
) -> RealNodeId {
fn create_template_node(&mut self, node: &TemplateNode) -> RealNodeId {
match node {
TemplateNode::Element {
tag,
@@ -139,27 +135,18 @@ impl<S: State> RealDom<S> {
});
let node_id = self.create_node(node);
for child in *children {
let child_id = self.create_template_node(child, mutations_vec);
let child_id = self.create_template_node(child);
self.add_child(node_id, child_id);
}
node_id
}
TemplateNode::Text { text } => {
let node_id = self.create_node(Node::new(NodeType::Text {
text: text.to_string(),
}));
node_id
}
TemplateNode::Dynamic { .. } => {
let node_id = self.create_node(Node::new(NodeType::Placeholder));
node_id
}
TemplateNode::DynamicText { .. } => {
let node_id = self.create_node(Node::new(NodeType::Text {
text: String::new(),
}));
node_id
}
TemplateNode::Text { text } => self.create_node(Node::new(NodeType::Text {
text: text.to_string(),
})),
TemplateNode::Dynamic { .. } => self.create_node(Node::new(NodeType::Placeholder)),
TemplateNode::DynamicText { .. } => self.create_node(Node::new(NodeType::Text {
text: String::new(),
})),
}
}
@@ -172,7 +159,7 @@ impl<S: State> RealDom<S> {
for template in mutations.templates {
let mut template_root_ids = Vec::new();
for root in template.roots {
let id = self.create_template_node(root, &mut nodes_updated);
let id = self.create_template_node(root);
template_root_ids.push(id);
}
self.templates
@@ -283,26 +270,7 @@ impl<S: State> RealDom<S> {
namespace: ns.map(|s| s.to_string()),
volatile: false,
},
crate::node::OwnedAttributeValue::Text(value.to_string()),
);
mark_dirty(
node_id,
NodeMask::new_with_attrs(AttributeMask::single(name)),
&mut nodes_updated,
);
}
}
SetBoolAttribute { name, value, id } => {
let node_id = self.element_to_node_id(id);
let node = self.tree.get_mut(node_id).unwrap();
if let NodeType::Element { attributes, .. } = &mut node.node_data.node_type {
attributes.insert(
OwnedAttributeDiscription {
name: name.to_string(),
namespace: None,
volatile: false,
},
crate::node::OwnedAttributeValue::Bool(value),
OwnedAttributeValue::from(value),
);
mark_dirty(
node_id,