fix: 页面设计器组件搜索改用matchSorter

This commit is contained in:
wutong25 2023-10-11 15:52:19 +08:00
parent 6ed6cb89d3
commit 33bc90e828
2 changed files with 25 additions and 42 deletions

View File

@ -4,6 +4,7 @@ import {Icon, InputBox, resolveVariable} from 'amis';
import cx from 'classnames';
import {autobind, stringRegExp} from '../../util';
import isString from 'lodash/isString';
import {matchSorter} from 'match-sorter';
/**
*
@ -182,7 +183,7 @@ export default class SearchPanel extends React.Component<
/**
*
*/
groupedResultByKeyword(keywords?: string) {
groupedResultByKeyword(keywords: string = '') {
const {allResult} = this.props;
let curSearchResult: any[] = [];
let curSearchResultByTag: {
@ -194,21 +195,15 @@ export default class SearchPanel extends React.Component<
? new RegExp(stringRegExp(curKeyword), 'i')
: null;
allResult.forEach(item => {
if (isString(item) && regular && regular.test(item)) {
if (allResult.length && isString(allResult[0])) {
matchSorter(allResult, keywords).forEach(item => {
// 兼容字符串类型
curSearchResult.push(item);
} else if (
!keywords ||
['name', 'description', 'scaffold.type', 'searchKeywords'].some(key => {
return (
resolveVariable(key, item) &&
regular &&
(regular.test(resolveVariable(key, item)) ||
regular.test(resolveVariable(key, item)?.replaceAll('-', '')))
);
})
) {
});
} else {
matchSorter(allResult, keywords, {
keys: ['name', 'description', 'scaffold.type', 'searchKeywords']
}).forEach(item => {
if (item[curTagKey]) {
const tags = Array.isArray(item[curTagKey])
? item[curTagKey].concat()
@ -222,8 +217,8 @@ export default class SearchPanel extends React.Component<
} else {
curSearchResult.push(item);
}
}
});
});
}
// 更新当前搜索结果数据(备注: 附带重置功能)
this.setState({

View File

@ -53,6 +53,7 @@ import isPlainObject from 'lodash/isPlainObject';
import {EditorManagerConfig} from '../manager';
import {EditorNode, EditorNodeType} from './node';
import findIndex from 'lodash/findIndex';
import {matchSorter} from 'match-sorter';
export interface SchemaHistory {
versionId: number;
@ -684,39 +685,26 @@ export const MainStore = types
/** 根据关键字过滤组件 */
groupedRenderersByKeyword(
_subRenderers: Array<SubRendererInfo>,
keywords?: string
keywords: string = ''
) {
const subRenderers = _subRenderers;
const grouped: {
[propName: string]: Array<SubRendererInfo>;
} = {};
const regular = keywords
? new RegExp(stringRegExp(keywords), 'i')
: null;
matchSorter(subRenderers, keywords, {
keys: ['name', 'description', 'scaffold.type', 'searchKeywords']
}).forEach(item => {
const tags = Array.isArray(item.tags)
? item.tags.concat()
: item.tags
? [item.tags]
: ['其他'];
subRenderers.forEach(item => {
if (
!keywords ||
['name', 'description', 'scaffold.type', 'searchKeywords'].some(
key =>
resolveVariable(key, item) &&
regular &&
(regular.test(resolveVariable(key, item)) ||
regular.test(resolveVariable(key, item)?.replaceAll('-', '')))
)
) {
const tags = Array.isArray(item.tags)
? item.tags.concat()
: item.tags
? [item.tags]
: ['其他'];
tags.forEach(tag => {
grouped[tag] = grouped[tag] || [];
grouped[tag].push(item);
});
}
tags.forEach(tag => {
grouped[tag] = grouped[tag] || [];
grouped[tag].push(item);
});
});
return grouped;