From 6b64cd80b467dbd54bf52b367e06bc359e734daa Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 14 Jul 2022 08:24:31 +0200 Subject: [PATCH] Improved #51 and fixed #80 --- src/components/ModalVault.svelte | 4 ++-- src/modals.ts | 9 ++------- src/notes.ts | 13 +++++-------- src/utils.ts | 6 +++++- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index 9efb444..ce5ca32 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -10,7 +10,7 @@ import ModalContainer from "./ModalContainer.svelte" import { eventBus, type ResultNote } from "src/globals" import { createNote, openNote } from "src/notes" import { getSuggestions, reindexNotes } from "src/search" -import { loopIndex } from "src/utils" +import { getCtrlKeyLabel, loopIndex } from "src/utils" import { OmnisearchInFileModal, type OmnisearchVaultModal } from "src/modals" import ResultItemVault from "./ResultItemVault.svelte" import { Query } from "src/query" @@ -176,7 +176,7 @@ async function scrollIntoView(): Promise {
- ctrl ↵ + {getCtrlKeyLabel()} ↵ to open in a new pane
diff --git a/src/modals.ts b/src/modals.ts index b777a31..57ab1ec 100644 --- a/src/modals.ts +++ b/src/modals.ts @@ -1,4 +1,4 @@ -import { App, Modal, TFile } from 'obsidian' +import { App, Modal, TFile, Platform } from 'obsidian' import ModalVault from './components/ModalVault.svelte' import ModalInFile from './components/ModalInFile.svelte' import { eventBus, isInputComposition } from './globals' @@ -61,15 +61,10 @@ abstract class OmnisearchModal extends Modal { } // #endregion Up/Down navigation - - this.scope.register(['Ctrl'], 'Enter', e => { + this.scope.register(['Mod'], 'Enter', e => { e.preventDefault() eventBus.emit('ctrl-enter') // Open in new pane }) - this.scope.register(['Meta'], 'Enter', e => { - e.preventDefault() - eventBus.emit('ctrl-enter') // Open in new pane (but on Mac) - }) this.scope.register(['Alt'], 'Enter', e => { e.preventDefault() diff --git a/src/notes.ts b/src/notes.ts index 29e174a..85b2575 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -62,23 +62,20 @@ export async function openNote( reg.exec(item.content) const offset = reg.lastIndex - // Check if the note is already open - // const pane = MarkdownView.getPane(item.path) - // Check if the note is already open, // to avoid opening it twice if the first one is pinned - let existing = false + let alreadyOpenAndPinned = false app.workspace.iterateAllLeaves(leaf => { if (leaf.view instanceof MarkdownView) { - if (leaf.getViewState().state?.file === item.path) { + if (!newPane && leaf.getViewState().state?.file === item.path && leaf.getViewState()?.pinned) { app.workspace.setActiveLeaf(leaf, false, true) - existing = true + alreadyOpenAndPinned = true } } }) - if (!existing) { - // Open a new note + if (!alreadyOpenAndPinned) { + // Open the note normally await app.workspace.openLinkText(item.path, '', newPane) } diff --git a/src/utils.ts b/src/utils.ts index 342aedb..c56ce04 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import type { CachedMetadata } from 'obsidian' +import { Platform, type CachedMetadata } from 'obsidian' import { excerptAfter, excerptBefore, @@ -164,3 +164,7 @@ export function getTagsFromMetadata(metadata: CachedMetadata | null): string[] { export function removeDiacritics(str: string): string { return str.normalize('NFD').replace(/\p{Diacritic}/gu, '') } + +export function getCtrlKeyLabel(): 'ctrl' | '⌘' { + return Platform.isMacOS ? '⌘' : 'ctrl' +}