Squashed commit of the following:
commit ac82511ddd17d5472ae3cfea9bbad9754f5a4d62 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Sat Oct 22 08:23:42 2022 +0200 Screw that cache, seriously. commit 8ba40d1be73daaaffea09e07bc56c339266db9b6 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Fri Oct 21 22:36:48 2022 +0200 Stuff commit 27b8fd7dc809be9714a109d3a458eb1276a47e2e Author: Simon Cambier <simon.cambier@protonmail.com> Date: Fri Oct 21 22:22:20 2022 +0200 Moved files commit fb1349c914907e586e103ca54fb04b9ddd45ef5d Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 22:25:29 2022 +0200 Removed duplicate code commit e7371138e60cbe4155cfd4fb44e3ee1d2e3ee088 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 21:50:09 2022 +0200 Moved a bunch of files commit 2ee1b2a0e799d4b41ab3a444d8cc44dfff5b5623 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 21:32:21 2022 +0200 Removed useless code commit 76c530dfb9adbad1bbe9079de2330fe43a044249 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 20:44:11 2022 +0200 Split file reading and indexing
This commit is contained in:
167
src/components/modals.ts
Normal file
167
src/components/modals.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import { App, Modal, TFile } from 'obsidian'
|
||||
import ModalVault from './ModalVault.svelte'
|
||||
import ModalInFile from './ModalInFile.svelte'
|
||||
import {eventBus, EventNames, isInputComposition} from '../globals'
|
||||
import { settings } from '../settings'
|
||||
|
||||
abstract class OmnisearchModal extends Modal {
|
||||
protected constructor(app: App) {
|
||||
super(app)
|
||||
|
||||
// Remove all the default modal's children
|
||||
// so that we can more easily customize it
|
||||
// const closeEl = this.containerEl.find('.modal-close-button')
|
||||
this.modalEl.replaceChildren()
|
||||
// this.modalEl.append(closeEl)
|
||||
this.modalEl.addClass('omnisearch-modal', 'prompt')
|
||||
// this.modalEl.removeClass('modal')
|
||||
this.modalEl.tabIndex = -1
|
||||
|
||||
// Setup events that can be listened through the event bus
|
||||
|
||||
// #region Up/Down navigation
|
||||
|
||||
this.scope.register([], 'ArrowDown', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-down')
|
||||
})
|
||||
this.scope.register([], 'ArrowUp', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-up')
|
||||
})
|
||||
|
||||
// Ctrl+j/k
|
||||
for (const key of [
|
||||
{ k: 'j', dir: 'down' },
|
||||
{ k: 'k', dir: 'up' },
|
||||
] as const) {
|
||||
for (const modifier of ['Ctrl', 'Meta'] as const) {
|
||||
this.scope.register([modifier], key.k, e => {
|
||||
if (settings.CtrlJK && this.app.vault.getConfig('vimMode')) {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-' + key.dir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Ctrl+n/p
|
||||
for (const key of [
|
||||
{ k: 'n', dir: 'down' },
|
||||
{ k: 'p', dir: 'up' },
|
||||
] as const) {
|
||||
for (const modifier of ['Ctrl', 'Meta'] as const) {
|
||||
this.scope.register([modifier], key.k, e => {
|
||||
if (settings.CtrlNP && this.app.vault.getConfig('vimMode')) {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-' + key.dir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion Up/Down navigation
|
||||
|
||||
// Open in new pane
|
||||
this.scope.register(['Mod'], 'Enter', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('open-in-new-pane')
|
||||
})
|
||||
|
||||
// Insert link
|
||||
this.scope.register(['Alt'], 'Enter', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('insert-link')
|
||||
})
|
||||
|
||||
// Create a new note
|
||||
this.scope.register(['Shift'], 'Enter', e => {
|
||||
e.preventDefault()
|
||||
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')
|
||||
}
|
||||
})
|
||||
|
||||
this.scope.register([], 'Tab', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('tab') // Switch context
|
||||
})
|
||||
|
||||
// Search history
|
||||
this.scope.register(['Alt'], 'ArrowDown', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('next-search-history')
|
||||
})
|
||||
this.scope.register(['Alt'], 'ArrowUp', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('prev-search-history')
|
||||
})
|
||||
|
||||
// Context
|
||||
this.scope.register(['Ctrl'], 'h', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit(EventNames.ToggleExcerpts)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export class OmnisearchVaultModal extends OmnisearchModal {
|
||||
constructor(app: App) {
|
||||
super(app)
|
||||
const cmp = new ModalVault({
|
||||
target: this.modalEl,
|
||||
props: {
|
||||
modal: this,
|
||||
},
|
||||
})
|
||||
|
||||
this.onClose = () => {
|
||||
// Since the component is manually created,
|
||||
// we also need to manually destroy it
|
||||
cmp.$destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class OmnisearchInFileModal extends OmnisearchModal {
|
||||
constructor(
|
||||
app: App,
|
||||
file: TFile,
|
||||
searchQuery: string = '',
|
||||
parent?: OmnisearchModal
|
||||
) {
|
||||
super(app)
|
||||
|
||||
const cmp = new ModalInFile({
|
||||
target: this.modalEl,
|
||||
props: {
|
||||
modal: this,
|
||||
singleFilePath: file.path,
|
||||
parent: parent,
|
||||
searchQuery,
|
||||
},
|
||||
})
|
||||
|
||||
if (parent) {
|
||||
// Hide the parent vault modal, and show it back when this one is closed
|
||||
parent.containerEl.toggleVisibility(false)
|
||||
}
|
||||
this.onClose = () => {
|
||||
if (parent) {
|
||||
parent.containerEl.toggleVisibility(true)
|
||||
}
|
||||
cmp.$destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user