Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor improvements #486

Merged
merged 14 commits into from
Apr 6, 2019
Merged
73 changes: 71 additions & 2 deletions plume-front/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,30 @@ fn init_widget(
widget.focus();
widget.blur();

filter_paste(&widget);

Ok(widget)
}

fn filter_paste(elt: &HtmlElement) {
// Only insert text when pasting something
js! {
@{&elt}.addEventListener("paste", function (evt) {
evt.preventDefault();
document.execCommand("insertText", false, evt.clipboardData.getData("text"));
});
};
}

pub fn init() -> Result<(), EditorError> {
// Check if the user wants to use the basic editor
if let Some(x) = window().session_storage().get("basic-editor") {
if x == String::from("true") {
window().session_storage().remove("basic-editor");
elegaanz marked this conversation as resolved.
Show resolved Hide resolved
return Ok(()); // And stop here if they don't want the fancy editor
}
}

if let Some(ed) = document().get_element_by_id("plume-editor") {
// Show the editor
js!{ @{&ed}.style.display = "block"; };
Expand All @@ -102,7 +122,7 @@ pub fn init() -> Result<(), EditorError> {
// And pre-fill the new editor with this values
let title = init_widget(&ed, "h1", i18n!(CATALOG, "Title"), title_val, true)?;
let subtitle = init_widget(&ed, "h2", i18n!(CATALOG, "Subtitle or summary"), subtitle_val, true)?;
let content = init_widget(&ed, "article", i18n!(CATALOG, "Write your article here. Markdown is supported."), content_val.clone(), true)?;
let content = init_widget(&ed, "article", i18n!(CATALOG, "Write your article here. Markdown is supported."), content_val.clone(), false)?;
js! { @{&content}.innerHTML = @{content_val}; };

// character counter
Expand All @@ -128,11 +148,35 @@ pub fn init() -> Result<(), EditorError> {

popup.class_list().add("show").unwrap();
bg.class_list().add("show").unwrap();
}));
}));

show_errors();
setup_close_button();
}
Ok(())
}

fn setup_close_button() {
if let Some(button) = document().get_element_by_id("close-editor") {
button.add_event_listener(|_: ClickEvent| {
window().session_storage().insert("basic-editor", "true").unwrap();
window().history().go(0).unwrap(); // Refresh the page
});
}
}

fn show_errors() {
if let Ok(Some(header)) = document().query_selector("header") {
let list = document().create_element("header").unwrap();
list.class_list().add("messages").unwrap();
for error in document().query_selector_all("p.error").unwrap() {
error.parent_element().unwrap().remove_child(&error).unwrap();
list.append_child(&error);
}
header.parent_element().unwrap().insert_before(&list, &header.next_sibling().unwrap()).unwrap();
}
}

