Skip to content

Commit 09719dc

Browse files
committed
put function data into url on wasm
1 parent 435c74e commit 09719dc

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ softbuffer-wayland=["softbuffer/wayland"]
5050
rug=["kalc-lib/rug","kalc-lib"]
5151
fastnum=["kalc-lib/fastnum","kalc-lib"]
5252
kalc-lib=["dep:kalc-lib"]
53-
wasm=["dep:wasm-bindgen", "rupl/wasm"]
53+
wasm=["dep:wasm-bindgen", "rupl/wasm", "dep:web-sys", "dep:lz4_flex", "dep:base64"]
5454
wasm-draw=["rupl/wasm-draw", "wasm", "rupl/winit", "dep:winit"]
5555
wee=["dep:wee_alloc"]
5656
wasm-console = ["dep:console_error_panic_hook"]
@@ -65,7 +65,10 @@ dirs={version="6.0.0", optional=true}
6565
bitcode = {version="0.6.9",features = ["serde"],optional = true}
6666
serde = {version = "1.0.228", features = ["derive"], optional = true}
6767
rupl={version = "0.1.2",path="../rupl",default-features = false}
68-
kalc-lib={version="1.5.1",default-features=false,path="../kalc-lib",optional = true}
68+
kalc-lib={version="1.5.1",default-features=false,path="../kalc-lib",optional = true, features = ["fastrand"]}
6969
wasm-bindgen = {version="0.2.108",optional = true}
70+
web-sys = {version= "0.3.85", optional = true, features = ["Window","Location","History"]}
7071
console_error_panic_hook = { version = "0.1.7", optional = true }
7172
wee_alloc = {version = "0.4.5", optional = true}
73+
lz4_flex = {version="0.12.0", default-features = false, optional = true}
74+
base64 = {version="0.22.1", optional = true}

src/data.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ impl Data {
309309
i += 1;
310310
}
311311
let func = func.join("#").replace(";#", ";");
312+
#[cfg(feature = "wasm")]
313+
{
314+
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
315+
let data = lz4_flex::compress_prepend_size(func.as_bytes());
316+
let url = format!("#{}", URL_SAFE.encode(data));
317+
web_sys::window()
318+
.unwrap()
319+
.history()
320+
.unwrap()
321+
.replace_state_with_url(&wasm_bindgen::JsValue::NULL, "", Some(&url))
322+
.unwrap();
323+
}
312324
let new_name;
313325
let old_len = self.data.len();
314326
(self.data, new_name, _) = init(&func, &mut self.options, self.vars.clone()).unwrap_or((

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ pub fn main() {
4747
if !args.is_empty() {
4848
args.remove(0);
4949
}
50+
#[cfg(not(feature = "wasm"))]
5051
let s = String::new();
52+
#[cfg(not(feature = "wasm"))]
5153
let function = args.last().unwrap_or(&s);
54+
#[cfg(feature = "wasm")]
55+
let function: &String = &{
56+
let mut hash = web_sys::window().unwrap().location().hash().unwrap();
57+
if hash.is_empty() {
58+
String::new()
59+
} else {
60+
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
61+
hash.remove(0);
62+
let comp = URL_SAFE.decode(hash).unwrap_or_default();
63+
let bytes = lz4_flex::decompress_size_prepended(&comp).unwrap_or_default();
64+
String::try_from(bytes).unwrap_or_default()
65+
}
66+
};
5267
#[cfg(feature = "kalc-lib")]
5368
let data = if args.len() > 1 && args[0] == "-d" && cfg!(feature = "bincode") {
5469
#[cfg(feature = "bincode")]

0 commit comments

Comments
 (0)