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 { MarkdownView } from "obsidian"
|
||||||
import { getSuggestions } from "src/search"
|
import { getSuggestions } from "src/search"
|
||||||
import ModalContainer from "./ModalContainer.svelte"
|
import ModalContainer from "./ModalContainer.svelte"
|
||||||
import type { OmnisearchInFileModal, OmnisearchVaultModal } from "src/modals"
|
import { OmnisearchInFileModal, OmnisearchVaultModal } from "src/modals"
|
||||||
import ResultItemInFile from "./ResultItemInFile.svelte"
|
import ResultItemInFile from "./ResultItemInFile.svelte"
|
||||||
import { Query } from "src/query"
|
import { Query } from "src/query"
|
||||||
|
|
||||||
@@ -33,15 +33,16 @@ onMount(() => {
|
|||||||
if (lastSearch && !searchQuery) {
|
if (lastSearch && !searchQuery) {
|
||||||
searchQuery = lastSearch
|
searchQuery = lastSearch
|
||||||
}
|
}
|
||||||
eventBus.disable("vault")
|
eventBus.enable("infile")
|
||||||
|
|
||||||
eventBus.on("infile", "enter", openSelection)
|
eventBus.on("infile", "enter", openSelection)
|
||||||
eventBus.on("infile", "arrow-up", () => moveIndex(-1))
|
eventBus.on("infile", "arrow-up", () => moveIndex(-1))
|
||||||
eventBus.on("infile", "arrow-down", () => moveIndex(1))
|
eventBus.on("infile", "arrow-down", () => moveIndex(1))
|
||||||
|
eventBus.on("infile", "tab", switchToVaultModal)
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
eventBus.enable("vault")
|
eventBus.disable("infile")
|
||||||
})
|
})
|
||||||
|
|
||||||
$: (async () => {
|
$: (async () => {
|
||||||
@@ -127,6 +128,11 @@ async function openSelection(): Promise<void> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function switchToVaultModal(): void {
|
||||||
|
new OmnisearchVaultModal(app).open()
|
||||||
|
modal.close()
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="modal-title">Omnisearch - File</div>
|
<div class="modal-title">Omnisearch - File</div>
|
||||||
@@ -155,12 +161,16 @@ async function openSelection(): Promise<void> {
|
|||||||
<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>
|
||||||
|
<div class="prompt-instruction">
|
||||||
|
<span class="prompt-instruction-command">↹</span>
|
||||||
|
<span>to switch to Vault Search</span>
|
||||||
|
</div>
|
||||||
<div class="prompt-instruction">
|
<div class="prompt-instruction">
|
||||||
<span class="prompt-instruction-command">esc</span>
|
<span class="prompt-instruction-command">esc</span>
|
||||||
{#if !!parent}
|
{#if !!parent}
|
||||||
<span>to go back to Vault Search</span>
|
<span>to go back to Vault Search</span>
|
||||||
{:else}
|
{:else}
|
||||||
<span>to dismiss</span>
|
<span>to close</span>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ let lastSearch = ""
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Notice, TFile } from "obsidian"
|
import { MarkdownView, Notice, TFile } from "obsidian"
|
||||||
import { onMount, tick } from "svelte"
|
import { onMount, onDestroy, tick } from "svelte"
|
||||||
import InputSearch from "./InputSearch.svelte"
|
import InputSearch from "./InputSearch.svelte"
|
||||||
import ModalContainer from "./ModalContainer.svelte"
|
import ModalContainer from "./ModalContainer.svelte"
|
||||||
import { eventBus, type ResultNote } from "src/globals"
|
import { eventBus, type ResultNote } from "src/globals"
|
||||||
@@ -31,14 +31,20 @@ $: if (searchQuery) {
|
|||||||
onMount(() => {
|
onMount(() => {
|
||||||
reindexNotes()
|
reindexNotes()
|
||||||
searchQuery = lastSearch
|
searchQuery = lastSearch
|
||||||
eventBus.on("vault", "enter", onInputEnter)
|
eventBus.enable("vault")
|
||||||
eventBus.on("vault", "shift-enter", onInputShiftEnter)
|
eventBus.on("vault", "enter", openNoteAndCloseModal)
|
||||||
eventBus.on("vault", "ctrl-enter", onInputCtrlEnter)
|
eventBus.on("vault", "shift-enter", createNoteAndCloseModal)
|
||||||
eventBus.on("vault", "alt-enter", onInputAltEnter)
|
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-up", () => moveIndex(-1))
|
||||||
eventBus.on("vault", "arrow-down", () => moveIndex(1))
|
eventBus.on("vault", "arrow-down", () => moveIndex(1))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
eventBus.disable("vault")
|
||||||
|
})
|
||||||
|
|
||||||
async function updateResults() {
|
async function updateResults() {
|
||||||
query = new Query(searchQuery)
|
query = new Query(searchQuery)
|
||||||
resultNotes = await getSuggestions(query)
|
resultNotes = await getSuggestions(query)
|
||||||
@@ -54,36 +60,71 @@ function onClick() {
|
|||||||
modal.close()
|
modal.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
function onInputEnter(): void {
|
function openNoteAndCloseModal(): void {
|
||||||
// console.log(event.detail)
|
// console.log(event.detail)
|
||||||
if (!selectedNote) return
|
if (!selectedNote) return
|
||||||
openNote(selectedNote)
|
openNote(selectedNote)
|
||||||
modal.close()
|
modal.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
function onInputCtrlEnter(): void {
|
function openNoteInNewPane(): void {
|
||||||
if (!selectedNote) return
|
if (!selectedNote) return
|
||||||
openNote(selectedNote, true)
|
openNote(selectedNote, true)
|
||||||
modal.close()
|
modal.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onInputShiftEnter(): Promise<void> {
|
async function createNoteAndCloseModal(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await createNote(searchQuery)
|
await createNote(searchQuery)
|
||||||
}
|
} catch (e) {
|
||||||
catch(e) {
|
|
||||||
new Notice((e as Error).message)
|
new Notice((e as Error).message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
modal.close()
|
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) {
|
if (selectedNote) {
|
||||||
|
// Open in-file modal for selected search result
|
||||||
const file = app.vault.getAbstractFileByPath(selectedNote.path)
|
const file = app.vault.getAbstractFileByPath(selectedNote.path)
|
||||||
if (file && file instanceof TFile) {
|
if (file && file instanceof TFile) {
|
||||||
// modal.close()
|
new OmnisearchInFileModal(app, file, searchQuery).open()
|
||||||
new OmnisearchInFileModal(app, file, searchQuery, modal).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">
|
<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 expand in-note results</span>
|
|
||||||
</div>
|
|
||||||
<br />
|
|
||||||
<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>
|
||||||
|
<div class="prompt-instruction">
|
||||||
|
<span class="prompt-instruction-command">↹</span>
|
||||||
|
<span>to switch to In-File Search</span>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
|
||||||
<div class="prompt-instruction">
|
<div class="prompt-instruction">
|
||||||
<span class="prompt-instruction-command">ctrl ↵</span>
|
<span class="prompt-instruction-command">ctrl ↵</span>
|
||||||
<span>to open in a new pane</span>
|
<span>to open in a new pane</span>
|
||||||
@@ -142,6 +184,10 @@ async function scrollIntoView(): Promise<void> {
|
|||||||
<span>to create</span>
|
<span>to create</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="prompt-instruction">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ export class EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public emit(event: string, ...args: any[]): void {
|
public emit(event: string, ...args: any[]): void {
|
||||||
for (const [key, handler] of this.handlers.entries()) {
|
const entries = [...this.handlers.entries()].filter(
|
||||||
const ctx = key.split('@')[0]
|
([k, h]) => !this.disabled.includes(k.split('@')[0]),
|
||||||
if (this.disabled.includes(ctx)) continue
|
)
|
||||||
|
for (const [key, handler] of entries) {
|
||||||
if (key.endsWith(`@${event}`)) {
|
if (key.endsWith(`@${event}`)) {
|
||||||
handler(...args)
|
handler(...args)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
} from './search'
|
} from './search'
|
||||||
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
||||||
import { loadSettings, SettingsTab } from './settings'
|
import { loadSettings, SettingsTab } from './settings'
|
||||||
import { OmnisearchSuggest } from './suggestions'
|
import { eventBus } from './globals'
|
||||||
|
|
||||||
// let mainWindow: { on: any; off: any } | null = null
|
// let mainWindow: { on: any; off: any } | null = null
|
||||||
// try {
|
// try {
|
||||||
@@ -21,7 +21,8 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
this.addSettingTab(new SettingsTab(this))
|
this.addSettingTab(new SettingsTab(this))
|
||||||
this.registerEditorSuggest(new OmnisearchSuggest(app))
|
eventBus.disable('vault')
|
||||||
|
eventBus.disable('infile')
|
||||||
|
|
||||||
// Commands to display Omnisearch modals
|
// Commands to display Omnisearch modals
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ abstract class OmnisearchModal extends Modal {
|
|||||||
|
|
||||||
this.scope.register(['Alt'], 'Enter', e => {
|
this.scope.register(['Alt'], 'Enter', e => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
eventBus.emit('alt-enter') // Open the InFile modal
|
eventBus.emit('alt-enter') // Insert link
|
||||||
})
|
})
|
||||||
|
|
||||||
this.scope.register(['Shift'], 'Enter', e => {
|
this.scope.register(['Shift'], 'Enter', e => {
|
||||||
@@ -88,6 +88,11 @@ abstract class OmnisearchModal extends Modal {
|
|||||||
eventBus.emit('enter') // Open in current pane
|
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 {
|
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')
|
return new RegExp(strings.map(s => `(${escapeRegex(s)})`).join('|'), 'gi')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user