114 lines
2.7 KiB
TypeScript
114 lines
2.7 KiB
TypeScript
import { ref } from 'vue'
|
|||
|
|
|
||
|
|
const STORAGE_KEY = 'pub_search_history'
|
||
|
|
const MAX_ENTRIES = 50
|
||
|
|
|
||
|
|
export interface HistoryEntry {
|
||
|
|
id: string
|
||
|
|
query: string
|
||
|
|
expanded_query: string
|
||
|
|
result_count: number | null
|
||
|
|
timestamp: string
|
||
|
|
}
|
||
|
|
|
||
|
|
function loadAll(): HistoryEntry[] {
|
||
|
|
try {
|
||
|
|
const raw = localStorage.getItem(STORAGE_KEY)
|
||
|
|
return raw ? JSON.parse(raw) : []
|
||
|
|
} catch {
|
||
|
|
return []
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function saveAll(entries: HistoryEntry[]) {
|
||
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(entries))
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 把 #N 引用替换为 expanded_query(加括号保护优先级) */
|
||
|
|
export function resolveQuery(query: string, entries: HistoryEntry[]): string {
|
||
|
|
return query.replace(/#(\d+)/g, (_m, num) => {
|
||
|
|
const found = entries.find(e => e.id === `#${num}`)
|
||
|
|
return found ? `(${found.expanded_query})` : _m
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 递归展开所有 #N 引用为纯查询 */
|
||
|
|
export function expandQuery(query: string, entries: HistoryEntry[]): string {
|
||
|
|
let prev = ''
|
||
|
|
let current = query
|
||
|
|
for (let i = 0; i < 10; i++) {
|
||
|
|
if (current === prev) break
|
||
|
|
prev = current
|
||
|
|
current = resolveQuery(current, entries)
|
||
|
|
}
|
||
|
|
return current
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useSearchHistory() {
|
||
|
|
const entries = ref<HistoryEntry[]>(loadAll())
|
||
|
|
|
||
|
|
function getAll(): HistoryEntry[] {
|
||
|
|
return [...entries.value]
|
||
|
|
}
|
||
|
|
|
||
|
|
function add(query: string, resultCount?: number | null): HistoryEntry {
|
||
|
|
const all = loadAll()
|
||
|
|
const nextNum = all.length > 0
|
||
|
|
? Math.max(...all.map(e => parseInt(e.id.slice(1), 10))) + 1
|
||
|
|
: 1
|
||
|
|
const id = `#${nextNum}`
|
||
|
|
const expanded = expandQuery(query, all)
|
||
|
|
const entry: HistoryEntry = {
|
||
|
|
id,
|
||
|
|
query,
|
||
|
|
expanded_query: expanded,
|
||
|
|
result_count: resultCount ?? null,
|
||
|
|
timestamp: new Date().toISOString(),
|
||
|
|
}
|
||
|
|
|
||
|
|
if (all.length >= MAX_ENTRIES) {
|
||
|
|
all.sort((a, b) => a.timestamp.localeCompare(b.timestamp))
|
||
|
|
all.shift()
|
||
|
|
}
|
||
|
|
|
||
|
|
all.push(entry)
|
||
|
|
saveAll(all)
|
||
|
|
entries.value = [...all]
|
||
|
|
return entry
|
||
|
|
}
|
||
|
|
|
||
|
|
function remove(id: string) {
|
||
|
|
const all = loadAll().filter(e => e.id !== id)
|
||
|
|
saveAll(all)
|
||
|
|
entries.value = all
|
||
|
|
}
|
||
|
|
|
||
|
|
function clear() {
|
||
|
|
localStorage.removeItem(STORAGE_KEY)
|
||
|
|
entries.value = []
|
||
|
|
}
|
||
|
|
|
||
|
|
function download() {
|
||
|
|
const all = loadAll()
|
||
|
|
const lines = all.map(e =>
|
||
|
|
`${e.id}\t${e.result_count ?? ''}\t${e.timestamp}\t${e.query}`
|
||
|
|
)
|
||
|
|
const blob = new Blob([lines.join('\n')], { type: 'text/plain;charset=utf-8' })
|
||
|
|
const url = URL.createObjectURL(blob)
|
||
|
|
const a = document.createElement('a')
|
||
|
|
a.href = url
|
||
|
|
a.download = `pubmed-search-history-${new Date().toISOString().slice(0, 10)}.tsv`
|
||
|
|
a.click()
|
||
|
|
URL.revokeObjectURL(url)
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
entries,
|
||
|
|
getAll,
|
||
|
|
add,
|
||
|
|
remove,
|
||
|
|
clear,
|
||
|
|
download,
|
||
|
|
}
|
||
|
|
}
|