#90 - search history ok
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
<script lang="ts" context="module">
|
||||
let lastSearches: string[] = []
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { MarkdownView, Notice, TFile } from 'obsidian'
|
||||
import { onMount, onDestroy, tick } from 'svelte'
|
||||
@@ -14,10 +10,11 @@
|
||||
import { OmnisearchInFileModal, type OmnisearchVaultModal } from 'src/modals'
|
||||
import ResultItemVault from './ResultItemVault.svelte'
|
||||
import { Query } from 'src/query'
|
||||
import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
|
||||
export let modal: OmnisearchVaultModal
|
||||
let selectedIndex = 0
|
||||
let lastSearchIndex = 0
|
||||
let historySearchIndex = 0
|
||||
let searchQuery: string
|
||||
let resultNotes: ResultNote[] = []
|
||||
let query: Query
|
||||
@@ -32,8 +29,7 @@
|
||||
|
||||
onMount(async () => {
|
||||
await reindexNotes()
|
||||
searchQuery = lastSearches[lastSearchIndex]
|
||||
console.log(lastSearches)
|
||||
searchQuery = searchHistory[historySearchIndex]
|
||||
eventBus.enable('vault')
|
||||
eventBus.on('vault', 'enter', openNoteAndCloseModal)
|
||||
eventBus.on('vault', 'shift-enter', createNoteAndCloseModal)
|
||||
@@ -42,25 +38,25 @@
|
||||
eventBus.on('vault', 'tab', switchToInFileModal)
|
||||
eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
|
||||
eventBus.on('vault', 'arrow-down', () => moveIndex(1))
|
||||
eventBus.on('vault', 'prev-search-history', () => prevSearch())
|
||||
eventBus.on('vault', 'next-search-history', () => nextSearch())
|
||||
eventBus.on('vault', 'prev-search-history', () => prevSearchHistory())
|
||||
eventBus.on('vault', 'next-search-history', () => nextSearchHistory())
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
eventBus.disable('vault')
|
||||
})
|
||||
|
||||
function prevSearch() {
|
||||
if (++lastSearchIndex >= lastSearches.length) {
|
||||
lastSearchIndex = 0
|
||||
function prevSearchHistory() {
|
||||
if (++historySearchIndex >= searchHistory.length) {
|
||||
historySearchIndex = 0
|
||||
}
|
||||
searchQuery = lastSearches[lastSearchIndex]
|
||||
searchQuery = searchHistory[historySearchIndex]
|
||||
}
|
||||
function nextSearch() {
|
||||
if (--lastSearchIndex < 0) {
|
||||
lastSearchIndex = lastSearches.length ? lastSearches.length - 1 : 0
|
||||
function nextSearchHistory() {
|
||||
if (--historySearchIndex < 0) {
|
||||
historySearchIndex = searchHistory.length ? searchHistory.length - 1 : 0
|
||||
}
|
||||
searchQuery = lastSearches[lastSearchIndex]
|
||||
searchQuery = searchHistory[historySearchIndex]
|
||||
}
|
||||
|
||||
async function updateResults() {
|
||||
@@ -91,7 +87,8 @@
|
||||
}
|
||||
|
||||
function openSearchResult(note: ResultNote, newPane = false) {
|
||||
lastSearches.unshift(searchQuery)
|
||||
searchHistory.unshift(searchQuery)
|
||||
saveSearchHistory()
|
||||
openNote(note, newPane)
|
||||
}
|
||||
|
||||
@@ -187,6 +184,10 @@
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">alt ↑↓</span><span
|
||||
>to cycle history</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||
</div>
|
||||
|
||||
@@ -16,6 +16,7 @@ export const eventBus = new EventBus()
|
||||
|
||||
export const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json`
|
||||
export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
|
||||
export const historyFilePath = `${app.vault.configDir}/plugins/omnisearch/historyCache.json`
|
||||
|
||||
export type SearchNote = {
|
||||
path: string
|
||||
|
||||
@@ -10,6 +10,7 @@ import { loadSettings, settings, SettingsTab } from './settings'
|
||||
import { eventBus } from './globals'
|
||||
import { registerAPI } from '@vanakat/plugin-api'
|
||||
import api from './api'
|
||||
import { loadSearchHistory } from './search-history'
|
||||
|
||||
// let mainWindow: { on: any; off: any } | null = null
|
||||
// try {
|
||||
@@ -30,6 +31,7 @@ function _registerAPI(plugin: OmnisearchPlugin): void {
|
||||
export default class OmnisearchPlugin extends Plugin {
|
||||
async onload(): Promise<void> {
|
||||
await loadSettings(this)
|
||||
await loadSearchHistory()
|
||||
|
||||
_registerAPI(this)
|
||||
|
||||
|
||||
28
src/search-history.ts
Normal file
28
src/search-history.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { searchIndexFilePath } from './globals'
|
||||
|
||||
export let searchHistory: string[] = []
|
||||
|
||||
export async function loadSearchHistory(): Promise<void> {
|
||||
if (await app.vault.adapter.exists(searchIndexFilePath)) {
|
||||
try {
|
||||
searchHistory = JSON.parse(
|
||||
await app.vault.adapter.read(searchIndexFilePath)
|
||||
)
|
||||
// Keep the last 100 searches
|
||||
searchHistory = searchHistory.slice(0, 100)
|
||||
} catch (e) {
|
||||
console.trace('Could not load search history from the file')
|
||||
console.error(e)
|
||||
searchHistory = []
|
||||
}
|
||||
} else {
|
||||
searchHistory = []
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSearchHistory(): Promise<void> {
|
||||
await app.vault.adapter.write(
|
||||
searchIndexFilePath,
|
||||
JSON.stringify(searchHistory)
|
||||
)
|
||||
}
|
||||
@@ -71,8 +71,10 @@ export class SettingsTab extends PluginSettingTab {
|
||||
|
||||
const serializedIndexDesc = new DocumentFragment()
|
||||
serializedIndexDesc.createSpan({}, span => {
|
||||
span.innerHTML = `The search index is stored on disk, instead of being rebuilt at every startup. This results in faster loading times for bigger vaults and mobile devices.<br />
|
||||
span.innerHTML = `The search index is stored on disk, instead of being rebuilt at every startup.
|
||||
This results in faster loading times for bigger vaults and mobile devices.<br />
|
||||
<em>⚠️ Note: the index can become corrupted - if you notice any issue, disable and re-enable this option to clear the cache.</em><br/>
|
||||
<em>⚠️ Cache files in <code>.obsidian/plugins/omnisearch/</code> must not be synchronized.</em><br/>
|
||||
<strong>Needs a restart to fully take effect.</strong>
|
||||
`
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user