| @@ -654,16 +654,17 @@ watch( |
| 654 | 654 | () => |
| 655 | 655 | [ |
| 656 | 656 | searchWorkerReady.value, |
| 657 | showSearch.value, |
| 657 | 658 | urlSearchMode.value, |
| 658 | 659 | urlFilterDebounced.value |
| 659 | 660 | ] as const, |
| 660 | | async ([ready, active, query], _old, onCleanup) => { |
| 661 | async ([ready, isOpen, active, query], _old, onCleanup) => { |
| 661 | 662 | let canceled = false |
| 662 | 663 | onCleanup(() => { |
| 663 | 664 | canceled = true |
| 664 | 665 | }) |
| 665 | 666 | |
| 666 | | if (!active || !query.trim()) { |
| 667 | if (!active || !isOpen || !query.trim()) { |
| 667 | 668 | urlMatches.value = [] |
| 668 | 669 | urlPageGroupCounts.value = [] |
| 669 | 670 | return |
| @@ -691,7 +692,17 @@ watch( |
| 691 | 692 | ) |
| 692 | 693 | |
| 693 | 694 | function getPageKey(id: string) { |
| 694 | | return id.split('#')[0].replace(/\/$/, '') |
| 695 | return getDocId(id).replace(/\/$/, '') |
| 696 | } |
| 697 | |
| 698 | function getDocId(id: string) { |
| 699 | const hashIndex = id.indexOf('#') |
| 700 | return hashIndex === -1 ? id : id.slice(0, hashIndex) |
| 701 | } |
| 702 | |
| 703 | function getDocAnchor(id: string) { |
| 704 | const hashIndex = id.indexOf('#') |
| 705 | return hashIndex === -1 ? '' : id.slice(hashIndex + 1) |
| 695 | 706 | } |
| 696 | 707 | |
| 697 | 708 | function getPageLabel(key: string) { |
| @@ -1014,6 +1025,7 @@ watchDebounced( |
| 1014 | 1025 | () => |
| 1015 | 1026 | [ |
| 1016 | 1027 | searchWorkerReady.value, |
| 1028 | showSearch.value, |
| 1017 | 1029 | localeIndex.value, |
| 1018 | 1030 | searchIndexVersion.value, |
| 1019 | 1031 | searchWorkerConfigKey.value, |
| @@ -1024,6 +1036,7 @@ watchDebounced( |
| 1024 | 1036 | async ( |
| 1025 | 1037 | [ |
| 1026 | 1038 | ready, |
| 1039 | isOpen, |
| 1027 | 1040 | localeIndexValue, |
| 1028 | 1041 | indexVersion, |
| 1029 | 1042 | configKey, |
| @@ -1035,9 +1048,9 @@ watchDebounced( |
| 1035 | 1048 | onCleanup |
| 1036 | 1049 | ) => { |
| 1037 | 1050 | if ( |
| 1038 | | old?.[1] !== localeIndexValue || |
| 1039 | | old?.[2] !== indexVersion || |
| 1040 | | old?.[3] !== configKey |
| 1051 | old?.[2] !== localeIndexValue || |
| 1052 | old?.[3] !== indexVersion || |
| 1053 | old?.[4] !== configKey |
| 1041 | 1054 | ) { |
| 1042 | 1055 | // Locale/config changes rebuild the worker index, so cached excerpts may no |
| 1043 | 1056 | // longer match the active result set. |
| @@ -1049,13 +1062,21 @@ watchDebounced( |
| 1049 | 1062 | canceled = true |
| 1050 | 1063 | }) |
| 1051 | 1064 | |
| 1052 | | if (!ready) return |
| 1065 | if (!ready || !isOpen) return |
| 1053 | 1066 | |
| 1054 | 1067 | // In URL search mode, skip normal text search |
| 1055 | 1068 | if (mode === 'url') { |
| 1056 | 1069 | results.value = [] |
| 1057 | 1070 | currentTerms.value = new Set() |
| 1058 | | enableNoResults.value = true |
| 1071 | enableNoResults.value = Boolean(filterTextValue.trim()) |
| 1072 | return |
| 1073 | } |
| 1074 | |
| 1075 | const searchQuery = filterTextValue.trim() |
| 1076 | if (!searchQuery) { |
| 1077 | results.value = [] |
| 1078 | currentTerms.value = new Set() |
| 1079 | enableNoResults.value = false |
| 1059 | 1080 | return |
| 1060 | 1081 | } |
| 1061 | 1082 | |
| @@ -1080,7 +1101,7 @@ watchDebounced( |
| 1080 | 1101 | |
| 1081 | 1102 | workerPayload = await postSearchWorker<TextSearchWorkerPayload>({ |
| 1082 | 1103 | type: 'text-search', |
| 1083 | | query: filterTextValue, |
| 1104 | query: searchQuery, |
| 1084 | 1105 | mode, |
| 1085 | 1106 | pageOrderEntries: getPageOrderEntries() |
| 1086 | 1107 | }) |
| @@ -1107,8 +1128,9 @@ watchDebounced( |
| 1107 | 1128 | |
| 1108 | 1129 | const toRows = (list: (SearchResult & Result)[]) => |
| 1109 | 1130 | list.map((r) => { |
| 1110 | | const [id, anchor] = r.id.split('#') |
| 1111 | | const map = cache.get(`${id}\n${filterTextValue}`) |
| 1131 | const id = getDocId(r.id) |
| 1132 | const anchor = getDocAnchor(r.id) |
| 1133 | const map = cache.get(`${id}\n${searchQuery}`) |
| 1112 | 1134 | return { ...r, text: map?.get(anchor) ?? '' } |
| 1113 | 1135 | }) |
| 1114 | 1136 | |
| @@ -1130,7 +1152,7 @@ watchDebounced( |
| 1130 | 1152 | const docOrderList: string[] = [] |
| 1131 | 1153 | const seenDocs = new Set<string>() |
| 1132 | 1154 | for (const r of _result) { |
| 1133 | | const docId = r.id.slice(0, r.id.indexOf('#')) |
| 1155 | const docId = getDocId(r.id) |
| 1134 | 1156 | if (!seenDocs.has(docId)) { |
| 1135 | 1157 | seenDocs.add(docId) |
| 1136 | 1158 | docOrderList.push(docId) |
| @@ -1144,10 +1166,10 @@ watchDebounced( |
| 1144 | 1166 | if (showDetailedListValue) { |
| 1145 | 1167 | const eagerDocs = new Set<string>() |
| 1146 | 1168 | for (const r of _result.slice(0, EAGER_EXCERPTS)) { |
| 1147 | | eagerDocs.add(r.id.slice(0, r.id.indexOf('#'))) |
| 1169 | eagerDocs.add(getDocId(r.id)) |
| 1148 | 1170 | } |
| 1149 | 1171 | await Promise.all( |
| 1150 | | [...eagerDocs].map((docId) => buildDocExcerpt(docId, filterTextValue)) |
| 1172 | [...eagerDocs].map((docId) => buildDocExcerpt(docId, searchQuery)) |
| 1151 | 1173 | ) |
| 1152 | 1174 | if (canceled) return |
| 1153 | 1175 | } |
| @@ -1169,12 +1191,12 @@ watchDebounced( |
| 1169 | 1191 | if (showDetailedListValue) { |
| 1170 | 1192 | const built = new Set<string>() |
| 1171 | 1193 | for (const r of _result.slice(0, EAGER_EXCERPTS)) { |
| 1172 | | built.add(r.id.slice(0, r.id.indexOf('#'))) |
| 1194 | built.add(getDocId(r.id)) |
| 1173 | 1195 | } |
| 1174 | 1196 | const remaining = docOrderList.filter((d) => !built.has(d)) |
| 1175 | 1197 | if (remaining.length) { |
| 1176 | 1198 | for (const docId of remaining) { |
| 1177 | | await buildDocExcerpt(docId, filterTextValue) |
| 1199 | await buildDocExcerpt(docId, searchQuery) |
| 1178 | 1200 | if (canceled) return |
| 1179 | 1201 | await nextFrame() |
| 1180 | 1202 | if (canceled) return |
| @@ -1254,8 +1276,7 @@ onBeforeUnmount(() => { |
| 1254 | 1276 | const excerptCache = new LRUCache<string, unknown>(24) |
| 1255 | 1277 | |
| 1256 | 1278 | async function fetchExcerpt(id: string) { |
| 1257 | | const hashIndex = id.indexOf('#') |
| 1258 | | const docId = hashIndex === -1 ? id : id.slice(0, hashIndex) |
| 1279 | const docId = getDocId(id) |
| 1259 | 1280 | |
| 1260 | 1281 | const cached = excerptCache.get(docId) |
| 1261 | 1282 | if (cached) return { id, mod: cached } |