Skip to content

Commit

Permalink
Merge pull request #28 from azriel91/feature/better-error-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 authored Aug 6, 2024
2 parents 781f67a + 34bf2c6 commit 8ecdac1
Show file tree
Hide file tree
Showing 14 changed files with 665 additions and 219 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
* Take in `tags` which only include tag names.
* Take in `tag_items` for nodes and edges instead of tags per node.
* Support theming nodes based on tag focus.
* Add `ThemeAttr::Animate` to support setting [`animation`].
* Add `ThemeAttr::Visibility` to support setting [`visibility`].
* Fix black outline shown on focused nodes in Chrome / Edge.
* Show feedback to user when stroke / outline / fill class partials are not all specified.

[monaco]: https://github.com/microsoft/monaco-editor
[rust-monaco]: https://github.com/siku2/rust-monaco
[cursor styling]: https://tailwindcss.com/docs/cursor
[`animation`]: https://tailwindcss.com/docs/animation
[`visibility`]: https://tailwindcss.com/docs/visibility


## 0.7.0 (2024-06-30)
Expand Down
30 changes: 30 additions & 0 deletions crate/model/src/common/dot_src_and_styles.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
use serde::{Deserialize, Serialize};

use crate::theme::ThemeWarnings;

/// Graphviz dot source and CSS styles.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DotSrcAndStyles {
/// Graphviz dot source to run in `dot`.
pub dot_src: String,
/// Tailwind CSS styles to put into `<styles>..</styles>`.
pub styles: String,
/// Warnings detected while computing CSS utility classes.
pub theme_warnings: ThemeWarnings,
}

impl DotSrcAndStyles {
/// Returns a new `DotSrcAndStyles` object.
pub fn new(dot_src: String, styles: String, theme_warnings: ThemeWarnings) -> Self {
Self {
dot_src,
styles,
theme_warnings,
}
}

/// Returns the Graphviz dot source to run in `dot`.
pub fn dot_src(&self) -> &str {
&self.dot_src
}

/// Returns the tailwind CSS styles to put into `<styles>..</styles>`.
pub fn styles(&self) -> &str {
&self.styles
}

/// Returns the warnings detected while computing CSS utility classes.
pub fn theme_warnings(&self) -> &ThemeWarnings {
&self.theme_warnings
}
}
78 changes: 60 additions & 18 deletions crate/model/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ use crate::common::{AnyId, EdgeId, NodeId, TagId};
pub use self::{
any_id_or_defaults::AnyIdOrDefaults, color_params::ColorParams,
css_class_merger::CssClassMerger, css_class_partials::CssClassPartials,
css_classes::CssClasses, css_classes_builder::CssClassesBuilder, el_css_classes::ElCssClasses,
css_classes::CssClasses, css_classes_and_warnings::CssClassesAndWarnings,
css_classes_builder::CssClassesBuilder, el_css_classes::ElCssClasses,
highlight_state::HighlightState, stroke_params::StrokeParams, style_for::StyleFor,
theme_attr::ThemeAttr, theme_styles::ThemeStyles, themeable::Themeable,
theme_attr::ThemeAttr, theme_styles::ThemeStyles, theme_warnings::ThemeWarnings,
themeable::Themeable,
};

mod any_id_or_defaults;
mod color_params;
mod css_class_merger;
mod css_class_partials;
mod css_classes;
mod css_classes_and_warnings;
mod css_classes_builder;
mod el_css_classes;
mod highlight_state;
mod stroke_params;
mod style_for;
mod theme_attr;
mod theme_styles;
mod theme_warnings;
mod themeable;

/// Theme to style the generated diagram.
Expand Down Expand Up @@ -227,7 +231,7 @@ impl Theme {
///
/// The [`CssClasses`] produced will contain an entry for each node / edge
/// ID from the themeable type.
pub fn el_css_classes<T>(&self, themeable: &T) -> ElCssClasses
pub fn el_css_classes<T>(&self, themeable: &T) -> (ElCssClasses, ThemeWarnings)
where
T: Themeable,
{
Expand All @@ -240,13 +244,32 @@ impl Theme {
theme
.node_el_css_classes(themeable)
.chain(theme.edge_el_css_classes(themeable))
.collect()
.fold(
(
ElCssClasses::with_capacity(
themeable.node_ids().count() + themeable.edge_ids().count(),
),
ThemeWarnings::new(),
),
|(mut el_css_classes, mut theme_warnings_acc),
(any_id, css_classes_and_warnings)| {
let CssClassesAndWarnings {
css_classes,
theme_warnings,
} = css_classes_and_warnings;

el_css_classes.insert(any_id, css_classes);
theme_warnings_acc.extend(theme_warnings.into_inner());

(el_css_classes, theme_warnings_acc)
},
)
}

fn node_el_css_classes<'f, T>(
&'f self,
themeable: &'f T,
) -> impl Iterator<Item = (AnyId, CssClasses)> + 'f
) -> impl Iterator<Item = (AnyId, CssClassesAndWarnings)> + 'f
where
T: Themeable,
{
Expand All @@ -255,13 +278,13 @@ impl Theme {
let node_class_partials_specified = self.node_class_partials_specified(node_id);

let any_id = Some(AnyId::from(node_id.clone()));
let node_classes = CssClassMerger::node_classes(
let node_classes_and_warnings = CssClassMerger::node_classes(
node_class_partials_defaults,
node_class_partials_specified,
themeable,
);

any_id.map(|any_id| (any_id, node_classes))
any_id.map(|any_id| (any_id, node_classes_and_warnings))
})
}

