Skip to content

Commit

Permalink
feat(app): stringify search
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Oct 25, 2023
1 parent 6164a35 commit 9ab22cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
16 changes: 12 additions & 4 deletions packages/app/src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
goTo,
goToSearch,
parseSearch,
resolveSearchURL
stringifySearch
} from './utils';

{
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 39 additions & 3 deletions packages/app/src/components/Search/utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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];
Expand All @@ -51,6 +51,7 @@ export function parseSearch(search: string) {
// otherwise
word += search[j];
}

j++;
}

Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9ab22cb

Please sign in to comment.