update template

This commit is contained in:
2024-08-25 21:35:42 +08:00
parent 6e7e207274
commit 8882726770
15 changed files with 464 additions and 130 deletions
+63 -14
View File
@@ -98,6 +98,7 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
classNames: {
form: "d-flex",
},
placeholder: language["search-text-placeholder"],
translations: {
clearButtonTitle: language["search-clear-button-title"],
detachedCancelButtonText: language["search-detached-cancel-button-title"],
@@ -392,7 +393,12 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
return focusedEl.tagName.toLowerCase() === tag;
});
if (kbds && kbds.includes(key) && !isFormElFocused) {
if (
kbds &&
kbds.includes(key) &&
!isFormElFocused &&
!document.activeElement.isContentEditable
) {
event.preventDefault();
window.quartoOpenSearch();
}
@@ -669,6 +675,18 @@ function showCopyLink(query, options) {
// create the index
var fuseIndex = undefined;
var shownWarning = false;
// fuse index options
const kFuseIndexOptions = {
keys: [
{ name: "title", weight: 20 },
{ name: "section", weight: 20 },
{ name: "text", weight: 10 },
],
ignoreLocation: true,
threshold: 0.1,
};
async function readSearchData() {
// Initialize the search index on demand
if (fuseIndex === undefined) {
@@ -679,17 +697,7 @@ async function readSearchData() {
shownWarning = true;
return;
}
// create fuse index
const options = {
keys: [
{ name: "title", weight: 20 },
{ name: "section", weight: 20 },
{ name: "text", weight: 10 },
],
ignoreLocation: true,
threshold: 0.1,
};
const fuse = new window.Fuse([], options);
const fuse = new window.Fuse([], kFuseIndexOptions);
// fetch the main search.json
const response = await fetch(offsetURL("search.json"));
@@ -1220,8 +1228,34 @@ function algoliaSearch(query, limit, algoliaOptions) {
});
}
function fuseSearch(query, fuse, fuseOptions) {
return fuse.search(query, fuseOptions).map((result) => {
let subSearchTerm = undefined;
let subSearchFuse = undefined;
const kFuseMaxWait = 125;
async function fuseSearch(query, fuse, fuseOptions) {
let index = fuse;
// Fuse.js using the Bitap algorithm for text matching which runs in
// O(nm) time (no matter the structure of the text). In our case this
// means that long search terms mixed with large index gets very slow
//
// This injects a subIndex that will be used once the terms get long enough
// Usually making this subindex is cheap since there will typically be
// a subset of results matching the existing query
if (subSearchFuse !== undefined && query.startsWith(subSearchTerm)) {
// Use the existing subSearchFuse
index = subSearchFuse;
} else if (subSearchFuse !== undefined) {
// The term changed, discard the existing fuse
subSearchFuse = undefined;
subSearchTerm = undefined;
}
// Search using the active fuse
const then = performance.now();
const resultsRaw = await index.search(query, fuseOptions);
const now = performance.now();
const results = resultsRaw.map((result) => {
const addParam = (url, name, value) => {
const anchorParts = url.split("#");
const baseUrl = anchorParts[0];
@@ -1238,4 +1272,19 @@ function fuseSearch(query, fuse, fuseOptions) {
crumbs: result.item.crumbs,
};
});
// If we don't have a subfuse and the query is long enough, go ahead
// and create a subfuse to use for subsequent queries
if (
now - then > kFuseMaxWait &&
subSearchFuse === undefined &&
resultsRaw.length < fuseOptions.limit
) {
subSearchTerm = query;
subSearchFuse = new window.Fuse([], kFuseIndexOptions);
resultsRaw.forEach((rr) => {
subSearchFuse.add(rr.item);
});
}
return results;
}