Skip to content

Commit

Permalink
Merge pull request #3 from LyonSyonII/dev
Browse files Browse the repository at this point in the history
Improve editing process with tab and backspace
  • Loading branch information
LyonSyonII authored Jun 16, 2024
2 parents a901592 + 4f58a91 commit 0615ba6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
27 changes: 27 additions & 0 deletions example/src/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function setupEditor(input: HTMLTextAreaElement) {
input.addEventListener("keydown", (e) => {
switch (e.key) {
case "Tab": {
e.preventDefault();
e.stopPropagation();
const [start, end] = [input.selectionStart, input.selectionEnd];
input.setRangeText(" ", start, end, "end");
break;
}
case "Backspace": {
let [start, end] = [input.selectionStart, input.selectionEnd];
if (start !== end) {
return;
}
e.preventDefault();
e.stopPropagation();
if (input.value.substring(start-2, start+1) === " ") {
start -= 2;
} else {
start -= 1;
}
start >= 0 && input.setRangeText("", start, end, "end");
}
}
})
}
2 changes: 0 additions & 2 deletions example/src/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ async function cached_or_fetch(path: string) {
// Downloads or caches the file from `path`
// Files of more than 10MB aren't cached by fetch, so it must be done manually
const base = import.meta.env.BASE_URL === "/" ? "" : import.meta.env.BASE_URL;
console.log({base, url: import.meta.url, path});

path = base + path;
try {
caches
Expand Down
5 changes: 4 additions & 1 deletion example/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const base = import.meta.env.BASE_URL === "/" ? "" : import.meta.env.BASE_URL;

<script>
import { Interpreter } from "../interpreter";
import { setupEditor } from "../editor";
import { AnsiUp } from "ansi_up";

const input = document.body.querySelector<HTMLTextAreaElement>("textarea#input")!;
Expand Down Expand Up @@ -105,7 +106,9 @@ const base = import.meta.env.BASE_URL === "/" ? "" : import.meta.env.BASE_URL;
input.dispatchEvent(new Event("input"));
}
});


setupEditor(input);

// Run the Interpreter when on "Live Edit" or the "Run" button is clicked
input.addEventListener("input", async () => {
if (!liveEdit.checked || button.disabled) {
Expand Down

0 comments on commit 0615ba6

Please sign in to comment.