From 930050d9c86590dc8a42572b7444d9ae7a173b82 Mon Sep 17 00:00:00 2001 From: 231tr0n Date: Sun, 3 Mar 2024 22:11:40 +0530 Subject: [PATCH] feat(pager-keybinds): added support for keybinds and smooth transitioning between terminal and pager --- .eslintrc.cjs | 3 +- package.json | 1 + src/lib/components/Terminal.svelte | 81 ++++++++++++++----------- static/images/clipboard-check-dark.svg | 5 ++ static/images/clipboard-check-light.svg | 5 ++ static/images/clipboard-dark.svg | 4 ++ static/images/clipboard-light.svg | 4 ++ 7 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 static/images/clipboard-check-dark.svg create mode 100644 static/images/clipboard-check-light.svg create mode 100644 static/images/clipboard-dark.svg create mode 100644 static/images/clipboard-light.svg diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 3a4962e..ff522e8 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -32,7 +32,8 @@ module.exports = { { ignoreWarnings: true } - ] + ], + 'svelte/no-at-html-tags': ['warn'] } } ] diff --git a/package.json b/package.json index 0894dba..9f68b9e 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "svelte-check": "^3.6.6", "tslib": "^2.6.2", "typescript": "^5.3.3", + "unlighthouse": "^0.11.3", "vite": "^5.1.4", "vitest": "^1.3.1" }, diff --git a/src/lib/components/Terminal.svelte b/src/lib/components/Terminal.svelte index b0242a7..a66b81b 100644 --- a/src/lib/components/Terminal.svelte +++ b/src/lib/components/Terminal.svelte @@ -34,6 +34,7 @@ let fitTermAddon: FitAddon; let resizeObserver: ResizeObserver; let mutationObserver: MutationObserver; + let markDownHtml = 'Loading'; let termThemeDark: ITheme = { brightBlack: '#383a42', black: '#383a42', @@ -159,33 +160,6 @@ ' ' ); }; - // TODO: Get the key listeners working on pager. - let pagerKeyListener = (e: KeyboardEvent) => { - console.log(e.key); - if (e.key == 'q' || e.key == 'Q') { - pagerElem.style.display = 'none'; - termElem.style.display = 'block'; - termElem.focus(); - term.focus(); - return; - } - if (e.key == 'j' || e.key == 'J') { - pagerElem.scrollBy({ - top: 10, - left: 0, - behavior: 'smooth' - }); - return; - } - if (e.key == 'k' || e.key == 'K') { - pagerElem.scrollBy({ - top: -10, - left: 0, - behavior: 'smooth' - }); - return; - } - }; onMount(async () => { marked.use( @@ -247,8 +221,31 @@ fitTermAddon.fit(); - // TODO: Implement all commands and pager element functionality. term.onData(async (e) => { + if (pagerElem.style.display == 'block') { + if (e == 'q' || e == 'Q') { + pagerElem.style.display = 'none'; + markDownHtml = 'Loading'; + return; + } + if (e == 'j' || e == 'J') { + pagerElem.scrollBy({ + top: 50, + left: 0, + behavior: 'smooth' + }); + return; + } + if (e == 'k' || e == 'K') { + pagerElem.scrollBy({ + top: -50, + left: 0, + behavior: 'smooth' + }); + return; + } + return; + } if (e == '\r') { const cmds = typedPrompt.split(' '); let pwdContents = fileSystem; @@ -312,10 +309,10 @@ if (pwdContents[file]) { if (typeof pwdContents[file] === 'string') { pagerElem.style.display = 'block'; - pagerElem.focus(); - termElem.style.display = 'none'; - const data: string = await (await fetch(pwdContents[file] as string)).text(); - pagerElem.innerHTML = dompurify.sanitize(await marked.parse(data)) as string; + markDownHtml = 'Loading'; + markDownHtml = dompurify.sanitize( + await marked.parse(await (await fetch(pwdContents[file] as string)).text()) + ) as string; break; } throw new Error(file + ' is a directory'); @@ -330,9 +327,7 @@ 'pwd: Prints out the present working directory', 'cd: Used to change or navigate through directories', 'clear: Clear terminal screen', - 'less: Pager to view files', - 'find: Used to find files', - 'grep: Used to search for content' + 'less: Pager to view files' ].join('\r\n') ); break; @@ -376,6 +371,7 @@ mutationObserver.observe(document.body, { attributes: true }); + term.focus(); }); onDestroy(() => { @@ -385,8 +381,19 @@ }); -
-
+
term.focus()}>
+
term.focus()} + on:scrollend={() => term.focus()} + on:scroll={() => term.focus()} + on:click={() => term.focus()} + on:dblclick={() => term.focus()} + on:select={() => term.focus()} +> + {@html markDownHtml} +