Expand All @@ -278,7 +301,7 @@ impl Theme {
fn edge_el_css_classes<'f, T>(
&'f self,
themeable: &'f T,
) -> impl Iterator<Item = (AnyId, CssClasses)> + 'f
) -> impl Iterator<Item = (AnyId, CssClassesAndWarnings)> + 'f
where
T: Themeable,
{
Expand All @@ -288,13 +311,13 @@ impl Theme {
let edge_class_partials_specified = self.edge_class_partials_specified(edge_id);

let any_id = Some(AnyId::from(edge_id.clone()));
let edge_classes = CssClassMerger::edge_classes(
let edge_classes_and_warnings = CssClassMerger::edge_classes(
edge_class_partials_defaults,
edge_class_partials_specified,
themeable,
);

any_id.map(|any_id| (any_id, edge_classes))
any_id.map(|any_id| (any_id, edge_classes_and_warnings))
})
}

Expand All @@ -314,7 +337,7 @@ impl Theme {
themeable: &T,
diagram_theme: &Theme,
tag_id: &TagId,
) -> ElCssClasses
) -> (ElCssClasses, ThemeWarnings)
where
T: Themeable,
{
Expand All @@ -339,14 +362,33 @@ impl Theme {
tag_theme
.node_tag_el_css_classes(themeable, tag_id)
.chain(tag_theme.edge_tag_el_css_classes(themeable, tag_id))
.collect()
.fold(
(
ElCssClasses::with_capacity(
themeable.node_ids().count() + themeable.edge_ids().count(),
),
ThemeWarnings::new(),
),
|(mut el_css_classes, mut theme_warnings_acc),
(any_id, css_classes_and_warnings)| {
let CssClassesAndWarnings {
css_classes,
theme_warnings,
} = css_classes_and_warnings;

el_css_classes.insert(any_id, css_classes);
theme_warnings_acc.extend(theme_warnings.into_inner());

(el_css_classes, theme_warnings_acc)
},
)
}

fn node_tag_el_css_classes<'f, T>(
&'f self,
themeable: &'f T,
tag_id: &'f TagId,
) -> impl Iterator<Item = (AnyId, CssClasses)> + 'f
) -> impl Iterator<Item = (AnyId, CssClassesAndWarnings)> + 'f
where
T: Themeable,
{
Expand All @@ -355,22 +397,22 @@ impl Theme {
let node_class_partials_specified = self.node_class_partials_specified(node_id);

let any_id = Some(AnyId::from(node_id.clone()));
let node_classes = CssClassMerger::node_tag_classes(
let node_classes_and_warnings = CssClassMerger::node_tag_classes(
node_class_partials_defaults,
node_class_partials_specified,
themeable,
tag_id,
);

any_id.map(|any_id| (any_id, node_classes))
any_id.map(|any_id| (any_id, node_classes_and_warnings))
})
}

fn edge_tag_el_css_classes<'f, T>(
&'f self,
themeable: &'f T,
tag_id: &'f TagId,
) -> impl Iterator<Item = (AnyId, CssClasses)> + 'f
) -> impl Iterator<Item = (AnyId, CssClassesAndWarnings)> + 'f
where
T: Themeable,
{
Expand All @@ -380,14 +422,14 @@ impl Theme {
let edge_class_partials_specified = self.edge_class_partials_specified(edge_id);

let any_id = Some(AnyId::from(edge_id.clone()));
let edge_classes = CssClassMerger::edge_tag_classes(
let edge_classes_and_warnings = CssClassMerger::edge_tag_classes(
edge_class_partials_defaults,
edge_class_partials_specified,
themeable,
tag_id,
);

any_id.map(|any_id| (any_id, edge_classes))
any_id.map(|any_id| (any_id, edge_classes_and_warnings))
})
}
}
Expand Down
Loading

0 comments on commit 8ecdac1

Please sign in to comment.