From 9da17f6b4e183c46a69829c7fa7163fa4f0a66ae Mon Sep 17 00:00:00 2001 From: Omer Abdulaziz Date: Fri, 6 Sep 2024 03:12:35 +0300 Subject: [PATCH] feat: Save the last selected rule. (#64) --- Cargo.toml | 1 + src/lib.rs | 14 ++++++++++++++ static/scripts/editor.ts | 9 +++++++++ 3 files changed, 24 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 505b6c5..9136b8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,5 +30,6 @@ features = [ "HtmlTextAreaElement", "HtmlCollection", "InputEvent", + "Storage", ] version = "0.3" diff --git a/src/lib.rs b/src/lib.rs index 3a1a0d1..dbb0ef8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,14 @@ fn create_element(tag: &str) -> T { .expect_throw("wrong element type") } +fn storage() -> web_sys::Storage { + web_sys::window() + .unwrap_throw() + .local_storage() + .unwrap_throw() + .unwrap_throw() +} + fn listen_for_input() { let input = element::(".editor-input-text"); @@ -321,6 +329,12 @@ pub fn lint(grammar: JsValue) -> JsValue { #[wasm_bindgen(start)] pub fn start() { + if let Ok(last_selected) = storage().get_item("last-selected-rule") { + unsafe { + LAST_SELECTION = last_selected; + } + } + listen_for_input(); } diff --git a/static/scripts/editor.ts b/static/scripts/editor.ts index 5742256..bba106c 100644 --- a/static/scripts/editor.ts +++ b/static/scripts/editor.ts @@ -17,6 +17,9 @@ const outputDom = document.querySelector(".editor-output")!; const modeBtn = document.querySelector("#modeBtn")!; const formatBtn = document.querySelector("#formatBtn")!; +const editorInputSelect = document.querySelector( + ".editor-input-select", +); const windowHeight = window.innerHeight; @@ -146,6 +149,11 @@ function getSavedCode() { return parsed || { grammar: "", input: "" }; } +function saveRule() { + const selectedRule = editorInputSelect?.value || ""; + localStorage.setItem("last-selected-rule", selectedRule); +} + function wideMode() { modeBtn.onclick = restore; modeBtn.innerText = "Normal Mode"; @@ -186,3 +194,4 @@ init().then(() => { inputTextDom.addEventListener("input", saveCode); myCodeMirror.on("change", saveCode); +editorInputSelect?.addEventListener("change", saveRule);