windjammer_ui/components/generated/
codeblock.rs

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