(method = "replace")
| 300 | } |
| 301 | |
| 302 | window.requestAnimationFrame(syncVisiblePromptToggles); |
| 303 | } |
| 304 | |
| 305 | // Keep the library's state shareable without discarding tracking or other |
| 306 | // query parameters owned by the surrounding site. |
| 307 | function syncUrlState(method = "replace") { |
| 308 | if (typeof window.history?.replaceState !== "function") { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | const params = new URLSearchParams(window.location.search); |
| 313 | const query = searchInput?.value.trim() ?? ""; |
| 314 | |
| 315 | if (query) { |
| 316 | params.set("q", query); |
| 317 | } else { |
| 318 | params.delete("q"); |
| 319 | } |
| 320 | |
| 321 | if (activeCategory && activeCategory !== "all") { |
| 322 | params.set("category", activeCategory); |
| 323 | } else { |
| 324 | params.delete("category"); |
| 325 | } |
| 326 | |
| 327 | if (activeSort !== "featured") { |
| 328 | params.set("sort", activeSort); |
| 329 | } else { |
| 330 | params.delete("sort"); |
| 331 | } |
| 332 | |
| 333 | if (currentPage > 1) { |
| 334 | params.set("page", String(currentPage)); |
| 335 | } else { |
| 336 | params.delete("page"); |
| 337 | } |
| 338 | |
| 339 | const search = params.toString(); |
| 340 | const nextUrl = `${window.location.pathname}${search ? `?${search}` : ""}${ |
| 341 | window.location.hash |
| 342 | }`; |
| 343 | const currentUrl = `${window.location.pathname}${window.location.search}${window.location.hash}`; |
| 344 | |
| 345 | if (nextUrl === currentUrl) { |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (method === "push" && typeof window.history.pushState === "function") { |
| 350 | window.history.pushState(null, "", nextUrl); |
| 351 | } else { |
| 352 | window.history.replaceState(null, "", nextUrl); |
no test coverage detected