Skip to content

Commit

Permalink
Remove unsafe code, remove generic getters when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
epsilon-phase committed Nov 1, 2019
1 parent 632b83f commit 7bb12d0
Showing 1 changed file with 40 additions and 72 deletions.
112 changes: 40 additions & 72 deletions plume-front/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json;
use std::sync::Mutex;
use stdweb::{
unstable::{TryFrom, TryInto},
web::{event::*, html_element::*, *},
Expand All @@ -18,9 +19,13 @@ macro_rules! mv {
fn get_elt_value(id: &'static str) -> String {
let elt = document().get_element_by_id(id).unwrap();
let inp: Result<InputElement, _> = elt.clone().try_into();
let textarea: Result<TextAreaElement, _> = elt.try_into();
inp.map(|i| i.raw_value())
.unwrap_or_else(|_| textarea.unwrap().value())
let textarea: Result<TextAreaElement, _> = elt.clone().try_into();
let select: Result<SelectElement, _> = elt.try_into();
inp.map(|i| i.raw_value()).unwrap_or_else(|_| {
textarea
.map(|t| t.value())
.unwrap_or_else(|_| select.unwrap().raw_value())
})
}

fn set_value<S: AsRef<str>>(id: &'static str, val: S) {
Expand Down Expand Up @@ -69,6 +74,7 @@ impl From<stdweb::private::ConversionError> for EditorError {
EditorError::TypeError
}
}
const AUTOSAVE_DEBOUNCE_TIME: u32 = 5000;
#[derive(Serialize, Deserialize)]
struct AutosaveInformation {
contents: String,
Expand All @@ -89,21 +95,19 @@ fn is_basic_editor() -> bool {
}
fn get_title() -> String {
if is_basic_editor() {
return InputElement::try_from(document().get_element_by_id("title").unwrap())
get_elt_value("title")
}else{
let title_field = HtmlElement::try_from(
document()
.query_selector("#plume-editor > h1")
.ok()
.unwrap()
.unwrap(),
)
.ok()
.unwrap()
.raw_value();
.unwrap();
title_field.inner_text()
}
let title_field = HtmlElement::try_from(
document()
.query_selector("#plume-editor > h1")
.ok()
.unwrap()
.unwrap(),
)
.ok()
.unwrap();
title_field.inner_text()
}
fn get_autosave_id() -> String {
format!(
Expand All @@ -113,13 +117,7 @@ fn get_autosave_id() -> String {
}
fn get_editor_contents() -> String {
if is_basic_editor() {
console!(log, "Found basic editor");
let editor =
TextAreaElement::try_from(document().get_element_by_id("editor-content").unwrap())
.ok()
.unwrap();
console!(log, "Found the thing");
editor.value()
get_elt_value("editor-content")
} else {
let editor =
HtmlElement::try_from(document().query_selector("article").ok().unwrap().unwrap())
Expand Down Expand Up @@ -147,12 +145,7 @@ fn get_editor_contents() -> String {
}
fn get_subtitle() -> String {
if is_basic_editor() {
let subtitle: InputElement =
InputElement::try_from(document().get_element_by_id("subtitle").unwrap())
.ok()
.unwrap();
console!(log, "Got subtitle");
subtitle.raw_value()
get_elt_value("subtitle")
} else {
let subtitle_element = HtmlElement::try_from(
document()
Expand All @@ -165,37 +158,15 @@ fn get_subtitle() -> String {
subtitle_element.inner_text()
}
}
fn get_tags() -> String {
let tags: InputElement = InputElement::try_from(document().get_element_by_id("tags").unwrap())
.ok()
.unwrap();
tags.raw_value()
}
fn get_license() -> String {
let license: InputElement =
InputElement::try_from(document().get_element_by_id("license").unwrap())
.ok()
.unwrap();
console!(log, "Got license");
license.raw_value()
}
fn get_cover() -> String {
let cover: SelectElement =
SelectElement::try_from(document().get_element_by_id("cover").unwrap())
.ok()
.unwrap();
console!(log, "Got cover");
cover.raw_value()
}
fn autosave() {
let info: AutosaveInformation = AutosaveInformation {
let info = AutosaveInformation {
contents: get_editor_contents(),
title: get_title(),
subtitle: get_subtitle(),
tags: get_tags(),
license: get_license(),
tags: get_elt_value("tags"),
license: get_elt_value("license"),
last_saved: Date::now(),
cover: get_cover(),
cover: get_elt_value("cover"),
};
let id = get_autosave_id();
match window()
Expand All @@ -208,17 +179,15 @@ fn autosave() {
}
//This is only necessary until we go to stdweb 4.20 at least
fn confirm(message: &str) -> bool {
return js! {return confirm(@{message});} == true;
let result: bool = js! {return confirm(@{message});} == true;
result
}
fn load_autosave() {
if let Some(autosave_str) = window().local_storage().get(&get_autosave_id()) {
let autosave_info: AutosaveInformation = serde_json::from_str(&autosave_str).ok().unwrap();
let message = format!(
"{} {}?",
i18n!(
CATALOG,
"Do you want to load the local autosave last edited at"
),
let message = i18n!(
CATALOG,
"Do you want to load the local autosave last edited at {}?";
Date::from_time(autosave_info.last_saved).to_date_string()
);
if confirm(&message) {
Expand All @@ -228,7 +197,6 @@ fn load_autosave() {
set_value("tags", &autosave_info.tags);
set_value("license", &autosave_info.license);
set_value("cover", &autosave_info.cover);
console!(log, "Loaded autosave.");
} else {
clear_autosave();
}
Expand All @@ -238,13 +206,15 @@ fn clear_autosave() {
window().local_storage().remove(&get_autosave_id());
console!(log, &format!("Saved to {}", &get_autosave_id()));
}
static mut AUTOSAVE_TIMEOUT: Option<TimeoutHandle> = None;

unsafe fn autosave_debounce(interval: u32) {
if let Some(timeout) = AUTOSAVE_TIMEOUT.take() {
lazy_static! {
static ref AUTOSAVE_TIMEOUT: Mutex<Option<TimeoutHandle>> = Mutex::new(None);
}
fn autosave_debounce() {
let ref mut timeout = AUTOSAVE_TIMEOUT.lock().unwrap();
if let Some(timeout) = timeout.take() {
timeout.clear();
}
AUTOSAVE_TIMEOUT = Some(window().set_clearable_timeout(autosave, interval));
**timeout = Some(window().set_clearable_timeout(autosave, AUTOSAVE_DEBOUNCE_TIME));
}
fn init_widget(
parent: &Element,
Expand Down Expand Up @@ -305,9 +275,7 @@ pub fn init() -> Result<(), EditorError> {
document()
.get_element_by_id("editor-content")
.unwrap()
.add_event_listener(|_: KeyDownEvent| unsafe {
autosave_debounce(5000);
});
.add_event_listener(|_: KeyDownEvent| autosave_debounce());
return Ok(());
}
}
Expand Down Expand Up @@ -363,7 +331,7 @@ fn init_editor() -> Result<(), EditorError> {
}).ok();
};
}), 0);
unsafe {autosave_debounce(5000);}
autosave_debounce();
}));

document().get_element_by_id("publish")?.add_event_listener(
Expand Down

0 comments on commit 7bb12d0

Please sign in to comment.