diff --git a/materialious/src/lib/components/Search.svelte b/materialious/src/lib/components/Search.svelte index 3c22de02..0116af20 100644 --- a/materialious/src/lib/components/Search.svelte +++ b/materialious/src/lib/components/Search.svelte @@ -10,6 +10,7 @@ isAndroidTvStore, searchHistoryStore } from '../store'; + import { isVideoID } from '$lib/misc'; const dispatch = createEventDispatcher(); @@ -39,20 +40,27 @@ function handleSubmit(event: Event | undefined = undefined) { if (event) event.preventDefault(); - if (search.trim() === '') return; + const searchTrimed = search.trim(); + + if (searchTrimed) return; + + if (isVideoID(searchTrimed)) { + // Go directly to video if Video ID provided + goto(`/watch/${searchTrimed}`); + } selectedSuggestionIndex = -1; - goto(`/search/${encodeURIComponent(search)}`); + goto(`/search/${encodeURIComponent(searchTrimed)}`); suggestionsForSearch = []; showSearchBox = false; - if ($interfaceSearchHistoryEnabled && !$searchHistoryStore.includes(search)) { + if ($interfaceSearchHistoryEnabled && !$searchHistoryStore.includes(searchTrimed)) { let pastHistory = $searchHistoryStore; if (pastHistory.length > 15) { pastHistory.pop(); } - searchHistoryStore.set([search, ...pastHistory]); + searchHistoryStore.set([searchTrimed, ...pastHistory]); } } diff --git a/materialious/src/lib/misc.ts b/materialious/src/lib/misc.ts index 68dab9a4..069c1b5f 100644 --- a/materialious/src/lib/misc.ts +++ b/materialious/src/lib/misc.ts @@ -14,6 +14,11 @@ import type { VideoBase } from './api/model'; +export function isVideoID(videoId: string): boolean { + var regExp = /^[a-zA-Z0-9_-]{11}$/; + return regExp.test(videoId); +} + export function truncate(value: string, maxLength: number = 50): string { return value.length > maxLength ? `${value.substring(0, maxLength)}...` : value; }