Plume/plume-front/src/lib.rs

166 lines
4.2 KiB
Rust
Raw Normal View History

2021-02-10 15:51:09 +01:00
#![recursion_limit = "128"]
#![feature(decl_macro, proc_macro_hygiene, try_trait)]
#[macro_use]
extern crate gettext_macros;
#[macro_use]
extern crate lazy_static;
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::{console, window, Element, Event, HtmlInputElement, TouchEvent};
2021-02-10 15:51:09 +01:00
init_i18n!(
"plume-front",
af,
ar,
bg,
ca,
cs,
cy,
da,
de,
el,
en,
eo,
es,
fa,
fi,
fr,
gl,
he,
hi,
hr,
hu,
it,
ja,
ko,
nb,
nl,
no,
pl,
pt,
ro,
ru,
sat,
si,
sk,
sl,
sr,
sv,
tr,
uk,
vi,
zh
);
mod editor;
2021-02-10 17:24:41 +01:00
2021-02-10 15:51:09 +01:00
compile_i18n!();
lazy_static! {
static ref CATALOG: gettext::Catalog = {
let catalogs = include_i18n!();
let lang = window().unwrap().navigator().language().unwrap();
let lang = lang.splitn(2, '-').next().unwrap_or("en");
let english_position = catalogs
.iter()
.position(|(language_code, _)| *language_code == "en")
.unwrap();
catalogs
.iter()
.find(|(l, _)| l == &lang)
.unwrap_or(&catalogs[english_position])
.clone()
.1
};
}
#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
extern crate console_error_panic_hook;
use std::panic;
panic::set_hook(Box::new(console_error_panic_hook::hook));
2021-02-10 15:51:09 +01:00
menu();
2021-02-10 17:19:42 +01:00
search();
editor::init()
.map_err(|e| console::error_1(&&format!("Editor error: {:?}", e).into()))
.ok();
2021-02-10 15:51:09 +01:00
Ok(())
}
/// Toggle menu on mobile devices
///
/// It should normally be working fine even without this code
/// But :focus-within is not yet supported by Webkit/Blink
fn menu() {
let document = window().unwrap().document().unwrap();
if let Some(button) = document.get_element_by_id("menu") {
if let Some(menu) = document.get_element_by_id("content") {
2021-02-10 17:24:41 +01:00
let show_menu = Closure::wrap(Box::new(|_: TouchEvent| {
2021-02-10 15:51:09 +01:00
window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("menu")
.map(|menu| menu.class_list().add_1("show"))
.unwrap()
.unwrap();
2021-02-10 17:24:41 +01:00
}) as Box<dyn FnMut(TouchEvent)>);
2021-02-10 15:51:09 +01:00
button
.add_event_listener_with_callback("touchend", show_menu.as_ref().unchecked_ref())
.unwrap();
show_menu.forget();
2021-02-10 17:24:41 +01:00
let close_menu = Closure::wrap(Box::new(|_: TouchEvent| {
2021-02-10 15:51:09 +01:00
window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("menu")
.map(|menu| menu.class_list().remove_1("show"))
.unwrap()
.unwrap()
2021-02-10 17:24:41 +01:00
}) as Box<dyn FnMut(TouchEvent)>);
2021-02-10 15:51:09 +01:00
menu.add_event_listener_with_callback("touchend", close_menu.as_ref().unchecked_ref())
.unwrap();
close_menu.forget();
}
}
}
2021-02-10 17:19:42 +01:00
/// Clear the URL of the search page before submitting request
fn search() {
if let Some(form) = window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("form")
{
2021-02-10 17:24:41 +01:00
let normalize_query = Closure::wrap(Box::new(|_: Event| {
2021-02-10 17:19:42 +01:00
window()
.unwrap()
.document()
.unwrap()
.query_selector_all("#form input")
.map(|inputs| {
for i in 0..inputs.length() {
let input = inputs.get(i).unwrap();
2021-02-10 17:24:41 +01:00
let input = input.dyn_ref::<HtmlInputElement>().unwrap();
2021-02-10 17:19:42 +01:00
if input.name().is_empty() {
2021-02-10 17:24:41 +01:00
input.set_name(&input.dyn_ref::<Element>().unwrap().id());
2021-02-10 17:19:42 +01:00
}
if !input.name().is_empty() && input.value().is_empty() {
input.set_name("");
}
}
})
.unwrap();
2021-02-10 17:24:41 +01:00
}) as Box<dyn FnMut(Event)>);
2021-02-10 17:19:42 +01:00
form.add_event_listener_with_callback("submit", normalize_query.as_ref().unchecked_ref())
.unwrap();
normalize_query.forget();
}
}