Skip to content

Commit

Permalink
Improved mechanism to show hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
alberti42 committed Sep 28, 2024
1 parent 20e353e commit 29228d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
39 changes: 5 additions & 34 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isLinkType,
isAttachmentFolderLocationType,
AttachmentFolderLocationType,
isHotkeysSettingTab,
} from './types';

import { updateVisibilityAttachmentFolders } from "patchFileExplorer";
Expand Down Expand Up @@ -481,40 +482,10 @@ export class ImportAttachmentsSettingTab extends PluginSettingTab {
const em = createEl('em');
const link = frag.createEl('a', { href: '#', text: 'Hotkeys'});
link.onclick = () => {

// Create a MutationObserver to monitor changes in the settings container
const observer = new MutationObserver((mutations: MutationRecord[], observer: MutationObserver) => {
let doBreak = false;
for (const mutation of mutations) {
// Loop through added nodes to check if the desired element has been added
mutation.addedNodes.forEach((node) => {
if (!doBreak && node instanceof HTMLElement && node.querySelector('.search-input-container.mod-hotkey input')) {
const searchInput = node.querySelector('.search-input-container.mod-hotkey input') as HTMLInputElement;
if (searchInput) {
// Set the text to be passed to the input field
searchInput.value = 'import-attachments-plus';

// Trigger an input event to simulate the text being typed
searchInput.dispatchEvent(new Event('input', { bubbles: true }));

// Stop searching
doBreak = true;
}
}
});
if(doBreak) break;
}
// Stop observing
observer.disconnect();
});

// Start observing the settings pane for child nodes being added
observer.observe(this.app.setting.tabContentContainer, {
childList: true,
subtree: true
});

this.app.setting.openTabById('hotkeys');
const tab = this.app.setting.openTabById('hotkeys');
if(isHotkeysSettingTab(tab)) {
tab.setQuery(this.plugin.manifest.id)
}
};

em.appendChild(link);
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// types.ts

import { DEFAULT_SETTINGS } from "default";
import { HotkeysSettingTab } from "obsidian";

export enum ImportActionType {
MOVE='MOVE',
Expand Down Expand Up @@ -194,4 +195,9 @@ export interface ImportAttachmentsSettings_1_3_0 extends Omit<ImportAttachmentsS
relativeLocation: RelativeLocation;
folderPath: string;
linkFormat: LinkFormat_1_3_0;
}
}

export function isHotkeysSettingTab(obj: unknown): obj is HotkeysSettingTab {
// Check if `obj` is an object and has the `setQuery` method
return typeof obj === 'object' && obj !== null && 'setQuery' in obj && typeof (obj as HotkeysSettingTab).setQuery === 'function';
}
10 changes: 9 additions & 1 deletion src/types/obsidian-augment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ declare module 'obsidian' {
}

interface Setting {
openTabById(id: string): void;
openTabById(id: string): SettingTab;
tabContentContainer:HTMLElement;
}
interface SettingTab {
id: string;
name: string;
navEl: HTMLElement;
}
interface HotkeysSettingTab extends SettingTab {
setQuery: (str: string) => void;
}
}

0 comments on commit 29228d9

Please sign in to comment.