From f6a10af0a81a1b3a83bdce660ed5ee88abcc063a Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 28 Dec 2022 11:35:45 -0500 Subject: first attempt Signed-off-by: Xe Iaso --- src/frontend/build.sh | 1 + src/frontend/stories.tsx | 119 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/frontend/stories.tsx (limited to 'src/frontend') diff --git a/src/frontend/build.sh b/src/frontend/build.sh index 33577ae..81b4485 100755 --- a/src/frontend/build.sh +++ b/src/frontend/build.sh @@ -10,3 +10,4 @@ set -e export RUST_LOG=info denobuild ./mastodon_share_button.tsx ../../static/js/mastodon_share_button.js denobuild ./wasiterm.tsx ../../static/js/wasiterm.js +denobuild ./stories.tsx ../../static/js/stories.js diff --git a/src/frontend/stories.tsx b/src/frontend/stories.tsx new file mode 100644 index 0000000..cb52439 --- /dev/null +++ b/src/frontend/stories.tsx @@ -0,0 +1,119 @@ +import { g, u, x } from "xeact"; + +type Story = { + steps: Step[], +}; + +type Step = { + file: string, + title: string, + text: string, +}; + +const init = async (elemID: string, story: string) => { + let root = g(elemID); + x(root); + let resp = await fetch(u(`/static/${story}.json`)); + if (!resp.ok) { + root.appendChild(
+ Oopsie-whoopsie! +

Oopsie-whoopsie uwu we made a fucky-wucky! A wittle fucko boingo! The code monkeys at our headquarters are working reawy hard to fix this!

+ + Wanted response to be ok, but unexpectedly got: {resp.status} + +
); + return; + } + + let data: Story = await resp.json(); + console.log(data); + + const steps = data.steps.map(s => { + console.log(s); + //
+ const style = `--bg: url(https://cdn.xeiaso.net/file/christine-static/stories/${story}/${s.file}.jpg)`; + console.log(style); + const result =
+
+
+
{s.title}
+
+
+
{s.text}
+
; + console.log(result.style.cssText); + + result.style.cssText = result.style.cssText.replace("\\", ""); + console.log(result.style.cssText); + + return result; + }); + + const stories =
+
+ {steps} +
+
; + + root.appendChild(stories); + + const median = stories.offsetLeft + (stories.clientWidth / 2); + const state = { + current_story: stories?.firstElementChild?.lastElementChild, + }; + + const navigateStories = (direction: "next" | "prev") => { + const story = state.current_story; + const lastItemInUserStory = story?.parentNode?.firstElementChild; + const firstItemInUserStory = story?.parentNode?.lastElementChild; + const hasNextUserStory = story?.parentElement?.nextElementSibling; + const hasPrevUserStory = story?.parentElement?.previousElementSibling; + + if (direction === "next") { + if (lastItemInUserStory === story && !hasNextUserStory) return; + else if (lastItemInUserStory === story && hasNextUserStory) { + state.current_story = + story.parentElement.nextElementSibling.lastElementChild; + story?.parentElement.nextElementSibling.scrollIntoView({ + behavior: "smooth", + }); + } else { + story?.classList.add("seen"); + state.current_story = story?.previousElementSibling; + } + } else if (direction === "prev") { + if (firstItemInUserStory === story && !hasPrevUserStory) return; + else if (firstItemInUserStory === story && hasPrevUserStory) { + state.current_story = + story.parentElement.previousElementSibling.firstElementChild; + story.parentElement.previousElementSibling.scrollIntoView({ + behavior: "smooth", + }); + } else { + story?.nextElementSibling?.classList.remove("seen"); + state.current_story = story?.nextElementSibling; + } + } + + console.log(state.current_story.innerText); + }; + + root.addEventListener("click", (e) => { + if (!(e.target instanceof HTMLElement)) { + return; + } + if (e.target?.nodeName !== "ARTICLE") { + return; + } + + navigateStories(e.clientX > median ? "next" : "prev"); + }); + + document.addEventListener("keydown", ({ key }) => { + if (key === "ArrowDown" || key === "ArrowUp") { + navigateStories(key === "ArrowDown" ? "next" : "prev"); + } + }); +}; + +export { init }; -- cgit v1.2.3