From ae7f3063564c71a6b87032bf7f5d655330ded3a6 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 5 Sep 2024 00:07:22 +0300 Subject: [PATCH] changed randompage --- quartz/components/_RandomPageButton.tsx | 33 +++++++++++++++ .../components/scripts/_randomPage.inline.ts | 28 +++++++++++++ quartz/components/styles/_randomPage.scss | 41 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 quartz/components/_RandomPageButton.tsx create mode 100644 quartz/components/scripts/_randomPage.inline.ts create mode 100644 quartz/components/styles/_randomPage.scss diff --git a/quartz/components/_RandomPageButton.tsx b/quartz/components/_RandomPageButton.tsx new file mode 100644 index 000000000..5744e1e26 --- /dev/null +++ b/quartz/components/_RandomPageButton.tsx @@ -0,0 +1,33 @@ +import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types" +// @ts-ignore +import script from "./scripts/_randomPage.inline" +import style from "./styles/_randomPage.scss" +import { classNames } from "../util/lang" + +const RandomPageButton: QuartzComponent = ({ displayClass, fileData }: QuartzComponentProps) => { + return ( +
+ {/* */} + + {/*

Go to a random page 🎲

*/} +
+ ) +} +RandomPageButton.css = style +RandomPageButton.afterDOMLoaded = script +export default (() => RandomPageButton) satisfies QuartzComponentConstructor diff --git a/quartz/components/scripts/_randomPage.inline.ts b/quartz/components/scripts/_randomPage.inline.ts new file mode 100644 index 000000000..87bc89989 --- /dev/null +++ b/quartz/components/scripts/_randomPage.inline.ts @@ -0,0 +1,28 @@ +import { FullSlug, getFullSlug, pathToRoot, simplifySlug } from "../../util/path" + +function getRandomInt(max: number) { + return Math.floor(Math.random() * max); + } + +async function navigateToRandomPage() { + const fullSlug = getFullSlug(window) + const data = await fetchData + const allPosts = Object.keys(data).map((slug) => simplifySlug(slug as FullSlug)) + // window.location.href = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}` + let newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; + + if (newSlug === fullSlug) { + // Generate a new random slug until it's different from the starting fullSlug + do { + newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; + } while (newSlug === fullSlug); + } + window.location.href = newSlug; +} + +document.addEventListener("nav", async (e: unknown) => { +// const slug = (e as CustomEventMap["nav"]).detail.url + const button = document.getElementById("random-page-button") + button?.removeEventListener("click", navigateToRandomPage) + button?.addEventListener("click", navigateToRandomPage) +}) diff --git a/quartz/components/styles/_randomPage.scss b/quartz/components/styles/_randomPage.scss new file mode 100644 index 000000000..2c8406add --- /dev/null +++ b/quartz/components/styles/_randomPage.scss @@ -0,0 +1,41 @@ +.random-page { + position: relative; + width: 20px; + height: 20px; + margin-left: auto; + margin: 0 10px; + + &>ul { + list-style: none; + padding: 0; + margin: 0.5rem 0; + } + + > svg { + cursor: pointer; + position: absolute; + width: 20px; + height: 20px; + top: calc(50% - 10px); + } + + &:hover { + opacity: 0.7; /* Decrease opacity on hover */ + cursor: pointer; + } + &>h3 { + font-size: 1rem; + margin: 0; + } + } + + .random-page-ellipse { + stroke: var(--darkgray); + fill: var(--darkgray); + transition: stroke 0.5s ease; + } + + .random-page-square { + stroke: var(--darkgray); + transition: stroke 0.5s ease; + }