@farming-labs/theme 0.2.134 → 0.2.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/command-search-dialog.mjs +701 -0
- package/dist/deferred-dialog.mjs +27 -12
- package/dist/docs-command-search.d.mts +2 -8
- package/dist/docs-command-search.mjs +17 -688
- package/package.json +4 -3
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { emitClientAnalyticsEvent } from "./client-analytics.mjs";
|
|
4
|
+
import { useWindowSearchParams } from "./client-location.mjs";
|
|
5
|
+
import { resolveClientLocale, withLangInUrl } from "./i18n.mjs";
|
|
6
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
7
|
+
import { createPortal } from "react-dom";
|
|
8
|
+
import { useRouter } from "fumadocs-core/framework";
|
|
9
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
|
|
11
|
+
//#region src/command-search-dialog.tsx
|
|
12
|
+
function cn(...classes) {
|
|
13
|
+
return classes.filter(Boolean).join(" ");
|
|
14
|
+
}
|
|
15
|
+
const BREADCRUMB_SEPARATOR = "\xA0\xA0>\xA0\xA0";
|
|
16
|
+
const FILTER_LABELS = {
|
|
17
|
+
all: "All",
|
|
18
|
+
pages: "Pages",
|
|
19
|
+
inside: "Inside pages"
|
|
20
|
+
};
|
|
21
|
+
function stripHtml(html) {
|
|
22
|
+
if (typeof document !== "undefined") {
|
|
23
|
+
const el = document.createElement("div");
|
|
24
|
+
el.innerHTML = html;
|
|
25
|
+
return el.textContent || el.innerText || "";
|
|
26
|
+
}
|
|
27
|
+
return html.replace(/<[^>]+>/g, "");
|
|
28
|
+
}
|
|
29
|
+
function stripSearchPreview(text) {
|
|
30
|
+
return text.replace(/```[\s\S]*?```/g, "").replace(/~~~[\s\S]*?~~~/g, "").replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^#{1,6}\s+/gm, "").replace(/^\|?[\s:-]+(\|[\s:-]+)+\|?\s*$/gm, "").replace(/\|/g, " ").replace(/^[-*+]\s+/gm, "").replace(/(\*{1,3}|_{1,3})(.*?)\1/g, "$2").replace(/`([^`]+)`/g, "$1").replace(/`+/g, "").replace(/\s{2,}/g, " ").trim();
|
|
31
|
+
}
|
|
32
|
+
function breadcrumbForUrl(url) {
|
|
33
|
+
try {
|
|
34
|
+
const parts = new URL(url, "https://docs.local").pathname.split("/").filter(Boolean).map((part) => decodeURIComponent(part).replace(/[-_]+/g, " ").replace(/\b\w/g, (char) => char.toUpperCase()));
|
|
35
|
+
return parts.length > 0 ? parts.join(BREADCRUMB_SEPARATOR) : "Docs";
|
|
36
|
+
} catch {
|
|
37
|
+
return "Docs";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function normalizeSearchPhrase(value) {
|
|
41
|
+
return value.toLowerCase().replace(/[?!.,;:]+$/g, "").replace(/\s+/g, " ").trim();
|
|
42
|
+
}
|
|
43
|
+
function escapeRegExp(value) {
|
|
44
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
45
|
+
}
|
|
46
|
+
function literalMatchPriority(query, value) {
|
|
47
|
+
const q = normalizeSearchPhrase(query);
|
|
48
|
+
const text = normalizeSearchPhrase(value ?? "");
|
|
49
|
+
if (!q || !text) return 0;
|
|
50
|
+
if (text === q) return 2;
|
|
51
|
+
const boundary = "[^\\p{L}\\p{N}]";
|
|
52
|
+
return new RegExp(`(^|${boundary})${escapeRegExp(q)}(?=$|${boundary})`, "u").test(text) ? 1 : 0;
|
|
53
|
+
}
|
|
54
|
+
function tokenizeLiteralQuery(query) {
|
|
55
|
+
return Array.from(new Set(query.toLowerCase().replace(/[^\p{L}\p{N}@/_:.-]+/gu, " ").split(/\s+/).map((word) => word.replace(/^[^\p{L}\p{N}@]+|[^\p{L}\p{N}]+$/gu, "")).filter((word) => word.length > 1)));
|
|
56
|
+
}
|
|
57
|
+
function isLiteralLookupQuery(query) {
|
|
58
|
+
const q = normalizeSearchPhrase(query);
|
|
59
|
+
const words = tokenizeLiteralQuery(q);
|
|
60
|
+
return words.length > 0 && words.length <= 3 && words.join(" ") === q;
|
|
61
|
+
}
|
|
62
|
+
function hasDistinctSearchSection(result, label) {
|
|
63
|
+
if (result.type === "page") return false;
|
|
64
|
+
if (!result.section) return true;
|
|
65
|
+
const title = label.split(/\s+[—–]\s+/)[0] ?? "";
|
|
66
|
+
return normalizeSearchPhrase(result.section) !== normalizeSearchPhrase(title);
|
|
67
|
+
}
|
|
68
|
+
function getUrlSearchSegments(url) {
|
|
69
|
+
try {
|
|
70
|
+
const parsed = new URL(url, "https://docs.local");
|
|
71
|
+
return Array.from(new Set(parsed.pathname.split("/").flatMap((segment) => {
|
|
72
|
+
const decoded = decodeURIComponent(segment);
|
|
73
|
+
return [decoded, decoded.replace(/[-_]+/g, " ")];
|
|
74
|
+
}).map(normalizeSearchPhrase).filter(Boolean)));
|
|
75
|
+
} catch {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function exactMatchPriority(query, label, url) {
|
|
80
|
+
const q = normalizeSearchPhrase(query);
|
|
81
|
+
if (!q) return 0;
|
|
82
|
+
const normalizedLabel = normalizeSearchPhrase(label);
|
|
83
|
+
const joinedLabel = normalizeSearchPhrase(label.replace(/\s+[—–-]\s+/g, " "));
|
|
84
|
+
const labelParts = normalizedLabel.split(/\s+[—–-]\s+/).map(normalizeSearchPhrase).filter(Boolean);
|
|
85
|
+
if (normalizedLabel === q || joinedLabel === q) return 4;
|
|
86
|
+
if (labelParts.includes(q)) return 3;
|
|
87
|
+
if (getUrlSearchSegments(url).includes(q)) return 2;
|
|
88
|
+
if (normalizedLabel.startsWith(q) || joinedLabel.startsWith(q)) return 1;
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
function resultDisplayLabel(result) {
|
|
92
|
+
const section = result.section ? stripSearchPreview(stripHtml(result.section)) : "";
|
|
93
|
+
if (section) return section;
|
|
94
|
+
const label = stripSearchPreview(stripHtml(result.content));
|
|
95
|
+
const parts = label.split(/\s+[—–]\s+/).map((part) => part.trim()).filter(Boolean);
|
|
96
|
+
if (result.type === "heading" && parts.length > 1) return parts[parts.length - 1] ?? label;
|
|
97
|
+
return label;
|
|
98
|
+
}
|
|
99
|
+
function fuzzyScore(query, text) {
|
|
100
|
+
const q = query.trim().toLowerCase();
|
|
101
|
+
const t = text.toLowerCase();
|
|
102
|
+
if (!q) return {
|
|
103
|
+
score: 0,
|
|
104
|
+
indices: []
|
|
105
|
+
};
|
|
106
|
+
let score = 0;
|
|
107
|
+
const indices = [];
|
|
108
|
+
const idx = t.indexOf(q);
|
|
109
|
+
if (idx >= 0) {
|
|
110
|
+
score += 100 + Math.max(0, 20 - idx);
|
|
111
|
+
for (let i = 0; i < q.length; i++) indices.push(idx + i);
|
|
112
|
+
} else {
|
|
113
|
+
let tPos = 0;
|
|
114
|
+
let chain = 0;
|
|
115
|
+
for (let i = 0; i < q.length; i++) {
|
|
116
|
+
const found = t.indexOf(q[i], tPos);
|
|
117
|
+
if (found === -1) score -= 5;
|
|
118
|
+
else {
|
|
119
|
+
indices.push(found);
|
|
120
|
+
if (found === tPos) chain += 2;
|
|
121
|
+
else chain = 0;
|
|
122
|
+
score += 2 + chain;
|
|
123
|
+
tPos = found + 1;
|
|
124
|
+
if (found === 0 || /\s|-|_|\/|\./.test(text[found - 1])) score += 3;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
score,
|
|
130
|
+
indices: Array.from(new Set(indices)).sort((a, b) => a - b)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function HighlightedLabel({ label, indices = [] }) {
|
|
134
|
+
if (!indices.length) return /* @__PURE__ */ jsx(Fragment$1, { children: label });
|
|
135
|
+
const out = [];
|
|
136
|
+
const highlighted = new Set(indices);
|
|
137
|
+
let pos = 0;
|
|
138
|
+
while (pos < label.length) {
|
|
139
|
+
const marked = highlighted.has(pos);
|
|
140
|
+
let end = pos + 1;
|
|
141
|
+
while (end < label.length && highlighted.has(end) === marked) end++;
|
|
142
|
+
const run = label.slice(pos, end);
|
|
143
|
+
if (marked) out.push(/* @__PURE__ */ jsx("mark", {
|
|
144
|
+
className: "omni-highlight",
|
|
145
|
+
children: run
|
|
146
|
+
}, `m-${pos}`));
|
|
147
|
+
else out.push(/* @__PURE__ */ jsx("span", { children: run }, `t-${pos}`));
|
|
148
|
+
pos = end;
|
|
149
|
+
}
|
|
150
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: out });
|
|
151
|
+
}
|
|
152
|
+
function SearchIcon() {
|
|
153
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
154
|
+
width: "16",
|
|
155
|
+
height: "16",
|
|
156
|
+
viewBox: "0 0 24 24",
|
|
157
|
+
fill: "none",
|
|
158
|
+
stroke: "currentColor",
|
|
159
|
+
strokeWidth: "2",
|
|
160
|
+
strokeLinecap: "round",
|
|
161
|
+
strokeLinejoin: "round",
|
|
162
|
+
children: [/* @__PURE__ */ jsx("circle", {
|
|
163
|
+
cx: "11",
|
|
164
|
+
cy: "11",
|
|
165
|
+
r: "8"
|
|
166
|
+
}), /* @__PURE__ */ jsx("path", { d: "m21 21-4.3-4.3" })]
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function ArrowDownIcon() {
|
|
170
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
171
|
+
width: "12",
|
|
172
|
+
height: "12",
|
|
173
|
+
viewBox: "0 0 24 24",
|
|
174
|
+
fill: "none",
|
|
175
|
+
stroke: "currentColor",
|
|
176
|
+
strokeWidth: "2",
|
|
177
|
+
strokeLinecap: "round",
|
|
178
|
+
strokeLinejoin: "round",
|
|
179
|
+
children: /* @__PURE__ */ jsx("path", { d: "m6 9 6 6 6-6" })
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function ArrowUpIcon() {
|
|
183
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
184
|
+
width: "12",
|
|
185
|
+
height: "12",
|
|
186
|
+
viewBox: "0 0 24 24",
|
|
187
|
+
fill: "none",
|
|
188
|
+
stroke: "currentColor",
|
|
189
|
+
strokeWidth: "2",
|
|
190
|
+
strokeLinecap: "round",
|
|
191
|
+
strokeLinejoin: "round",
|
|
192
|
+
children: /* @__PURE__ */ jsx("path", { d: "m18 15-6-6-6 6" })
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function CornerDownLeftIcon() {
|
|
196
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
197
|
+
width: "12",
|
|
198
|
+
height: "12",
|
|
199
|
+
viewBox: "0 0 24 24",
|
|
200
|
+
fill: "none",
|
|
201
|
+
stroke: "currentColor",
|
|
202
|
+
strokeWidth: "2",
|
|
203
|
+
strokeLinecap: "round",
|
|
204
|
+
strokeLinejoin: "round",
|
|
205
|
+
children: [/* @__PURE__ */ jsx("path", { d: "m9 10-5 5 5 5" }), /* @__PURE__ */ jsx("path", { d: "M20 4v7a4 4 0 0 1-4 4H4" })]
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
function LoaderIcon() {
|
|
209
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
210
|
+
width: "12",
|
|
211
|
+
height: "12",
|
|
212
|
+
viewBox: "0 0 24 24",
|
|
213
|
+
fill: "none",
|
|
214
|
+
stroke: "currentColor",
|
|
215
|
+
strokeWidth: "2",
|
|
216
|
+
strokeLinecap: "round",
|
|
217
|
+
strokeLinejoin: "round",
|
|
218
|
+
className: "omni-spin",
|
|
219
|
+
children: /* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function HistoryIcon() {
|
|
223
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
224
|
+
width: "16",
|
|
225
|
+
height: "16",
|
|
226
|
+
viewBox: "0 0 24 24",
|
|
227
|
+
fill: "none",
|
|
228
|
+
stroke: "currentColor",
|
|
229
|
+
strokeWidth: "2",
|
|
230
|
+
strokeLinecap: "round",
|
|
231
|
+
strokeLinejoin: "round",
|
|
232
|
+
children: [
|
|
233
|
+
/* @__PURE__ */ jsx("path", { d: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" }),
|
|
234
|
+
/* @__PURE__ */ jsx("path", { d: "M3 3v5h5" }),
|
|
235
|
+
/* @__PURE__ */ jsx("path", { d: "M12 7v5l4 2" })
|
|
236
|
+
]
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Built-in docs search command palette.
|
|
241
|
+
* Intercepts Cmd+K and sidebar search button to provide an advanced
|
|
242
|
+
* fuzzy-search experience. Styled entirely via omni-* CSS classes
|
|
243
|
+
* so each theme provides its own visual variant.
|
|
244
|
+
*/
|
|
245
|
+
function CommandSearchDialog({ api = "/api/docs", locale, analytics = false, open, setOpen, setOpenWithAnalytics }) {
|
|
246
|
+
const router = useRouter();
|
|
247
|
+
const [query, setQuery] = useState("");
|
|
248
|
+
const [debouncedQuery, setDebouncedQuery] = useState("");
|
|
249
|
+
const [results, setResults] = useState([]);
|
|
250
|
+
const [loading, setLoading] = useState(false);
|
|
251
|
+
const [activeIndex, setActiveIndex] = useState(0);
|
|
252
|
+
const [recents, setRecents] = useState([]);
|
|
253
|
+
const [filter, setFilter] = useState("all");
|
|
254
|
+
const [filterOpen, setFilterOpen] = useState(false);
|
|
255
|
+
const [mounted, setMounted] = useState(false);
|
|
256
|
+
const activeLocale = resolveClientLocale(useWindowSearchParams(), locale);
|
|
257
|
+
const searchApi = useMemo(() => withLangInUrl(api, activeLocale), [activeLocale, api]);
|
|
258
|
+
const inputRef = useRef(null);
|
|
259
|
+
const listRef = useRef(null);
|
|
260
|
+
const restoreFocusRef = useRef(null);
|
|
261
|
+
const searchCacheRef = useRef(/* @__PURE__ */ new Map());
|
|
262
|
+
useEffect(() => {
|
|
263
|
+
setMounted(true);
|
|
264
|
+
try {
|
|
265
|
+
const raw = localStorage.getItem("fd:omni:recents");
|
|
266
|
+
if (raw) setRecents(JSON.parse(raw));
|
|
267
|
+
} catch {}
|
|
268
|
+
}, []);
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
const id = setTimeout(() => setDebouncedQuery(query), 150);
|
|
271
|
+
return () => clearTimeout(id);
|
|
272
|
+
}, [query]);
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
if (!debouncedQuery.trim()) {
|
|
275
|
+
setLoading(false);
|
|
276
|
+
setResults([]);
|
|
277
|
+
setActiveIndex(0);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
let cancelled = false;
|
|
281
|
+
const controller = new AbortController();
|
|
282
|
+
const cacheKey = `${activeLocale ?? ""}:${searchApi}:${debouncedQuery}`;
|
|
283
|
+
const cached = searchCacheRef.current.get(cacheKey);
|
|
284
|
+
if (cached) {
|
|
285
|
+
setResults(cached);
|
|
286
|
+
setActiveIndex(0);
|
|
287
|
+
setLoading(false);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
setLoading(true);
|
|
291
|
+
(async () => {
|
|
292
|
+
const startedAt = Date.now();
|
|
293
|
+
try {
|
|
294
|
+
const requestUrl = new URL(searchApi, window.location.origin);
|
|
295
|
+
requestUrl.searchParams.set("query", debouncedQuery);
|
|
296
|
+
const res = await fetch(requestUrl.toString(), { signal: controller.signal });
|
|
297
|
+
if (!res.ok || cancelled) return;
|
|
298
|
+
const items = (await res.json()).map((r, sourceIndex) => {
|
|
299
|
+
const sourceLabel = stripSearchPreview(stripHtml(r.content));
|
|
300
|
+
const label = resultDisplayLabel(r);
|
|
301
|
+
const description = r.description ? stripSearchPreview(stripHtml(r.description)).slice(0, 220) : void 0;
|
|
302
|
+
const { score, indices } = fuzzyScore(debouncedQuery, label);
|
|
303
|
+
const descriptionMatch = description ? fuzzyScore(debouncedQuery, description) : { indices: [] };
|
|
304
|
+
const url = withLangInUrl(r.url, activeLocale);
|
|
305
|
+
return {
|
|
306
|
+
id: r.id,
|
|
307
|
+
label,
|
|
308
|
+
subtitle: breadcrumbForUrl(r.url),
|
|
309
|
+
description,
|
|
310
|
+
url,
|
|
311
|
+
type: r.type,
|
|
312
|
+
score,
|
|
313
|
+
exactPriority: exactMatchPriority(debouncedQuery, sourceLabel, r.url),
|
|
314
|
+
insideLiteralPriority: hasDistinctSearchSection(r, sourceLabel) && isLiteralLookupQuery(debouncedQuery) ? Math.max(literalMatchPriority(debouncedQuery, r.section), literalMatchPriority(debouncedQuery, description)) : 0,
|
|
315
|
+
sourceIndex,
|
|
316
|
+
indices,
|
|
317
|
+
descriptionIndices: descriptionMatch.indices
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
items.sort((a, b) => {
|
|
321
|
+
const literalDelta = b.insideLiteralPriority - a.insideLiteralPriority;
|
|
322
|
+
if (literalDelta) return literalDelta;
|
|
323
|
+
if (a.insideLiteralPriority > 0 && b.insideLiteralPriority > 0) return a.sourceIndex - b.sourceIndex;
|
|
324
|
+
return b.exactPriority - a.exactPriority || b.score - a.score || a.sourceIndex - b.sourceIndex || a.label.localeCompare(b.label);
|
|
325
|
+
});
|
|
326
|
+
if (!cancelled) {
|
|
327
|
+
if (searchCacheRef.current.size >= 20) {
|
|
328
|
+
const firstKey = searchCacheRef.current.keys().next().value;
|
|
329
|
+
if (firstKey) searchCacheRef.current.delete(firstKey);
|
|
330
|
+
}
|
|
331
|
+
searchCacheRef.current.set(cacheKey, items);
|
|
332
|
+
setResults(items);
|
|
333
|
+
setActiveIndex(0);
|
|
334
|
+
if (analytics) emitClientAnalyticsEvent({
|
|
335
|
+
type: "search_query",
|
|
336
|
+
locale: activeLocale,
|
|
337
|
+
properties: {
|
|
338
|
+
mode: "command",
|
|
339
|
+
queryLength: debouncedQuery.length,
|
|
340
|
+
resultCount: items.length,
|
|
341
|
+
durationMs: Math.max(0, Date.now() - startedAt)
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
} catch {
|
|
346
|
+
if (controller.signal.aborted) return;
|
|
347
|
+
if (!cancelled && analytics) emitClientAnalyticsEvent({
|
|
348
|
+
type: "search_error",
|
|
349
|
+
locale: activeLocale,
|
|
350
|
+
properties: {
|
|
351
|
+
mode: "command",
|
|
352
|
+
queryLength: debouncedQuery.length,
|
|
353
|
+
durationMs: Math.max(0, Date.now() - startedAt)
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
if (!cancelled) setLoading(false);
|
|
358
|
+
})();
|
|
359
|
+
return () => {
|
|
360
|
+
cancelled = true;
|
|
361
|
+
controller.abort();
|
|
362
|
+
};
|
|
363
|
+
}, [
|
|
364
|
+
activeLocale,
|
|
365
|
+
analytics,
|
|
366
|
+
debouncedQuery,
|
|
367
|
+
searchApi
|
|
368
|
+
]);
|
|
369
|
+
useEffect(() => {
|
|
370
|
+
if (open) {
|
|
371
|
+
const activeElement = document.activeElement;
|
|
372
|
+
restoreFocusRef.current = activeElement instanceof HTMLElement ? activeElement : null;
|
|
373
|
+
const focusTimer = window.setTimeout(() => inputRef.current?.focus(), 10);
|
|
374
|
+
return () => window.clearTimeout(focusTimer);
|
|
375
|
+
} else {
|
|
376
|
+
setQuery("");
|
|
377
|
+
setResults([]);
|
|
378
|
+
setFilter("all");
|
|
379
|
+
setFilterOpen(false);
|
|
380
|
+
setActiveIndex(0);
|
|
381
|
+
const restoreTarget = restoreFocusRef.current;
|
|
382
|
+
restoreFocusRef.current = null;
|
|
383
|
+
if (restoreTarget?.isConnected) {
|
|
384
|
+
const focusFrame = window.requestAnimationFrame(() => restoreTarget.focus({ preventScroll: true }));
|
|
385
|
+
return () => window.cancelAnimationFrame(focusFrame);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}, [open]);
|
|
389
|
+
useEffect(() => {
|
|
390
|
+
if (!open) return;
|
|
391
|
+
const { body, documentElement } = document;
|
|
392
|
+
const previousBodyOverflow = body.style.overflow;
|
|
393
|
+
const previousBodyPaddingRight = body.style.paddingRight;
|
|
394
|
+
const previousHtmlOverflow = documentElement.style.overflow;
|
|
395
|
+
const scrollbarWidth = window.innerWidth - documentElement.clientWidth;
|
|
396
|
+
body.style.overflow = "hidden";
|
|
397
|
+
documentElement.style.overflow = "hidden";
|
|
398
|
+
if (scrollbarWidth > 0) body.style.paddingRight = `${scrollbarWidth}px`;
|
|
399
|
+
return () => {
|
|
400
|
+
body.style.overflow = previousBodyOverflow;
|
|
401
|
+
body.style.paddingRight = previousBodyPaddingRight;
|
|
402
|
+
documentElement.style.overflow = previousHtmlOverflow;
|
|
403
|
+
};
|
|
404
|
+
}, [open]);
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
if (!open) return;
|
|
407
|
+
function handler(e) {
|
|
408
|
+
if (e.key === "Escape") setOpenWithAnalytics(false, "escape");
|
|
409
|
+
}
|
|
410
|
+
document.addEventListener("keydown", handler);
|
|
411
|
+
return () => document.removeEventListener("keydown", handler);
|
|
412
|
+
}, [open, setOpenWithAnalytics]);
|
|
413
|
+
const saveRecent = useCallback((item) => {
|
|
414
|
+
try {
|
|
415
|
+
const entry = {
|
|
416
|
+
id: item.id,
|
|
417
|
+
label: item.label,
|
|
418
|
+
url: item.url
|
|
419
|
+
};
|
|
420
|
+
const next = [entry, ...recents.filter((r) => r.id !== entry.id)].slice(0, 8);
|
|
421
|
+
setRecents(next);
|
|
422
|
+
localStorage.setItem("fd:omni:recents", JSON.stringify(next));
|
|
423
|
+
} catch {}
|
|
424
|
+
}, [recents]);
|
|
425
|
+
const execute = useCallback((item) => {
|
|
426
|
+
if (analytics) emitClientAnalyticsEvent({
|
|
427
|
+
type: "search_result_click",
|
|
428
|
+
locale: activeLocale,
|
|
429
|
+
path: item.url,
|
|
430
|
+
properties: {
|
|
431
|
+
mode: "command",
|
|
432
|
+
resultId: item.id,
|
|
433
|
+
resultUrl: item.url,
|
|
434
|
+
labelLength: item.label.length,
|
|
435
|
+
queryLength: query.length
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
saveRecent(item);
|
|
439
|
+
setOpen(false);
|
|
440
|
+
if (item.url.startsWith("http")) window.open(item.url, "_blank", "noopener");
|
|
441
|
+
else router.push(item.url);
|
|
442
|
+
}, [
|
|
443
|
+
activeLocale,
|
|
444
|
+
analytics,
|
|
445
|
+
query.length,
|
|
446
|
+
router,
|
|
447
|
+
saveRecent
|
|
448
|
+
]);
|
|
449
|
+
const displayItems = useMemo(() => {
|
|
450
|
+
if (results.length === 0) return [];
|
|
451
|
+
if (filter === "pages") return results.filter((item) => item.type === "page");
|
|
452
|
+
if (filter === "inside") return results.filter((item) => item.type !== "page");
|
|
453
|
+
return results;
|
|
454
|
+
}, [filter, results]);
|
|
455
|
+
const recentItems = useMemo(() => {
|
|
456
|
+
if (query.trim() || results.length > 0) return [];
|
|
457
|
+
return recents.map((r) => ({
|
|
458
|
+
id: r.id,
|
|
459
|
+
label: r.label,
|
|
460
|
+
subtitle: "Recently viewed",
|
|
461
|
+
url: r.url,
|
|
462
|
+
type: "page",
|
|
463
|
+
score: 0,
|
|
464
|
+
exactPriority: 0,
|
|
465
|
+
insideLiteralPriority: 0,
|
|
466
|
+
sourceIndex: 0,
|
|
467
|
+
indices: [],
|
|
468
|
+
descriptionIndices: []
|
|
469
|
+
}));
|
|
470
|
+
}, [
|
|
471
|
+
query,
|
|
472
|
+
results,
|
|
473
|
+
recents
|
|
474
|
+
]);
|
|
475
|
+
useEffect(() => {
|
|
476
|
+
setActiveIndex(0);
|
|
477
|
+
}, [filter, debouncedQuery]);
|
|
478
|
+
function updateFilter(nextFilter) {
|
|
479
|
+
setFilter(nextFilter);
|
|
480
|
+
setFilterOpen(false);
|
|
481
|
+
setActiveIndex(0);
|
|
482
|
+
}
|
|
483
|
+
function moveActive(delta) {
|
|
484
|
+
const total = displayItems.length + recentItems.length;
|
|
485
|
+
if (!total) return;
|
|
486
|
+
let next = activeIndex + delta;
|
|
487
|
+
if (next < 0) next = total - 1;
|
|
488
|
+
if (next >= total) next = 0;
|
|
489
|
+
setActiveIndex(next);
|
|
490
|
+
const allItems = [...recentItems, ...displayItems];
|
|
491
|
+
(listRef.current?.querySelector(`[data-id="${allItems[next]?.id}"]`))?.scrollIntoView({ block: "nearest" });
|
|
492
|
+
}
|
|
493
|
+
function handleKeyDown(e) {
|
|
494
|
+
if (e.key === "ArrowDown") {
|
|
495
|
+
e.preventDefault();
|
|
496
|
+
moveActive(1);
|
|
497
|
+
} else if (e.key === "ArrowUp") {
|
|
498
|
+
e.preventDefault();
|
|
499
|
+
moveActive(-1);
|
|
500
|
+
} else if (e.key === "Enter") {
|
|
501
|
+
e.preventDefault();
|
|
502
|
+
const allItems = [...recentItems, ...displayItems];
|
|
503
|
+
if (allItems[activeIndex]) execute(allItems[activeIndex]);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (!mounted || !open) return null;
|
|
507
|
+
const allItems = [...recentItems, ...displayItems];
|
|
508
|
+
return createPortal(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("div", {
|
|
509
|
+
className: "omni-overlay",
|
|
510
|
+
onClick: () => setOpenWithAnalytics(false, "overlay")
|
|
511
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
512
|
+
className: "omni-content",
|
|
513
|
+
role: "dialog",
|
|
514
|
+
"aria-modal": "true",
|
|
515
|
+
"aria-label": "Search documentation",
|
|
516
|
+
children: [
|
|
517
|
+
/* @__PURE__ */ jsx("div", {
|
|
518
|
+
className: "omni-header",
|
|
519
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
520
|
+
className: "omni-search-row",
|
|
521
|
+
children: [
|
|
522
|
+
/* @__PURE__ */ jsx("span", {
|
|
523
|
+
className: "omni-search-icon",
|
|
524
|
+
children: /* @__PURE__ */ jsx(SearchIcon, {})
|
|
525
|
+
}),
|
|
526
|
+
/* @__PURE__ */ jsx("input", {
|
|
527
|
+
ref: inputRef,
|
|
528
|
+
type: "text",
|
|
529
|
+
role: "combobox",
|
|
530
|
+
"aria-expanded": "true",
|
|
531
|
+
placeholder: "Search",
|
|
532
|
+
value: query,
|
|
533
|
+
onChange: (e) => setQuery(e.target.value),
|
|
534
|
+
onKeyDown: handleKeyDown,
|
|
535
|
+
className: "omni-search-input"
|
|
536
|
+
}),
|
|
537
|
+
/* @__PURE__ */ jsx("button", {
|
|
538
|
+
"aria-label": "Close",
|
|
539
|
+
className: "omni-close-btn",
|
|
540
|
+
onClick: () => setOpenWithAnalytics(false, "button"),
|
|
541
|
+
children: "ESC"
|
|
542
|
+
})
|
|
543
|
+
]
|
|
544
|
+
})
|
|
545
|
+
}),
|
|
546
|
+
/* @__PURE__ */ jsxs("div", {
|
|
547
|
+
className: "omni-body",
|
|
548
|
+
ref: listRef,
|
|
549
|
+
role: "listbox",
|
|
550
|
+
"aria-label": "Search results",
|
|
551
|
+
children: [
|
|
552
|
+
loading && /* @__PURE__ */ jsxs("div", {
|
|
553
|
+
className: "omni-loading",
|
|
554
|
+
children: [/* @__PURE__ */ jsx(LoaderIcon, {}), " Searching…"]
|
|
555
|
+
}),
|
|
556
|
+
recentItems.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
557
|
+
className: "omni-group",
|
|
558
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
559
|
+
className: "omni-group-label",
|
|
560
|
+
children: "Recent"
|
|
561
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
562
|
+
className: "omni-group-items",
|
|
563
|
+
children: recentItems.map((item, i) => /* @__PURE__ */ jsx("button", {
|
|
564
|
+
"data-id": item.id,
|
|
565
|
+
role: "option",
|
|
566
|
+
"aria-selected": i === activeIndex,
|
|
567
|
+
onMouseEnter: () => setActiveIndex(i),
|
|
568
|
+
onClick: () => execute(item),
|
|
569
|
+
className: cn("omni-item", i === activeIndex && "omni-item-active"),
|
|
570
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
571
|
+
className: "omni-item-text",
|
|
572
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
573
|
+
className: "omni-item-subtitle",
|
|
574
|
+
children: item.subtitle
|
|
575
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
576
|
+
className: "omni-item-label",
|
|
577
|
+
children: item.label
|
|
578
|
+
})]
|
|
579
|
+
})
|
|
580
|
+
}, item.id))
|
|
581
|
+
})]
|
|
582
|
+
}),
|
|
583
|
+
displayItems.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
584
|
+
className: "omni-group",
|
|
585
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
586
|
+
className: "omni-group-label",
|
|
587
|
+
children: "Documentation"
|
|
588
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
589
|
+
className: "omni-group-items",
|
|
590
|
+
children: displayItems.map((item, i) => {
|
|
591
|
+
const idx = recentItems.length + i;
|
|
592
|
+
return /* @__PURE__ */ jsx("button", {
|
|
593
|
+
"data-id": item.id,
|
|
594
|
+
role: "option",
|
|
595
|
+
"aria-selected": idx === activeIndex,
|
|
596
|
+
onMouseEnter: () => setActiveIndex(idx),
|
|
597
|
+
onClick: () => execute(item),
|
|
598
|
+
className: cn("omni-item", idx === activeIndex && "omni-item-active"),
|
|
599
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
600
|
+
className: "omni-item-text",
|
|
601
|
+
children: [
|
|
602
|
+
/* @__PURE__ */ jsx("div", {
|
|
603
|
+
className: "omni-item-subtitle",
|
|
604
|
+
children: item.subtitle
|
|
605
|
+
}),
|
|
606
|
+
/* @__PURE__ */ jsx("div", {
|
|
607
|
+
className: "omni-item-label",
|
|
608
|
+
children: /* @__PURE__ */ jsx(HighlightedLabel, {
|
|
609
|
+
label: item.label,
|
|
610
|
+
indices: item.indices
|
|
611
|
+
})
|
|
612
|
+
}),
|
|
613
|
+
item.description && /* @__PURE__ */ jsx("div", {
|
|
614
|
+
className: "omni-item-description",
|
|
615
|
+
children: /* @__PURE__ */ jsx(HighlightedLabel, {
|
|
616
|
+
label: item.description,
|
|
617
|
+
indices: item.descriptionIndices
|
|
618
|
+
})
|
|
619
|
+
})
|
|
620
|
+
]
|
|
621
|
+
})
|
|
622
|
+
}, item.id);
|
|
623
|
+
})
|
|
624
|
+
})]
|
|
625
|
+
}),
|
|
626
|
+
allItems.length === 0 && !loading && /* @__PURE__ */ jsxs("div", {
|
|
627
|
+
className: "omni-empty",
|
|
628
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
629
|
+
className: "omni-empty-icon",
|
|
630
|
+
children: /* @__PURE__ */ jsx(HistoryIcon, {})
|
|
631
|
+
}), debouncedQuery && results.length > 0 ? `No ${FILTER_LABELS[filter].toLowerCase()} results found.` : debouncedQuery ? "No results found. Try a different query." : "Type to search the docs, or browse recent items."]
|
|
632
|
+
})
|
|
633
|
+
]
|
|
634
|
+
}),
|
|
635
|
+
/* @__PURE__ */ jsx("div", {
|
|
636
|
+
className: "omni-footer",
|
|
637
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
638
|
+
className: "omni-footer-inner",
|
|
639
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
640
|
+
className: "omni-footer-hints",
|
|
641
|
+
children: [
|
|
642
|
+
/* @__PURE__ */ jsxs("span", {
|
|
643
|
+
className: "omni-footer-hint",
|
|
644
|
+
children: [/* @__PURE__ */ jsx(CornerDownLeftIcon, {}), " to select"]
|
|
645
|
+
}),
|
|
646
|
+
/* @__PURE__ */ jsxs("span", {
|
|
647
|
+
className: "omni-footer-hint",
|
|
648
|
+
children: [
|
|
649
|
+
/* @__PURE__ */ jsx(ArrowUpIcon, {}),
|
|
650
|
+
/* @__PURE__ */ jsx(ArrowDownIcon, {}),
|
|
651
|
+
" to navigate"
|
|
652
|
+
]
|
|
653
|
+
}),
|
|
654
|
+
/* @__PURE__ */ jsxs("span", {
|
|
655
|
+
className: "omni-footer-hint omni-footer-hint-desktop",
|
|
656
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
657
|
+
className: "omni-kbd-sm",
|
|
658
|
+
children: "ESC"
|
|
659
|
+
}), " to close"]
|
|
660
|
+
})
|
|
661
|
+
]
|
|
662
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
663
|
+
className: "omni-footer-filter",
|
|
664
|
+
children: [
|
|
665
|
+
/* @__PURE__ */ jsx("span", {
|
|
666
|
+
className: "omni-filter-label",
|
|
667
|
+
children: "Filter"
|
|
668
|
+
}),
|
|
669
|
+
/* @__PURE__ */ jsxs("button", {
|
|
670
|
+
type: "button",
|
|
671
|
+
className: "omni-filter-button",
|
|
672
|
+
"aria-expanded": filterOpen,
|
|
673
|
+
onClick: () => setFilterOpen((value) => !value),
|
|
674
|
+
children: [FILTER_LABELS[filter], /* @__PURE__ */ jsx(ArrowDownIcon, {})]
|
|
675
|
+
}),
|
|
676
|
+
filterOpen && /* @__PURE__ */ jsx("div", {
|
|
677
|
+
className: "omni-filter-menu",
|
|
678
|
+
role: "group",
|
|
679
|
+
"aria-label": "Search filter",
|
|
680
|
+
children: [
|
|
681
|
+
"all",
|
|
682
|
+
"pages",
|
|
683
|
+
"inside"
|
|
684
|
+
].map((mode) => /* @__PURE__ */ jsx("button", {
|
|
685
|
+
type: "button",
|
|
686
|
+
"aria-pressed": filter === mode,
|
|
687
|
+
className: cn("omni-filter-option", filter === mode && "omni-filter-option-active"),
|
|
688
|
+
onClick: () => updateFilter(mode),
|
|
689
|
+
children: FILTER_LABELS[mode]
|
|
690
|
+
}, mode))
|
|
691
|
+
})
|
|
692
|
+
]
|
|
693
|
+
})]
|
|
694
|
+
})
|
|
695
|
+
})
|
|
696
|
+
]
|
|
697
|
+
})] }), document.body);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
//#endregion
|
|
701
|
+
export { CommandSearchDialog };
|