viewy 2.0.8

A web UI toolkit that combine the advantages of a design system and an ui library.
use crate::node::Node;
use crate::{DefaultModifiers, Renderable};
use std::borrow::BorrowMut;

#[derive(Debug, Clone)]
pub enum SanitizationLevel {
    /// No sanitization is performed.
    None,
    /// Only basic HTML tags are allowed.
    /// Use this level if you need to render content issued from RichTextArea
    Basic,
    /// Default level.
    /// No HTML tags allowed, only text
    Strict,
}

/// Used to set the typographic style of a Text view.
#[derive(Debug, Clone)]
pub enum TextStyle {
    LargeTitle,
    H1,
    H2,
    H3,
    Headline,
    Subtitle1,
    Subtitle2,
    Subtitle3,
    Body,
    Article,
    Button,
    Label,
    Overline,
    Caption,
}

/// A view that displays one or more lines of read-only text.
#[derive(Debug, Clone)]
pub struct Text {
    node: Node,
    pub content: String,
    pub style: TextStyle,
    pub no_wrap: bool,
    pub sanitization_level: SanitizationLevel,
}

impl Text {
    pub fn new(content: &str, style: TextStyle) -> Self {
        Text {
            node: Node::default(),
            content: content.to_string(),
            style,
            no_wrap: false,
            sanitization_level: SanitizationLevel::Strict,
        }
    }
    pub fn bold(&mut self, is_bold: bool) -> &mut Self {
        if is_bold {
            self.node
                .node_style
                .push(("font-weight".to_string(), "bold".to_string()));
        } else {
            self.node
                .node_style
                .push(("font-weight".to_string(), "normal".to_string()));
        }
        self
    }
    pub fn uppercase(&mut self, is_uppercase: bool) -> &mut Self {
        if is_uppercase {
            self.node
                .node_style
                .push(("text-transform".to_string(), "uppercase".to_string()));
        } else {
            self.node
                .node_style
                .push(("text-transform".to_string(), "none".to_string()));
        }
        self
    }

    pub fn no_wrap(&mut self, is_no_wrap: bool) -> &mut Self {
        self.no_wrap = is_no_wrap;
        self
    }

    #[deprecated(since = "0.13.16", note = "Use sanitization_level instead")]
    pub fn disable_purification(&mut self) -> &mut Self {
        self.sanitization_level = SanitizationLevel::Basic;
        self
    }

    /// Define sanitization level
    pub fn sanitization_level(&mut self, level: SanitizationLevel) -> &mut Self {
        self.sanitization_level = level;
        self
    }

    pub fn text_overflow(&mut self) -> &mut Self {
        self.node
            .node_style
            .push(("text-overflow".to_string(), "ellipsis".to_string()));
        self
    }
    pub fn text_shadow(&mut self, rule: &str) -> &mut Self {
        self.node
            .node_style
            .push(("text-shadow".to_string(), rule.to_string()));
        self
    }

    pub fn text_decoration(&mut self, decoration: &str) -> &mut Self {
        self.node
            .node_style
            .push(("text-decoration".to_string(), decoration.to_string()));
        self
    }
}

impl std::ops::Deref for Text {
    type Target = Node;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}

impl std::ops::DerefMut for Text {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.node
    }
}

impl DefaultModifiers for Text {}

impl Renderable for Text {
    fn render(mut self) -> Node {
        let text_style = format!("text--{:?}", self.style).to_lowercase();
        self.add_class("text").add_class(text_style.as_str());

        match self.sanitization_level {
            SanitizationLevel::None => self.node.text = Some(self.content.to_string()),
            SanitizationLevel::Basic => self.node.text = Some(ammonia::clean(&self.content)),
            SanitizationLevel::Strict => {
                self.node.text = Some(ammonia::Builder::empty().clean(&self.content).to_string())
            }
        }

        if self.no_wrap {
            self.add_class("text--nowrap");
        }
        self.node
    }
}