fn init_popup(title: &HtmlElement, subtitle: &HtmlElement, content: &HtmlElement, old_ed: &Element) -> Result<Element, EditorError> {
let popup = document().create_element("div")?;
popup.class_list().add("popup")?;
Expand All @@ -151,17 +195,42 @@ fn init_popup(title: &HtmlElement, subtitle: &HtmlElement, content: &HtmlElement
popup.append_child(&cover_label);
popup.append_child(&cover);

if let Some(draft_checkbox) = document().get_element_by_id("draft") {
let draft_label = document().create_element("label")?;
draft_label.set_attribute("for", "popup-draft")?;

let draft = document().create_element("input").unwrap();
js!{
@{&draft}.id = "popup-draft";
@{&draft}.name = "popup-draft";
@{&draft}.type = "checkbox";
@{&draft}.checked = @{&draft_checkbox}.checked;
};

draft_label.append_child(&draft);
draft_label.append_child(&document().create_text_node(&i18n!(CATALOG, "This is a draft")));
popup.append_child(&draft_label);
}

let button = document().create_element("input")?;
js!{
@{&button}.type = "submit";
@{&button}.value = @{i18n!(CATALOG, "Publish")};
};
button.append_child(&document().create_text_node(&i18n!(CATALOG, "Publish")));
button.add_event_listener(mv!(title, subtitle, content, old_ed => move |_: ClickEvent| {
title.focus(); // Remove the placeholder before publishing
set_value("title", title.inner_text());
subtitle.focus();
set_value("subtitle", subtitle.inner_text());
content.focus();
set_value("editor-content", js!{ return @{&content}.innerHTML }.as_str().unwrap_or_default());
set_value("tags", get_elt_value("popup-tags"));
if let Some(draft) = document().get_element_by_id("popup-draft") {
js!{
document.getElementById("draft").checked = @{draft}.checked;
};
}
let cover = document().get_element_by_id("cover").unwrap();
cover.parent_element().unwrap().remove_child(&cover).ok();
old_ed.append_child(&cover);
Expand Down
4 changes: 4 additions & 0 deletions po/plume-front/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ msgstr ""
msgid "Cover"
msgstr ""

# plume-front/src/editor.rs:167
msgid "This is a draft"
msgstr ""

# plume-front/src/editor.rs:111
msgid "Publish"
msgstr ""
4 changes: 4 additions & 0 deletions po/plume-front/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ msgstr ""
msgid "Cover"
msgstr ""

# plume-front/src/editor.rs:167
msgid "This is a draft"
msgstr ""

# plume-front/src/editor.rs:111
msgid "Publish"
msgstr "Publier"
20 changes: 12 additions & 8 deletions po/plume-front/plume-front.pot
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,38 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

# plume-front/src/editor.rs:103
# plume-front/src/editor.rs:123
msgid "Title"
msgstr ""

# plume-front/src/editor.rs:104
# plume-front/src/editor.rs:124
msgid "Subtitle or summary"
msgstr ""

# plume-front/src/editor.rs:105
# plume-front/src/editor.rs:125
msgid "Write your article here. Markdown is supported."
msgstr ""

# plume-front/src/editor.rs:113
# plume-front/src/editor.rs:133
msgid "Around {} characters left"
msgstr ""

# plume-front/src/editor.rs:143
# plume-front/src/editor.rs:187
msgid "Tags"
msgstr ""

# plume-front/src/editor.rs:144
# plume-front/src/editor.rs:188
msgid "License"
msgstr ""

# plume-front/src/editor.rs:147
# plume-front/src/editor.rs:191
msgid "Cover"
msgstr ""

# plume-front/src/editor.rs:157
# plume-front/src/editor.rs:211
msgid "This is a draft"
msgstr ""

# plume-front/src/editor.rs:218
msgid "Publish"
msgstr ""
3 changes: 3 additions & 0 deletions po/plume/ar.po
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ msgstr "اسم المستخدم أو عنوان البريد الالكترون
msgid "Publish"
msgstr "انشر"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "العنوان الثانوي"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ msgstr "Nutzername oder E-Mail"
msgid "Publish"
msgstr "Veröffentlichen"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "Untertitel"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ msgstr ""
msgid "Publish"
msgstr ""

msgid "Classic editor (any changes will be lost)"
msgstr ""

# src/template_utils.rs:144
msgid "Subtitle"
msgstr ""
Expand Down
3 changes: 3 additions & 0 deletions po/plume/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ msgstr "Nombre de usuario o correo electrónico"
msgid "Publish"
msgstr "Publicar"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions po/plume/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ msgstr "Nom d’utilisateur ou adresse électronique"
msgid "Publish"
msgstr "Publier"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "Sous-titre"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/gl.po
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ msgstr "Usuaria ou correo-e"
msgid "Publish"
msgstr "Publicar"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "Subtítulo"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ msgstr "Nome utente o email"
msgid "Publish"
msgstr "Pubblica"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "Sottotitolo"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ msgstr "ユーザー名またはメールアドレス"
msgid "Publish"
msgstr "公開"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "サブタイトル"

Expand Down
3 changes: 3 additions & 0 deletions po/plume/nb.po
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ msgstr "Brukernavn eller epost"
msgid "Publish"
msgstr ""

msgid "Classic editor (any changes will be lost)"
msgstr ""

#, fuzzy
msgid "Subtitle"
msgstr "Tittel"
Expand Down
3 changes: 3 additions & 0 deletions po/plume/pl.po
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ msgstr "Nazwa użytkownika lub adres e-mail"
msgid "Publish"
msgstr "Opublikuj"

msgid "Classic editor (any changes will be lost)"
msgstr ""

msgid "Subtitle"
msgstr "Podtytuł"

Expand Down
Loading