File size: 8,670 Bytes
0ad74ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
<script lang="ts">
import Search_Worker from "./search-worker?worker";
import SearchIcon from "./SearchIcon.svelte";
import { onNavigate } from "$app/navigation";
import type { Result } from "./search";
import { browser } from "$app/environment";
let search: "idle" | "load" | "ready" = "idle";
let search_term = "";
let results: Result[] = [];
let search_worker: Worker;
function initialize() {
open = true;
if (search === "ready") return;
search = "load";
search_worker = new Search_Worker();
search_worker.addEventListener("message", (e) => {
const { type, payload } = e.data;
type === "ready" && (search = "ready");
type === "results" && (results = payload.results);
});
search_worker.postMessage({ type: "load" });
}
let open: boolean = false;
onNavigate(() => {
open = false;
});
$: if (search === "ready") {
search_worker.postMessage({ type: "search", payload: { search_term } });
}
$: if (search_term && !open) {
search_term = "";
}
let content_elem: HTMLElement;
let search_button_elem: HTMLElement;
function focus_input(el: HTMLInputElement) {
el.focus();
}
function get_os(): string {
// @ts-ignore - userAgentData is not yet in the TS types as it is currently experimental
if (typeof navigator !== "undefined") {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("win") > -1) return "Windows";
if (userAgent.indexOf("mac") > -1) return "MacOS";
if (userAgent.indexOf("linux") > -1) return "Linux";
if (userAgent.indexOf("android") > -1) return "Android";
if (userAgent.indexOf("iphone") > -1 || userAgent.indexOf("ipad") > -1)
return "iOS";
}
return "Unknown";
}
let meta_key = "⌘";
$: if (browser && navigator) {
let os = get_os();
meta_key = os === "MacOS" || os === "iOS" ? "⌘" : "CTRL+";
}
function handle_key_down(e: KeyboardEvent): void {
if (e.ctrlKey || e.metaKey) {
if (e.key === "k" || e.key === "K") {
e.preventDefault();
initialize();
}
}
if (e.key === "Escape") {
open = false;
}
if ((e.key === "ArrowUp" || e.key === "ArrowDown") && open) {
e.preventDefault();
const current = document.activeElement;
const items = [...document.getElementsByClassName("res-block")];
const current_index = current ? items.indexOf(current) : -1;
let new_index;
if (current_index === -1) {
new_index = 1;
} else {
if (e.key === "ArrowUp") {
new_index = (current_index + items.length - 1) % items.length;
} else {
new_index = (current_index + 1) % items.length;
}
}
(current as HTMLElement).blur();
const newElement = items[new_index] as HTMLElement;
if (newElement) {
newElement.focus();
document
.querySelectorAll(".res-block")
.forEach((el) => el.classList.remove("first-res"));
}
}
const search_input = document.getElementById(
"search-input"
) as HTMLInputElement;
const first_result = document.querySelector(".res-block") as HTMLElement;
if (e.key === "Enter" && document.activeElement === search_input) {
first_result.click();
}
}
function on_click(e: MouseEvent) {
if (content_elem) {
if (!content_elem.contains(e.target as Node) && open) {
open = false;
}
} else {
if (search_button_elem.contains(e.target as Node)) {
initialize();
}
}
}
$: if (browser && document.querySelector(".res-block")) {
document.querySelector(".res-block")?.classList.add("first-res");
}
</script>
<svelte:window on:keydown={handle_key_down} on:click={on_click} />
<button class="search-button" bind:this={search_button_elem}>
<SearchIcon />
<span class="pl-1 pr-5">Search</span>
<div class="shortcut">
<div class="text-sm">
{meta_key}K
</div>
</div>
</button>
{#if open}
<div class="overlay" />
<div class="content" bind:this={content_elem}>
<div class="search-bar">
{#if search === "load"}
<div class="loader"></div>
{:else}
<SearchIcon />
{/if}
<input
bind:value={search_term}
placeholder="What are you searching for?"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
enterkeyhint="go"
maxlength="64"
spellcheck="false"
type="search"
use:focus_input
id="search-input"
/>
<button
on:click={() => {
open = false;
}}
class="text-xs font-semibold rounded-md p-1 border-gray-300 border"
>
ESC
</button>
</div>
<div class="results">
{#if results.length}
<ul>
{#each results as result, i}
{#if result.content.length > 0}
<li>
<a
class="res-block"
class:first-res={i === 0}
href={result.slug}
>
<p
class:text-green-700={result.type == "DOCS"}
class:bg-green-100={result.type == "DOCS"}
class:text-orange-700={result.type == "GUIDE"}
class:bg-orange-100={result.type == "GUIDE"}
class="float-left text-xs font-semibold rounded-md p-1 px-2 mx-1 mt-[3px]"
>
{result.type}
</p>
<div class="float-right">
<div class="enter">↵</div>
</div>
<p>{@html result.title}</p>
<ol>
{#each result.content as content}
<li class="res-content">{@html content}</li>
{/each}
</ol>
</a>
</li>
{/if}
{/each}
</ul>
{:else}
{#if search_term}
{#if search === "load"}
<p class="mx-auto w-fit text-gray-500">Searching for results...</p>
{:else}
<p class="mx-auto w-fit text-gray-500">
No results found. Try using a different term.
</p>
{/if}
{/if}
<ul>
<p class="">Suggestions</p>
<li>
<a class="res-block first-res" href="/quickstart">
<p
class="float-left text-xs mx-1 font-semibold text-orange-700 bg-orange-100 rounded-md p-1 px-2 mt-[3px]"
>
GUIDE
</p>
<div class="float-right">
<div class="enter">↵</div>
</div>
<p>Quickstart</p>
</a>
</li>
<li>
<a class="res-block" href="/docs/gradio/interface">
<p
class="float-left text-xs font-semibold text-green-700 bg-green-100 rounded-md p-1 px-2 mx-1 mt-[3px]"
>
DOCS
</p>
<div class="float-right">
<div class="enter">↵</div>
</div>
<p>Interface</p>
</a>
</li>
<li>
<a class="res-block" href="/docs/gradio/blocks">
<p
class="float-left text-xs font-semibold text-green-700 bg-green-100 rounded-md p-1 px-2 mx-1 mt-[3px]"
>
DOCS
</p>
<div class="float-right">
<div class="enter">↵</div>
</div>
<p>Blocks</p>
</a>
</li>
</ul>
{/if}
</div>
</div>
{/if}
<style>
.overlay {
@apply fixed inset-0 z-30 backdrop-blur-sm bg-black/20;
}
.search-bar {
@apply font-sans text-lg z-10 px-4 relative flex flex-none items-center border-b border-gray-100 text-gray-500;
}
.search-bar input {
@apply text-lg appearance-none h-14 text-black mx-1 flex-auto min-w-0 border-none cursor-text;
outline: none;
box-shadow: none;
}
.content {
@apply fixed left-1/2 top-[7%] -translate-x-1/2 mx-auto w-[95vw] max-w-3xl flex flex-col min-h-0 rounded-lg shadow-2xl bg-white z-40;
}
.results {
@apply p-5 overflow-y-auto;
max-height: 60vh;
scrollbar-width: thin;
& ol {
margin-block-start: 2px;
}
& li:not(:last-child) {
margin-block-end: 4px;
padding-block-end: 4px;
}
& a {
display: block;
}
}
.search-button {
@apply flex flex-row rounded-full items-center cursor-pointer px-2 text-gray-400 border-gray-300 border text-lg outline-none font-sans;
}
:global(.res-content .mark) {
color: #ff7c00;
text-decoration: underline;
}
:global(.res-content) {
@apply text-gray-500;
}
:global(.res-block) {
@apply m-2 p-2 border border-gray-100 rounded-md bg-gray-50 hover:bg-gray-100 hover:scale-[1.01] focus:bg-gray-100 focus:scale-[1.01] focus:outline-none;
}
:global(.first-res) {
@apply bg-gray-100 scale-[1.01];
}
:global(.res-block:focus .enter) {
display: block !important;
}
:global(.first-res .enter) {
display: block !important;
}
.enter {
display: none;
}
.enter {
@apply text-xs font-semibold rounded-md p-1 border-gray-300 border text-gray-500 font-sans bg-white;
}
.loader {
border: 1px solid #d0cfcf;
border-top: 2px solid #475469;
border-radius: 50%;
width: 15px;
height: 15px;
animation: spin 1.2s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
|