#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/loading.md"))]
use std::marker::PhantomData;
use builder::ObjectBuilder;
#[allow(unused_imports)]
use tracing::{debug, error, info, trace, warn};
use wasm_bindgen::prelude::*;
use web_sys::js_sys;
mod builder;
#[wasm_bindgen]
unsafe extern "C" {
pub type MathQuill;
#[wasm_bindgen(js_namespace = MathQuill)]
pub fn getInterface(version: u8) -> MathQuill;
}
impl MathQuill {
pub fn get_field(&self, el: &web_sys::HtmlElement) -> Option<MathField> {
let api_as_fn: &js_sys::Function = self.unchecked_ref();
let res = api_as_fn.call1(&JsValue::NULL, el).unwrap();
if !res.is_truthy() {
return None;
}
return Some(res.unchecked_into());
}
pub fn get_static_field(&self, el: &web_sys::HtmlElement) -> Option<StaticMath> {
let api_as_fn: &js_sys::Function = self.unchecked_ref();
let res = api_as_fn.call1(&JsValue::NULL, el).unwrap();
if !res.is_truthy() {
return None;
}
return Some(res.unchecked_into());
}
}
pub struct Config<MathField> {
pub space_behaves_like_tab: Option<bool>,
pub handlers: Handlers<MathField>,
}
impl<T> Default for Config<T> {
fn default() -> Self {
Self {
space_behaves_like_tab: None,
handlers: Handlers::default(),
}
}
}
impl<T> std::fmt::Debug for Config<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("space_behaves_like_tab", &self.space_behaves_like_tab)
.field("handlers", &self.handlers)
.finish()
}
}
impl Config<MathField> {
pub fn get_js_value(&self) -> JsValue {
let mut obj = ObjectBuilder::new();
obj.set("spaceBehavesLikeTab", &self.space_behaves_like_tab.into());
obj.set("handlers", &self.handlers.get_js_value());
obj.build()
}
}
pub struct Handlers<MathField> {
pub edit: Option<Closure<dyn FnMut()>>,
pub _phantom: PhantomData<MathField>,
}
impl<T> Default for Handlers<T> {
fn default() -> Self {
Self {
edit: None,
_phantom: PhantomData,
}
}
}
impl<T> std::fmt::Debug for Handlers<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Handlers")
.field("edit", &self.edit)
.finish()
}
}
impl Handlers<MathField> {
pub fn get_js_value(&self) -> JsValue {
let mut obj = ObjectBuilder::new();
obj.set(
"edit",
&self
.edit
.as_ref()
.map(|closure| closure.as_ref().clone())
.into(),
);
obj.build()
}
}
#[wasm_bindgen]
unsafe extern "C" {
pub type MathField;
#[wasm_bindgen(method)]
pub fn MathField(
this: &MathQuill,
html_element: &web_sys::HtmlElement,
config: JsValue,
) -> MathField;
pub type StaticMath;
#[wasm_bindgen(method)]
pub fn StaticMath(this: &MathQuill, html_element: &web_sys::HtmlElement) -> StaticMath;
}
#[wasm_bindgen]
unsafe extern "C" {
#[wasm_bindgen(method)]
pub fn latex(this: &StaticMath) -> String;
#[wasm_bindgen(method)]
pub fn latex(this: &MathField) -> String;
#[wasm_bindgen(method, js_name = "latex")]
pub fn set_latex(this: &StaticMath, latex: &str);
#[wasm_bindgen(method, js_name = "latex")]
pub fn set_latex(this: &MathField, latex: &str);
}