#87 - create in a new pane + fixed some settings stuff

This commit is contained in:
Simon Cambier
2022-09-10 23:01:25 +02:00
parent 3a0b3cbc3a
commit beaa8e5656
6 changed files with 52 additions and 29 deletions

View File

@@ -34,9 +34,9 @@
searchQuery = searchHistory[historySearchIndex] searchQuery = searchHistory[historySearchIndex]
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', 'create-note', createNoteAndCloseModal)
eventBus.on('vault', 'ctrl-enter', openNoteInNewPane) eventBus.on('vault', 'open-in-new-pane', openNoteInNewPane)
eventBus.on('vault', 'alt-enter', insertLink) eventBus.on('vault', 'insert-link', insertLink)
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))
@@ -105,9 +105,12 @@
openNote(note, newPane) openNote(note, newPane)
} }
async function createNoteAndCloseModal(): Promise<void> { async function createNoteAndCloseModal(opt?: {
newLeaf: boolean
}): Promise<void> {
console.log(opt)
try { try {
await createNote(searchQuery) await createNote(searchQuery, opt?.newLeaf)
} catch (e) { } catch (e) {
new Notice((e as Error).message) new Notice((e as Error).message)
return return
@@ -208,6 +211,7 @@
<span class="prompt-instruction-command"></span> <span class="prompt-instruction-command"></span>
<span>to switch to In-File Search</span> <span>to switch to In-File Search</span>
</div> </div>
<br /> <br />
<div class="prompt-instruction"> <div class="prompt-instruction">
@@ -218,17 +222,22 @@
<span class="prompt-instruction-command">shift ↵</span> <span class="prompt-instruction-command">shift ↵</span>
<span>to create</span> <span>to create</span>
</div> </div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">ctrl shift ↵</span>
<span>to create in a new pane</span>
</div>
<br />
<div class="prompt-instruction"> <div class="prompt-instruction">
<span class="prompt-instruction-command">alt ↵</span> <span class="prompt-instruction-command">alt ↵</span>
<span>to insert a link</span> <span>to insert a link</span>
</div> </div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to close</span>
</div>
<br />
<div class="prompt-instruction"> <div class="prompt-instruction">
<span class="prompt-instruction-command">ctrl+h</span><span <span class="prompt-instruction-command">ctrl+h</span><span
>to toggle context</span> >to toggle context</span>
</div> </div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to close</span>
</div>
</div> </div>

View File

@@ -11,6 +11,7 @@ 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' import { loadSearchHistory } from './search-history'
import { get } from 'svelte/store'
// let mainWindow: { on: any; off: any } | null = null // let mainWindow: { on: any; off: any } | null = null
// try { // try {
@@ -35,7 +36,7 @@ export default class OmnisearchPlugin extends Plugin {
_registerAPI(this) _registerAPI(this)
if (settings.ribbonIcon) { if (get(settings).ribbonIcon) {
this.addRibbonButton() this.addRibbonButton()
} }

View File

@@ -62,26 +62,35 @@ abstract class OmnisearchModal extends Modal {
} }
// #endregion Up/Down navigation // #endregion Up/Down navigation
// Open in new pane
this.scope.register(['Mod'], 'Enter', e => { this.scope.register(['Mod'], 'Enter', e => {
e.preventDefault() e.preventDefault()
eventBus.emit('ctrl-enter') // Open in new pane eventBus.emit('open-in-new-pane')
}) })
// Insert link
this.scope.register(['Alt'], 'Enter', e => { this.scope.register(['Alt'], 'Enter', e => {
e.preventDefault() e.preventDefault()
eventBus.emit('alt-enter') // Insert link eventBus.emit('insert-link')
}) })
// Create a new note
this.scope.register(['Shift'], 'Enter', e => { this.scope.register(['Shift'], 'Enter', e => {
e.preventDefault() e.preventDefault()
eventBus.emit('shift-enter') // Create a new note eventBus.emit('create-note')
})
this.scope.register(['Ctrl', 'Shift'], 'Enter', e => {
e.preventDefault()
eventBus.emit('create-note', { newLeaf: true })
}) })
// Open in current pane
this.scope.register([], 'Enter', e => { this.scope.register([], 'Enter', e => {
if (!isInputComposition()) { if (!isInputComposition()) {
// Check if the user is still typing // Check if the user is still typing
e.preventDefault() e.preventDefault()
eventBus.emit('enter') // Open in current pane eventBus.emit('enter')
} }
}) })

View File

@@ -1,11 +1,17 @@
import { MarkdownView, TFile, type CachedMetadata } from 'obsidian' import {
MarkdownView,
TFile,
WorkspaceLeaf,
type CachedMetadata,
} from 'obsidian'
import { import {
notesCacheFilePath, notesCacheFilePath,
type IndexedNote, type IndexedNote,
type ResultNote, type ResultNote,
} from './globals' } from './globals'
import { stringsToRegex } from './utils' import { stringsToRegex, wait } from './utils'
import { settings } from './settings' import { settings } from './settings'
import { get } from 'svelte/store'
/** /**
* This is an in-memory cache of the notes, with all their computed fields * This is an in-memory cache of the notes, with all their computed fields
@@ -20,7 +26,7 @@ export function resetNotesCache(): void {
export async function loadNotesCache(): Promise<void> { export async function loadNotesCache(): Promise<void> {
if ( if (
settings.storeIndexInFile && get(settings).storeIndexInFile &&
(await app.vault.adapter.exists(notesCacheFilePath)) (await app.vault.adapter.exists(notesCacheFilePath))
) { ) {
try { try {
@@ -93,7 +99,7 @@ export async function openNote(
}) })
} }
export async function createNote(name: string): Promise<void> { export async function createNote(name: string, newLeaf = false): Promise<void> {
try { try {
let pathPrefix = '' let pathPrefix = ''
switch (app.vault.getConfig('newFileLocation')) { switch (app.vault.getConfig('newFileLocation')) {
@@ -107,14 +113,7 @@ export async function createNote(name: string): Promise<void> {
pathPrefix = '' pathPrefix = ''
break break
} }
const file = await app.vault.create(`${pathPrefix}${name}.md`, '') app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf)
await app.workspace.openLinkText(file.path, '')
const view = app.workspace.getActiveViewOfType(MarkdownView)
if (!view) {
throw new Error('OmniSearch - No active MarkdownView')
}
const pos = view.editor.offsetToPos(name.length + 5)
pos.ch = 0
} catch (e) { } catch (e) {
;(e as any).message = ;(e as any).message =
'OmniSearch - Could not create note: ' + (e as any).message 'OmniSearch - Could not create note: ' + (e as any).message

View File

@@ -1,3 +1,4 @@
import { get } from 'svelte/store'
import { settings } from './settings' import { settings } from './settings'
import { removeDiacritics, stripSurroundingQuotes } from './utils' import { removeDiacritics, stripSurroundingQuotes } from './utils'
import { parseQuery } from './vendor/parse-query' import { parseQuery } from './vendor/parse-query'
@@ -22,7 +23,7 @@ export class Query {
public exclusions: QueryToken[] = [] public exclusions: QueryToken[] = []
constructor(text = '') { constructor(text = '') {
if (settings.ignoreDiacritics) text = removeDiacritics(text) if (get(settings).ignoreDiacritics) text = removeDiacritics(text)
const tokens = parseQuery(text.toLowerCase(), { tokenize: true }) const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
this.exclusions = tokens.exclude.text this.exclusions = tokens.exclude.text
.map(this.formatToken) .map(this.formatToken)

View File

@@ -18,7 +18,7 @@ import {
wait, wait,
} from './utils' } from './utils'
import type { Query } from './query' import type { Query } from './query'
import { settings } from './settings' import { settings as storeSettings } from './settings'
import { import {
removeNoteFromCache, removeNoteFromCache,
getNoteFromCache, getNoteFromCache,
@@ -31,10 +31,13 @@ import {
saveNotesCacheToFile, saveNotesCacheToFile,
isCacheOutdated, isCacheOutdated,
} from './notes' } from './notes'
import { get } from 'svelte/store'
let minisearchInstance: MiniSearch<IndexedNote> let minisearchInstance: MiniSearch<IndexedNote>
let isIndexChanged: boolean let isIndexChanged: boolean
const settings = get(storeSettings)
const tokenize = (text: string): string[] => { const tokenize = (text: string): string[] => {
const tokens = text.split(SPACE_OR_PUNCTUATION) const tokens = text.split(SPACE_OR_PUNCTUATION)
const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch'] const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch']
@@ -140,6 +143,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
*/ */
async function search(query: Query): Promise<SearchResult[]> { async function search(query: Query): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return [] if (!query.segmentsToStr()) return []
let results = minisearchInstance.search(query.segmentsToStr(), { let results = minisearchInstance.search(query.segmentsToStr(), {
prefix: true, prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false), fuzzy: term => (term.length > 4 ? 0.2 : false),