Skip to content

Commit

Permalink
Add shortcut to open note in a new split pane (PR scambier#202)
Browse files Browse the repository at this point in the history
This adds the shortcut `cmd/ctrl + alt + enter` to the search modal to open a found file in a new leaf. I used the term "split" in user-facing text, because I think that's the more common

Some things to note:
- This function ignores the existing "open in new pane by default" setting.
- It also does not check to see if the file is already open. It will continue opening the value in a new split as often as the user wants.
  • Loading branch information
pricebaldwin committed Mar 28, 2024
1 parent 01eadce commit 42ca67e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/components/ModalVault.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
let openInCurrentPaneKey: string
let createInNewPaneKey: string
let createInCurrentPaneKey: string
let openInNewLeafKey: string = getCtrlKeyLabel() + ' alt ↵'
$: selectedNote = resultNotes[selectedIndex]
$: searchQuery = searchQuery ?? previousQuery
Expand Down Expand Up @@ -101,6 +102,7 @@
eventBus.on('vault', Action.ArrowDown, () => moveIndex(1))
eventBus.on('vault', Action.PrevSearchHistory, prevSearchHistory)
eventBus.on('vault', Action.NextSearchHistory, nextSearchHistory)
eventBus.on('vault', Action.OpenInNewLeaf, openNoteInNewLeaf)
await NotesIndex.refreshIndex()
await updateResultsDebounced()
})
Expand Down Expand Up @@ -178,16 +180,27 @@
modal.close()
}
function openNoteInNewLeaf(): void {
console.log('open in new leaf')
if (!selectedNote) return
openSearchResult(selectedNote, true, true)
modal.close()
}
function saveCurrentQuery() {
if (searchQuery) {
cacheManager.addToSearchHistory(searchQuery)
}
}
function openSearchResult(note: ResultNote, newPane = false) {
function openSearchResult(
note: ResultNote,
newPane = false,
newLeaf = false
) {
saveCurrentQuery()
const offset = note.matches?.[0]?.offset ?? 0
openNote(note, offset, newPane)
openNote(note, offset, newPane, newLeaf)
}
async function onClickCreateNote(_e: MouseEvent) {
Expand Down Expand Up @@ -354,6 +367,11 @@
<span>to open in a new pane</span>
</div>

<div class="prompt-instruction">
<span class="prompt-instruction-command">{openInNewLeafKey}</span>
<span>to open in a new split</span>
</div>

<div class="prompt-instruction">
<span class="prompt-instruction-command">alt o</span>
<span>to open in the background</span>
Expand Down
7 changes: 7 additions & 0 deletions src/components/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ abstract class OmnisearchModal extends Modal {
let openInNewPaneKey: Modifier[]
let createInCurrentPaneKey: Modifier[]
let createInNewPaneKey: Modifier[]
let openInNewLeafKey: Modifier[] = ['Mod', 'Alt']
if (settings.openInNewPane) {
openInCurrentPaneKey = ['Mod']
openInNewPaneKey = []
Expand All @@ -86,6 +87,12 @@ abstract class OmnisearchModal extends Modal {
eventBus.emit(Action.OpenInNewPane)
})

// Open in a new leaf
this.scope.register(openInNewLeafKey, 'Enter', e => {
e.preventDefault()
eventBus.emit(Action.OpenInNewLeaf)
})

// Insert link
this.scope.register(['Alt'], 'Enter', e => {
e.preventDefault()
Expand Down
1 change: 1 addition & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const enum Action {
ArrowDown = 'arrow-down',
PrevSearchHistory = 'prev-search-history',
NextSearchHistory = 'next-search-history',
OpenInNewLeaf = 'open-in-new-leaf',
}

export type DocumentRef = { path: string; mtime: number }
Expand Down
5 changes: 3 additions & 2 deletions src/tools/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { stringsToRegex } from './text-processing'
export async function openNote(
item: ResultNote,
offset = 0,
newPane = false
newPane = false,
newLeaf = false
): Promise<void> {
// Check if the note is already open,
// to avoid opening it twice if the first one is pinned
Expand All @@ -25,7 +26,7 @@ export async function openNote(

if (!alreadyOpenAndPinned) {
// Open the note normally
await app.workspace.openLinkText(item.path, '', newPane)
await app.workspace.openLinkText(item.path, '', newLeaf ? 'split' : newPane)
}

const view = app.workspace.getActiveViewOfType(MarkdownView)
Expand Down

0 comments on commit 42ca67e

Please sign in to comment.