Fixed #116, overall safer code in ModalVault

This commit is contained in:
Simon Cambier
2022-10-24 22:44:43 +02:00
parent d62fff6ce4
commit 6f7d454470
2 changed files with 18 additions and 11 deletions

View File

@@ -17,8 +17,8 @@
export let modal: OmnisearchVaultModal export let modal: OmnisearchVaultModal
let selectedIndex = 0 let selectedIndex = 0
let historySearchIndex = 0 let historySearchIndex = 0
let searchQuery: string let searchQuery: string | undefined
let previousQuery: string let previousQuery: string | undefined
let resultNotes: ResultNote[] = [] let resultNotes: ResultNote[] = []
let query: Query let query: Query
@@ -100,8 +100,10 @@
} }
function saveCurrentQuery() { function saveCurrentQuery() {
if (searchQuery) {
cacheManager.addToSearchHistory(searchQuery) cacheManager.addToSearchHistory(searchQuery)
} }
}
function openSearchResult(note: ResultNote, newPane = false) { function openSearchResult(note: ResultNote, newPane = false) {
saveCurrentQuery() saveCurrentQuery()
@@ -115,6 +117,7 @@
async function createNoteAndCloseModal(opt?: { async function createNoteAndCloseModal(opt?: {
newLeaf: boolean newLeaf: boolean
}): Promise<void> { }): Promise<void> {
if (searchQuery) {
try { try {
await createNote(searchQuery, opt?.newLeaf) await createNote(searchQuery, opt?.newLeaf)
} catch (e) { } catch (e) {
@@ -123,6 +126,7 @@
} }
modal.close() modal.close()
} }
}
function insertLink(): void { function insertLink(): void {
if (!selectedNote) return if (!selectedNote) return
@@ -154,11 +158,14 @@
} }
function switchToInFileModal(): void { function switchToInFileModal(): void {
if (selectedNote.path.endsWith('.pdf')) { // Do nothing if the selectedNote is a PDF
if (selectedNote?.path.endsWith('.pdf')) {
return return
} }
saveCurrentQuery() saveCurrentQuery()
modal.close() modal.close()
if (selectedNote) { if (selectedNote) {
// Open in-file modal for selected search result // Open in-file modal for selected search result
const file = app.vault.getAbstractFileByPath(selectedNote.path) const file = app.vault.getAbstractFileByPath(selectedNote.path)

View File

@@ -192,7 +192,7 @@ export function getPlaintextExtensions(): string[] {
export function getExtension(path: string): string { export function getExtension(path: string): string {
const split = path.split('.') const split = path.split('.')
return split[split.length - 1] return split[split.length - 1] ?? ''
} }
export function makeMD5(data: BinaryLike): string { export function makeMD5(data: BinaryLike): string {