#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">
|
<script lang="ts">
|
||||||
import { MarkdownView, Notice, TFile } from 'obsidian'
|
import { MarkdownView, Notice, TFile } from 'obsidian'
|
||||||
import { onMount, onDestroy, tick } from 'svelte'
|
import { onMount, onDestroy, tick } from 'svelte'
|
||||||
@@ -14,10 +10,11 @@
|
|||||||
import { OmnisearchInFileModal, type OmnisearchVaultModal } from 'src/modals'
|
import { OmnisearchInFileModal, type OmnisearchVaultModal } from 'src/modals'
|
||||||
import ResultItemVault from './ResultItemVault.svelte'
|
import ResultItemVault from './ResultItemVault.svelte'
|
||||||
import { Query } from 'src/query'
|
import { Query } from 'src/query'
|
||||||
|
import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||||
|
|
||||||
export let modal: OmnisearchVaultModal
|
export let modal: OmnisearchVaultModal
|
||||||
let selectedIndex = 0
|
let selectedIndex = 0
|
||||||
let lastSearchIndex = 0
|
let historySearchIndex = 0
|
||||||
let searchQuery: string
|
let searchQuery: string
|
||||||
let resultNotes: ResultNote[] = []
|
let resultNotes: ResultNote[] = []
|
||||||
let query: Query
|
let query: Query
|
||||||
@@ -32,8 +29,7 @@
|
|||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await reindexNotes()
|
await reindexNotes()
|
||||||
searchQuery = lastSearches[lastSearchIndex]
|
searchQuery = searchHistory[historySearchIndex]
|
||||||
console.log(lastSearches)
|
|
||||||
eventBus.enable('vault')
|
eventBus.enable('vault')
|
||||||
eventBus.on('vault', 'enter', openNoteAndCloseModal)
|
eventBus.on('vault', 'enter', openNoteAndCloseModal)
|
||||||
eventBus.on('vault', 'shift-enter', createNoteAndCloseModal)
|
eventBus.on('vault', 'shift-enter', createNoteAndCloseModal)
|
||||||
@@ -42,25 +38,25 @@
|
|||||||
eventBus.on('vault', 'tab', switchToInFileModal)
|
eventBus.on('vault', 'tab', switchToInFileModal)
|
||||||
eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
|
eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
|
||||||
eventBus.on('vault', 'arrow-down', () => moveIndex(1))
|
eventBus.on('vault', 'arrow-down', () => moveIndex(1))
|
||||||
eventBus.on('vault', 'prev-search-history', () => prevSearch())
|
eventBus.on('vault', 'prev-search-history', () => prevSearchHistory())
|
||||||
eventBus.on('vault', 'next-search-history', () => nextSearch())
|
eventBus.on('vault', 'next-search-history', () => nextSearchHistory())
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
eventBus.disable('vault')
|
eventBus.disable('vault')
|
||||||
})
|
})
|
||||||
|
|
||||||
function prevSearch() {
|
function prevSearchHistory() {
|
||||||
if (++lastSearchIndex >= lastSearches.length) {
|
if (++historySearchIndex >= searchHistory.length) {
|
||||||
lastSearchIndex = 0
|
historySearchIndex = 0
|
||||||
}
|
}
|
||||||
searchQuery = lastSearches[lastSearchIndex]
|
searchQuery = searchHistory[historySearchIndex]
|
||||||
}
|
}
|
||||||
function nextSearch() {
|
function nextSearchHistory() {
|
||||||
if (--lastSearchIndex < 0) {
|
if (--historySearchIndex < 0) {
|
||||||
lastSearchIndex = lastSearches.length ? lastSearches.length - 1 : 0
|
historySearchIndex = searchHistory.length ? searchHistory.length - 1 : 0
|
||||||
}
|
}
|
||||||
searchQuery = lastSearches[lastSearchIndex]
|
searchQuery = searchHistory[historySearchIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateResults() {
|
async function updateResults() {
|
||||||
@@ -91,7 +87,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function openSearchResult(note: ResultNote, newPane = false) {
|
function openSearchResult(note: ResultNote, newPane = false) {
|
||||||
lastSearches.unshift(searchQuery)
|
searchHistory.unshift(searchQuery)
|
||||||
|
saveSearchHistory()
|
||||||
openNote(note, newPane)
|
openNote(note, newPane)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +184,10 @@
|
|||||||
<div class="prompt-instruction">
|
<div class="prompt-instruction">
|
||||||
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
|
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="prompt-instruction">
|
||||||
|
<span class="prompt-instruction-command">alt ↑↓</span><span
|
||||||
|
>to cycle history</span>
|
||||||
|
</div>
|
||||||
<div class="prompt-instruction">
|
<div class="prompt-instruction">
|
||||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export const eventBus = new EventBus()
|
|||||||
|
|
||||||
export const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json`
|
export const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/searchIndex.json`
|
||||||
export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
|
export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
|
||||||
|
export const historyFilePath = `${app.vault.configDir}/plugins/omnisearch/historyCache.json`
|
||||||
|
|
||||||
export type SearchNote = {
|
export type SearchNote = {
|
||||||
path: string
|
path: string
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { loadSettings, settings, SettingsTab } from './settings'
|
|||||||
import { eventBus } from './globals'
|
import { eventBus } from './globals'
|
||||||
import { registerAPI } from '@vanakat/plugin-api'
|
import { registerAPI } from '@vanakat/plugin-api'
|
||||||
import api from './api'
|
import api from './api'
|
||||||
|
import { loadSearchHistory } from './search-history'
|
||||||
|
|
||||||
// let mainWindow: { on: any; off: any } | null = null
|
// let mainWindow: { on: any; off: any } | null = null
|
||||||
// try {
|
// try {
|
||||||
@@ -30,6 +31,7 @@ function _registerAPI(plugin: OmnisearchPlugin): void {
|
|||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
|
await loadSearchHistory()
|
||||||
|
|
||||||
_registerAPI(this)
|
_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()
|
const serializedIndexDesc = new DocumentFragment()
|
||||||
serializedIndexDesc.createSpan({}, span => {
|
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>⚠️ 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>
|
<strong>Needs a restart to fully take effect.</strong>
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user