Refactoring to Svelte - still a bit messy

Also, issues with eslint
This commit is contained in:
Simon Cambier
2022-04-16 14:59:02 +02:00
parent 2e5bd085bd
commit 75af87849b
7 changed files with 185 additions and 195 deletions

View File

@@ -1,18 +1,23 @@
<script lang="ts">
import CmpInput from './CmpInput.svelte'
import CmpNoteResult from './CmpNoteResult.svelte'
import type { ResultNote } from './globals'
import type OmnisearchPlugin from './main'
import { resultNotes, searchQuery, selectedNoteId } from './stores'
import { escapeHTML, escapeRegex, getAllIndexes, highlighter } from './utils'
import { MarkdownView, TFile } from "obsidian"
import { tick } from "svelte"
import CmpInput from "./CmpInput.svelte"
import CmpNoteResult from "./CmpNoteResult.svelte"
import type { ResultNote } from "./globals"
import type OmnisearchPlugin from "./main"
import type { OmnisearchModal } from "./modal"
import { resultNotes, searchQuery, selectedNote } from "./stores"
import { escapeHTML, escapeRegex, getAllIndexes, highlighter } from "./utils"
export let plugin: OmnisearchPlugin
export let modal: OmnisearchModal
searchQuery.subscribe(q => {
searchQuery.subscribe(async (q) => {
const results = getSuggestions(q)
resultNotes.set(results)
if (results.length) {
selectedNoteId.set(results[0].path)
await tick()
selectedNote.set(results[0])
}
})
@@ -20,8 +25,8 @@ function getSuggestions(query: string): ResultNote[] {
const results = plugin.minisearch
.search(query, {
prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false),
combineWith: 'AND',
fuzzy: (term) => (term.length > 4 ? 0.2 : false),
combineWith: "AND",
boost: {
basename: 2,
headings1: 1.5,
@@ -34,62 +39,83 @@ function getSuggestions(query: string): ResultNote[] {
// console.log(`Omnisearch - Results for "${query}"`)
// console.log(results)
const suggestions =
results.map(result => {
let note = plugin.indexedNotes[result.id]
let basename = escapeHTML(note.basename)
let content = escapeHTML(note.content)
const suggestions = results.map((result) => {
let note = plugin.indexedNotes[result.id]
let basename = escapeHTML(note.basename)
let content = escapeHTML(note.content)
// Sort the terms from smaller to larger
// and highlight them in the title and body
const terms = result.terms.sort((a, b) => a.length - b.length)
const reg = new RegExp(terms.map(escapeRegex).join('|'), 'gi')
const matches = getAllIndexes(content, reg)
// Sort the terms from smaller to larger
// and highlight them in the title and body
const terms = result.terms.sort((a, b) => a.length - b.length)
const reg = new RegExp(terms.map(escapeRegex).join("|"), "gi")
const matches = getAllIndexes(content, reg)
// If the body contains a searched term, find its position
// and trim the text around it
const pos = content.toLowerCase().indexOf(result.terms[0])
const surroundLen = 180
if (pos > -1) {
const from = Math.max(0, pos - surroundLen)
const to = Math.min(content.length - 1, pos + surroundLen)
content =
(from > 0 ? '…' : '') +
content.slice(from, to).trim() +
(to < content.length - 1 ? '…' : '')
}
// If the body contains a searched term, find its position
// and trim the text around it
const pos = content.toLowerCase().indexOf(result.terms[0])
const surroundLen = 180
if (pos > -1) {
const from = Math.max(0, pos - surroundLen)
const to = Math.min(content.length - 1, pos + surroundLen)
content =
(from > 0 ? "…" : "") +
content.slice(from, to).trim() +
(to < content.length - 1 ? "…" : "")
}
// console.log(matches)
content = content.replace(reg, highlighter)
basename = basename.replace(reg, highlighter)
// console.log(matches)
content = content.replace(reg, highlighter)
basename = basename.replace(reg, highlighter)
const resultNote: ResultNote = {
content,
basename,
path: note.path,
matches,
occurence: 0,
}
return resultNote
})
const resultNote: ResultNote = {
content,
basename,
path: note.path,
matches,
occurence: 0,
}
return resultNote
})
return suggestions
}
async function onChooseSuggestion(item: ResultNote): Promise<void> {
const file = plugin.app.vault.getAbstractFileByPath(item.path) as TFile
// const fileCache = this.app.metadataCache.getFileCache(file)
// console.log(fileCache)
const content = (await plugin.app.vault.cachedRead(file)).toLowerCase()
const offset = content.indexOf(
item.matches[item.occurence].match.toLowerCase()
)
await plugin.app.workspace.openLinkText(item.path, "")
const view = plugin.app.workspace.getActiveViewOfType(MarkdownView)
if (!view) {
throw new Error("OmniSearch - No active MarkdownView")
}
const pos = view.editor.offsetToPos(offset)
pos.ch = 0
view.editor.setCursor(pos)
view.editor.scrollIntoView({
from: { line: pos.line - 10, ch: 0 },
to: { line: pos.line + 10, ch: 0 },
})
}
function openNote(event: CustomEvent<ResultNote>): void {
onChooseSuggestion(event.detail)
modal.close()
}
</script>
<CmpInput />
<CmpInput on:enter={openNote} />
<div class="prompt-results">
{#each $resultNotes as result (result.path)}
<CmpNoteResult
selected={result.path === $selectedNoteId}
id={result.path}
title={result.basename}
content={result.content}
nbMatches={result.matches.length}
/>
<CmpNoteResult selected={result === $selectedNote} note={result} />
{/each}
</div>
<div class="prompt-instructions">