Skip to content

Commit

Permalink
#87 - create in a new pane + fixed some settings stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed Sep 10, 2022
1 parent 3a0b3cb commit beaa8e5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
29 changes: 19 additions & 10 deletions src/components/ModalVault.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
searchQuery = searchHistory[historySearchIndex]
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', 'create-note', createNoteAndCloseModal)
eventBus.on('vault', 'open-in-new-pane', openNoteInNewPane)
eventBus.on('vault', 'insert-link', insertLink)
eventBus.on('vault', 'tab', switchToInFileModal)
eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
eventBus.on('vault', 'arrow-down', () => moveIndex(1))
Expand Down Expand Up @@ -105,9 +105,12 @@
openNote(note, newPane)
}
async function createNoteAndCloseModal(): Promise<void> {
async function createNoteAndCloseModal(opt?: {
newLeaf: boolean
}): Promise<void> {
console.log(opt)
try {
await createNote(searchQuery)
await createNote(searchQuery, opt?.newLeaf)
} catch (e) {
new Notice((e as Error).message)
return
Expand Down Expand Up @@ -208,6 +211,7 @@
<span class="prompt-instruction-command">↹</span>
<span>to switch to In-File Search</span>
</div>

<br />

<div class="prompt-instruction">
Expand All @@ -219,16 +223,21 @@
<span>to create</span>
</div>
<div class="prompt-instruction">
<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>
<span class="prompt-instruction-command">ctrl shift ↵</span>
<span>to create in a new pane</span>
</div>

<br />

<div class="prompt-instruction">
<span class="prompt-instruction-command">alt ↵</span>
<span>to insert a link</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">ctrl+h</span><span
>to toggle context</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to close</span>
</div>
</div>
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { eventBus } from './globals'
import { registerAPI } from '@vanakat/plugin-api'
import api from './api'
import { loadSearchHistory } from './search-history'
import { get } from 'svelte/store'

// let mainWindow: { on: any; off: any } | null = null
// try {
Expand All @@ -35,7 +36,7 @@ export default class OmnisearchPlugin extends Plugin {

_registerAPI(this)

if (settings.ribbonIcon) {
if (get(settings).ribbonIcon) {
this.addRibbonButton()
}

Expand Down
17 changes: 13 additions & 4 deletions src/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,35 @@ abstract class OmnisearchModal extends Modal {
}

// #endregion Up/Down navigation

// Open in new pane
this.scope.register(['Mod'], 'Enter', e => {
e.preventDefault()
eventBus.emit('ctrl-enter') // Open in new pane
eventBus.emit('open-in-new-pane')
})

// Insert link
this.scope.register(['Alt'], 'Enter', e => {
e.preventDefault()
eventBus.emit('alt-enter') // Insert link
eventBus.emit('insert-link')
})

// Create a new note
this.scope.register(['Shift'], 'Enter', e => {
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 => {
if (!isInputComposition()) {
// Check if the user is still typing
e.preventDefault()
eventBus.emit('enter') // Open in current pane
eventBus.emit('enter')
}
})

Expand Down
23 changes: 11 additions & 12 deletions src/notes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { MarkdownView, TFile, type CachedMetadata } from 'obsidian'
import {
MarkdownView,
TFile,
WorkspaceLeaf,
type CachedMetadata,
} from 'obsidian'
import {
notesCacheFilePath,
type IndexedNote,
type ResultNote,
} from './globals'
import { stringsToRegex } from './utils'
import { stringsToRegex, wait } from './utils'
import { settings } from './settings'
import { get } from 'svelte/store'

/**
* This is an in-memory cache of the notes, with all their computed fields
Expand All @@ -20,7 +26,7 @@ export function resetNotesCache(): void {

export async function loadNotesCache(): Promise<void> {
if (
settings.storeIndexInFile &&
get(settings).storeIndexInFile &&
(await app.vault.adapter.exists(notesCacheFilePath))
) {
try {
Expand Down Expand Up @@ -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 {
let pathPrefix = ''
switch (app.vault.getConfig('newFileLocation')) {
Expand All @@ -107,14 +113,7 @@ export async function createNote(name: string): Promise<void> {
pathPrefix = ''
break
}
const file = await app.vault.create(`${pathPrefix}${name}.md`, '')
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
app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf)
} catch (e) {
;(e as any).message =
'OmniSearch - Could not create note: ' + (e as any).message
Expand Down
3 changes: 2 additions & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get } from 'svelte/store'
import { settings } from './settings'
import { removeDiacritics, stripSurroundingQuotes } from './utils'
import { parseQuery } from './vendor/parse-query'
Expand All @@ -22,7 +23,7 @@ export class Query {
public exclusions: QueryToken[] = []

constructor(text = '') {
if (settings.ignoreDiacritics) text = removeDiacritics(text)
if (get(settings).ignoreDiacritics) text = removeDiacritics(text)
const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
this.exclusions = tokens.exclude.text
.map(this.formatToken)
Expand Down
6 changes: 5 additions & 1 deletion src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
wait,
} from './utils'
import type { Query } from './query'
import { settings } from './settings'
import { settings as storeSettings } from './settings'
import {
removeNoteFromCache,
getNoteFromCache,
Expand All @@ -31,10 +31,13 @@ import {
saveNotesCacheToFile,
isCacheOutdated,
} from './notes'
import { get } from 'svelte/store'

let minisearchInstance: MiniSearch<IndexedNote>
let isIndexChanged: boolean

const settings = get(storeSettings)

const tokenize = (text: string): string[] => {
const tokens = text.split(SPACE_OR_PUNCTUATION)
const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch']
Expand Down Expand Up @@ -140,6 +143,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
*/
async function search(query: Query): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return []

let results = minisearchInstance.search(query.segmentsToStr(), {
prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false),
Expand Down

0 comments on commit beaa8e5

Please sign in to comment.