Skip to content

Commit

Permalink
fix indexing causing main thread freeze, various polish
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Jul 4, 2023
1 parent e0ebee5 commit ab9da02
Show file tree
Hide file tree
Showing 33 changed files with 249 additions and 135 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"mdast-util-find-and-replace": "^2.2.2",
"mdast-util-to-string": "^3.2.0",
"micromorph": "^0.4.5",
"plausible-tracker": "^0.3.8",
"preact": "^10.14.1",
"preact-render-to-string": "^6.0.3",
"pretty-time": "^1.1.0",
Expand Down
4 changes: 2 additions & 2 deletions quartz.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const contentPageLayout: PageLayout = {
left: [
Component.PageTitle(),
Component.Search(),
Component.TableOfContents(),
Component.Darkmode()
Component.Darkmode(),
Component.DesktopOnly(Component.TableOfContents()),
],
right: [
Component.Graph(),
Expand Down
20 changes: 20 additions & 0 deletions quartz/components/DesktopOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"

export default ((component?: QuartzComponent) => {
if (component) {
const Component = component
function DesktopOnly(props: QuartzComponentProps) {
return <div class="desktop-only">
<Component {...props} />
</div>
}

DesktopOnly.displayName = component.displayName
DesktopOnly.afterDOMLoaded = component?.afterDOMLoaded
DesktopOnly.beforeDOMLoaded = component?.beforeDOMLoaded
DesktopOnly.css = component?.css
return DesktopOnly
} else {
return () => <></>
}
}) satisfies QuartzComponentConstructor
18 changes: 9 additions & 9 deletions quartz/components/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ const defaultOptions: GraphOptions = {
drag: true,
zoom: true,
depth: 1,
scale: 1.2,
repelForce: 2,
centerForce: 1,
scale: 1.1,
repelForce: 0.5,
centerForce: 0.3,
linkDistance: 30,
fontSize: 0.6,
opacityScale: 3
opacityScale: 1
},
globalGraph: {
drag: true,
zoom: true,
depth: -1,
scale: 1.2,
repelForce: 1,
centerForce: 1,
scale: 0.9,
repelForce: 0.5,
centerForce: 0.3,
linkDistance: 30,
fontSize: 0.5,
opacityScale: 3
fontSize: 0.6,
opacityScale: 1
}
}

Expand Down
4 changes: 2 additions & 2 deletions quartz/components/Head.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { resolveToRoot } from "../path"
import { clientSideSlug, resolveToRoot } from "../path"
import { JSResourceToScriptElement } from "../resources"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"

export default (() => {
function Head({ fileData, externalResources }: QuartzComponentProps) {
const slug = fileData.slug!
const slug = clientSideSlug(fileData.slug!)
const title = fileData.frontmatter?.title ?? "Untitled"
const description = fileData.description ?? "No description provided"
const { css, js } = externalResources
Expand Down
20 changes: 20 additions & 0 deletions quartz/components/MobileOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"

export default ((component?: QuartzComponent) => {
if (component) {
const Component = component
function MobileOnly(props: QuartzComponentProps) {
return <div class="mobile-only">
<Component {...props} />
</div>
}

MobileOnly.displayName = component.displayName
MobileOnly.afterDOMLoaded = component?.afterDOMLoaded
MobileOnly.beforeDOMLoaded = component?.beforeDOMLoaded
MobileOnly.css = component?.css
return MobileOnly
} else {
return () => <></>
}
}) satisfies QuartzComponentConstructor
2 changes: 1 addition & 1 deletion quartz/components/PageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function byDateAndAlphabetical(f1: QuartzPluginData, f2: QuartzPluginData): numb

export function PageList({ fileData, allFiles }: QuartzComponentProps) {
const slug = fileData.slug!
return <ul class="section-ul popover-hint">
return <ul class="section-ul">
{allFiles.sort(byDateAndAlphabetical).map(page => {
const title = page.frontmatter?.title
const pageSlug = page.slug!
Expand Down
3 changes: 1 addition & 2 deletions quartz/components/ReadingTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import readingTime from "reading-time"

function ReadingTime({ fileData }: QuartzComponentProps) {
const text = fileData.text
const isHomePage = fileData.slug === "index"
if (text && !isHomePage) {
if (text) {
const { text: timeTaken, words } = readingTime(text)
return <p class="reading-time">{words} words, {timeTaken}</p>
} else {
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/TableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function TableOfContents({ fileData }: QuartzComponentProps) {
return null
}

return <div>
return <div class="desktop-only">
<button type="button" id="toc">
<h3>Table of Contents</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold">
Expand Down
1 change: 1 addition & 0 deletions quartz/components/TagList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ TagList.css = `
.tags > li {
display: inline-block;
white-space: nowrap;
margin: 0;
overflow-wrap: normal;
}
Expand Down
6 changes: 5 additions & 1 deletion quartz/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import Graph from "./Graph"
import Backlinks from "./Backlinks"
import Search from "./Search"
import Footer from "./Footer"
import DesktopOnly from "./DesktopOnly"
import MobileOnly from "./MobileOnly"

export {
ArticleTitle,
Expand All @@ -29,5 +31,7 @@ export {
Graph,
Backlinks,
Search,
Footer
Footer,
DesktopOnly,
MobileOnly
}
10 changes: 6 additions & 4 deletions quartz/components/pages/FolderContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from "path"
import style from '../styles/listPage.scss'
import { PageList } from "../PageList"

function TagContent(props: QuartzComponentProps) {
function FolderContent(props: QuartzComponentProps) {
const { tree, fileData, allFiles } = props
const folderSlug = fileData.slug!
const allPagesInFolder = allFiles.filter(file => {
Expand All @@ -25,13 +25,15 @@ function TagContent(props: QuartzComponentProps) {

// @ts-ignore
const content = toJsxRuntime(tree, { Fragment, jsx, jsxs, elementAttributeNameCase: 'html' })
return <div>
return <div class="popover-hint">
<article>{content}</article>
<hr/>
<p>{allPagesInFolder.length} items under this folder.</p>
<div>
<PageList {...listProps} />
</div>
</div>
}

TagContent.css = style + PageList.css
export default (() => TagContent) satisfies QuartzComponentConstructor
FolderContent.css = style + PageList.css
export default (() => FolderContent) satisfies QuartzComponentConstructor
9 changes: 6 additions & 3 deletions quartz/components/pages/TagContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Fragment, jsx, jsxs } from 'preact/jsx-runtime'
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import style from '../styles/listPage.scss'
import { PageList } from "../PageList"
import { clientSideSlug } from "../../path"

function TagContent(props: QuartzComponentProps) {
const { tree, fileData, allFiles } = props
const slug = fileData.slug
if (slug?.startsWith("tags/")) {
const tag = slug.slice("tags/".length)

if (slug?.startsWith("tags/")) {
const tag = clientSideSlug(slug.slice("tags/".length))
const allPagesWithTag = allFiles.filter(file => (file.frontmatter?.tags ?? []).includes(tag))
const listProps = {
...props,
Expand All @@ -18,8 +19,10 @@ function TagContent(props: QuartzComponentProps) {

// @ts-ignore
const content = toJsxRuntime(tree, { Fragment, jsx, jsxs, elementAttributeNameCase: 'html' })
return <div>
return <div class="popover-hint">
<article>{content}</article>
<hr/>
<p>{allPagesWithTag.length} items with this tag.</p>
<div>
<PageList {...listProps} />
</div>
Expand Down
2 changes: 1 addition & 1 deletion quartz/components/renderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function pageResources(slug: string, staticResources: StaticResources): S
css: [baseDir + "/index.css", ...staticResources.css],
js: [
{ src: baseDir + "/prescript.js", loadTime: "beforeDOMReady", contentType: "external" },
{ loadTime: "afterDOMReady", contentType: "inline", spaPreserve: true, script: contentIndexScript },
{ loadTime: "beforeDOMReady", contentType: "inline", spaPreserve: true, script: contentIndexScript },
...staticResources.js,
{ src: baseDir + "/postscript.js", loadTime: "afterDOMReady", moduleType: 'module', contentType: "external" }
]
Expand Down
18 changes: 11 additions & 7 deletions quartz/components/scripts/graph.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ async function renderGraph(container: string, slug: string) {
.join("line")
.attr("class", "link")
.attr("stroke", "var(--lightgray)")
.attr("stroke-width", 2)
.attr("stroke-width", 1)

// svg groups
const graphNode = svg.append("g").selectAll("g").data(graphData.nodes).enter().append("g")

// calculate radius
// calculate color
const color = (d: NodeData) => {
const isCurrent = d.id === slug
if (isCurrent) {
Expand Down Expand Up @@ -182,7 +182,12 @@ async function renderGraph(container: string, slug: string) {
neighbourNodes.transition().duration(200).attr("fill", color)

// highlight links
linkNodes.transition().duration(200).attr("stroke", "var(--gray)")
linkNodes
.transition()
.duration(200)
.attr("stroke", "var(--gray)")
.attr("stroke-width", 1)


const bigFont = fontSize * 1.5

Expand Down Expand Up @@ -220,7 +225,7 @@ async function renderGraph(container: string, slug: string) {
const labels = graphNode
.append("text")
.attr("dx", 0)
.attr("dy", (d) => nodeRadius(d) + 8 + "px")
.attr("dy", (d) => nodeRadius(d) - 8 + "px")
.attr("text-anchor", "middle")
.text((d) => data[d.id]?.title || (d.id.charAt(1).toUpperCase() + d.id.slice(2)).replace("-", " "))
.style('opacity', (opacityScale - 1) / 3.75)
Expand Down Expand Up @@ -266,12 +271,11 @@ async function renderGraph(container: string, slug: string) {
})
}

async function renderGlobalGraph() {
function renderGlobalGraph() {
const slug = document.body.dataset["slug"]!
await renderGraph("global-graph-container", slug)
const container = document.getElementById("global-graph-outer")
container?.classList.add("active")

renderGraph("global-graph-container", slug)

function hideGlobalGraph() {
container?.classList.remove("active")
Expand Down
Loading

0 comments on commit ab9da02

Please sign in to comment.