windjammer_ui/components/generated/
codeblock.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3use super::traits::Renderable;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
6pub struct CodeBlock {
7    pub code: String,
8    pub language: String,
9    pub show_line_numbers: bool,
10    pub show_copy_button: bool,
11}
12
13impl CodeBlock {
14    #[inline]
15    pub fn new(code: String) -> CodeBlock {
16        CodeBlock {
17            code,
18            language: String::from(""),
19            show_line_numbers: false,
20            show_copy_button: true,
21        }
22    }
23    #[inline]
24    pub fn language(mut self, language: String) -> CodeBlock {
25        self.language = language;
26        self
27    }
28    #[inline]
29    pub fn show_line_numbers(mut self, show: bool) -> CodeBlock {
30        self.show_line_numbers = show;
31        self
32    }
33    #[inline]
34    pub fn show_copy_button(mut self, show: bool) -> CodeBlock {
35        self.show_copy_button = show;
36        self
37    }
38}
39
40impl Renderable for CodeBlock {
41    #[inline]
42    fn render(self) -> String {
43        let language_label = {
44            if self.language.len() > (0 as usize) {
45                format!("<div class='wj-codeblock-language'>{}</div>", self.language)
46            } else {
47                String::from("")
48            }
49        };
50        let copy_button = {
51            if self.show_copy_button {
52                format!("<button class='wj-codeblock-copy' onclick='navigator.clipboard.writeText(this.parentElement.querySelector(\"code\").textContent); this.textContent=\"✓ Copied!\"; setTimeout(() => this.textContent=\"📋 Copy\", 2000)'>
53                    📋 Copy
54                </button>")
55            } else {
56                String::from("")
57            }
58        };
59        let line_number_class = {
60            if self.show_line_numbers {
61                " wj-codeblock-numbered".to_string()
62            } else {
63                "".to_string()
64            }
65        };
66        format!(
67            "<div class='wj-codeblock{}'>
68                <div class='wj-codeblock-header'>
69                    {}
70                    {}
71                </div>
72                <pre class='wj-codeblock-pre'><code class='wj-codeblock-code'>{}</code></pre>
73            </div>",
74            line_number_class, language_label, copy_button, self.code
75        )
76    }
77}