Skip to content

Commit

Permalink
feat(vscode): 优化搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Aug 20, 2024
1 parent bb23f61 commit 573ca04
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 154 deletions.
2 changes: 1 addition & 1 deletion packages/shared/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function runApp(app: App, register: any) {

let message = err?.message || 'error';
if (err instanceof FetchException) message = '网络请求异常';
else if (err instanceof JsVmException) message = '网络请求异常';
else if (err instanceof JsVmException) message = '执行脚本异常';
else if (err instanceof AnalyzerException) message = '规则解析异常';
return result([], message, -1);
});
Expand Down
98 changes: 98 additions & 0 deletions packages/web/src/pages/common/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { v4 as uuidV4 } from 'uuid';
import pLimit from 'p-limit';
import { ContentType } from '@any-reader/rule-utils';
import { searchByRuleId } from '@/api';
import { useRulesStore } from '@/stores/rules';

export function useSearch() {
const rulesStore = useRulesStore();

const route = useRoute();
const router = useRouter();

let uuid: string = '';
const searchText = ref('');
const contentType = ref(ContentType.NOVEL);
const contentTypes = computed(() => [contentType.value]);
const loading = ref(false);
const total = ref(0);
const runCount = ref(0);

const list = ref<any[]>([]);

async function onSearch(uid?: string) {
const runPromise = pLimit(10);
const lastUuid = uid || uuidV4();
uuid = lastUuid;
list.value = [];
total.value = 0;
runCount.value = 0;
loading.value = true;
const rules = rulesStore.list.filter((e) => contentTypes.value.includes(e.contentType) && e.enableSearch);
total.value = rules.length;

const tasks = rules.map((rule) =>
runPromise(() => {
return new Promise((c) => {
if (lastUuid !== uuid) return;
runCount.value++;
searchByRuleId({ ruleId: rule.id, keyword: searchText.value })
.then((res) => {
if (res.code === 0) {
const rows = res.data;
if (!rows.length) return;
list.value.push({
rule: rule,
list: rows
});
}
})
.finally(() => c(true));
});
})
);
await Promise.all(tasks).catch(() => {});
loading.value = false;
}

function cancelSearch() {
uuid = uuidV4();
loading.value = false;
}

function init() {
const { keyword, _uuid } = route.query;
if (searchText.value === keyword && uuid === _uuid) return;
if (keyword) {
searchText.value = keyword as string;
onSearch(_uuid as string);
}
}

onMounted(init);
onActivated(init);
onDeactivated(cancelSearch);

function getChapter(row: any, rule: any) {
router.push({
path: '/chapter',
query: {
...row,
filePath: row.url,
ruleId: rule.id
}
});
}

return {
getChapter,
contentType,
cancelSearch,
searchText,
onSearch,
loading,
runCount,
total,
list
};
}
84 changes: 4 additions & 80 deletions packages/web/src/pages/pc/search/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@
v-for="(row, idx) in item.list"
:key="idx"
class="node relative w-102 flex flex-shrink-0 flex-col cursor-pointer hover:op-70"
@click="
router.push({
path: '/chapter',
query: {
...row,
ruleId: item.rule.id,
name: row.name,
filePath: row.url
}
})
"
@click="getChapter(row, item.rule)"
>
<div class="mb-5 h-136 w-102 overflow-hidden rounded-5">
<ARCover
Expand Down Expand Up @@ -86,82 +76,16 @@
</template>

