-
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.
- Loading branch information
Showing
7 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
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,71 @@ | ||
import lunr from "lunr"; | ||
|
||
interface MatchData { | ||
metadata: Record< | ||
string, | ||
{ | ||
text: Record<string, string>; | ||
} | ||
>; | ||
} | ||
|
||
function doSearch(l: lunr.Index, query: string, resultEl: HTMLDivElement) { | ||
const results = l.search(query); | ||
|
||
resultEl.innerHTML = results.map((r) => buildResultHTML(r, query)).join("\n"); | ||
} | ||
|
||
function buildResultHTML(result: lunr.Index.Result, query: string): string { | ||
console.log(result.matchData.metadata); | ||
return ` | ||
<div class="DJSearchResult"> | ||
<h1>${result.ref}</h1> | ||
<div>${JSON.stringify( | ||
(result.matchData as MatchData).metadata[query] | ||
)}</div> | ||
</div>`; | ||
} | ||
|
||
window.addEventListener("dj-onload", () => { | ||
const inputEl = document.querySelector( | ||
"#dj-search-input" | ||
) as HTMLInputElement | null; | ||
if (!inputEl) return; | ||
|
||
const resultEl = document.querySelector( | ||
"#dj-search-menu-results" | ||
)! as HTMLDivElement; | ||
if (!resultEl) return; | ||
|
||
const win = window as { djSearchIndex?: { name: string; text: string }[] }; | ||
if (!win.djSearchIndex) { | ||
console.warn("Search index not found"); | ||
return; | ||
} | ||
const searchIndex = win.djSearchIndex; | ||
|
||
const l = lunr(function () { | ||
this.ref("name"); | ||
this.field("text"); | ||
|
||
for (const doc of searchIndex) { | ||
this.add(doc); | ||
} | ||
}); | ||
|
||
(document.querySelector("#dj-search-menu")! as HTMLDivElement).showPopover(); | ||
|
||
// use 'input' for keystrokes, 'change' for enter or unfocus | ||
inputEl.addEventListener("change", (e) => { | ||
console.log(e); | ||
|
||
doSearch(l, (e.target! as HTMLInputElement).value, resultEl); | ||
}); | ||
|
||
( | ||
document.querySelector(".DJOpenSearchButton")! as HTMLButtonElement | ||
).addEventListener("click", () => { | ||
inputEl.focus(); | ||
return true; | ||
}); | ||
}); |
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#dj-search-menu { | ||
border: none; | ||
padding: 0; | ||
background-color: var(--color-bg-1); | ||
color: var(--color-fg-1); | ||
} | ||
|
||
#dj-search-menu > div { | ||
width: 100vw; | ||
height: 100vh; | ||
|
||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: flex-start; | ||
} | ||
|
||
#dj-search-menu::backdrop { | ||
background-color: rgba(0, 0, 0, 0.15); | ||
} | ||
|
||
.DJSearchMenu_InputContainer { | ||
margin-top: calc(min(20vh, 10rem)); | ||
} | ||
|
||
#dj-search-menu h1 { | ||
font-size: var(--fs-normal); | ||
font-weight: var(--fw-bold); | ||
} | ||
|
||
.DJSearchMenu_Results { | ||
overflow-y: auto; | ||
width: 100%; | ||
max-width: var(--page-max-width); | ||
padding: var(--ms); | ||
} | ||
|
||
.DJSearchResult:first-child { | ||
border-top: var(--border-weak); | ||
} | ||
|
||
.DJSearchResult { | ||
border-bottom: var(--border-weak); | ||
padding: var(--ms) 0; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.