layoutcss_parser/components/
grid.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use indoc::formatdoc;
use std::collections::HashSet;

use crate::harmonic::get_harmonic;

const GRID_STYLE: &str = r#"
  grid-l{
    display: grid;
  }
"#;

fn grid_gap_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        grid-l[layout~="gap:{value}"]{{
            gap: {harmonic};
        }}
        "#,
    )
}

fn grid_gap_x_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        grid-l[layout~="gap-x:{value}"]{{
            column-gap: {harmonic};
        }}
        "#,
    )
}

fn grid_gap_y_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        grid-l[layout~="gap-y:{value}"]{{
            row-gap: {harmonic};
        }}
        "#,
    )
}

fn grid_group_empty(min_cell_width: &str) -> String {
    formatdoc!(
        r#"
        grid-l[layout*="min-cell-width:{min_cell_width}"] {{
            grid-template-columns: repeat(auto-fit, minmax(min({min_cell_width}, 100%),1fr));
        }}
        "#,
    )
}

fn grid_group_max_cols(min_cell_width: &str, max_cols: &str, gap_delta_max: &str) -> String {
    formatdoc!(
        r#"
        grid-l[layout*="min-cell-width:{min_cell_width}"][layout*="max-cols:{max_cols}"]{{
            grid-template-columns: repeat(auto-fit, minmax(min(100%, max({min_cell_width}, (100% / {max_cols} - {gap_delta_max}))),1fr));
        }}
        "#,
    )
}

fn grid_group_min_cols(min_cell_width: &str, min_cols: &str, gap_delta_min: &str) -> String {
    formatdoc!(
        r#"
        grid-l[layout*="min-cell-width:{min_cell_width}"][layout*="min-cols:{min_cols}"]:has(:nth-child({min_cols})){{
            grid-template-columns: repeat(auto-fit, minmax(min((100% / {min_cols} - {gap_delta_min}), {min_cell_width}), 1fr));
        }}
        grid-l[layout*="min-cell-width:{min_cell_width}"][layout*="min-cols:{min_cols}"]{{
            grid-template-columns: repeat({min_cols}, 1fr);
        }}
        "#,
    )
}

fn grid_group_min_cols_max_cols(
    min_cell_width: &str,
    min_cols: &str,
    max_cols: &str,
    gap_delta_min: &str,
    gap_delta_max: &str,
    fr: f64,
) -> String {
    formatdoc!(
        r#"
        grid-l[layout*="min-cell-width:{min_cell_width}"][layout*="min-cols:{min_cols}"][layout*="max-cols:{max_cols}"]:has(:nth-child({min_cols})){{
            grid-template-columns:
                repeat(auto-fit,
                    minmax(
                        min(
                            (100% / {min_cols} - {gap_delta_min}),
                                max({min_cell_width}, (100% / {max_cols} - {gap_delta_max}))
                            ),
                            {fr}fr
                            )
                        )
                    }}
        grid-l[layout*="min-cell-width:{min_cell_width}"][layout*="min-cols:{min_cols}"][layout*="max-cols:{max_cols}"]{{
            grid-template-columns: repeat({min_cols}, 1fr);
        }}
        "#,
    )
}

fn gap_delta(cols: &str, gap: Option<&str>, harmonic_ratio: f64) -> String {
    if let Some(value) = gap {
        match cols.parse::<f64>() {
            Ok(cols_number) => {
                let hr =  get_harmonic(&value, harmonic_ratio);
                format!("{hr} * ({cols_number} - 0.98) / {cols_number}").to_string()
                //get_harmonic(&value, harmonic_ratio) * (val - 0.98) / val,
            },
            Err(_) => "0px".to_string(),
        }
    } else {
        "0px".to_string()
    }
}

pub fn grid_css(
    min_cell_width: Option<&str>,
    min_cols: Option<&str>,
    max_cols: Option<&str>,
    gap: Option<&str>,
    gap_x: Option<&str>,
    gap_y: Option<&str>,
    harmonic_ratio: f64,
    set: &mut HashSet<String>,
) {
    set.insert(GRID_STYLE.to_string());
    if let Some(ref value) = gap {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(grid_gap_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_x {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(grid_gap_x_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_y {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(grid_gap_y_style(value, harmonic_value));
    }
    if let Some(min_cell_width) = min_cell_width {
        match (min_cols, max_cols) {
            (Some(min_cols), Some(max_cols)) => {

                let gap_delta_min = gap_delta(min_cols, gap, harmonic_ratio);
                let gap_delta_max = gap_delta(max_cols, gap, harmonic_ratio);
                let fr = 1.0 / min_cols.parse::<f64>().unwrap_or(-1.0);
                set.insert(grid_group_min_cols_max_cols(
                    min_cell_width,
                    min_cols,
                    max_cols,
                    &gap_delta_min,
                    &gap_delta_max,
                    fr,
                ));
            }
            (Some(min_cols), None) => {
                let gap_delta_min = gap_delta(min_cols, gap, harmonic_ratio);
                set.insert(grid_group_min_cols(min_cell_width, min_cols, &gap_delta_min));
            }
            (None, Some(max_cols)) => {
                let gap_delta_max = gap_delta(max_cols, gap, harmonic_ratio);
                set.insert(grid_group_max_cols(min_cell_width, max_cols, &gap_delta_max));
            }
            _ => {
                set.insert(grid_group_empty(min_cell_width));
            }
        }
    }
}