Tab to switch contexts, alt+enter to inject a link
This commit is contained in:
@@ -15,7 +15,7 @@ import { onDestroy, onMount, tick } from "svelte"
|
||||
import { MarkdownView } from "obsidian"
|
||||
import { getSuggestions } from "src/search"
|
||||
import ModalContainer from "./ModalContainer.svelte"
|
||||
import type { OmnisearchInFileModal, OmnisearchVaultModal } from "src/modals"
|
||||
import { OmnisearchInFileModal, OmnisearchVaultModal } from "src/modals"
|
||||
import ResultItemInFile from "./ResultItemInFile.svelte"
|
||||
import { Query } from "src/query"
|
||||
|
||||
@@ -33,15 +33,16 @@ onMount(() => {
|
||||
if (lastSearch && !searchQuery) {
|
||||
searchQuery = lastSearch
|
||||
}
|
||||
eventBus.disable("vault")
|
||||
eventBus.enable("infile")
|
||||
|
||||
eventBus.on("infile", "enter", openSelection)
|
||||
eventBus.on("infile", "arrow-up", () => moveIndex(-1))
|
||||
eventBus.on("infile", "arrow-down", () => moveIndex(1))
|
||||
eventBus.on("infile", "tab", switchToVaultModal)
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
eventBus.enable("vault")
|
||||
eventBus.disable("infile")
|
||||
})
|
||||
|
||||
$: (async () => {
|
||||
@@ -127,6 +128,11 @@ async function openSelection(): Promise<void> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function switchToVaultModal(): void {
|
||||
new OmnisearchVaultModal(app).open()
|
||||
modal.close()
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="modal-title">Omnisearch - File</div>
|
||||
@@ -155,12 +161,16 @@ async function openSelection(): Promise<void> {
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↹</span>
|
||||
<span>to switch to Vault Search</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span>
|
||||
{#if !!parent}
|
||||
<span>to go back to Vault Search</span>
|
||||
{:else}
|
||||
<span>to dismiss</span>
|
||||
<span>to close</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,8 +3,8 @@ let lastSearch = ""
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { Notice, TFile } from "obsidian"
|
||||
import { onMount, tick } from "svelte"
|
||||
import { MarkdownView, Notice, TFile } from "obsidian"
|
||||
import { onMount, onDestroy, tick } from "svelte"
|
||||
import InputSearch from "./InputSearch.svelte"
|
||||
import ModalContainer from "./ModalContainer.svelte"
|
||||
import { eventBus, type ResultNote } from "src/globals"
|
||||
@@ -31,14 +31,20 @@ $: if (searchQuery) {
|
||||
onMount(() => {
|
||||
reindexNotes()
|
||||
searchQuery = lastSearch
|
||||
eventBus.on("vault", "enter", onInputEnter)
|
||||
eventBus.on("vault", "shift-enter", onInputShiftEnter)
|
||||
eventBus.on("vault", "ctrl-enter", onInputCtrlEnter)
|
||||
eventBus.on("vault", "alt-enter", onInputAltEnter)
|
||||
eventBus.enable("vault")
|
||||
eventBus.on("vault", "enter", openNoteAndCloseModal)
|
||||
eventBus.on("vault", "shift-enter", createNoteAndCloseModal)
|
||||
eventBus.on("vault", "ctrl-enter", openNoteInNewPane)
|
||||
eventBus.on("vault", "alt-enter", insertLink)
|
||||
eventBus.on("vault", "tab", switchToInFileModal)
|
||||
eventBus.on("vault", "arrow-up", () => moveIndex(-1))
|
||||
eventBus.on("vault", "arrow-down", () => moveIndex(1))
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
eventBus.disable("vault")
|
||||
})
|
||||
|
||||
async function updateResults() {
|
||||
query = new Query(searchQuery)
|
||||
resultNotes = await getSuggestions(query)
|
||||
@@ -54,36 +60,71 @@ function onClick() {
|
||||
modal.close()
|
||||
}
|
||||
|
||||
function onInputEnter(): void {
|
||||
function openNoteAndCloseModal(): void {
|
||||
// console.log(event.detail)
|
||||
if (!selectedNote) return
|
||||
openNote(selectedNote)
|
||||
modal.close()
|
||||
}
|
||||
|
||||
function onInputCtrlEnter(): void {
|
||||
function openNoteInNewPane(): void {
|
||||
if (!selectedNote) return
|
||||
openNote(selectedNote, true)
|
||||
modal.close()
|
||||
}
|
||||
|
||||
async function onInputShiftEnter(): Promise<void> {
|
||||
async function createNoteAndCloseModal(): Promise<void> {
|
||||
try {
|
||||
await createNote(searchQuery)
|
||||
}
|
||||
catch(e) {
|
||||
} catch (e) {
|
||||
new Notice((e as Error).message)
|
||||
return
|
||||
}
|
||||
modal.close()
|
||||
}
|
||||
|
||||
function onInputAltEnter(): void {
|
||||
function insertLink(): void {
|
||||
if (!selectedNote) return
|
||||
const file = app.vault
|
||||
.getMarkdownFiles()
|
||||
.find((f) => f.path === selectedNote.path)
|
||||
const active = app.workspace.getActiveFile()
|
||||
const view = app.workspace.getActiveViewOfType(MarkdownView)
|
||||
if (!view?.editor) {
|
||||
new Notice("Omnisearch - Error - No active editor", 3000)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate link
|
||||
let link: string
|
||||
if (file && active) {
|
||||
link = app.fileManager.generateMarkdownLink(file, active.path)
|
||||
} else {
|
||||
link = `[[${selectedNote.basename}.md]]`
|
||||
}
|
||||
|
||||
// Inject link
|
||||
const cursor = view.editor.getCursor()
|
||||
view.editor.replaceRange(link, cursor, cursor)
|
||||
cursor.ch += link.length
|
||||
view.editor.setCursor(cursor)
|
||||
|
||||
modal.close()
|
||||
}
|
||||
|
||||
function switchToInFileModal(): void {
|
||||
modal.close()
|
||||
if (selectedNote) {
|
||||
// Open in-file modal for selected search result
|
||||
const file = app.vault.getAbstractFileByPath(selectedNote.path)
|
||||
if (file && file instanceof TFile) {
|
||||
// modal.close()
|
||||
new OmnisearchInFileModal(app, file, searchQuery, modal).open()
|
||||
new OmnisearchInFileModal(app, file, searchQuery).open()
|
||||
}
|
||||
} else {
|
||||
// Open in-file modal for active file
|
||||
const view = app.workspace.getActiveViewOfType(MarkdownView)
|
||||
if (view) {
|
||||
new OmnisearchInFileModal(app, view.file, searchQuery).open()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,14 +166,15 @@ async function scrollIntoView(): Promise<void> {
|
||||
<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 expand in-note results</span>
|
||||
</div>
|
||||
<br />
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↹</span>
|
||||
<span>to switch to In-File Search</span>
|
||||
</div>
|
||||
<br />
|
||||
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">ctrl ↵</span>
|
||||
<span>to open in a new pane</span>
|
||||
@@ -142,6 +184,10 @@ async function scrollIntoView(): Promise<void> {
|
||||
<span>to create</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
|
||||
<span class="prompt-instruction-command">alt ↵</span>
|
||||
<span>to insert a link</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span><span>to close</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -34,9 +34,10 @@ export class EventBus {
|
||||
}
|
||||
|
||||
public emit(event: string, ...args: any[]): void {
|
||||
for (const [key, handler] of this.handlers.entries()) {
|
||||
const ctx = key.split('@')[0]
|
||||
if (this.disabled.includes(ctx)) continue
|
||||
const entries = [...this.handlers.entries()].filter(
|
||||
([k, h]) => !this.disabled.includes(k.split('@')[0]),
|
||||
)
|
||||
for (const [key, handler] of entries) {
|
||||
if (key.endsWith(`@${event}`)) {
|
||||
handler(...args)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from './search'
|
||||
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
||||
import { loadSettings, SettingsTab } from './settings'
|
||||
import { OmnisearchSuggest } from './suggestions'
|
||||
import { eventBus } from './globals'
|
||||
|
||||
// let mainWindow: { on: any; off: any } | null = null
|
||||
// try {
|
||||
@@ -21,7 +21,8 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
async onload(): Promise<void> {
|
||||
await loadSettings(this)
|
||||
this.addSettingTab(new SettingsTab(this))
|
||||
this.registerEditorSuggest(new OmnisearchSuggest(app))
|
||||
eventBus.disable('vault')
|
||||
eventBus.disable('infile')
|
||||
|
||||
// Commands to display Omnisearch modals
|
||||
this.addCommand({
|
||||
|
||||
@@ -73,7 +73,7 @@ abstract class OmnisearchModal extends Modal {
|
||||
|
||||
this.scope.register(['Alt'], 'Enter', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('alt-enter') // Open the InFile modal
|
||||
eventBus.emit('alt-enter') // Insert link
|
||||
})
|
||||
|
||||
this.scope.register(['Shift'], 'Enter', e => {
|
||||
@@ -88,6 +88,11 @@ abstract class OmnisearchModal extends Modal {
|
||||
eventBus.emit('enter') // Open in current pane
|
||||
}
|
||||
})
|
||||
|
||||
this.scope.register([], 'Tab', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('tab') // Switch context
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
// Parts of this code come from https://github.com/valentine195/obsidian-admonition/blob/e4aa52fe04bb68a5483421ef98414c2617d666b7/src/suggest/suggest.ts
|
||||
|
||||
import {
|
||||
Editor,
|
||||
EditorSuggest,
|
||||
TFile,
|
||||
type EditorPosition,
|
||||
type EditorSuggestContext,
|
||||
type EditorSuggestTriggerInfo,
|
||||
} from 'obsidian'
|
||||
|
||||
export class OmnisearchSuggest extends EditorSuggest<string> {
|
||||
onTrigger(
|
||||
cursor: EditorPosition,
|
||||
editor: Editor,
|
||||
file: TFile,
|
||||
): EditorSuggestTriggerInfo | null {
|
||||
const line = editor.getLine(cursor.line)
|
||||
// not inside the bracket
|
||||
if (/\[@.+\]/.test(line.slice(0, cursor.ch))) return null
|
||||
if (!/\[@.*/.test(line)) return null
|
||||
|
||||
const match = line.match(/\[@([^\]]*)\]?/) // [@(foo bar)] baz
|
||||
if (!match) return null
|
||||
|
||||
const [_, query] = match
|
||||
if (!query) {
|
||||
return null
|
||||
}
|
||||
const matchData = {
|
||||
end: cursor,
|
||||
start: {
|
||||
ch: (match.index ?? 0) + 4,
|
||||
line: cursor.line,
|
||||
},
|
||||
query,
|
||||
}
|
||||
return matchData
|
||||
}
|
||||
|
||||
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
||||
return ['foo', 'bar']
|
||||
}
|
||||
|
||||
renderSuggestion(value: string, el: HTMLElement): void {
|
||||
el.createSpan({ text: value })
|
||||
}
|
||||
|
||||
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
||||
throw new Error('Method not implemented.')
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] {
|
||||
}
|
||||
|
||||
export function stringsToRegex(strings: string[]): RegExp {
|
||||
if (!strings.length) return /^$/
|
||||
if (!strings.length) return /^$/g
|
||||
return new RegExp(strings.map(s => `(${escapeRegex(s)})`).join('|'), 'gi')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user