Plume/plume-front/src/lib.rs

156 lines
4 KiB
Rust
Raw Normal View History

2021-02-10 14:51:09 +00: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, Document, Element, Event, HtmlInputElement, TouchEvent};
2021-02-10 14:51:09 +00: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 16:24:41 +00:00
2021-02-10 14:51:09 +00: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 14:51:09 +00:00
menu();
2021-02-10 16:19:42 +00:00
search();
editor::init()
.map_err(|e| console::error_1(&&format!("Editor error: {:?}", e).into()))
.ok();
2021-02-10 14:51:09 +00: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() {
2021-02-12 09:48:33 +00:00
let document = document();
if let Ok(Some(button)) = document.query_selector("#menu a") {
2021-02-10 14:51:09 +00:00
if let Some(menu) = document.get_element_by_id("content") {
2021-02-10 16:24:41 +00:00
let show_menu = Closure::wrap(Box::new(|_: TouchEvent| {
2021-02-12 09:48:33 +00:00
self::document()
2021-02-10 14:51:09 +00:00
.get_element_by_id("menu")
.map(|menu| menu.class_list().add_1("show"))
.unwrap()
.unwrap();
2021-02-10 16:24:41 +00:00
}) as Box<dyn FnMut(TouchEvent)>);
2021-02-10 14:51:09 +00:00
button
.add_event_listener_with_callback("touchend", show_menu.as_ref().unchecked_ref())
.unwrap();
show_menu.forget();
2021-02-10 16:24:41 +00:00
let close_menu = Closure::wrap(Box::new(|_: TouchEvent| {
2021-02-12 09:48:33 +00:00
self::document()
2021-02-10 14:51:09 +00:00
.get_element_by_id("menu")
.map(|menu| menu.class_list().remove_1("show"))
.unwrap()
.unwrap()
2021-02-10 16:24:41 +00:00
}) as Box<dyn FnMut(TouchEvent)>);
2021-02-10 14:51:09 +00:00
menu.add_event_listener_with_callback("touchend", close_menu.as_ref().unchecked_ref())
.unwrap();
close_menu.forget();
}
}
}
2021-02-10 16:19:42 +00:00
/// Clear the URL of the search page before submitting request
fn search() {
2021-02-12 09:48:33 +00:00
if let Some(form) = document().get_element_by_id("form") {
2021-02-10 16:24:41 +00:00
let normalize_query = Closure::wrap(Box::new(|_: Event| {
2021-02-12 09:48:33 +00:00
document()
2021-02-10 16:19:42 +00:00
.query_selector_all("#form input")
.map(|inputs| {
for i in 0..inputs.length() {
let input = inputs.get(i).unwrap();
2021-02-10 16:24:41 +00:00
let input = input.dyn_ref::<HtmlInputElement>().unwrap();
2021-02-10 16:19:42 +00:00
if input.name().is_empty() {
2021-02-10 16:24:41 +00:00
input.set_name(&input.dyn_ref::<Element>().unwrap().id());
2021-02-10 16:19:42 +00:00
}
if !input.name().is_empty() && input.value().is_empty() {
input.set_name("");
}
}
})
.unwrap();
2021-02-10 16:24:41 +00:00
}) as Box<dyn FnMut(Event)>);
2021-02-10 16:19:42 +00:00
form.add_event_listener_with_callback("submit", normalize_query.as_ref().unchecked_ref())
.unwrap();
normalize_query.forget();
}
}
fn document() -> Document {
window().unwrap().document().unwrap()
}