wip: more polish on display
This commit is contained in:
1 parent
238677208e
commit
12afd2b0f2
3 files changed
+127
-130
No files matched your search
@@ -754,13 +754,12 @@ impl Debug for NodeFactory<'_> {
|
|||||||
impl Debug for VNode<'_> {
|
impl Debug for VNode<'_> {
|
||||||
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||||
match &self {
|
match &self {
|
||||||
VNode::Element(el) => {
|
VNode::Element(el) => s
|
||||||
//
|
.debug_struct("VElement")
|
||||||
s.debug_struct("VElement")
|
.field("name", &el.tag_name)
|
||||||
.field("name", &el.tag_name)
|
.field("key", &el.key)
|
||||||
.field("key", &el.key)
|
.finish(),
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
VNode::Text(t) => write!(s, "VText {{ text: {} }}", t.text),
|
VNode::Text(t) => write!(s, "VText {{ text: {} }}", t.text),
|
||||||
VNode::Anchor(_) => write!(s, "VAnchor"),
|
VNode::Anchor(_) => write!(s, "VAnchor"),
|
||||||
|
|
||||||
|
|||||||
@@ -1,131 +1,107 @@
|
|||||||
use crate::innerlude::*;
|
use crate::innerlude::*;
|
||||||
|
|
||||||
|
pub(crate) struct ScopeRenderer<'a> {
|
||||||
|
pub skip_components: bool,
|
||||||
|
pub show_fragments: bool,
|
||||||
|
pub _scope: &'a Scope,
|
||||||
|
pub _pre_render: bool,
|
||||||
|
pub _newline: bool,
|
||||||
|
pub _indent: bool,
|
||||||
|
pub _max_depth: usize,
|
||||||
|
}
|
||||||
|
|
||||||
// this is more or less a debug tool, but it'll render the entire tree to the terminal
|
// this is more or less a debug tool, but it'll render the entire tree to the terminal
|
||||||
impl std::fmt::Display for VirtualDom {
|
impl<'a> ScopeRenderer<'a> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
pub fn render(
|
||||||
struct ScopeRenderer<'a> {
|
&self,
|
||||||
scope: &'a Scope,
|
vdom: &VirtualDom,
|
||||||
cfg: Cfg,
|
node: &VNode,
|
||||||
}
|
f: &mut std::fmt::Formatter,
|
||||||
|
il: u16,
|
||||||
struct Cfg {
|
) -> std::fmt::Result {
|
||||||
pre_render: bool,
|
const INDENT: &str = " ";
|
||||||
newline: bool,
|
let write_indent = |_f: &mut std::fmt::Formatter, le| {
|
||||||
indent: bool,
|
for _ in 0..le {
|
||||||
max_depth: usize,
|
write!(_f, "{}", INDENT).unwrap();
|
||||||
skip_components: bool,
|
|
||||||
show_fragments: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> ScopeRenderer<'a> {
|
|
||||||
fn html_render(
|
|
||||||
&self,
|
|
||||||
vdom: &VirtualDom,
|
|
||||||
node: &VNode,
|
|
||||||
f: &mut std::fmt::Formatter,
|
|
||||||
il: u16,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
const INDENT: &str = " ";
|
|
||||||
let write_indent = |_f: &mut std::fmt::Formatter, le| {
|
|
||||||
for _ in 0..le {
|
|
||||||
write!(_f, "{}", INDENT).unwrap();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match &node {
|
|
||||||
VNode::Text(text) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
write!(f, "\"{}\"\n", text.text)?
|
|
||||||
}
|
|
||||||
VNode::Anchor(anchor) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
write!(f, "Anchor {{}}\n")?;
|
|
||||||
}
|
|
||||||
VNode::Element(el) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
write!(f, "{} {{\n", el.tag_name)?;
|
|
||||||
// write!(f, "element: {}", el.tag_name)?;
|
|
||||||
let mut attr_iter = el.attributes.iter().peekable();
|
|
||||||
|
|
||||||
while let Some(attr) = attr_iter.next() {
|
|
||||||
match attr.namespace {
|
|
||||||
None => {
|
|
||||||
//
|
|
||||||
write_indent(f, il + 1);
|
|
||||||
write!(f, "{}: \"{}\"\n", attr.name, attr.value)?
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(ns) => {
|
|
||||||
// write the opening tag
|
|
||||||
write_indent(f, il + 1);
|
|
||||||
write!(f, " {}:\"", ns)?;
|
|
||||||
let mut cur_ns_el = attr;
|
|
||||||
'ns_parse: loop {
|
|
||||||
write!(f, "{}:{};", cur_ns_el.name, cur_ns_el.value)?;
|
|
||||||
match attr_iter.peek() {
|
|
||||||
Some(next_attr) if next_attr.namespace == Some(ns) => {
|
|
||||||
cur_ns_el = attr_iter.next().unwrap();
|
|
||||||
}
|
|
||||||
_ => break 'ns_parse,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// write the closing tag
|
|
||||||
write!(f, "\"")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for child in el.children {
|
|
||||||
self.html_render(vdom, child, f, il + 1)?;
|
|
||||||
}
|
|
||||||
write_indent(f, il);
|
|
||||||
|
|
||||||
write!(f, "}}\n")?;
|
|
||||||
}
|
|
||||||
VNode::Fragment(frag) => {
|
|
||||||
if self.cfg.show_fragments {
|
|
||||||
write_indent(f, il);
|
|
||||||
write!(f, "Fragment {{\n")?;
|
|
||||||
for child in frag.children {
|
|
||||||
self.html_render(vdom, child, f, il + 1)?;
|
|
||||||
}
|
|
||||||
write_indent(f, il);
|
|
||||||
write!(f, "}}\n")?;
|
|
||||||
} else {
|
|
||||||
for child in frag.children {
|
|
||||||
self.html_render(vdom, child, f, il)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VNode::Component(vcomp) => {
|
|
||||||
let idx = vcomp.associated_scope.get().unwrap();
|
|
||||||
if !self.cfg.skip_components {
|
|
||||||
let new_node = vdom.get_scope(idx).unwrap().root_node();
|
|
||||||
self.html_render(vdom, new_node, f, il)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VNode::Suspended { .. } => {
|
|
||||||
// we can't do anything with suspended nodes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let base = self.base_scope();
|
|
||||||
let root = base.root_node();
|
|
||||||
let renderer = ScopeRenderer {
|
|
||||||
scope: base,
|
|
||||||
cfg: Cfg {
|
|
||||||
show_fragments: false,
|
|
||||||
pre_render: false,
|
|
||||||
newline: true,
|
|
||||||
indent: true,
|
|
||||||
max_depth: usize::MAX,
|
|
||||||
skip_components: false,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
renderer.html_render(self, root, f, 0)
|
match &node {
|
||||||
|
VNode::Text(text) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
write!(f, "\"{}\"\n", text.text)?
|
||||||
|
}
|
||||||
|
VNode::Anchor(anchor) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
write!(f, "Anchor {{}}\n")?;
|
||||||
|
}
|
||||||
|
VNode::Element(el) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
write!(f, "{} {{\n", el.tag_name)?;
|
||||||
|
// write!(f, "element: {}", el.tag_name)?;
|
||||||
|
let mut attr_iter = el.attributes.iter().peekable();
|
||||||
|
|
||||||
|
while let Some(attr) = attr_iter.next() {
|
||||||
|
match attr.namespace {
|
||||||
|
None => {
|
||||||
|
//
|
||||||
|
write_indent(f, il + 1);
|
||||||
|
write!(f, "{}: \"{}\"\n", attr.name, attr.value)?
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(ns) => {
|
||||||
|
// write the opening tag
|
||||||
|
write_indent(f, il + 1);
|
||||||
|
write!(f, " {}:\"", ns)?;
|
||||||
|
let mut cur_ns_el = attr;
|
||||||
|
'ns_parse: loop {
|
||||||
|
write!(f, "{}:{};", cur_ns_el.name, cur_ns_el.value)?;
|
||||||
|
match attr_iter.peek() {
|
||||||
|
Some(next_attr) if next_attr.namespace == Some(ns) => {
|
||||||
|
cur_ns_el = attr_iter.next().unwrap();
|
||||||
|
}
|
||||||
|
_ => break 'ns_parse,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// write the closing tag
|
||||||
|
write!(f, "\"")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for child in el.children {
|
||||||
|
self.render(vdom, child, f, il + 1)?;
|
||||||
|
}
|
||||||
|
write_indent(f, il);
|
||||||
|
|
||||||
|
write!(f, "}}\n")?;
|
||||||
|
}
|
||||||
|
VNode::Fragment(frag) => {
|
||||||
|
if self.show_fragments {
|
||||||
|
write_indent(f, il);
|
||||||
|
write!(f, "Fragment {{\n")?;
|
||||||
|
for child in frag.children {
|
||||||
|
self.render(vdom, child, f, il + 1)?;
|
||||||
|
}
|
||||||
|
write_indent(f, il);
|
||||||
|
write!(f, "}}\n")?;
|
||||||
|
} else {
|
||||||
|
for child in frag.children {
|
||||||
|
self.render(vdom, child, f, il)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VNode::Component(vcomp) => {
|
||||||
|
let idx = vcomp.associated_scope.get().unwrap();
|
||||||
|
if !self.skip_components {
|
||||||
|
let new_node = vdom.get_scope(idx).unwrap().root_node();
|
||||||
|
self.render(vdom, new_node, f, il)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VNode::Suspended { .. } => {
|
||||||
|
// we can't do anything with suspended nodes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -356,3 +356,25 @@ impl VirtualDom {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for VirtualDom {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let base = self.base_scope();
|
||||||
|
let root = base.root_node();
|
||||||
|
|
||||||
|
use crate::vdomdisplay::ScopeRenderer;
|
||||||
|
|
||||||
|
let renderer = ScopeRenderer {
|
||||||
|
show_fragments: false,
|
||||||
|
skip_components: false,
|
||||||
|
|
||||||
|
_scope: base,
|
||||||
|
_pre_render: false,
|
||||||
|
_newline: true,
|
||||||
|
_indent: true,
|
||||||
|
_max_depth: usize::MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
renderer.render(self, root, f, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user