rumtk_web/components/
formatted_label.rs1use crate::components::html::{markdown, Markdown};
22use crate::defaults::PARAMS_CONTENTS;
23use crate::utils::defaults::{
24 DEFAULT_NO_TEXT, DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_TYPE, SECTION_TEXT,
25};
26use crate::utils::types::{SharedAppState, URLParams, URLPath};
27use crate::utils::DEFAULT_TEXTMAP;
28use crate::{rumtk_web_get_config, rumtk_web_get_config_string, rumtk_web_get_text_item, rumtk_web_params_map, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
29use rumtk_core::strings::RUMString;
30
31#[derive(RUMWebTemplate, Debug, Clone)]
32#[template(
33 source = "
34 {% if custom_css_enabled %}
35 <link href='/static/components/formatted_label.css' rel='stylesheet'>
36 {% endif %}
37 <div class='formatted-label-{{css_class}}'>
38 {{md}}
39 </div>
40 ",
41 ext = "html"
42)]
43pub struct FormattedLabel {
44 md: Markdown,
45 css_class: RUMString,
46 custom_css_enabled: bool,
47}
48
49impl RUMWebTemplateSafe for FormattedLabel {}
50
51pub fn formatted_label<'a>(
52 _path_components: URLPath<'a, 'a>,
53 params: URLParams<'a>,
54 state: SharedAppState,
55) -> ComponentResult<FormattedLabel> {
56 let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_TEXT_ITEM);
57 let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
58
59 let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
60
61 let text_store = rumtk_web_get_config_string!(state, SECTION_TEXT);
62 let itm = rumtk_web_get_text_item!(&text_store, typ, &DEFAULT_TEXTMAP);
63 let desc = rumtk_web_get_text_item!(&itm, "description", DEFAULT_NO_TEXT);
64
65 let md_params = rumtk_web_params_map!([(PARAMS_CONTENTS, desc)]);
66 let html = markdown(
67 _path_components,
68 md_params.get_inner(),
69 state.clone()
70 )?;
71
72 Ok(FormattedLabel {
73 md: html,
74 css_class,
75 custom_css_enabled
76 })
77}