Skip to content

Commit

Permalink
feat: Save the last selected rule. (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
omer-biz authored Sep 6, 2024
1 parent cff773b commit 9da17f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ features = [
"HtmlTextAreaElement",
"HtmlCollection",
"InputEvent",
"Storage",
]
version = "0.3"
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ fn create_element<T: JsCast>(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::<Node>(".editor-input-text");

Expand Down Expand Up @@ -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();
}

Expand Down
9 changes: 9 additions & 0 deletions static/scripts/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const outputDom =
document.querySelector<HTMLTextAreaElement>(".editor-output")!;
const modeBtn = document.querySelector<HTMLButtonElement>("#modeBtn")!;
const formatBtn = document.querySelector<HTMLButtonElement>("#formatBtn")!;
const editorInputSelect = document.querySelector<HTMLSelectElement>(
".editor-input-select",
);

const windowHeight = window.innerHeight;

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -186,3 +194,4 @@ init().then(() => {

inputTextDom.addEventListener("input", saveCode);
myCodeMirror.on("change", saveCode);
editorInputSelect?.addEventListener("change", saveRule);

0 comments on commit 9da17f6

Please sign in to comment.