|
1 | 1 | use base64::{engine::general_purpose::STANDARD, Engine};
|
2 | 2 |
|
3 |
| -use web_sys::HtmlInputElement; |
| 3 | +use web_sys::{HtmlElement, HtmlInputElement}; |
4 | 4 | use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast};
|
5 | 5 |
|
6 | 6 | use crate::color::palette::palette_from_abbr;
|
7 |
| -use crate::file_data; |
8 | 7 | use crate::image::tile;
|
9 | 8 | use crate::parser::ParserType;
|
10 |
| -use crate::png; |
| 9 | +use crate::{file_data, png}; |
11 | 10 |
|
12 | 11 | use crate::wasm::FileUpload;
|
13 | 12 |
|
@@ -42,6 +41,28 @@ impl ImageComponent {
|
42 | 41 | };
|
43 | 42 | format!("data:application/png;base64,{}", STANDARD.encode(data))
|
44 | 43 | }
|
| 44 | + |
| 45 | + pub fn previews(&self, file: &FileUpload) -> Html { |
| 46 | + if file.mime_type.contains("image") { |
| 47 | + "".into() |
| 48 | + } else { |
| 49 | + let file_data = file_data::Raw::new(&file.data); |
| 50 | + file_data |
| 51 | + .previews() |
| 52 | + .iter() |
| 53 | + .map(|p| { |
| 54 | + let palette = palette_from_abbr("cga0"); |
| 55 | + let mut bytes: Vec<u8> = Vec::new(); |
| 56 | + |
| 57 | + let _ = png::write_to(&mut bytes, tile(p.data(), self.height), palette.clone()); |
| 58 | + let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes)); |
| 59 | + html! { |
| 60 | + <img src={ src } /> |
| 61 | + } |
| 62 | + }) |
| 63 | + .collect() |
| 64 | + } |
| 65 | + } |
45 | 66 | }
|
46 | 67 |
|
47 | 68 | impl Component for ImageComponent {
|
@@ -76,18 +97,21 @@ impl Component for ImageComponent {
|
76 | 97 | let file = &ctx.props().file;
|
77 | 98 |
|
78 | 99 | html! {
|
79 |
| - <div class="preview-tile "> |
80 |
| - <form onsubmit={noop}> |
81 |
| - <label for="width">{"[Tile] Width"}</label> |
82 |
| - <input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} /> |
83 |
| - <label for="height">{"[Tile] Height"}</label> |
84 |
| - <input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} /> |
85 |
| - </form> |
86 |
| - <p class="preview-name">{ file.name.to_string() }</p> |
87 |
| - <div class=".preview-media"> |
88 |
| - <img src={ self.src(file) } /> |
| 100 | + <> |
| 101 | + <div class="preview-tile "> |
| 102 | + <div class=".preview-media"> |
| 103 | + <p class="preview-name">{ file.name.to_string() }</p> |
| 104 | + <img src={ self.src(file) } /> |
| 105 | + </div> |
| 106 | + <form onsubmit={noop}> |
| 107 | + <label for="width">{"[Tile] Width"}</label> |
| 108 | + <input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} /> |
| 109 | + <label for="height">{"[Tile] Height"}</label> |
| 110 | + <input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} /> |
| 111 | + </form> |
89 | 112 | </div>
|
90 |
| - </div> |
| 113 | + {self.previews(file)} |
| 114 | + </> |
91 | 115 | }
|
92 | 116 | }
|
93 | 117 | }
|
0 commit comments