-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from tjheffner/lazyload
Update lazyload, add TOC
- Loading branch information
Showing
30 changed files
with
169 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<script> | ||
import { onMount } from "svelte"; | ||
export let tocStore; | ||
let isOpen = false; | ||
onMount(() => { | ||
// set isOpen if window width is mobile checking the media query | ||
if (window.matchMedia("(min-width: 640px)").matches) { | ||
isOpen = true; | ||
} | ||
}) | ||
console.log($tocStore.items) | ||
</script> | ||
|
||
<!-- Table of contents thing --> | ||
{#if Object.values($tocStore.items).length && Object.values($tocStore.items).length > 1} | ||
<section | ||
class="sticky max-w-[12em] top-10 h-0 z-10" | ||
> | ||
<button | ||
class="text-accent font-bold -ml-1 lg:ml-7" | ||
aria-label="{isOpen ? 'Close' : 'Open'} Table of Contents" | ||
on:click={() => (isOpen = !isOpen)} | ||
> | ||
{isOpen ? '<' : '>'} | ||
</button> | ||
|
||
<!-- adjust margins to slide in/out, works with sticky --> | ||
<!-- need to adjust left margin to slide text, right margin to slide bg --> | ||
<ul class="toc-list max-h-fit {isOpen ? '-ml-6 mr-0 lg:ml-2' : '-ml-96 mr-96'}"> | ||
{#each Object.values($tocStore.items) as { id, text, element }} | ||
<li> | ||
<a | ||
class="ml-2 block text-sm" | ||
class:toc-active={$tocStore.activeItem?.id === id} | ||
class:pl-2={element.nodeName === 'H2' || element.nodeName === 'H1'} | ||
class:pl-4={element.nodeName === 'H3'} | ||
href="#{id}" | ||
> | ||
{text} | ||
</a> | ||
</li> | ||
{/each} | ||
</ul> | ||
</section> | ||
{/if} | ||
|
||
<style> | ||
.toc-list { | ||
@apply text-secondary py-5 px-8 -mt-10 space-y-1 overflow-auto transition-all duration-500; | ||
@apply bg-orange-100 dark:bg-slate-900; | ||
} | ||
.toc-active { | ||
@apply text-accent font-bold border-s-2 border-current; | ||
/* margin-left:; */ | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,55 @@ | ||
|
||
// this is an action that loads utterances comments when the element is in view | ||
// so as not to incur the JS cost upfront if the person never scrolls down to the comments | ||
|
||
// https://svelte.dev/repl/c6a402704224403f96a3db56c2f48dfc?version=3.55.1 | ||
|
||
import { GH_USER_REPO } from "$lib/siteConfig"; | ||
import { GH_USER_REPO } from '$lib/siteConfig' | ||
|
||
/** @type any */ | ||
let intersectionObserver; | ||
let intersectionObserver | ||
|
||
let hasLoaded = false | ||
|
||
export function injectScript(element, number) { | ||
// have to do this because direct injection using @html doesnt work | ||
// adapted from https://github.com/utterance/utterances/issues/161#issuecomment-550991248 | ||
const scriptElem = document.createElement("script"); | ||
scriptElem.src = "https://utteranc.es/client.js"; | ||
scriptElem.async = true; | ||
scriptElem.crossOrigin = "anonymous"; | ||
scriptElem.setAttribute("repo", GH_USER_REPO); | ||
scriptElem.setAttribute("issue-number", number); | ||
const theme = document.documentElement.classList.contains('dark') ? 'github-dark-orange' : 'boxy-light'; | ||
scriptElem.setAttribute("theme", theme); | ||
const scriptElem = document.createElement('script') | ||
scriptElem.src = 'https://utteranc.es/client.js' | ||
scriptElem.async = true | ||
scriptElem.crossOrigin = 'anonymous' | ||
scriptElem.setAttribute('repo', GH_USER_REPO) | ||
scriptElem.setAttribute('issue-number', number) | ||
const theme = document.documentElement.classList.contains('dark') | ||
? 'github-dark-orange' | ||
: 'boxy-light' | ||
scriptElem.setAttribute('theme', theme) | ||
|
||
// replace all contents of element and append script | ||
element.innerHTML = ""; | ||
element.appendChild(scriptElem); | ||
element.innerHTML = '' | ||
element.appendChild(scriptElem) | ||
} | ||
|
||
export default function viewport(element, { number }) { | ||
|
||
function ensureIntersectionObserver() { | ||
if (intersectionObserver) return; | ||
|
||
intersectionObserver = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach(entry => { | ||
if (!hasLoaded && entry.isIntersecting) { | ||
injectScript(element, number); | ||
hasLoaded = true | ||
} | ||
}); | ||
} | ||
); | ||
if (intersectionObserver) return | ||
|
||
intersectionObserver = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
if (!hasLoaded && entry.isIntersecting) { | ||
injectScript(element, number) | ||
hasLoaded = true | ||
} | ||
}) | ||
}) | ||
} | ||
ensureIntersectionObserver(); | ||
ensureIntersectionObserver() | ||
|
||
intersectionObserver.observe(element); | ||
intersectionObserver.observe(element) | ||
|
||
return { | ||
destroy() { | ||
hasLoaded = false | ||
intersectionObserver.unobserve(element); | ||
} | ||
intersectionObserver.unobserve(element) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// test that a route doesn't end in .xml | ||
// so [slug] can greedy match everything except .xml routes | ||
export function match(value) { | ||
return /^(?!.*[.]xml$).*$/.test(value) | ||
return /^(?!.*[.]xml$).*$/.test(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.