Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/typst-ide-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ comemo = { workspace = true }
ecow = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
ttf-parser = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
Expand Down
33 changes: 33 additions & 0 deletions crates/typst-ide-wasm/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The JavaScript-facing surface of the crate.

use wasm_bindgen::JsError;
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::wasm_bindgen;

Expand Down Expand Up @@ -113,6 +114,28 @@ impl IdeSession {
None => JsValue::NULL,
}
}

/// Replaces the set of library image names: images a project file that
/// does not exist resolves to by its bare file name. Invalid names are
/// ignored.
#[wasm_bindgen(js_name = setLibraryImages)]
pub fn set_library_images(&mut self, names: Vec<String>) {
self.0.set_library_images(&names);
}

/// Supplies the bytes of a library image. Ignored unless `name` is in the
/// current set.
#[wasm_bindgen(js_name = addLibraryImage)]
pub fn add_library_image(&mut self, name: &str, bytes: &[u8]) {
self.0.add_library_image(name, bytes);
}

/// Returns, sorted, the library images the last lookups needed but whose
/// bytes have not been supplied yet, and forgets them.
#[wasm_bindgen(js_name = takeMissingLibraryImages)]
pub fn take_missing_library_images(&mut self) -> Vec<String> {
self.0.take_missing_library_images()
}
}

impl Default for IdeSession {
Expand All @@ -121,6 +144,16 @@ impl Default for IdeSession {
}
}

/// Reads every face of a font file.
///
/// Returns `[{ index, family, style, weight, stretch, monospace, serif, math,
/// variable }]` and throws when no face can be read.
#[wasm_bindgen(js_name = fontFaces)]
pub fn font_faces(bytes: &[u8]) -> Result<JsValue, JsError> {
let faces = crate::font_faces(bytes).map_err(|message| JsError::new(&message))?;
serde_wasm_bindgen::to_value(&faces).map_err(|error| JsError::new(&error.to_string()))
}

/// Serializes a result, falling back to `null` if that somehow fails.
fn to_js(value: &impl serde::Serialize) -> JsValue {
serde_wasm_bindgen::to_value(value).unwrap_or(JsValue::NULL)
Expand Down
Loading
Loading