From 9ab22cb1a971d610a27fb382158f429cba88d038 Mon Sep 17 00:00:00 2001 From: XLor Date: Thu, 26 Oct 2023 01:07:45 +0800 Subject: [PATCH] feat(app): stringify search --- packages/app/src/components/Search/Search.tsx | 16 +++++-- packages/app/src/components/Search/utils.ts | 42 +++++++++++++++++-- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/packages/app/src/components/Search/Search.tsx b/packages/app/src/components/Search/Search.tsx index 750f2515..32e54263 100644 --- a/packages/app/src/components/Search/Search.tsx +++ b/packages/app/src/components/Search/Search.tsx @@ -16,7 +16,7 @@ import { goTo, goToSearch, parseSearch, - resolveSearchURL + stringifySearch } from './utils'; { @@ -48,20 +48,28 @@ export default function Search() { const fn = () => { try { const input = window.sessionStorage.getItem(SEARCH_INPUT_KEY); + window.sessionStorage.removeItem(SEARCH_INPUT_KEY); + console.log('Input', input); if (input) { setInput(input); - window.sessionStorage.removeItem(SEARCH_INPUT_KEY); } else { - setInput(''); + console.log(location.pathname.startsWith('/resources/')); + if (location.pathname.startsWith('/resources/')) { + const content = stringifySearch(new URLSearchParams(location.search)); + setInput(content); + } else { + setInput(''); + } } } catch {} }; + fn(); document.addEventListener('astro:page-load', fn); return () => { document.removeEventListener('astro:page-load', fn); }; - }); + }, []); const setDebounceSearch = debounce((value: string) => { if (value !== search) { diff --git a/packages/app/src/components/Search/utils.ts b/packages/app/src/components/Search/utils.ts index c16304c5..ecc68c75 100644 --- a/packages/app/src/components/Search/utils.ts +++ b/packages/app/src/components/Search/utils.ts @@ -1,5 +1,5 @@ import { navigate } from 'astro:transitions/client'; -import { findFansub, stringifySearchURL } from 'animegarden'; +import { findFansub, parseSearchURL, stringifySearchURL } from 'animegarden'; import { loading } from '../../state'; @@ -25,7 +25,7 @@ export function parseSearch(search: string) { let j = i; let word = ''; - while (j < search.length) { + while (j < search.length && !/\s/.test(search[j])) { if (Object.keys(matchQuotes).includes(search[j])) { // Split by quote "..." or '...' const quote = matchQuotes[search[j] as keyof typeof matchQuotes]; @@ -51,6 +51,7 @@ export function parseSearch(search: string) { // otherwise word += search[j]; } + j++; } @@ -78,7 +79,7 @@ export function parseSearch(search: string) { exclude.push(word); }, 'fansub:,字幕:,字幕组:': (word) => { - if (/^\d$/.test(word)) { + if (/^\d+$/.test(word)) { fansub.push(+word); } else { const found = findFansub(word, { fuzzy: true }); @@ -125,6 +126,41 @@ export function parseSearch(search: string) { }; } +export function stringifySearch(search: URLSearchParams) { + const filter = parseSearchURL(search, { pageSize: 80 }); + console.log(filter); + const content: string[] = []; + + if (filter.search) { + content.push(...filter.search.map((f) => wrap(f))); + } + if (filter.include && filter.include.length === 1) { + content.push(...filter.include[0].map((f) => wrap(f))); + } + if (filter.exclude) { + content.push(...filter.exclude.map((ex) => '排除:' + wrap(ex))); + } + if (filter.fansubId) { + content.push(...filter.fansubId.map((f) => '字幕组:' + (findFansub(f)?.name ?? f))); + } + if (filter.fansubName) { + content.push(...filter.fansubName.map((f) => '字幕组:' + f)); + } + if (filter.after) { + content.push('开始:' + filter.after.toISOString()); + } + if (filter.before) { + content.push('结束:' + filter.before.toISOString()); + } + + return content.map((c) => c).join(' '); + + function wrap(t: string) { + if (t.indexOf(' ') !== -1) return `"${t.slice(1, t.length - 1).replace(/"/g, '\\"')}"`; + else return t.slice(1, t.length - 1); + } +} + export function resolveSearchURL(search: string) { if (search.startsWith(location.origin)) { return search.slice(location.origin.length);