<script setup lang="tsx">
import { v4 as uuidV4 } from 'uuid';
import pLimit from 'p-limit';
import { StarOutlined, StarFilled } from '@ant-design/icons-vue';
import { ContentType } from '@any-reader/rule-utils';
import { CONTENT_TYPES } from '@/constants';
import { searchByRuleId } from '@/api';
import { useFavoritesStore } from '@/stores/favorites';
import { useRulesStore } from '@/stores/rules';
import { useFavoritesStore } from '@/stores/favorites';
import { useSearch } from '@/pages/common/search';
const favoritesStore = useFavoritesStore();
const rulesStore = useRulesStore();
const router = useRouter();
const route = useRoute();
const runPromise = pLimit(10);
let uuid: string = '';
const searchText = ref('');
const contentType = ref(ContentType.NOVEL);
const contentTypes = computed(() => [contentType.value]);
const loading = ref(false);
const total = ref(0);
const runCount = ref(0);
const list = ref<any[]>([]);
async function onSearch(uid?: string) {
const lastUuid = uid || uuidV4();
uuid = lastUuid;
list.value = [];
total.value = 0;
runCount.value = 0;
loading.value = true;
const rules = rulesStore.list.filter((e) => contentTypes.value.includes(e.contentType) && e.enableSearch);
total.value = rules.length;
const tasks = rules.map((rule) =>
runPromise(() => {
return new Promise((c) => {
if (lastUuid !== uuid) return;
runCount.value++;
searchByRuleId({ ruleId: rule.id, keyword: searchText.value })
.then((res) => {
if (res.code === 0) {
const rows = res.data;
if (!rows.length) return;
list.value.push({
rule: rule,
list: rows
});
}
})
.finally(() => c(true));
});
})
);
await Promise.all(tasks).catch(() => {});
loading.value = false;
}
function cancelSearch() {
uuid = uuidV4();
loading.value = false;
}
function init() {
const { keyword, _uuid } = route.query;
if (searchText.value === keyword && uuid === _uuid) return;
if (keyword) {
searchText.value = keyword as string;
onSearch(_uuid as string);
}
}
onActivated(init);
onMounted(init);
const { contentType, searchText, onSearch, cancelSearch, loading, runCount, total, list, getChapter } = useSearch();
</script>

<style scoped lang="scss">
Expand Down
79 changes: 6 additions & 73 deletions packages/web/src/pages/vscode/search/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<vscode-button appearance="Secondary" @click="cancelSearch">取消</vscode-button>
</div>

<div v-if="!rulesStore.list.length" class="py-20 text-center">您还没有配置规则!无法使用搜索功能!</div>

<div class="mt-10 flex-1 overflow-auto">
<template v-for="item in displayList" :key="item.rule.id">
<div
Expand All @@ -61,36 +63,20 @@
</div>
</template>
<script setup lang="tsx">
import { v4 as uuidV4 } from 'uuid';
import pLimit from 'p-limit';
import { ContentType } from '@any-reader/rule-utils';
import { CONTENT_TYPES } from '@/constants';
import { searchByRuleId } from '@/api';
import { useRulesStore } from '@/stores/rules';
import TreeItem from '@/components/vsc/TreeItem.vue';
import { useSearch } from '@/pages/common/search';
import { useRulesStore } from '@/stores/rules';
const rulesStore = useRulesStore();
const router = useRouter();
const runPromise = pLimit(5);
let uuid: string = '';
const searchText = ref('');
const expand = ref(false);
const contentType = ref(ContentType.NOVEL);
const filterType = ref(1);
const loading = ref(false);
const total = ref(0);
const runCount = ref(0);
type List = {
opened: boolean;
rule: any;
list: any[];
};
const list = ref<List[]>([]);
const { contentType, searchText, onSearch, cancelSearch, loading, runCount, total, list, getChapter } = useSearch();
const displayList = computed<List[]>(() => {
const displayList = computed<any[]>(() => {
if (filterType.value === 1) {
return list.value;
}
Expand All @@ -104,63 +90,10 @@ const displayList = computed<List[]>(() => {
.filter((ruleRow: any) => ruleRow.list.length);
});
async function onSearch() {
const lastUuid = uuidV4();
uuid = lastUuid;
list.value = [];
total.value = 0;
runCount.value = 0;
loading.value = true;
const rules = rulesStore.list.filter((e) => contentType.value === e.contentType && e.enableSearch);
total.value = rules.length;
const tasks = rules.map((rule) =>
runPromise(async () => {
if (lastUuid !== uuid) return;
runCount.value++;
const res = await searchByRuleId({ ruleId: rule.id, keyword: searchText.value });
if (res.code === 0) {
const rows = res.data;
if (!rows.length) return;
console.log({ rows });
list.value.push({
opened: true,
rule: rule,
list: rows
});
}
})
);
await Promise.all(tasks).catch(() => {});
loading.value = false;
}
function cancelSearch() {
uuid = uuidV4();
loading.value = false;
}
function getChapter(row: any, rule: any) {
router.push({
path: '/chapter',
query: {
...row,
filePath: row.url,
ruleId: rule.id
}
});
}
function changeOpened(ruleId: string) {
const row = list.value.find((e) => e.rule.id === ruleId);
if (row) {
row.opened = !row.opened;
}
}
onDeactivated(() => {
cancelSearch();
});
</script>

0 comments on commit 573ca04

Please sign in to comment.