Skip to content

Commit 764701e

Browse files
committed
start using a hash for the preview so you can return all multiple sets
1 parent c275462 commit 764701e

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ gui = ["dep:sdl2"]
3636
#Web usage, may want to build with no-default-features to skip irrelevant terminal stuff
3737
wasm = ["png", "dep:base64", "dep:gloo", "dep:js-sys", "dep:web-sys", "dep:yew"]
3838
#Web component, may want to build with no-default-features to skip irrelevant terminal stuff
39-
webc = ["png", "dep:base64", "dep:gloo", "dep:js-sys", "dep:wasm-bindgen", "dep:web-sys"]
39+
webc = ["png", "dep:base64", "dep:gloo", "gloo-utils", "dep:js-sys", "dep:wasm-bindgen", "dep:web-sys"]
4040

4141

4242
[dev-dependencies]
@@ -56,6 +56,7 @@ sdl2 = { optional = true, version = "0.37.0", features = ["gfx"], default-featur
5656
#wasm
5757
base64 = { optional = true, version = "0.21.5"}
5858
gloo = { optional = true, version = "0.10" }
59+
gloo-utils = { optional = true, version = "0.1", features = ["serde"] }
5960
js-sys = { optional = true, version = "0.3"}
6061
wasm-bindgen = { optional = true, version = "0.2" }
6162
web-sys = { optional = true, version = "0.3", features = ["File", "DragEvent", "DataTransfer"] }

src/file_data.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ impl Raw {
3535
pub fn previews(&self) -> Vec<Image> {
3636
// if let Some(width) = width {
3737
// }else {
38-
let mut images: Vec<Image> = self
39-
.widths(ImageType::CGA)
38+
self.widths(ImageType::CGA)
4039
.iter()
41-
.map(|w| self.parse(ParserType::CGA, *w as usize))
42-
.collect();
40+
.map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize)))
41+
.collect()
4342
// }
44-
images.push(self.parse(ParserType::CGA, 320));
45-
images
4643
}
4744
}

src/webc/index.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
let $ = (query) => document.querySelector(query);
1515

1616
function fileLoaded(array) {
17-
const container = document.createElement("preview-area");
18-
let images = window.wasmBindings.previews(array);
19-
images.forEach(function(src) {
20-
container.appendChild(new PreviewItem(src));
21-
});
22-
$('preview-wrapper').appendChild(container);
17+
let hash = new Map(Object.entries(window.wasmBindings.previews(array)));
18+
hash.forEach((images, key) => {
19+
const container = document.createElement("preview-area");
20+
images.forEach(src => container.appendChild(new PreviewItem(src)));
21+
$('preview-wrapper').appendChild(container);
22+
})
2323
}
2424

2525
document.addEventListener("DOMContentLoaded", () => {

src/webc/main.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![cfg(feature = "webc")]
22

3+
use std::collections::HashMap;
4+
35
use base64::engine::general_purpose::STANDARD;
46
use base64::Engine;
7+
use gloo_utils::format::JsValueSerdeExt;
58
use wasm_bindgen::prelude::*;
69

710
use cega::color::palette::palette_from_abbr;
@@ -20,19 +23,21 @@ pub fn png(data: &[u8]) -> String {
2023
}
2124

2225
#[wasm_bindgen]
23-
pub fn previews(data: &[u8]) -> Vec<String> {
26+
pub fn previews(data: &[u8]) -> JsValue {
2427
let file_data = Raw::new(data);
2528
let palette = palette_from_abbr("cga0");
26-
file_data
29+
let mut hm = HashMap::new();
30+
let images: Vec<String> = file_data
2731
.previews()
2832
.iter()
2933
.map(|p| {
3034
format!(
3135
"data:application/png;base64,{}",
3236
STANDARD.encode(png::write2(p.data(), palette.clone()))
3337
)
34-
})
35-
.collect()
38+
}).collect();
39+
hm.insert("CGA".to_string(), images);
40+
JsValue::from_serde(&hm).unwrap()
3641
}
3742

3843
fn main() {}

0 commit comments

Comments
 (0)