From 1504586ab545c65d08e5f874ba041dfe5e02e01c Mon Sep 17 00:00:00 2001 From: omer Date: Wed, 4 Sep 2024 18:08:00 +0300 Subject: [PATCH 1/3] feat: Save the last selected rule. --- Cargo.toml | 1 + src/lib.rs | 12 ++++++++++++ static/scripts/editor.ts | 7 +++++++ 3 files changed, 20 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..52b61f0 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,10 @@ pub fn lint(grammar: JsValue) -> JsValue { #[wasm_bindgen(start)] pub fn start() { + let last_selected = storage().get_item("last-selected-rule").unwrap_throw(); + unsafe { + LAST_SELECTION = last_selected; + } listen_for_input(); } diff --git a/static/scripts/editor.ts b/static/scripts/editor.ts index 5742256..6a638e4 100644 --- a/static/scripts/editor.ts +++ b/static/scripts/editor.ts @@ -17,6 +17,7 @@ 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 +147,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 +192,4 @@ init().then(() => { inputTextDom.addEventListener("input", saveCode); myCodeMirror.on("change", saveCode); +editorInputSelect?.addEventListener("change", saveRule); From 687bf385a93b15c2a3ee5df31d53c2a6a21f8b34 Mon Sep 17 00:00:00 2001 From: omer Date: Wed, 4 Sep 2024 18:21:06 +0300 Subject: [PATCH 2/3] make prettier happy --- static/scripts/editor.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/static/scripts/editor.ts b/static/scripts/editor.ts index 6a638e4..bba106c 100644 --- a/static/scripts/editor.ts +++ b/static/scripts/editor.ts @@ -17,7 +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 editorInputSelect = document.querySelector( + ".editor-input-select", +); const windowHeight = window.innerHeight; From 6bb5419332ed029e720b34d2ba1a6c9252b1a7fb Mon Sep 17 00:00:00 2001 From: omer Date: Thu, 5 Sep 2024 17:53:14 +0300 Subject: [PATCH 3/3] fix: ignore if can't access localStorage. --- src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 52b61f0..dbb0ef8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -329,10 +329,12 @@ pub fn lint(grammar: JsValue) -> JsValue { #[wasm_bindgen(start)] pub fn start() { - let last_selected = storage().get_item("last-selected-rule").unwrap_throw(); - unsafe { - LAST_SELECTION = last_selected; + if let Ok(last_selected) = storage().get_item("last-selected-rule") { + unsafe { + LAST_SELECTION = last_selected; + } } + listen_for_input(); }