|
| 1 | +package zone.nox.components; |
| 2 | + |
| 3 | +import dev.nipafx.ginevra.css.Css; |
| 4 | +import dev.nipafx.ginevra.css.CssStyle; |
| 5 | +import dev.nipafx.ginevra.css.StyledWith; |
| 6 | +import dev.nipafx.ginevra.html.*; |
| 7 | +import dev.nipafx.ginevra.outline.Resources; |
| 8 | + |
| 9 | +import static dev.nipafx.ginevra.html.HtmlElement.div; |
| 10 | +import static dev.nipafx.ginevra.html.HtmlElement.img; |
| 11 | + |
| 12 | +public record PostImage(String src, Ratio ratio, String alt) implements Component { |
| 13 | + |
| 14 | + public record Style(Classes outer, Classes inner, Classes image, |
| 15 | + Classes r_16_9, Classes r_3_2, Classes r_4_3, Classes r_1_1, Classes r_3_4, Classes r_2_3, Classes r_9_16, |
| 16 | + Css css) implements CssStyle { } |
| 17 | + |
| 18 | + @StyledWith |
| 19 | + public static final Style STYLE = Css.parse(Style.class, """ |
| 20 | + .outer { |
| 21 | + padding: 0 1em; |
| 22 | + } |
| 23 | +
|
| 24 | + .inner { |
| 25 | + position: relative; |
| 26 | + overflow: hidden; |
| 27 | +
|
| 28 | + border: 1px solid var(--yellow); |
| 29 | + } |
| 30 | +
|
| 31 | + .r_16_9 { |
| 32 | + padding-top: 56.25%; |
| 33 | + } |
| 34 | +
|
| 35 | + .r_3_2 { |
| 36 | + padding-top: 66.67%; |
| 37 | + } |
| 38 | +
|
| 39 | + .r_4_3 { |
| 40 | + padding-top: 75%; |
| 41 | + } |
| 42 | +
|
| 43 | + .r_1_1 { |
| 44 | + padding-top: 100%; |
| 45 | + } |
| 46 | +
|
| 47 | + .r_3_4 { |
| 48 | + padding-top: 133.33%; |
| 49 | + } |
| 50 | +
|
| 51 | + .r_2_3 { |
| 52 | + padding-top: 150%; |
| 53 | + } |
| 54 | +
|
| 55 | + .r_9_16 { |
| 56 | + padding-top: 177.78%; |
| 57 | + } |
| 58 | +
|
| 59 | + .image { |
| 60 | + display: block; |
| 61 | + position: absolute; |
| 62 | + top: 0; |
| 63 | + left: 0; |
| 64 | + width: 100%; |
| 65 | + height: 100%; |
| 66 | + } |
| 67 | + |
| 68 | + .youTubeVideo { |
| 69 | + position: relative; |
| 70 | + overflow: hidden; |
| 71 | + /* 16:9 portrait aspect ratio */ |
| 72 | + padding-top: 177.78%; |
| 73 | + } |
| 74 | +
|
| 75 | + .youTubeVideo > image { |
| 76 | + display: block; |
| 77 | + position: absolute; |
| 78 | + top: 0; |
| 79 | + left: 0; |
| 80 | + width: 100%; |
| 81 | + height: 100%; |
| 82 | + border: 0; |
| 83 | + } |
| 84 | + """); |
| 85 | + |
| 86 | + public PostImage(Image image) { |
| 87 | + var src = image.src().asHtmlValue(); |
| 88 | + var settings = image.alt().split(" \\| "); |
| 89 | + var ratio = Ratio.parse(settings[0]); |
| 90 | + var alt = settings.length > 1 ? settings[1] : ""; |
| 91 | + this(src, ratio, alt); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Element compose() { |
| 96 | + return div |
| 97 | + .classes(STYLE.outer) |
| 98 | + .children(div |
| 99 | + .classes(STYLE.inner.plus(ratio.toCssClass())) |
| 100 | + .children(img |
| 101 | + .classes(STYLE.image) |
| 102 | + .src(Resources.include(src)) |
| 103 | + .alt(alt))); |
| 104 | + } |
| 105 | + |
| 106 | + private enum Ratio { |
| 107 | + |
| 108 | + _16_9, _3_2, _4_3, _1_1, _3_4, _2_3, _9_16; |
| 109 | + |
| 110 | + static Ratio parse(String value) { |
| 111 | + return switch (value) { |
| 112 | + case "16:9" -> _16_9; |
| 113 | + case "3:2" -> _3_2; |
| 114 | + case "4:3" -> _4_3; |
| 115 | + case "1:1" -> _1_1; |
| 116 | + case "3:4" -> _3_4; |
| 117 | + case "2:3" -> _2_3; |
| 118 | + case "9:16" -> _9_16; |
| 119 | + default -> throw new IllegalArgumentException("Unknown ratio: " + value); |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + Classes toCssClass() { |
| 124 | + return switch (this) { |
| 125 | + case _16_9 -> STYLE.r_16_9; |
| 126 | + case _3_2 -> STYLE.r_3_2; |
| 127 | + case _4_3 -> STYLE.r_4_3; |
| 128 | + case _1_1 -> STYLE.r_1_1; |
| 129 | + case _3_4 -> STYLE.r_3_4; |
| 130 | + case _2_3 -> STYLE.r_2_3; |
| 131 | + case _9_16 -> STYLE.r_9_16; |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments