popover implementation

This commit is contained in:
Jacky Zhao 2022-01-03 13:18:31 -05:00
parent 2b432d7f0b
commit 4a3c4fdef5
9 changed files with 81 additions and 18 deletions

View file

@ -11,9 +11,6 @@
}
</style>
<script>
const index = {{$.Site.Data.linkIndex.index}}
const links = {{$.Site.Data.linkIndex.links}}
const content = {{$.Site.Data.contentIndex}}
const curPage = {{ strings.TrimRight "/" .Page.RelPermalink }}
const pathColors = {{$.Site.Data.graphConfig.paths}}
let depth = {{$.Site.Data.graphConfig.depth}}

View file

@ -1,7 +1,7 @@
<head>
<!-- Meta tags -->
<meta charset="UTF-8">
<meta name="description" content="{{$.Site.Data.config.description}}">
<meta name="description" content="{{if .IsHome}}{{$.Site.Data.config.description}}{{else}}{{.Summary}}{{end}}">
<title>{{ if .Title }}{{ .Title }}{{ else }}{{ $.Site.Data.config.page_title }}{{ end }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href="/icon.png" />
@ -23,5 +23,13 @@
{{.Content | safeJS }}
</script>
{{- end -}}
<!-- Preload page vars -->
<script>
const content = {{$.Site.Data.contentIndex}}
const index = {{$.Site.Data.linkIndex.index}}
const links = {{$.Site.Data.linkIndex.links}}
</script>
</head>
{{ template "_internal/google_analytics.html" . }}
{{ partial "popover.html" .}}

View file

@ -0,0 +1,29 @@
<script>
function htmlToElement(html) {
const template = document.createElement('template')
html = html.trim()
template.innerHTML = html
return template.content.firstChild
}
document.addEventListener("DOMContentLoaded", () => {
[...document.getElementsByClassName("internal-link")]
.forEach(li => {
const linkDest = content[li.dataset.src]
if (linkDest) {
const popoverElement = `<div class="popover">
<h3>${linkDest.title}</h3>
<p>${removeMarkdown(linkDest.content).split(" ", 15).join(" ")}...</p>
</div>`
const el = htmlToElement(popoverElement)
li.appendChild(el)
li.addEventListener("mouseover", () => {
el.classList.add("visible")
})
li.addEventListener("mouseout", () => {
el.classList.remove("visible")
})
}
})
})
</script>

View file

@ -89,8 +89,7 @@
}
})
const scrapedContent = {{$.Site.Data.contentIndex}}
for (const [key, value] of Object.entries(scrapedContent)) {
for (const [key, value] of Object.entries(content)) {
contentIndex.add({
id: key,
title: value.title,
@ -151,8 +150,8 @@
const fetch = id => ({
id,
url: id,
title: scrapedContent[id].title,
content: scrapedContent[id].content
title: content[id].title,
content: content[id].content
})
const source = document.getElementById('search-bar')