mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: fix the problematic search stringify function
This commit is contained in:
6 files changed
+41
-6
No files matched your search
@@ -49,7 +49,6 @@
|
|||||||
"require-resolve-hook": "^1.1.0",
|
"require-resolve-hook": "^1.1.0",
|
||||||
"saslprep": "^1.0.3",
|
"saslprep": "^1.0.3",
|
||||||
"schemastery": "^3.11.1",
|
"schemastery": "^3.11.1",
|
||||||
"search-query-parser": "^1.6.0",
|
|
||||||
"semver": "^7.5.4",
|
"semver": "^7.5.4",
|
||||||
"serialize-javascript": "^6.0.1",
|
"serialize-javascript": "^6.0.1",
|
||||||
"superagent": "^8.0.9",
|
"superagent": "^8.0.9",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import { Filter, ObjectId } from 'mongodb';
|
import { Filter, ObjectId } from 'mongodb';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import parser from 'search-query-parser';
|
import parser from '@hydrooj/utils/lib/search';
|
||||||
import { sortFiles, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
import { sortFiles, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
||||||
import {
|
import {
|
||||||
BadRequestError, ContestNotAttendedError, ContestNotEndedError, ContestNotFoundError, ContestNotLiveError,
|
BadRequestError, ContestNotAttendedError, ContestNotEndedError, ContestNotFoundError, ContestNotLiveError,
|
||||||
|
|||||||
@@ -126,7 +126,6 @@
|
|||||||
"markdown-it-table-of-contents": "^0.6.0",
|
"markdown-it-table-of-contents": "^0.6.0",
|
||||||
"nunjucks": "^3.2.4",
|
"nunjucks": "^3.2.4",
|
||||||
"schemastery-jsonschema": "^1.0.3",
|
"schemastery-jsonschema": "^1.0.3",
|
||||||
"search-query-parser": "^1.6.0",
|
|
||||||
"streamsaver": "^2.0.6",
|
"streamsaver": "^2.0.6",
|
||||||
"xss": "^1.0.14"
|
"xss": "^1.0.14"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
import parser, { SearchParserResult } from '@hydrooj/utils/lib/search';
|
||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import parser from 'search-query-parser';
|
|
||||||
import DomainSelectAutoComplete from 'vj/components/autocomplete/DomainSelectAutoComplete';
|
import DomainSelectAutoComplete from 'vj/components/autocomplete/DomainSelectAutoComplete';
|
||||||
import { ActionDialog, ConfirmDialog } from 'vj/components/dialog';
|
import { ActionDialog, ConfirmDialog } from 'vj/components/dialog';
|
||||||
import Dropdown from 'vj/components/dropdown/Dropdown';
|
import Dropdown from 'vj/components/dropdown/Dropdown';
|
||||||
@@ -30,7 +30,7 @@ const parserOptions = {
|
|||||||
|
|
||||||
function writeSelectionToInput() {
|
function writeSelectionToInput() {
|
||||||
const currentValue = $('[name="q"]').val() as string;
|
const currentValue = $('[name="q"]').val() as string;
|
||||||
const parsedCurrentValue = parser.parse(currentValue, parserOptions) as parser.SearchParserResult;
|
const parsedCurrentValue = parser.parse(currentValue, parserOptions) as SearchParserResult;
|
||||||
const q = parser.stringify({
|
const q = parser.stringify({
|
||||||
...parsedCurrentValue,
|
...parsedCurrentValue,
|
||||||
category: selections,
|
category: selections,
|
||||||
@@ -138,7 +138,7 @@ function buildCategoryFilter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseCategorySelection() {
|
function parseCategorySelection() {
|
||||||
const parsed = parser.parse($('[name="q"]').val() as string || '', parserOptions) as parser.SearchParserResult;
|
const parsed = parser.parse($('[name="q"]').val() as string || '', parserOptions) as SearchParserResult;
|
||||||
selections = _.uniq(parsed.category || []);
|
selections = _.uniq(parsed.category || []);
|
||||||
updateSelection();
|
updateSelection();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { parse, SearchParserOptions, SearchParserResult } from 'search-query-parser';
|
||||||
|
|
||||||
|
export { parse, SearchParserOptions, SearchParserResult } from 'search-query-parser';
|
||||||
|
|
||||||
|
export function stringify(queryObject: SearchParserResult, options: SearchParserOptions = {}, prefix = ''): string {
|
||||||
|
if (!Object.keys(queryObject || {}).length) return '';
|
||||||
|
prefix ||= '';
|
||||||
|
const toArray = (val: string | string[]) => (typeof val === 'string' ? [val] : val);
|
||||||
|
const addQuotes = (s: string) => (s.indexOf(' ') > -1 ? JSON.stringify(s) : s);
|
||||||
|
const addPrefix = (s: string) => prefix + s;
|
||||||
|
const parts = [];
|
||||||
|
if (queryObject.text) {
|
||||||
|
const value = toArray(queryObject.text);
|
||||||
|
if (value.length) parts.push(value.map(addQuotes).map(addPrefix).join(' '));
|
||||||
|
}
|
||||||
|
for (const range of options.ranges || []) {
|
||||||
|
if (!queryObject[range]) continue;
|
||||||
|
let value = queryObject[range].from;
|
||||||
|
const to = queryObject[range].to;
|
||||||
|
if (to) value = `${value}-${to}`;
|
||||||
|
if (value) parts.push(addPrefix(`${range}:${value}`));
|
||||||
|
}
|
||||||
|
for (const keyword of options.keywords || []) {
|
||||||
|
if (!queryObject[keyword]) continue;
|
||||||
|
const value = toArray(queryObject[keyword]);
|
||||||
|
if (value.length > 0) {
|
||||||
|
parts.push(addPrefix(`${keyword}:${addQuotes(value.join(','))}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Object.keys(queryObject.exclude || {}).length) {
|
||||||
|
parts.push(stringify(queryObject.exclude, options, '-'));
|
||||||
|
}
|
||||||
|
return parts.join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
export default { stringify, parse };
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
"moment-timezone": "^0.5.43",
|
"moment-timezone": "^0.5.43",
|
||||||
"mongodb": "^5.7.0",
|
"mongodb": "^5.7.0",
|
||||||
"reggol": "^1.4.4",
|
"reggol": "^1.4.4",
|
||||||
|
"search-query-parser": "^1.6.0",
|
||||||
"source-map-support": "^0.5.21",
|
"source-map-support": "^0.5.21",
|
||||||
"systeminformation": "^5.18.8"
|
"systeminformation": "^5.18.8"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user