Skip to content

Commit abdd9a1

Browse files
committed
dont use web-sys
1 parent fc9b5fc commit abdd9a1

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 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", "dep:web-sys", "dep:lz4_flex", "dep:base64"]
53+
wasm=["dep:wasm-bindgen", "rupl/wasm", "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"]
@@ -67,7 +67,6 @@ serde = {version = "1.0.228", features = ["derive"], optional = true}
6767
rupl={version = "0.1.2",path="../rupl",default-features = false}
6868
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"]}
7170
console_error_panic_hook = { version = "0.1.7", optional = true }
7271
wee_alloc = {version = "0.4.5", optional = true}
7372
lz4_flex = {version="0.12.0", default-features = false, optional = true}

src/data.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,8 @@ impl Data {
313313
if !plot.is_drag() {
314314
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
315315
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();
316+
let hash = format!("#{}", URL_SAFE.encode(data));
317+
crate::set_hash(&hash);
323318
}
324319
let new_name;
325320
let old_len = self.data.len();

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ pub type I = kalc_lib::types::f64::Integer<i128>;
3737
pub type F = kalc_lib::types::f64::Float<f64>;
3838
#[cfg(feature = "kalc-lib")]
3939
pub type C = kalc_lib::types::f64::Complex<f64>;
40+
#[cfg(feature = "wasm")]
41+
#[wasm_bindgen::prelude::wasm_bindgen(module = "/url.js")]
42+
extern "C" {
43+
fn set_hash(s: &str);
44+
fn get_hash() -> String;
45+
}
4046
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
4147
pub fn main() {
4248
#[cfg(feature = "wasm-console")]
@@ -53,13 +59,12 @@ pub fn main() {
5359
let function = args.last().unwrap_or(&s);
5460
#[cfg(feature = "wasm")]
5561
let function: &String = &{
56-
let mut hash = web_sys::window().unwrap().location().hash().unwrap();
62+
let hash = get_hash();
5763
if hash.is_empty() {
5864
String::new()
5965
} else {
6066
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
61-
hash.remove(0);
62-
let comp = URL_SAFE.decode(hash).unwrap_or_default();
67+
let comp = URL_SAFE.decode(&hash[1..]).unwrap_or_default();
6368
let bytes = lz4_flex::decompress_size_prepended(&comp).unwrap_or_default();
6469
String::try_from(bytes).unwrap_or_default()
6570
}

url.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function set_hash(s) {
2+
history.replaceState(null, "", s);
3+
}
4+
export function get_hash() {
5+
return location.hash;
6+
}

0 commit comments

Comments
 (0)