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

feat: Save the last selected rule. #64

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
12 changes: 12 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,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();
omer-biz marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
7 changes: 7 additions & 0 deletions static/scripts/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 +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";
Expand Down Expand Up @@ -186,3 +192,4 @@ init().then(() => {

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